blob: 34dfb32d38305e7eea9cc0380bfff6fe37a2b515 [file] [log] [blame]
Radek Krejci5708fe62016-09-14 12:25:12 +02001cmake_minimum_required(VERSION 2.8.9)
Radek Krejcie74ea8c2015-04-09 18:06:20 +02002project(libyang C)
3
Jan Kundrát37df09e2016-11-04 15:42:08 +01004include(GNUInstallDirs)
5
Michal Vaskoe5cf9922015-11-11 11:06:40 +01006set(LIBYANG_DESCRIPTION "libyang is YANG data modelling language parser and toolkit written (and providing API) in C.")
7
Radek Krejci8f7cf402015-08-13 08:05:48 +02008# set version
9set(LIBYANG_MAJOR_VERSION 0)
Michal Vasko42a71572016-10-18 11:56:10 +020010set(LIBYANG_MINOR_VERSION 11)
Radek Krejci7ca8bb42017-02-01 10:23:56 +010011set(LIBYANG_MICRO_VERSION 108)
Radek Krejci8f7cf402015-08-13 08:05:48 +020012set(LIBYANG_VERSION ${LIBYANG_MAJOR_VERSION}.${LIBYANG_MINOR_VERSION}.${LIBYANG_MICRO_VERSION})
13set(LIBYANG_SOVERSION ${LIBYANG_MAJOR_VERSION}.${LIBYANG_MINOR_VERSION})
Radek Krejci60cdf282016-10-10 16:22:22 +020014configure_file(${PROJECT_SOURCE_DIR}/src/libyang.h.in ${PROJECT_SOURCE_DIR}/src/libyang.h)
Radek Krejci8f7cf402015-08-13 08:05:48 +020015
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +020016# include custom Modules
Radek Krejci977c1c82015-09-01 15:18:03 +020017set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +020018
Pavol Vicanf7cc2852016-03-22 23:27:35 +010019find_package(FLEX)
Pavol Vicancae54882016-03-30 12:24:43 +020020find_package(BISON)
Pavol Vican0a9716f2016-05-22 17:04:06 +020021find_program (SED_TOOL NAMES sed)
Pavol Vicanf7cc2852016-03-22 23:27:35 +010022
Pavol Vicancae54882016-03-30 12:24:43 +020023if(NOT BISON_FOUND)
Radek Krejcif19243b2017-02-02 09:23:40 +010024 message(WARNING "Missing Bison.\nYou won't be able to generate source codes from changed flex/bison files.\nCompiling libyang should still works fine.")
Pavol Vicancae54882016-03-30 12:24:43 +020025elseif (NOT FLEX_FOUND)
Radek Krejcif19243b2017-02-02 09:23:40 +010026 message(WARNING "Missing Flex.\nYou won't be able to generate source codes from changed flex/bison files.\nCompiling libyang should still works fine.")
Pavol Vicancae54882016-03-30 12:24:43 +020027else()
28 if (BISON_VERSION VERSION_LESS 3)
29 set(EMPTYDIR "")
30 else ()
31 set(EMPTYDIR "%empty")
32 endif()
33 configure_file(${PROJECT_SOURCE_DIR}/src/yang.y.in ${PROJECT_SOURCE_DIR}/src/yang.y)
34 add_custom_target(bison
Pavol Vican15bf3842016-04-05 22:25:00 +020035 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/src
Pavol Vicanbe9e3832016-04-28 02:21:37 +020036 COMMAND bison -Wno-other -l -o parser_yang_bis.c --defines=parser_yang_bis.h yang.y
Pavol Vican6ab09d82016-10-24 11:30:02 +020037 COMMAND flex -8 -L -o parser_yang_lex.c --header-file=parser_yang_lex.h yang.l
Pavol Vican5c637a62016-10-03 14:47:45 +020038 COMMAND ${SED_TOOL} -i 's/int yychar\;/int yychar\;\\nchar *s = NULL, *tmp_s = NULL\;\\nstruct lys_include inc\;\\nstruct lys_module *trg = NULL\;\\nstruct lys_node *tpdf_parent = NULL, *data_node = NULL\;\\nvoid *actual = NULL\;\\nstruct lys_node_uses *refine_parent = NULL\;\\nint config_inherit = 0, actual_type = 0\;\\nint64_t cnt_val\;\\nint read_string = read_all\;\\nvoid *yang_type = NULL\;/' parser_yang_bis.c
Pavol Vicancae54882016-03-30 12:24:43 +020039 )
40endif()
Pavol Vicanf7cc2852016-03-22 23:27:35 +010041
Radek Krejcie74ea8c2015-04-09 18:06:20 +020042# check the supported platform
43if(NOT UNIX)
44 message(FATAL_ERROR "Only *nix like systems are supported.")
45endif()
46
Radek Krejcie74ea8c2015-04-09 18:06:20 +020047# set default build type if not specified by user
48if(NOT CMAKE_BUILD_TYPE)
49 set(CMAKE_BUILD_TYPE debug)
50endif()
51
Radek Krejci2cb30542015-04-10 08:33:57 +020052set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -fvisibility=hidden")
Radek Krejci3045cf32015-05-28 10:58:52 +020053set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
Radek Krejcif19ac672016-02-05 16:16:36 +010054set(CMAKE_C_FLAGS_DEBUG "-g -O0")
Radek Krejcie74ea8c2015-04-09 18:06:20 +020055
Radek Krejci61cd5562015-10-05 14:06:34 +020056if(CMAKE_BUILD_TYPE STREQUAL debug)
57 option(ENABLE_BUILD_TESTS "Build tests" ON)
58 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
59else()
60 option(ENABLE_BUILD_TESTS "Build tests" OFF)
61 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
62endif()
Mislav Novakovic021b9a12015-10-01 20:50:21 +020063
Radek Krejci5a065542015-05-22 15:02:07 +020064set(libsrc
Radek Krejci9b4ca392015-04-10 08:31:27 +020065 src/common.c
Radek Krejcida04f4a2015-05-21 12:54:09 +020066 src/context.c
Radek Krejci9b4ca392015-04-10 08:31:27 +020067 src/log.c
Radek Krejciee627582015-04-20 17:39:59 +020068 src/dict.c
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020069 src/resolve.c
Radek Krejcib1c12512015-08-11 11:22:04 +020070 src/validation.c
Radek Krejcida04f4a2015-05-21 12:54:09 +020071 src/xml.c
Radek Krejcib1c12512015-08-11 11:22:04 +020072 src/parser.c
Radek Krejci998a0b82015-08-17 13:14:36 +020073 src/parser_yin.c
74 src/parser_xml.c
Radek Krejci5449d472015-10-26 14:35:56 +010075 src/parser_json.c
Pavol Vicancae54882016-03-30 12:24:43 +020076 src/parser_yang_bis.c
77 src/parser_yang_lex.c
Pavol Vicanf7cc2852016-03-22 23:27:35 +010078 src/parser_yang.c
Michal Vasko2d162e12015-09-24 14:33:29 +020079 src/tree_schema.c
80 src/tree_data.c
Radek Krejciefdd0ce2015-05-26 16:48:29 +020081 src/printer.c
Michal Vaskoabbdaa02015-10-06 15:47:25 +020082 src/xpath.c
Radek Krejci998a0b82015-08-17 13:14:36 +020083 src/printer_yang.c
Michal Vaskoa63ca342016-02-05 14:29:19 +010084 src/printer_yin.c
Michal Vasko9b9e4a02015-08-21 09:11:14 +020085 src/printer_xml.c
Radek Krejci998a0b82015-08-17 13:14:36 +020086 src/printer_tree.c
87 src/printer_info.c
88 src/printer_json.c
Radek Krejcida04f4a2015-05-21 12:54:09 +020089 src/yang_types.c)
Radek Krejcie74ea8c2015-04-09 18:06:20 +020090
Radek Krejci5a065542015-05-22 15:02:07 +020091set(lintsrc
Michal Vaskof3e59f12015-06-18 11:53:56 +020092 tools/lint/main.c
Radek Krejci797590b2016-10-10 16:23:04 +020093 tools/lint/main_ni.c
Michal Vaskof3e59f12015-06-18 11:53:56 +020094 tools/lint/commands.c
Michal Vaskoe0cb2522015-07-01 10:24:53 +020095 tools/lint/completion.c
Michal Vasko203b4e72015-06-30 15:25:15 +020096 linenoise/linenoise.c)
Radek Krejci5a065542015-05-22 15:02:07 +020097
Michal Vasko9a542742016-06-23 13:04:51 +020098set(yang2yinsrc
99 tools/yang2yin/main.c)
100
Tomas Cejka422c0072015-07-21 18:18:25 +0200101set(headers
102 src/libyang.h
Michal Vasko2d162e12015-09-24 14:33:29 +0200103 src/tree_schema.h
Radek Krejcic6704c82015-10-06 11:12:45 +0200104 src/tree_data.h
Radek Krejci41912fe2015-10-22 10:22:12 +0200105 src/xml.h
106 src/dict.h)
Tomas Cejka422c0072015-07-21 18:18:25 +0200107
Abhi Keshav7afa0812016-10-25 14:52:51 -0700108# create static libyang library
109if(STATIC)
110 add_library(yang_static STATIC ${libsrc})
111 set_target_properties(yang_static PROPERTIES OUTPUT_NAME yang)
112 target_link_libraries(yang_static m)
113 target_link_libraries(yang_static ${CMAKE_THREAD_LIBS_INIT})
114 target_link_libraries(yang_static ${PCRE_LIBRARIES})
Radek Krejci4c79e702016-11-06 13:00:20 +0100115 install(TARGETS yang_static DESTINATION ${CMAKE_INSTALL_LIBDIR})
Abhi Keshav7afa0812016-10-25 14:52:51 -0700116endif(STATIC)
117
Radek Krejci5708fe62016-09-14 12:25:12 +0200118set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
119add_library(yangobj OBJECT ${libsrc})
120add_library(yang SHARED $<TARGET_OBJECTS:yangobj>)
Radek Krejci8f7cf402015-08-13 08:05:48 +0200121set_target_properties(yang PROPERTIES VERSION ${LIBYANG_VERSION} SOVERSION ${LIBYANG_SOVERSION})
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200122
Radek Krejci89658b32016-02-08 11:14:46 +0100123# link math
124target_link_libraries(yang m)
125
Radek Krejci7d9f46a2016-01-29 13:53:18 +0100126# find pthreads
127set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
128find_package(Threads REQUIRED)
129target_link_libraries(yang ${CMAKE_THREAD_LIBS_INIT})
130
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200131# find PCRE library
132find_package(PCRE REQUIRED)
133include_directories(${PCRE_INCLUDE_DIRS})
Radek Krejci1d949ec2015-10-02 13:35:05 +0200134target_link_libraries(yang ${PCRE_LIBRARIES})
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200135
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100136install(TARGETS yang DESTINATION ${CMAKE_INSTALL_LIBDIR})
137install(FILES ${headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libyang)
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200138
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100139find_package(PkgConfig)
140if(PKG_CONFIG_FOUND)
141 # generate and install pkg-config file
142 configure_file("libyang.pc.in" "libyang.pc" @ONLY)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100143 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libyang.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
Michal Vasko98ae1e12015-12-15 11:11:28 +0100144 # check that pkg-config includes the used path
145 execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --variable pc_path pkg-config RESULT_VARIABLE RETURN OUTPUT_VARIABLE PC_PATH ERROR_QUIET)
146 if(RETURN EQUAL 0)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100147 string(REGEX MATCH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig" SUBSTR "${PC_PATH}")
Michal Vasko33c001c2016-02-11 09:01:52 +0100148 string(LENGTH "${SUBSTR}" SUBSTR_LEN)
Michal Vaskob8d90a32016-02-09 13:59:42 +0100149 if(SUBSTR_LEN EQUAL 0)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100150 message(WARNING "pkg-config will not detect the new package after installation, adjust PKG_CONFIG_PATH using \"export PKG_CONFIG_PATH=\${PKG_CONFIG_PATH}:${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig\".")
Michal Vasko98ae1e12015-12-15 11:11:28 +0100151 endif()
152 endif()
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100153endif()
154
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200155# generate doxygen documentation for libyang API
Radek Krejcie2626092015-10-08 11:25:44 +0200156find_package(Doxygen)
157if(DOXYGEN_FOUND)
158 set(DOXYGEN_SKIP_DOT TRUE)
159 add_custom_target(doc
160 COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile
161 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
162 configure_file(Doxyfile.in Doxyfile)
163endif()
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200164
165# clean cmake cache
166add_custom_target(cclean
Radek Krejci25155ac2015-10-08 11:26:20 +0200167 COMMAND make clean
168 COMMAND find . -iname '*cmake*' -not -name CMakeLists.txt -not -path './CMakeModules*' -exec rm -rf {} +
169 COMMAND rm -rf Makefile Doxyfile
170 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
Radek Krejci2cb30542015-04-10 08:33:57 +0200171
Radek Krejci5a065542015-05-22 15:02:07 +0200172add_executable(yanglint ${lintsrc})
Radek Krejci1d949ec2015-10-02 13:35:05 +0200173target_link_libraries(yanglint yang)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100174install(TARGETS yanglint DESTINATION ${CMAKE_INSTALL_BINDIR})
175install(FILES ${PROJECT_SOURCE_DIR}/tools/lint/yanglint.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
Radek Krejci5a065542015-05-22 15:02:07 +0200176
Michal Vasko9a542742016-06-23 13:04:51 +0200177add_executable(yang2yin ${yang2yinsrc})
178
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200179if(ENABLE_VALGRIND_TESTS)
180 set(ENABLE_BUILD_TESTS ON)
181endif()
182
183if(ENABLE_BUILD_TESTS)
Radek Krejcie02ce0a2015-11-04 14:14:20 +0100184 find_package(CMocka 1.0.0)
185 if(CMOCKA_FOUND)
186 enable_testing()
187 add_subdirectory(tests)
188 endif(CMOCKA_FOUND)
189endif(ENABLE_BUILD_TESTS)
Mislav Novakovica827b5f2016-02-29 11:47:22 +0100190
191if(JAVASCRIPT_BINDING)
192 include(swig/javascript/CMakeLists.txt)
193endif()