blob: 6d158bb2343c0cf52a9be752e78d7bac0a322aff [file] [log] [blame]
Radek Krejcib0cd3942017-04-07 13:31:30 +02001cmake_minimum_required(VERSION 2.8.12)
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 Krejcib0cd3942017-04-07 13:31:30 +02008# Correct RPATH usage on OS X
9set(CMAKE_MACOSX_RPATH TRUE)
10
Radek Krejci8f7cf402015-08-13 08:05:48 +020011# set version
12set(LIBYANG_MAJOR_VERSION 0)
Radek Krejcie07cd062017-09-22 13:21:02 +020013set(LIBYANG_MINOR_VERSION 14)
Michal Vasko6a9c1fb2017-09-26 10:31:25 +020014set(LIBYANG_MICRO_VERSION 6)
Radek Krejci8f7cf402015-08-13 08:05:48 +020015set(LIBYANG_VERSION ${LIBYANG_MAJOR_VERSION}.${LIBYANG_MINOR_VERSION}.${LIBYANG_MICRO_VERSION})
16set(LIBYANG_SOVERSION ${LIBYANG_MAJOR_VERSION}.${LIBYANG_MINOR_VERSION})
Michal Vaskofcd974b2017-08-22 10:17:49 +020017
Michal Vasko8f3160e2017-09-27 11:25:26 +020018# set default build type if not specified by user
19if(NOT CMAKE_BUILD_TYPE)
20 set(CMAKE_BUILD_TYPE debug)
21endif()
22
23set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra")
24set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
25set(CMAKE_C_FLAGS_PACKAGE "-g -O2 -DNDEBUG")
26set(CMAKE_C_FLAGS_DEBUG "-g -O0")
27
28# options
29if((CMAKE_BUILD_TYPE STREQUAL debug) OR (CMAKE_BUILD_TYPE STREQUAL Package))
30 option(ENABLE_BUILD_TESTS "Build tests" ON)
31 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
32else()
33 option(ENABLE_BUILD_TESTS "Build tests" OFF)
34 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
35endif()
36option(ENABLE_LATEST_REVISIONS "Enable reusing of latest revisions of schemas" ON)
37option(ENABLE_CACHE "Enable data caching for schemas (time-efficient at the cost of increased space-complexity)" OFF)
38
39if (ENABLE_LATEST_REVISIONS)
40 set(ENABLE_LATEST_REVISIONS_MACRO "/**\n * @brief Latest revisions of loaded schemas will be reused.\n */\n#define LY_ENABLED_LATEST_REVISIONS")
41else()
42 set(ENABLE_LATEST_REVISIONS_MACRO "/**\n * @brief Latest revisions of loaded schemas will not be reused and always reloaded.\n */")
43endif()
44
Michal Vaskofcd974b2017-08-22 10:17:49 +020045if(ENABLE_CACHE)
46 set(ENABLE_CACHE_MACRO "/**\n * @brief Cache of some temporary information will be used.\n */\n#define LY_ENABLED_CACHE")
47else()
48 set(ENABLE_CACHE_MACRO "/**\n * @brief Cache of some temporary information will not be used.\n */")
49endif()
50
51configure_file(${PROJECT_SOURCE_DIR}/src/libyang.h.in ${PROJECT_SOURCE_DIR}/src/libyang.h @ONLY)
Radek Krejci8f7cf402015-08-13 08:05:48 +020052
Radek Krejcic43058d2017-01-04 16:29:58 +010053if(PLUGINS_DIR)
Michal Vasko8f3160e2017-09-27 11:25:26 +020054 set(LIBYANG_EXT_PLUGINS_DIR ${PLUGINS_DIR})
Radek Krejcic43058d2017-01-04 16:29:58 +010055else()
Michal Vasko8f3160e2017-09-27 11:25:26 +020056 set(LIBYANG_EXT_PLUGINS_DIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libyang)
Radek Krejcic43058d2017-01-04 16:29:58 +010057endif()
Radek Krejci858ad952017-01-04 11:16:32 +010058configure_file(${PROJECT_SOURCE_DIR}/src/extensions_config.h.in ${PROJECT_SOURCE_DIR}/src/extensions_config.h)
59
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +020060# include custom Modules
Radek Krejci977c1c82015-09-01 15:18:03 +020061set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +020062
Pavol Vicanf7cc2852016-03-22 23:27:35 +010063find_package(FLEX)
Pavol Vicancae54882016-03-30 12:24:43 +020064find_package(BISON)
PavolVicancf749252017-06-08 09:31:49 +020065find_program (DEB_BUILDER NAMES debuild)
66find_program (RPM_BUILDER NAMES rpmbuild)
Pavol Vican0a9716f2016-05-22 17:04:06 +020067find_program (SED_TOOL NAMES sed)
Pavol Vicanf7cc2852016-03-22 23:27:35 +010068
Pavol Vicancae54882016-03-30 12:24:43 +020069if(NOT BISON_FOUND)
Michal Vasko8f3160e2017-09-27 11:25:26 +020070 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 +020071elseif (NOT FLEX_FOUND)
Michal Vasko8f3160e2017-09-27 11:25:26 +020072 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 +020073else()
Michal Vasko8f3160e2017-09-27 11:25:26 +020074 if (BISON_VERSION VERSION_LESS 3)
75 set(EMPTYDIR "")
76 else ()
77 set(EMPTYDIR "%empty")
78 endif()
79 configure_file(${PROJECT_SOURCE_DIR}/src/yang.y.in ${PROJECT_SOURCE_DIR}/src/yang.y)
80 add_custom_target(bison
81 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/src
82 COMMAND bison -l -o parser_yang_bis.c --defines=parser_yang_bis.h yang.y
83 COMMAND flex -8 -L -o parser_yang_lex.c --header-file=parser_yang_lex.h yang.l
84 COMMAND ${SED_TOOL} -i 's/int yychar\;/int yychar\;\\nchar *s = NULL, *tmp_s = NULL, *ext_name = NULL\;\\nstruct lys_module *trg = NULL\;\\nstruct lys_node *tpdf_parent = NULL, *data_node = NULL\;\\nstruct lys_ext_instance_complex *ext_instance = NULL\;\\nint is_ext_instance\;\\nvoid *actual = NULL\;\\nenum yytokentype backup_type, actual_type = MODULE_KEYWORD\;\\nint64_t cnt_val = 0\;\\nint is_value = 0\;\\nvoid *yang_type = NULL\;/' parser_yang_bis.c
85 )
Pavol Vicancae54882016-03-30 12:24:43 +020086endif()
Pavol Vicanf7cc2852016-03-22 23:27:35 +010087
PavolVican5d377522017-06-07 15:08:14 +020088if (NOT DEFINED ENV{TRAVIS_BRANCH})
Michal Vasko8f3160e2017-09-27 11:25:26 +020089 execute_process(COMMAND "git" "rev-parse" "--abbrev-ref" "HEAD"
90 OUTPUT_VARIABLE GIT_BRANCH
91 OUTPUT_STRIP_TRAILING_WHITESPACE
92 ERROR_QUIET
93 )
94 if (NOT GIT_BRANCH)
95 set(ENV{TRAVIS_BRANCH} "master")
96 else()
97 if (GIT_BRANCH MATCHES "master|devel")
98 set(ENV{TRAVIS_BRANCH} ${GIT_BRANCH})
99 else()
100 set(ENV{TRAVIS_BRANCH} "master")
101 endif()
102 endif()
103 set(GIT_BRANCH $ENV{TRAVIS_BRANCH})
PavolVican37396272017-03-25 00:57:20 +0100104endif()
105
PavolVican5d377522017-06-07 15:08:14 +0200106if ($ENV{TRAVIS_BRANCH} STREQUAL "master")
Michal Vasko8f3160e2017-09-27 11:25:26 +0200107 set(PACKAGE_NAME "libyang")
108 set(BRANCH "master")
109 set(BUILD_TYPE "Package")
110 set(CONFLICT_PACKAGE_NAME "libyang-experimental")
PavolVican5d377522017-06-07 15:08:14 +0200111else ()
Michal Vasko8f3160e2017-09-27 11:25:26 +0200112 set(PACKAGE_NAME "libyang-experimental")
113 set(BRANCH "devel")
114 set(BUILD_TYPE "debug")
115 set(CONFLICT_PACKAGE_NAME "libyang")
PavolVican5d377522017-06-07 15:08:14 +0200116endif()
117# change version in config files
118configure_file(${PROJECT_SOURCE_DIR}/packages/libyang.spec.in ${PROJECT_BINARY_DIR}/build-packages/libyang.spec)
119configure_file(${PROJECT_SOURCE_DIR}/packages/libyang.dsc.in ${PROJECT_BINARY_DIR}/build-packages/libyang.dsc)
120configure_file(${PROJECT_SOURCE_DIR}/packages/debian.control.in ${PROJECT_BINARY_DIR}/build-packages/debian.control @ONLY)
121configure_file(${PROJECT_SOURCE_DIR}/packages/debian.rules.in ${PROJECT_BINARY_DIR}/build-packages/debian.rules)
122
PavolVicancf749252017-06-08 09:31:49 +0200123if (NOT DEB_BUILDER)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200124 message(WARNING "Missing tools (devscripts, debhelper package) for building deb package.\nYou won't be able to generate deb package from source code.\nCompiling libyang should still works fine.")
PavolVicancf749252017-06-08 09:31:49 +0200125else ()
Michal Vasko8f3160e2017-09-27 11:25:26 +0200126 # target for local build deb package
127 add_custom_target(build-deb
128 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
129 COMMAND build-packages/local-deb.sh
130 )
131 configure_file(${PROJECT_SOURCE_DIR}/packages/local-deb.sh.in ${PROJECT_BINARY_DIR}/build-packages/local-deb.sh @ONLY)
PavolVicancf749252017-06-08 09:31:49 +0200132endif()
PavolVican5d377522017-06-07 15:08:14 +0200133
PavolVicancf749252017-06-08 09:31:49 +0200134if (NOT RPM_BUILDER)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200135 message(WARNING "Missing tools (rpm package) for building rpm package. \nYou won't be able to generate rpm package from source code.\nCompiling libyang should still works fine.")
PavolVicancf749252017-06-08 09:31:49 +0200136else ()
Michal Vasko8f3160e2017-09-27 11:25:26 +0200137 # target for local build rpm package
138 string(REPLACE ${PROJECT_SOURCE_DIR} "." EXCLUDE_BUILD_DIR ${PROJECT_BINARY_DIR})
139 add_custom_target(build-rpm
140 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
141 COMMAND build-packages/local-rpm.sh
142 )
143 configure_file(${PROJECT_SOURCE_DIR}/packages/local-rpm.sh.in ${PROJECT_BINARY_DIR}/build-packages/local-rpm.sh @ONLY)
PavolVicancf749252017-06-08 09:31:49 +0200144endif()
PavolVican5d377522017-06-07 15:08:14 +0200145
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200146# check the supported platform
147if(NOT UNIX)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200148 message(FATAL_ERROR "Only *nix like systems are supported.")
Radek Krejci61cd5562015-10-05 14:06:34 +0200149endif()
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200150
Radek Krejci5a065542015-05-22 15:02:07 +0200151set(libsrc
Michal Vasko8f3160e2017-09-27 11:25:26 +0200152 src/common.c
153 src/context.c
154 src/log.c
155 src/dict.c
156 src/resolve.c
157 src/validation.c
158 src/xml.c
159 src/parser.c
160 src/parser_yin.c
161 src/parser_xml.c
162 src/parser_json.c
163 src/parser_yang_bis.c
164 src/parser_yang_lex.c
165 src/parser_yang.c
166 src/tree_schema.c
167 src/tree_data.c
168 src/extensions.c
169 src/printer.c
170 src/xpath.c
171 src/printer_yang.c
172 src/printer_yin.c
173 src/printer_xml.c
174 src/printer_tree.c
175 src/printer_info.c
176 src/printer_json.c
177 src/yang_types.c)
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200178
Radek Krejci5a065542015-05-22 15:02:07 +0200179set(lintsrc
Michal Vasko8f3160e2017-09-27 11:25:26 +0200180 tools/lint/main.c
181 tools/lint/main_ni.c
182 tools/lint/commands.c
183 tools/lint/completion.c
184 tools/lint/configuration.c
185 linenoise/linenoise.c)
Radek Krejci5a065542015-05-22 15:02:07 +0200186
Radek Krejcib4a481c2017-07-12 15:12:34 +0200187set(resrc
Michal Vasko8f3160e2017-09-27 11:25:26 +0200188 tools/re/main.c)
Radek Krejcib4a481c2017-07-12 15:12:34 +0200189
Michal Vasko9a542742016-06-23 13:04:51 +0200190set(yang2yinsrc
Michal Vasko8f3160e2017-09-27 11:25:26 +0200191 tools/yang2yin/main.c)
Michal Vasko9a542742016-06-23 13:04:51 +0200192
Tomas Cejka422c0072015-07-21 18:18:25 +0200193set(headers
Michal Vasko8f3160e2017-09-27 11:25:26 +0200194 src/libyang.h
195 src/tree_schema.h
196 src/tree_data.h
197 src/extensions.h
198 src/xml.h
199 src/dict.h)
Tomas Cejka422c0072015-07-21 18:18:25 +0200200
Radek Krejci5708fe62016-09-14 12:25:12 +0200201set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
202add_library(yangobj OBJECT ${libsrc})
203add_library(yang SHARED $<TARGET_OBJECTS:yangobj>)
Radek Krejci8f7cf402015-08-13 08:05:48 +0200204set_target_properties(yang PROPERTIES VERSION ${LIBYANG_VERSION} SOVERSION ${LIBYANG_SOVERSION})
Radek Krejcic43058d2017-01-04 16:29:58 +0100205set_target_properties(yang PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200206
Radek Krejci89658b32016-02-08 11:14:46 +0100207# link math
208target_link_libraries(yang m)
209
Radek Krejci858ad952017-01-04 11:16:32 +0100210#link dl
211target_link_libraries(yang ${CMAKE_DL_LIBS})
212
Radek Krejci7d9f46a2016-01-29 13:53:18 +0100213# find pthreads
214set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
215find_package(Threads REQUIRED)
216target_link_libraries(yang ${CMAKE_THREAD_LIBS_INIT})
217
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200218# find PCRE library
219find_package(PCRE REQUIRED)
220include_directories(${PCRE_INCLUDE_DIRS})
Radek Krejci1d949ec2015-10-02 13:35:05 +0200221target_link_libraries(yang ${PCRE_LIBRARIES})
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200222
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100223install(TARGETS yang DESTINATION ${CMAKE_INSTALL_LIBDIR})
224install(FILES ${headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libyang)
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200225
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100226find_package(PkgConfig)
227if(PKG_CONFIG_FOUND)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200228 # generate and install pkg-config file
229 configure_file("libyang.pc.in" "libyang.pc" @ONLY)
230 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libyang.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
231 # check that pkg-config includes the used path
232 execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --variable pc_path pkg-config RESULT_VARIABLE RETURN OUTPUT_VARIABLE PC_PATH ERROR_QUIET)
233 if(RETURN EQUAL 0)
234 string(REGEX MATCH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig" SUBSTR "${PC_PATH}")
235 string(LENGTH "${SUBSTR}" SUBSTR_LEN)
236 if(SUBSTR_LEN EQUAL 0)
237 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\".")
238 endif()
239 endif()
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100240endif()
241
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200242# generate doxygen documentation for libyang API
Radek Krejcie2626092015-10-08 11:25:44 +0200243find_package(Doxygen)
244if(DOXYGEN_FOUND)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200245 set(DOXYGEN_SKIP_DOT TRUE)
246 add_custom_target(doc
247 COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile
248 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
249 configure_file(Doxyfile.in Doxyfile)
Radek Krejcie2626092015-10-08 11:25:44 +0200250endif()
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200251
252# clean cmake cache
253add_custom_target(cclean
Michal Vasko8f3160e2017-09-27 11:25:26 +0200254 COMMAND make clean
255 COMMAND find . -iname '*cmake*' -not -name CMakeLists.txt -not -path './CMakeModules*' -exec rm -rf {} +
256 COMMAND rm -rf Makefile Doxyfile
257 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
Radek Krejci2cb30542015-04-10 08:33:57 +0200258
Radek Krejcic43058d2017-01-04 16:29:58 +0100259# YANG extensions plugins
260add_subdirectory(src/extensions)
261
262# yanglint
Radek Krejci5a065542015-05-22 15:02:07 +0200263add_executable(yanglint ${lintsrc})
Radek Krejci40dcad22017-06-08 11:35:32 +0200264target_link_libraries(yanglint yang)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100265install(TARGETS yanglint DESTINATION ${CMAKE_INSTALL_BINDIR})
266install(FILES ${PROJECT_SOURCE_DIR}/tools/lint/yanglint.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
Radek Krejci5a065542015-05-22 15:02:07 +0200267
Radek Krejcib4a481c2017-07-12 15:12:34 +0200268#yangre
269add_executable(yangre ${resrc})
270target_link_libraries(yangre yang)
271install(TARGETS yangre DESTINATION ${CMAKE_INSTALL_BINDIR})
272
Radek Krejcic43058d2017-01-04 16:29:58 +0100273# yang2yin
Michal Vasko9a542742016-06-23 13:04:51 +0200274add_executable(yang2yin ${yang2yinsrc})
275
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200276if(ENABLE_VALGRIND_TESTS)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200277 set(ENABLE_BUILD_TESTS ON)
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200278endif()
279
280if(ENABLE_BUILD_TESTS)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200281 find_package(CMocka 1.0.0)
282 if(CMOCKA_FOUND)
283 enable_testing()
284 add_subdirectory(tests)
285 endif(CMOCKA_FOUND)
Radek Krejcie02ce0a2015-11-04 14:14:20 +0100286endif(ENABLE_BUILD_TESTS)
Mislav Novakovica827b5f2016-02-29 11:47:22 +0100287
288if(JAVASCRIPT_BINDING)
Michal Vasko8f3160e2017-09-27 11:25:26 +0200289 include(swig/javascript/CMakeLists.txt)
Mislav Novakovica827b5f2016-02-29 11:47:22 +0100290endif()