blob: 3fa0909827eb5b543bbf273b0ab7ecb48b429919 [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)
Michal Vasko27b91d02017-05-23 14:40:00 +020013set(LIBYANG_MINOR_VERSION 13)
Michal Vasko37159fd2017-07-11 14:27:01 +020014set(LIBYANG_MICRO_VERSION 26)
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})
Radek Krejci60cdf282016-10-10 16:22:22 +020017configure_file(${PROJECT_SOURCE_DIR}/src/libyang.h.in ${PROJECT_SOURCE_DIR}/src/libyang.h)
Radek Krejci8f7cf402015-08-13 08:05:48 +020018
Radek Krejcic43058d2017-01-04 16:29:58 +010019if(PLUGINS_DIR)
20 set(LIBYANG_EXT_PLUGINS_DIR ${PLUGINS_DIR})
21else()
22 set(LIBYANG_EXT_PLUGINS_DIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libyang)
23endif()
Radek Krejci858ad952017-01-04 11:16:32 +010024configure_file(${PROJECT_SOURCE_DIR}/src/extensions_config.h.in ${PROJECT_SOURCE_DIR}/src/extensions_config.h)
25
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +020026# include custom Modules
Radek Krejci977c1c82015-09-01 15:18:03 +020027set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +020028
Pavol Vicanf7cc2852016-03-22 23:27:35 +010029find_package(FLEX)
Pavol Vicancae54882016-03-30 12:24:43 +020030find_package(BISON)
PavolVicancf749252017-06-08 09:31:49 +020031find_program (DEB_BUILDER NAMES debuild)
32find_program (RPM_BUILDER NAMES rpmbuild)
Pavol Vican0a9716f2016-05-22 17:04:06 +020033find_program (SED_TOOL NAMES sed)
Pavol Vicanf7cc2852016-03-22 23:27:35 +010034
Pavol Vicancae54882016-03-30 12:24:43 +020035if(NOT BISON_FOUND)
Radek Krejcif19243b2017-02-02 09:23:40 +010036 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 +020037elseif (NOT FLEX_FOUND)
Radek Krejcif19243b2017-02-02 09:23:40 +010038 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 +020039else()
40 if (BISON_VERSION VERSION_LESS 3)
41 set(EMPTYDIR "")
42 else ()
43 set(EMPTYDIR "%empty")
44 endif()
45 configure_file(${PROJECT_SOURCE_DIR}/src/yang.y.in ${PROJECT_SOURCE_DIR}/src/yang.y)
46 add_custom_target(bison
Pavol Vican15bf3842016-04-05 22:25:00 +020047 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/src
PavolVican22e88682017-02-14 22:38:18 +010048 COMMAND bison -l -o parser_yang_bis.c --defines=parser_yang_bis.h yang.y
Pavol Vican6ab09d82016-10-24 11:30:02 +020049 COMMAND flex -8 -L -o parser_yang_lex.c --header-file=parser_yang_lex.h yang.l
PavolVican19857132017-03-01 09:47:47 +010050 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
Pavol Vicancae54882016-03-30 12:24:43 +020051 )
52endif()
Pavol Vicanf7cc2852016-03-22 23:27:35 +010053
PavolVican5d377522017-06-07 15:08:14 +020054if (NOT DEFINED ENV{TRAVIS_BRANCH})
55 execute_process(COMMAND "git" "rev-parse" "--abbrev-ref" "HEAD"
56 OUTPUT_VARIABLE GIT_BRANCH
57 OUTPUT_STRIP_TRAILING_WHITESPACE
58 ERROR_QUIET
59 )
60 if (NOT GIT_BRANCH)
61 set(ENV{TRAVIS_BRANCH} "master")
62 else()
63 if (GIT_BRANCH MATCHES "master|devel")
64 set(ENV{TRAVIS_BRANCH} ${GIT_BRANCH})
65 else()
66 set(ENV{TRAVIS_BRANCH} "master")
67 endif()
PavolVican37396272017-03-25 00:57:20 +010068 endif()
PavolVican5d377522017-06-07 15:08:14 +020069 set(GIT_BRANCH $ENV{TRAVIS_BRANCH})
PavolVican37396272017-03-25 00:57:20 +010070endif()
71
PavolVican5d377522017-06-07 15:08:14 +020072if ($ENV{TRAVIS_BRANCH} STREQUAL "master")
73 set(PACKAGE_NAME "libyang")
74 set(BRANCH "master")
75 set(BUILD_TYPE "Package")
76 set(CONFLICT_PACKAGE_NAME "libyang-experimental")
77else ()
78 set(PACKAGE_NAME "libyang-experimental")
79 set(BRANCH "devel")
80 set(BUILD_TYPE "debug")
81 set(CONFLICT_PACKAGE_NAME "libyang")
82endif()
83# change version in config files
84configure_file(${PROJECT_SOURCE_DIR}/packages/libyang.spec.in ${PROJECT_BINARY_DIR}/build-packages/libyang.spec)
85configure_file(${PROJECT_SOURCE_DIR}/packages/libyang.dsc.in ${PROJECT_BINARY_DIR}/build-packages/libyang.dsc)
86configure_file(${PROJECT_SOURCE_DIR}/packages/debian.control.in ${PROJECT_BINARY_DIR}/build-packages/debian.control @ONLY)
87configure_file(${PROJECT_SOURCE_DIR}/packages/debian.rules.in ${PROJECT_BINARY_DIR}/build-packages/debian.rules)
88
PavolVicancf749252017-06-08 09:31:49 +020089if (NOT DEB_BUILDER)
90 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.")
91else ()
92 # target for local build deb package
93 add_custom_target(build-deb
94 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
95 COMMAND build-packages/local-deb.sh
96 )
97 configure_file(${PROJECT_SOURCE_DIR}/packages/local-deb.sh.in ${PROJECT_BINARY_DIR}/build-packages/local-deb.sh @ONLY)
98endif()
PavolVican5d377522017-06-07 15:08:14 +020099
PavolVicancf749252017-06-08 09:31:49 +0200100if (NOT RPM_BUILDER)
101 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.")
102else ()
103 # target for local build rpm package
104 string(REPLACE ${PROJECT_SOURCE_DIR} "." EXCLUDE_BUILD_DIR ${PROJECT_BINARY_DIR})
105 add_custom_target(build-rpm
106 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
107 COMMAND build-packages/local-rpm.sh
108 )
109 configure_file(${PROJECT_SOURCE_DIR}/packages/local-rpm.sh.in ${PROJECT_BINARY_DIR}/build-packages/local-rpm.sh @ONLY)
110endif()
PavolVican5d377522017-06-07 15:08:14 +0200111
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200112# check the supported platform
113if(NOT UNIX)
114 message(FATAL_ERROR "Only *nix like systems are supported.")
115endif()
116
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200117# set default build type if not specified by user
118if(NOT CMAKE_BUILD_TYPE)
119 set(CMAKE_BUILD_TYPE debug)
120endif()
121
Radek Krejcic43058d2017-01-04 16:29:58 +0100122set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra")
Radek Krejci3045cf32015-05-28 10:58:52 +0200123set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
PavolVican37396272017-03-25 00:57:20 +0100124set(CMAKE_C_FLAGS_PACKAGE "-g -O2 -DNDEBUG")
Radek Krejcif19ac672016-02-05 16:16:36 +0100125set(CMAKE_C_FLAGS_DEBUG "-g -O0")
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200126
PavolVican37396272017-03-25 00:57:20 +0100127if((CMAKE_BUILD_TYPE STREQUAL debug) OR (CMAKE_BUILD_TYPE STREQUAL Package))
Radek Krejci61cd5562015-10-05 14:06:34 +0200128 option(ENABLE_BUILD_TESTS "Build tests" ON)
129 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
130else()
131 option(ENABLE_BUILD_TESTS "Build tests" OFF)
132 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
133endif()
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200134
Radek Krejci5a065542015-05-22 15:02:07 +0200135set(libsrc
Radek Krejci9b4ca392015-04-10 08:31:27 +0200136 src/common.c
Radek Krejcida04f4a2015-05-21 12:54:09 +0200137 src/context.c
Radek Krejci9b4ca392015-04-10 08:31:27 +0200138 src/log.c
Radek Krejciee627582015-04-20 17:39:59 +0200139 src/dict.c
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200140 src/resolve.c
Radek Krejcib1c12512015-08-11 11:22:04 +0200141 src/validation.c
Radek Krejcida04f4a2015-05-21 12:54:09 +0200142 src/xml.c
Radek Krejcib1c12512015-08-11 11:22:04 +0200143 src/parser.c
Radek Krejci998a0b82015-08-17 13:14:36 +0200144 src/parser_yin.c
145 src/parser_xml.c
Radek Krejci5449d472015-10-26 14:35:56 +0100146 src/parser_json.c
Pavol Vicancae54882016-03-30 12:24:43 +0200147 src/parser_yang_bis.c
148 src/parser_yang_lex.c
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100149 src/parser_yang.c
Michal Vasko2d162e12015-09-24 14:33:29 +0200150 src/tree_schema.c
151 src/tree_data.c
Radek Krejci0aa821a2016-12-08 11:21:35 +0100152 src/extensions.c
Radek Krejciefdd0ce2015-05-26 16:48:29 +0200153 src/printer.c
Michal Vaskoabbdaa02015-10-06 15:47:25 +0200154 src/xpath.c
Radek Krejci998a0b82015-08-17 13:14:36 +0200155 src/printer_yang.c
Michal Vaskoa63ca342016-02-05 14:29:19 +0100156 src/printer_yin.c
Michal Vasko9b9e4a02015-08-21 09:11:14 +0200157 src/printer_xml.c
Radek Krejci998a0b82015-08-17 13:14:36 +0200158 src/printer_tree.c
159 src/printer_info.c
160 src/printer_json.c
Radek Krejcida04f4a2015-05-21 12:54:09 +0200161 src/yang_types.c)
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200162
Radek Krejci5a065542015-05-22 15:02:07 +0200163set(lintsrc
Michal Vaskof3e59f12015-06-18 11:53:56 +0200164 tools/lint/main.c
Radek Krejci797590b2016-10-10 16:23:04 +0200165 tools/lint/main_ni.c
Michal Vaskof3e59f12015-06-18 11:53:56 +0200166 tools/lint/commands.c
Michal Vaskoe0cb2522015-07-01 10:24:53 +0200167 tools/lint/completion.c
Michal Vasko203b4e72015-06-30 15:25:15 +0200168 linenoise/linenoise.c)
Radek Krejci5a065542015-05-22 15:02:07 +0200169
Michal Vasko9a542742016-06-23 13:04:51 +0200170set(yang2yinsrc
171 tools/yang2yin/main.c)
172
Tomas Cejka422c0072015-07-21 18:18:25 +0200173set(headers
174 src/libyang.h
Michal Vasko2d162e12015-09-24 14:33:29 +0200175 src/tree_schema.h
Radek Krejcic6704c82015-10-06 11:12:45 +0200176 src/tree_data.h
Radek Krejcia1a6b762016-11-14 09:53:38 +0900177 src/extensions.h
Radek Krejci41912fe2015-10-22 10:22:12 +0200178 src/xml.h
179 src/dict.h)
Tomas Cejka422c0072015-07-21 18:18:25 +0200180
Radek Krejci5708fe62016-09-14 12:25:12 +0200181set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
182add_library(yangobj OBJECT ${libsrc})
183add_library(yang SHARED $<TARGET_OBJECTS:yangobj>)
Radek Krejci8f7cf402015-08-13 08:05:48 +0200184set_target_properties(yang PROPERTIES VERSION ${LIBYANG_VERSION} SOVERSION ${LIBYANG_SOVERSION})
Radek Krejcic43058d2017-01-04 16:29:58 +0100185set_target_properties(yang PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200186
Radek Krejci89658b32016-02-08 11:14:46 +0100187# link math
188target_link_libraries(yang m)
189
Radek Krejci858ad952017-01-04 11:16:32 +0100190#link dl
191target_link_libraries(yang ${CMAKE_DL_LIBS})
192
Radek Krejci7d9f46a2016-01-29 13:53:18 +0100193# find pthreads
194set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
195find_package(Threads REQUIRED)
196target_link_libraries(yang ${CMAKE_THREAD_LIBS_INIT})
197
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200198# find PCRE library
199find_package(PCRE REQUIRED)
200include_directories(${PCRE_INCLUDE_DIRS})
Radek Krejci1d949ec2015-10-02 13:35:05 +0200201target_link_libraries(yang ${PCRE_LIBRARIES})
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200202
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100203install(TARGETS yang DESTINATION ${CMAKE_INSTALL_LIBDIR})
204install(FILES ${headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libyang)
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200205
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100206find_package(PkgConfig)
207if(PKG_CONFIG_FOUND)
208 # generate and install pkg-config file
209 configure_file("libyang.pc.in" "libyang.pc" @ONLY)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100210 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libyang.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
Michal Vasko98ae1e12015-12-15 11:11:28 +0100211 # check that pkg-config includes the used path
212 execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --variable pc_path pkg-config RESULT_VARIABLE RETURN OUTPUT_VARIABLE PC_PATH ERROR_QUIET)
213 if(RETURN EQUAL 0)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100214 string(REGEX MATCH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig" SUBSTR "${PC_PATH}")
Michal Vasko33c001c2016-02-11 09:01:52 +0100215 string(LENGTH "${SUBSTR}" SUBSTR_LEN)
Michal Vaskob8d90a32016-02-09 13:59:42 +0100216 if(SUBSTR_LEN EQUAL 0)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100217 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 +0100218 endif()
219 endif()
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100220endif()
221
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200222# generate doxygen documentation for libyang API
Radek Krejcie2626092015-10-08 11:25:44 +0200223find_package(Doxygen)
224if(DOXYGEN_FOUND)
225 set(DOXYGEN_SKIP_DOT TRUE)
226 add_custom_target(doc
227 COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile
228 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
229 configure_file(Doxyfile.in Doxyfile)
230endif()
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200231
232# clean cmake cache
233add_custom_target(cclean
Radek Krejci25155ac2015-10-08 11:26:20 +0200234 COMMAND make clean
235 COMMAND find . -iname '*cmake*' -not -name CMakeLists.txt -not -path './CMakeModules*' -exec rm -rf {} +
236 COMMAND rm -rf Makefile Doxyfile
237 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
Radek Krejci2cb30542015-04-10 08:33:57 +0200238
Radek Krejcic43058d2017-01-04 16:29:58 +0100239# YANG extensions plugins
240add_subdirectory(src/extensions)
241
242# yanglint
Radek Krejci5a065542015-05-22 15:02:07 +0200243add_executable(yanglint ${lintsrc})
Radek Krejci40dcad22017-06-08 11:35:32 +0200244target_link_libraries(yanglint yang)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100245install(TARGETS yanglint DESTINATION ${CMAKE_INSTALL_BINDIR})
246install(FILES ${PROJECT_SOURCE_DIR}/tools/lint/yanglint.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
Radek Krejci5a065542015-05-22 15:02:07 +0200247
Radek Krejcic43058d2017-01-04 16:29:58 +0100248# yang2yin
Michal Vasko9a542742016-06-23 13:04:51 +0200249add_executable(yang2yin ${yang2yinsrc})
250
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200251if(ENABLE_VALGRIND_TESTS)
252 set(ENABLE_BUILD_TESTS ON)
253endif()
254
255if(ENABLE_BUILD_TESTS)
Radek Krejcie02ce0a2015-11-04 14:14:20 +0100256 find_package(CMocka 1.0.0)
257 if(CMOCKA_FOUND)
258 enable_testing()
259 add_subdirectory(tests)
260 endif(CMOCKA_FOUND)
261endif(ENABLE_BUILD_TESTS)
Mislav Novakovica827b5f2016-02-29 11:47:22 +0100262
263if(JAVASCRIPT_BINDING)
264 include(swig/javascript/CMakeLists.txt)
265endif()