blob: 3674d24f454b446b8af4bbe290a40d107ae49660 [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)
Radek Krejci054aca22017-09-04 13:20:45 +020014set(LIBYANG_MICRO_VERSION 52)
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
18option(ENABLE_CACHE "Enable schema caching (time-efficient at the cost of increased space-complexity)" OFF)
19if(ENABLE_CACHE)
20 set(ENABLE_CACHE_MACRO "/**\n * @brief Cache of some temporary information will be used.\n */\n#define LY_ENABLED_CACHE")
21else()
22 set(ENABLE_CACHE_MACRO "/**\n * @brief Cache of some temporary information will not be used.\n */")
23endif()
24
25configure_file(${PROJECT_SOURCE_DIR}/src/libyang.h.in ${PROJECT_SOURCE_DIR}/src/libyang.h @ONLY)
Radek Krejci8f7cf402015-08-13 08:05:48 +020026
Radek Krejcic43058d2017-01-04 16:29:58 +010027if(PLUGINS_DIR)
28 set(LIBYANG_EXT_PLUGINS_DIR ${PLUGINS_DIR})
29else()
30 set(LIBYANG_EXT_PLUGINS_DIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libyang)
31endif()
Radek Krejci858ad952017-01-04 11:16:32 +010032configure_file(${PROJECT_SOURCE_DIR}/src/extensions_config.h.in ${PROJECT_SOURCE_DIR}/src/extensions_config.h)
33
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +020034# include custom Modules
Radek Krejci977c1c82015-09-01 15:18:03 +020035set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +020036
Pavol Vicanf7cc2852016-03-22 23:27:35 +010037find_package(FLEX)
Pavol Vicancae54882016-03-30 12:24:43 +020038find_package(BISON)
PavolVicancf749252017-06-08 09:31:49 +020039find_program (DEB_BUILDER NAMES debuild)
40find_program (RPM_BUILDER NAMES rpmbuild)
Pavol Vican0a9716f2016-05-22 17:04:06 +020041find_program (SED_TOOL NAMES sed)
Pavol Vicanf7cc2852016-03-22 23:27:35 +010042
Pavol Vicancae54882016-03-30 12:24:43 +020043if(NOT BISON_FOUND)
Radek Krejcif19243b2017-02-02 09:23:40 +010044 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 +020045elseif (NOT FLEX_FOUND)
Radek Krejcif19243b2017-02-02 09:23:40 +010046 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 +020047else()
48 if (BISON_VERSION VERSION_LESS 3)
49 set(EMPTYDIR "")
50 else ()
51 set(EMPTYDIR "%empty")
52 endif()
53 configure_file(${PROJECT_SOURCE_DIR}/src/yang.y.in ${PROJECT_SOURCE_DIR}/src/yang.y)
54 add_custom_target(bison
Pavol Vican15bf3842016-04-05 22:25:00 +020055 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/src
PavolVican22e88682017-02-14 22:38:18 +010056 COMMAND bison -l -o parser_yang_bis.c --defines=parser_yang_bis.h yang.y
Pavol Vican6ab09d82016-10-24 11:30:02 +020057 COMMAND flex -8 -L -o parser_yang_lex.c --header-file=parser_yang_lex.h yang.l
PavolVican19857132017-03-01 09:47:47 +010058 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 +020059 )
60endif()
Pavol Vicanf7cc2852016-03-22 23:27:35 +010061
PavolVican5d377522017-06-07 15:08:14 +020062if (NOT DEFINED ENV{TRAVIS_BRANCH})
63 execute_process(COMMAND "git" "rev-parse" "--abbrev-ref" "HEAD"
64 OUTPUT_VARIABLE GIT_BRANCH
65 OUTPUT_STRIP_TRAILING_WHITESPACE
66 ERROR_QUIET
67 )
68 if (NOT GIT_BRANCH)
69 set(ENV{TRAVIS_BRANCH} "master")
70 else()
71 if (GIT_BRANCH MATCHES "master|devel")
72 set(ENV{TRAVIS_BRANCH} ${GIT_BRANCH})
73 else()
74 set(ENV{TRAVIS_BRANCH} "master")
75 endif()
PavolVican37396272017-03-25 00:57:20 +010076 endif()
PavolVican5d377522017-06-07 15:08:14 +020077 set(GIT_BRANCH $ENV{TRAVIS_BRANCH})
PavolVican37396272017-03-25 00:57:20 +010078endif()
79
PavolVican5d377522017-06-07 15:08:14 +020080if ($ENV{TRAVIS_BRANCH} STREQUAL "master")
81 set(PACKAGE_NAME "libyang")
82 set(BRANCH "master")
83 set(BUILD_TYPE "Package")
84 set(CONFLICT_PACKAGE_NAME "libyang-experimental")
85else ()
86 set(PACKAGE_NAME "libyang-experimental")
87 set(BRANCH "devel")
88 set(BUILD_TYPE "debug")
89 set(CONFLICT_PACKAGE_NAME "libyang")
90endif()
91# change version in config files
92configure_file(${PROJECT_SOURCE_DIR}/packages/libyang.spec.in ${PROJECT_BINARY_DIR}/build-packages/libyang.spec)
93configure_file(${PROJECT_SOURCE_DIR}/packages/libyang.dsc.in ${PROJECT_BINARY_DIR}/build-packages/libyang.dsc)
94configure_file(${PROJECT_SOURCE_DIR}/packages/debian.control.in ${PROJECT_BINARY_DIR}/build-packages/debian.control @ONLY)
95configure_file(${PROJECT_SOURCE_DIR}/packages/debian.rules.in ${PROJECT_BINARY_DIR}/build-packages/debian.rules)
96
PavolVicancf749252017-06-08 09:31:49 +020097if (NOT DEB_BUILDER)
98 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.")
99else ()
100 # target for local build deb package
101 add_custom_target(build-deb
102 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
103 COMMAND build-packages/local-deb.sh
104 )
105 configure_file(${PROJECT_SOURCE_DIR}/packages/local-deb.sh.in ${PROJECT_BINARY_DIR}/build-packages/local-deb.sh @ONLY)
106endif()
PavolVican5d377522017-06-07 15:08:14 +0200107
PavolVicancf749252017-06-08 09:31:49 +0200108if (NOT RPM_BUILDER)
109 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.")
110else ()
111 # target for local build rpm package
112 string(REPLACE ${PROJECT_SOURCE_DIR} "." EXCLUDE_BUILD_DIR ${PROJECT_BINARY_DIR})
113 add_custom_target(build-rpm
114 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
115 COMMAND build-packages/local-rpm.sh
116 )
117 configure_file(${PROJECT_SOURCE_DIR}/packages/local-rpm.sh.in ${PROJECT_BINARY_DIR}/build-packages/local-rpm.sh @ONLY)
118endif()
PavolVican5d377522017-06-07 15:08:14 +0200119
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200120# check the supported platform
121if(NOT UNIX)
122 message(FATAL_ERROR "Only *nix like systems are supported.")
123endif()
124
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200125# set default build type if not specified by user
126if(NOT CMAKE_BUILD_TYPE)
127 set(CMAKE_BUILD_TYPE debug)
128endif()
129
Radek Krejcic43058d2017-01-04 16:29:58 +0100130set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra")
Radek Krejci3045cf32015-05-28 10:58:52 +0200131set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
PavolVican37396272017-03-25 00:57:20 +0100132set(CMAKE_C_FLAGS_PACKAGE "-g -O2 -DNDEBUG")
Radek Krejcif19ac672016-02-05 16:16:36 +0100133set(CMAKE_C_FLAGS_DEBUG "-g -O0")
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200134
Michal Vaskoc64f5ae2017-08-21 11:36:33 +0200135# options
PavolVican37396272017-03-25 00:57:20 +0100136if((CMAKE_BUILD_TYPE STREQUAL debug) OR (CMAKE_BUILD_TYPE STREQUAL Package))
Radek Krejci61cd5562015-10-05 14:06:34 +0200137 option(ENABLE_BUILD_TESTS "Build tests" ON)
138 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
139else()
140 option(ENABLE_BUILD_TESTS "Build tests" OFF)
141 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
142endif()
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200143
Radek Krejci5a065542015-05-22 15:02:07 +0200144set(libsrc
Radek Krejci9b4ca392015-04-10 08:31:27 +0200145 src/common.c
Radek Krejcida04f4a2015-05-21 12:54:09 +0200146 src/context.c
Radek Krejci9b4ca392015-04-10 08:31:27 +0200147 src/log.c
Radek Krejciee627582015-04-20 17:39:59 +0200148 src/dict.c
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200149 src/resolve.c
Radek Krejcib1c12512015-08-11 11:22:04 +0200150 src/validation.c
Radek Krejcida04f4a2015-05-21 12:54:09 +0200151 src/xml.c
Radek Krejcib1c12512015-08-11 11:22:04 +0200152 src/parser.c
Radek Krejci998a0b82015-08-17 13:14:36 +0200153 src/parser_yin.c
154 src/parser_xml.c
Radek Krejci5449d472015-10-26 14:35:56 +0100155 src/parser_json.c
Pavol Vicancae54882016-03-30 12:24:43 +0200156 src/parser_yang_bis.c
157 src/parser_yang_lex.c
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100158 src/parser_yang.c
Michal Vasko2d162e12015-09-24 14:33:29 +0200159 src/tree_schema.c
160 src/tree_data.c
Radek Krejci0aa821a2016-12-08 11:21:35 +0100161 src/extensions.c
Radek Krejciefdd0ce2015-05-26 16:48:29 +0200162 src/printer.c
Michal Vaskoabbdaa02015-10-06 15:47:25 +0200163 src/xpath.c
Radek Krejci998a0b82015-08-17 13:14:36 +0200164 src/printer_yang.c
Michal Vaskoa63ca342016-02-05 14:29:19 +0100165 src/printer_yin.c
Michal Vasko9b9e4a02015-08-21 09:11:14 +0200166 src/printer_xml.c
Radek Krejci998a0b82015-08-17 13:14:36 +0200167 src/printer_tree.c
168 src/printer_info.c
169 src/printer_json.c
Radek Krejcida04f4a2015-05-21 12:54:09 +0200170 src/yang_types.c)
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200171
Radek Krejci5a065542015-05-22 15:02:07 +0200172set(lintsrc
Michal Vaskof3e59f12015-06-18 11:53:56 +0200173 tools/lint/main.c
Radek Krejci797590b2016-10-10 16:23:04 +0200174 tools/lint/main_ni.c
Michal Vaskof3e59f12015-06-18 11:53:56 +0200175 tools/lint/commands.c
Michal Vaskoe0cb2522015-07-01 10:24:53 +0200176 tools/lint/completion.c
Michal Vasko203b4e72015-06-30 15:25:15 +0200177 linenoise/linenoise.c)
Radek Krejci5a065542015-05-22 15:02:07 +0200178
Radek Krejcib4a481c2017-07-12 15:12:34 +0200179set(resrc
180 tools/re/main.c)
181
Michal Vasko9a542742016-06-23 13:04:51 +0200182set(yang2yinsrc
183 tools/yang2yin/main.c)
184
Tomas Cejka422c0072015-07-21 18:18:25 +0200185set(headers
186 src/libyang.h
Michal Vasko2d162e12015-09-24 14:33:29 +0200187 src/tree_schema.h
Radek Krejcic6704c82015-10-06 11:12:45 +0200188 src/tree_data.h
Radek Krejcia1a6b762016-11-14 09:53:38 +0900189 src/extensions.h
Radek Krejci41912fe2015-10-22 10:22:12 +0200190 src/xml.h
191 src/dict.h)
Tomas Cejka422c0072015-07-21 18:18:25 +0200192
Radek Krejci5708fe62016-09-14 12:25:12 +0200193set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
194add_library(yangobj OBJECT ${libsrc})
195add_library(yang SHARED $<TARGET_OBJECTS:yangobj>)
Radek Krejci8f7cf402015-08-13 08:05:48 +0200196set_target_properties(yang PROPERTIES VERSION ${LIBYANG_VERSION} SOVERSION ${LIBYANG_SOVERSION})
Radek Krejcic43058d2017-01-04 16:29:58 +0100197set_target_properties(yang PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200198
Radek Krejci89658b32016-02-08 11:14:46 +0100199# link math
200target_link_libraries(yang m)
201
Radek Krejci858ad952017-01-04 11:16:32 +0100202#link dl
203target_link_libraries(yang ${CMAKE_DL_LIBS})
204
Radek Krejci7d9f46a2016-01-29 13:53:18 +0100205# find pthreads
206set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
207find_package(Threads REQUIRED)
208target_link_libraries(yang ${CMAKE_THREAD_LIBS_INIT})
209
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200210# find PCRE library
211find_package(PCRE REQUIRED)
212include_directories(${PCRE_INCLUDE_DIRS})
Radek Krejci1d949ec2015-10-02 13:35:05 +0200213target_link_libraries(yang ${PCRE_LIBRARIES})
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200214
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100215install(TARGETS yang DESTINATION ${CMAKE_INSTALL_LIBDIR})
216install(FILES ${headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libyang)
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200217
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100218find_package(PkgConfig)
219if(PKG_CONFIG_FOUND)
220 # generate and install pkg-config file
221 configure_file("libyang.pc.in" "libyang.pc" @ONLY)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100222 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libyang.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
Michal Vasko98ae1e12015-12-15 11:11:28 +0100223 # check that pkg-config includes the used path
224 execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --variable pc_path pkg-config RESULT_VARIABLE RETURN OUTPUT_VARIABLE PC_PATH ERROR_QUIET)
225 if(RETURN EQUAL 0)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100226 string(REGEX MATCH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig" SUBSTR "${PC_PATH}")
Michal Vasko33c001c2016-02-11 09:01:52 +0100227 string(LENGTH "${SUBSTR}" SUBSTR_LEN)
Michal Vaskob8d90a32016-02-09 13:59:42 +0100228 if(SUBSTR_LEN EQUAL 0)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100229 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 +0100230 endif()
231 endif()
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100232endif()
233
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200234# generate doxygen documentation for libyang API
Radek Krejcie2626092015-10-08 11:25:44 +0200235find_package(Doxygen)
236if(DOXYGEN_FOUND)
237 set(DOXYGEN_SKIP_DOT TRUE)
238 add_custom_target(doc
239 COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile
240 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
241 configure_file(Doxyfile.in Doxyfile)
242endif()
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200243
244# clean cmake cache
245add_custom_target(cclean
Radek Krejci25155ac2015-10-08 11:26:20 +0200246 COMMAND make clean
247 COMMAND find . -iname '*cmake*' -not -name CMakeLists.txt -not -path './CMakeModules*' -exec rm -rf {} +
248 COMMAND rm -rf Makefile Doxyfile
249 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
Radek Krejci2cb30542015-04-10 08:33:57 +0200250
Radek Krejcic43058d2017-01-04 16:29:58 +0100251# YANG extensions plugins
252add_subdirectory(src/extensions)
253
254# yanglint
Radek Krejci5a065542015-05-22 15:02:07 +0200255add_executable(yanglint ${lintsrc})
Radek Krejci40dcad22017-06-08 11:35:32 +0200256target_link_libraries(yanglint yang)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100257install(TARGETS yanglint DESTINATION ${CMAKE_INSTALL_BINDIR})
258install(FILES ${PROJECT_SOURCE_DIR}/tools/lint/yanglint.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
Radek Krejci5a065542015-05-22 15:02:07 +0200259
Radek Krejcib4a481c2017-07-12 15:12:34 +0200260#yangre
261add_executable(yangre ${resrc})
262target_link_libraries(yangre yang)
263install(TARGETS yangre DESTINATION ${CMAKE_INSTALL_BINDIR})
264
Radek Krejcic43058d2017-01-04 16:29:58 +0100265# yang2yin
Michal Vasko9a542742016-06-23 13:04:51 +0200266add_executable(yang2yin ${yang2yinsrc})
267
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200268if(ENABLE_VALGRIND_TESTS)
269 set(ENABLE_BUILD_TESTS ON)
270endif()
271
272if(ENABLE_BUILD_TESTS)
Radek Krejcie02ce0a2015-11-04 14:14:20 +0100273 find_package(CMocka 1.0.0)
274 if(CMOCKA_FOUND)
275 enable_testing()
276 add_subdirectory(tests)
277 endif(CMOCKA_FOUND)
278endif(ENABLE_BUILD_TESTS)
Mislav Novakovica827b5f2016-02-29 11:47:22 +0100279
280if(JAVASCRIPT_BINDING)
281 include(swig/javascript/CMakeLists.txt)
282endif()