blob: fe1890282155b0a885193a31fdc5bdbf0b181c54 [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
Radek Krejcib4a481c2017-07-12 15:12:34 +0200170set(resrc
171 tools/re/main.c)
172
Michal Vasko9a542742016-06-23 13:04:51 +0200173set(yang2yinsrc
174 tools/yang2yin/main.c)
175
Tomas Cejka422c0072015-07-21 18:18:25 +0200176set(headers
177 src/libyang.h
Michal Vasko2d162e12015-09-24 14:33:29 +0200178 src/tree_schema.h
Radek Krejcic6704c82015-10-06 11:12:45 +0200179 src/tree_data.h
Radek Krejcia1a6b762016-11-14 09:53:38 +0900180 src/extensions.h
Radek Krejci41912fe2015-10-22 10:22:12 +0200181 src/xml.h
182 src/dict.h)
Tomas Cejka422c0072015-07-21 18:18:25 +0200183
Radek Krejci5708fe62016-09-14 12:25:12 +0200184set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
185add_library(yangobj OBJECT ${libsrc})
186add_library(yang SHARED $<TARGET_OBJECTS:yangobj>)
Radek Krejci8f7cf402015-08-13 08:05:48 +0200187set_target_properties(yang PROPERTIES VERSION ${LIBYANG_VERSION} SOVERSION ${LIBYANG_SOVERSION})
Radek Krejcic43058d2017-01-04 16:29:58 +0100188set_target_properties(yang PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200189
Radek Krejci89658b32016-02-08 11:14:46 +0100190# link math
191target_link_libraries(yang m)
192
Radek Krejci858ad952017-01-04 11:16:32 +0100193#link dl
194target_link_libraries(yang ${CMAKE_DL_LIBS})
195
Radek Krejci7d9f46a2016-01-29 13:53:18 +0100196# find pthreads
197set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
198find_package(Threads REQUIRED)
199target_link_libraries(yang ${CMAKE_THREAD_LIBS_INIT})
200
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200201# find PCRE library
202find_package(PCRE REQUIRED)
203include_directories(${PCRE_INCLUDE_DIRS})
Radek Krejci1d949ec2015-10-02 13:35:05 +0200204target_link_libraries(yang ${PCRE_LIBRARIES})
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200205
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100206install(TARGETS yang DESTINATION ${CMAKE_INSTALL_LIBDIR})
207install(FILES ${headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libyang)
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200208
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100209find_package(PkgConfig)
210if(PKG_CONFIG_FOUND)
211 # generate and install pkg-config file
212 configure_file("libyang.pc.in" "libyang.pc" @ONLY)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100213 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libyang.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
Michal Vasko98ae1e12015-12-15 11:11:28 +0100214 # check that pkg-config includes the used path
215 execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --variable pc_path pkg-config RESULT_VARIABLE RETURN OUTPUT_VARIABLE PC_PATH ERROR_QUIET)
216 if(RETURN EQUAL 0)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100217 string(REGEX MATCH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig" SUBSTR "${PC_PATH}")
Michal Vasko33c001c2016-02-11 09:01:52 +0100218 string(LENGTH "${SUBSTR}" SUBSTR_LEN)
Michal Vaskob8d90a32016-02-09 13:59:42 +0100219 if(SUBSTR_LEN EQUAL 0)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100220 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 +0100221 endif()
222 endif()
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100223endif()
224
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200225# generate doxygen documentation for libyang API
Radek Krejcie2626092015-10-08 11:25:44 +0200226find_package(Doxygen)
227if(DOXYGEN_FOUND)
228 set(DOXYGEN_SKIP_DOT TRUE)
229 add_custom_target(doc
230 COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile
231 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
232 configure_file(Doxyfile.in Doxyfile)
233endif()
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200234
235# clean cmake cache
236add_custom_target(cclean
Radek Krejci25155ac2015-10-08 11:26:20 +0200237 COMMAND make clean
238 COMMAND find . -iname '*cmake*' -not -name CMakeLists.txt -not -path './CMakeModules*' -exec rm -rf {} +
239 COMMAND rm -rf Makefile Doxyfile
240 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
Radek Krejci2cb30542015-04-10 08:33:57 +0200241
Radek Krejcic43058d2017-01-04 16:29:58 +0100242# YANG extensions plugins
243add_subdirectory(src/extensions)
244
245# yanglint
Radek Krejci5a065542015-05-22 15:02:07 +0200246add_executable(yanglint ${lintsrc})
Radek Krejci40dcad22017-06-08 11:35:32 +0200247target_link_libraries(yanglint yang)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100248install(TARGETS yanglint DESTINATION ${CMAKE_INSTALL_BINDIR})
249install(FILES ${PROJECT_SOURCE_DIR}/tools/lint/yanglint.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
Radek Krejci5a065542015-05-22 15:02:07 +0200250
Radek Krejcib4a481c2017-07-12 15:12:34 +0200251#yangre
252add_executable(yangre ${resrc})
253target_link_libraries(yangre yang)
254install(TARGETS yangre DESTINATION ${CMAKE_INSTALL_BINDIR})
255
Radek Krejcic43058d2017-01-04 16:29:58 +0100256# yang2yin
Michal Vasko9a542742016-06-23 13:04:51 +0200257add_executable(yang2yin ${yang2yinsrc})
258
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200259if(ENABLE_VALGRIND_TESTS)
260 set(ENABLE_BUILD_TESTS ON)
261endif()
262
263if(ENABLE_BUILD_TESTS)
Radek Krejcie02ce0a2015-11-04 14:14:20 +0100264 find_package(CMocka 1.0.0)
265 if(CMOCKA_FOUND)
266 enable_testing()
267 add_subdirectory(tests)
268 endif(CMOCKA_FOUND)
269endif(ENABLE_BUILD_TESTS)
Mislav Novakovica827b5f2016-02-29 11:47:22 +0100270
271if(JAVASCRIPT_BINDING)
272 include(swig/javascript/CMakeLists.txt)
273endif()