blob: d7eac8f2df591fb8d565fee95fd1cf61d0b60ea5 [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)
Radek Krejcib92cbde2016-12-14 16:44:56 +010010set(LIBYANG_MINOR_VERSION 12)
Michal Vasko6a8641f2017-03-24 11:24:21 +010011set(LIBYANG_MICRO_VERSION 130)
PavolVican37396272017-03-25 00:57:20 +010012set(LIBYANG_VERSION ${LIBYANG_MAJOR_VERSION}.${LIBYANG_MINOR_VERSION}.${LIBYANG_MICRO_VERSION} CACHE STRING "")
Radek Krejci8f7cf402015-08-13 08:05:48 +020013set(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
Radek Krejcic43058d2017-01-04 16:29:58 +010016if(PLUGINS_DIR)
17 set(LIBYANG_EXT_PLUGINS_DIR ${PLUGINS_DIR})
18else()
19 set(LIBYANG_EXT_PLUGINS_DIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libyang)
20endif()
Radek Krejci858ad952017-01-04 11:16:32 +010021configure_file(${PROJECT_SOURCE_DIR}/src/extensions_config.h.in ${PROJECT_SOURCE_DIR}/src/extensions_config.h)
22
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +020023# include custom Modules
Radek Krejci977c1c82015-09-01 15:18:03 +020024set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +020025
Pavol Vicanf7cc2852016-03-22 23:27:35 +010026find_package(FLEX)
Pavol Vicancae54882016-03-30 12:24:43 +020027find_package(BISON)
Pavol Vican0a9716f2016-05-22 17:04:06 +020028find_program (SED_TOOL NAMES sed)
Pavol Vicanf7cc2852016-03-22 23:27:35 +010029
Pavol Vicancae54882016-03-30 12:24:43 +020030if(NOT BISON_FOUND)
Radek Krejcif19243b2017-02-02 09:23:40 +010031 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 +020032elseif (NOT FLEX_FOUND)
Radek Krejcif19243b2017-02-02 09:23:40 +010033 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 +020034else()
35 if (BISON_VERSION VERSION_LESS 3)
36 set(EMPTYDIR "")
37 else ()
38 set(EMPTYDIR "%empty")
39 endif()
40 configure_file(${PROJECT_SOURCE_DIR}/src/yang.y.in ${PROJECT_SOURCE_DIR}/src/yang.y)
41 add_custom_target(bison
Pavol Vican15bf3842016-04-05 22:25:00 +020042 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/src
PavolVican22e88682017-02-14 22:38:18 +010043 COMMAND bison -l -o parser_yang_bis.c --defines=parser_yang_bis.h yang.y
Pavol Vican6ab09d82016-10-24 11:30:02 +020044 COMMAND flex -8 -L -o parser_yang_lex.c --header-file=parser_yang_lex.h yang.l
PavolVican19857132017-03-01 09:47:47 +010045 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 +020046 )
47endif()
Pavol Vicanf7cc2852016-03-22 23:27:35 +010048
PavolVican37396272017-03-25 00:57:20 +010049if (DEFINED $ENV{TRAVIS_BRANCH})
50 if ($ENV{TRAVIS_BRANCH} STREQUAL "devel")
51 set(PACKAGE_NAME "libyang-experimental")
52 set(BRANCH "devel")
53 set(BUILD_TYPE "debug")
54 else ()
55 set(PACKAGE_NAME "libyang")
56 set(BRANCH "master")
57 set(BUILD_TYPE "Package")
58 endif()
59 # change version in config files
60 configure_file(${PROJECT_SOURCE_DIR}/packages/libyang.spec.in ${PROJECT_BINARY_DIR}/packages/libyang.spec)
61 configure_file(${PROJECT_SOURCE_DIR}/packages/libyang.dsc.in ${PROJECT_BINARY_DIR}/packages/libyang.dsc)
62 configure_file(${PROJECT_SOURCE_DIR}/packages/debian.control.in ${PROJECT_BINARY_DIR}/packages/debian.control)
63 configure_file(${PROJECT_SOURCE_DIR}/packages/debian.rules.in ${PROJECT_BINARY_DIR}/packages/debian.rules)
64endif()
65
66
Radek Krejcie74ea8c2015-04-09 18:06:20 +020067# check the supported platform
68if(NOT UNIX)
69 message(FATAL_ERROR "Only *nix like systems are supported.")
70endif()
71
Radek Krejcie74ea8c2015-04-09 18:06:20 +020072# set default build type if not specified by user
73if(NOT CMAKE_BUILD_TYPE)
74 set(CMAKE_BUILD_TYPE debug)
75endif()
76
Radek Krejcic43058d2017-01-04 16:29:58 +010077set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra")
Radek Krejci3045cf32015-05-28 10:58:52 +020078set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
PavolVican37396272017-03-25 00:57:20 +010079set(CMAKE_C_FLAGS_PACKAGE "-g -O2 -DNDEBUG")
Radek Krejcif19ac672016-02-05 16:16:36 +010080set(CMAKE_C_FLAGS_DEBUG "-g -O0")
Radek Krejcie74ea8c2015-04-09 18:06:20 +020081
PavolVican37396272017-03-25 00:57:20 +010082if((CMAKE_BUILD_TYPE STREQUAL debug) OR (CMAKE_BUILD_TYPE STREQUAL Package))
Radek Krejci61cd5562015-10-05 14:06:34 +020083 option(ENABLE_BUILD_TESTS "Build tests" ON)
84 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
85else()
86 option(ENABLE_BUILD_TESTS "Build tests" OFF)
87 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
88endif()
Mislav Novakovic021b9a12015-10-01 20:50:21 +020089
Radek Krejci5a065542015-05-22 15:02:07 +020090set(libsrc
Radek Krejci9b4ca392015-04-10 08:31:27 +020091 src/common.c
Radek Krejcida04f4a2015-05-21 12:54:09 +020092 src/context.c
Radek Krejci9b4ca392015-04-10 08:31:27 +020093 src/log.c
Radek Krejciee627582015-04-20 17:39:59 +020094 src/dict.c
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020095 src/resolve.c
Radek Krejcib1c12512015-08-11 11:22:04 +020096 src/validation.c
Radek Krejcida04f4a2015-05-21 12:54:09 +020097 src/xml.c
Radek Krejcib1c12512015-08-11 11:22:04 +020098 src/parser.c
Radek Krejci998a0b82015-08-17 13:14:36 +020099 src/parser_yin.c
100 src/parser_xml.c
Radek Krejci5449d472015-10-26 14:35:56 +0100101 src/parser_json.c
Pavol Vicancae54882016-03-30 12:24:43 +0200102 src/parser_yang_bis.c
103 src/parser_yang_lex.c
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100104 src/parser_yang.c
Michal Vasko2d162e12015-09-24 14:33:29 +0200105 src/tree_schema.c
106 src/tree_data.c
Radek Krejci0aa821a2016-12-08 11:21:35 +0100107 src/extensions.c
Radek Krejciefdd0ce2015-05-26 16:48:29 +0200108 src/printer.c
Michal Vaskoabbdaa02015-10-06 15:47:25 +0200109 src/xpath.c
Radek Krejci998a0b82015-08-17 13:14:36 +0200110 src/printer_yang.c
Michal Vaskoa63ca342016-02-05 14:29:19 +0100111 src/printer_yin.c
Michal Vasko9b9e4a02015-08-21 09:11:14 +0200112 src/printer_xml.c
Radek Krejci998a0b82015-08-17 13:14:36 +0200113 src/printer_tree.c
114 src/printer_info.c
115 src/printer_json.c
Radek Krejcida04f4a2015-05-21 12:54:09 +0200116 src/yang_types.c)
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200117
Radek Krejci5a065542015-05-22 15:02:07 +0200118set(lintsrc
Michal Vaskof3e59f12015-06-18 11:53:56 +0200119 tools/lint/main.c
Radek Krejci797590b2016-10-10 16:23:04 +0200120 tools/lint/main_ni.c
Michal Vaskof3e59f12015-06-18 11:53:56 +0200121 tools/lint/commands.c
Michal Vaskoe0cb2522015-07-01 10:24:53 +0200122 tools/lint/completion.c
Michal Vasko203b4e72015-06-30 15:25:15 +0200123 linenoise/linenoise.c)
Radek Krejci5a065542015-05-22 15:02:07 +0200124
Michal Vasko9a542742016-06-23 13:04:51 +0200125set(yang2yinsrc
126 tools/yang2yin/main.c)
127
Tomas Cejka422c0072015-07-21 18:18:25 +0200128set(headers
129 src/libyang.h
Michal Vasko2d162e12015-09-24 14:33:29 +0200130 src/tree_schema.h
Radek Krejcic6704c82015-10-06 11:12:45 +0200131 src/tree_data.h
Radek Krejcia1a6b762016-11-14 09:53:38 +0900132 src/extensions.h
Radek Krejci41912fe2015-10-22 10:22:12 +0200133 src/xml.h
134 src/dict.h)
Tomas Cejka422c0072015-07-21 18:18:25 +0200135
Abhi Keshav7afa0812016-10-25 14:52:51 -0700136# create static libyang library
137if(STATIC)
138 add_library(yang_static STATIC ${libsrc})
139 set_target_properties(yang_static PROPERTIES OUTPUT_NAME yang)
Radek Krejcic43058d2017-01-04 16:29:58 +0100140 set_target_properties(yang_static PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
Abhi Keshav7afa0812016-10-25 14:52:51 -0700141 target_link_libraries(yang_static m)
Radek Krejci858ad952017-01-04 11:16:32 +0100142 target_link_libraries(yang_static ${CMAKE_DL_LIBS})
Abhi Keshav7afa0812016-10-25 14:52:51 -0700143 target_link_libraries(yang_static ${CMAKE_THREAD_LIBS_INIT})
144 target_link_libraries(yang_static ${PCRE_LIBRARIES})
Radek Krejci4c79e702016-11-06 13:00:20 +0100145 install(TARGETS yang_static DESTINATION ${CMAKE_INSTALL_LIBDIR})
Abhi Keshav7afa0812016-10-25 14:52:51 -0700146endif(STATIC)
147
Radek Krejci5708fe62016-09-14 12:25:12 +0200148set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
149add_library(yangobj OBJECT ${libsrc})
150add_library(yang SHARED $<TARGET_OBJECTS:yangobj>)
Radek Krejci8f7cf402015-08-13 08:05:48 +0200151set_target_properties(yang PROPERTIES VERSION ${LIBYANG_VERSION} SOVERSION ${LIBYANG_SOVERSION})
Radek Krejcic43058d2017-01-04 16:29:58 +0100152set_target_properties(yang PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200153
Radek Krejci89658b32016-02-08 11:14:46 +0100154# link math
155target_link_libraries(yang m)
156
Radek Krejci858ad952017-01-04 11:16:32 +0100157#link dl
158target_link_libraries(yang ${CMAKE_DL_LIBS})
159
Radek Krejci7d9f46a2016-01-29 13:53:18 +0100160# find pthreads
161set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
162find_package(Threads REQUIRED)
163target_link_libraries(yang ${CMAKE_THREAD_LIBS_INIT})
164
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200165# find PCRE library
166find_package(PCRE REQUIRED)
167include_directories(${PCRE_INCLUDE_DIRS})
Radek Krejci1d949ec2015-10-02 13:35:05 +0200168target_link_libraries(yang ${PCRE_LIBRARIES})
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200169
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100170install(TARGETS yang DESTINATION ${CMAKE_INSTALL_LIBDIR})
171install(FILES ${headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libyang)
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200172
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100173find_package(PkgConfig)
174if(PKG_CONFIG_FOUND)
175 # generate and install pkg-config file
176 configure_file("libyang.pc.in" "libyang.pc" @ONLY)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100177 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libyang.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
Michal Vasko98ae1e12015-12-15 11:11:28 +0100178 # check that pkg-config includes the used path
179 execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --variable pc_path pkg-config RESULT_VARIABLE RETURN OUTPUT_VARIABLE PC_PATH ERROR_QUIET)
180 if(RETURN EQUAL 0)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100181 string(REGEX MATCH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig" SUBSTR "${PC_PATH}")
Michal Vasko33c001c2016-02-11 09:01:52 +0100182 string(LENGTH "${SUBSTR}" SUBSTR_LEN)
Michal Vaskob8d90a32016-02-09 13:59:42 +0100183 if(SUBSTR_LEN EQUAL 0)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100184 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 +0100185 endif()
186 endif()
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100187endif()
188
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200189# generate doxygen documentation for libyang API
Radek Krejcie2626092015-10-08 11:25:44 +0200190find_package(Doxygen)
191if(DOXYGEN_FOUND)
192 set(DOXYGEN_SKIP_DOT TRUE)
193 add_custom_target(doc
194 COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile
195 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
196 configure_file(Doxyfile.in Doxyfile)
197endif()
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200198
199# clean cmake cache
200add_custom_target(cclean
Radek Krejci25155ac2015-10-08 11:26:20 +0200201 COMMAND make clean
202 COMMAND find . -iname '*cmake*' -not -name CMakeLists.txt -not -path './CMakeModules*' -exec rm -rf {} +
203 COMMAND rm -rf Makefile Doxyfile
204 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
Radek Krejci2cb30542015-04-10 08:33:57 +0200205
Radek Krejcic43058d2017-01-04 16:29:58 +0100206# YANG extensions plugins
207add_subdirectory(src/extensions)
208
209# yanglint
Radek Krejci5a065542015-05-22 15:02:07 +0200210add_executable(yanglint ${lintsrc})
Radek Krejci1d949ec2015-10-02 13:35:05 +0200211target_link_libraries(yanglint yang)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100212install(TARGETS yanglint DESTINATION ${CMAKE_INSTALL_BINDIR})
213install(FILES ${PROJECT_SOURCE_DIR}/tools/lint/yanglint.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
Radek Krejci5a065542015-05-22 15:02:07 +0200214
Radek Krejcic43058d2017-01-04 16:29:58 +0100215# yang2yin
Michal Vasko9a542742016-06-23 13:04:51 +0200216add_executable(yang2yin ${yang2yinsrc})
217
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200218if(ENABLE_VALGRIND_TESTS)
219 set(ENABLE_BUILD_TESTS ON)
220endif()
221
222if(ENABLE_BUILD_TESTS)
Radek Krejcie02ce0a2015-11-04 14:14:20 +0100223 find_package(CMocka 1.0.0)
224 if(CMOCKA_FOUND)
225 enable_testing()
226 add_subdirectory(tests)
227 endif(CMOCKA_FOUND)
228endif(ENABLE_BUILD_TESTS)
Mislav Novakovica827b5f2016-02-29 11:47:22 +0100229
230if(JAVASCRIPT_BINDING)
231 include(swig/javascript/CMakeLists.txt)
232endif()