blob: dc8cad18725508aca7e3eeee32c0a7b81afce6ef [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)
Radek Krejci8f7cf402015-08-13 08:05:48 +020012set(LIBYANG_VERSION ${LIBYANG_MAJOR_VERSION}.${LIBYANG_MINOR_VERSION}.${LIBYANG_MICRO_VERSION})
13set(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
Radek Krejcie74ea8c2015-04-09 18:06:20 +020049# check the supported platform
50if(NOT UNIX)
51 message(FATAL_ERROR "Only *nix like systems are supported.")
52endif()
53
Radek Krejcie74ea8c2015-04-09 18:06:20 +020054# set default build type if not specified by user
55if(NOT CMAKE_BUILD_TYPE)
56 set(CMAKE_BUILD_TYPE debug)
57endif()
58
Radek Krejcic43058d2017-01-04 16:29:58 +010059set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra")
Radek Krejci3045cf32015-05-28 10:58:52 +020060set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
Radek Krejcif19ac672016-02-05 16:16:36 +010061set(CMAKE_C_FLAGS_DEBUG "-g -O0")
Radek Krejcie74ea8c2015-04-09 18:06:20 +020062
Radek Krejci61cd5562015-10-05 14:06:34 +020063if(CMAKE_BUILD_TYPE STREQUAL debug)
64 option(ENABLE_BUILD_TESTS "Build tests" ON)
65 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
66else()
67 option(ENABLE_BUILD_TESTS "Build tests" OFF)
68 option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
69endif()
Mislav Novakovic021b9a12015-10-01 20:50:21 +020070
Radek Krejci5a065542015-05-22 15:02:07 +020071set(libsrc
Radek Krejci9b4ca392015-04-10 08:31:27 +020072 src/common.c
Radek Krejcida04f4a2015-05-21 12:54:09 +020073 src/context.c
Radek Krejci9b4ca392015-04-10 08:31:27 +020074 src/log.c
Radek Krejciee627582015-04-20 17:39:59 +020075 src/dict.c
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020076 src/resolve.c
Radek Krejcib1c12512015-08-11 11:22:04 +020077 src/validation.c
Radek Krejcida04f4a2015-05-21 12:54:09 +020078 src/xml.c
Radek Krejcib1c12512015-08-11 11:22:04 +020079 src/parser.c
Radek Krejci998a0b82015-08-17 13:14:36 +020080 src/parser_yin.c
81 src/parser_xml.c
Radek Krejci5449d472015-10-26 14:35:56 +010082 src/parser_json.c
Pavol Vicancae54882016-03-30 12:24:43 +020083 src/parser_yang_bis.c
84 src/parser_yang_lex.c
Pavol Vicanf7cc2852016-03-22 23:27:35 +010085 src/parser_yang.c
Michal Vasko2d162e12015-09-24 14:33:29 +020086 src/tree_schema.c
87 src/tree_data.c
Radek Krejci0aa821a2016-12-08 11:21:35 +010088 src/extensions.c
Radek Krejciefdd0ce2015-05-26 16:48:29 +020089 src/printer.c
Michal Vaskoabbdaa02015-10-06 15:47:25 +020090 src/xpath.c
Radek Krejci998a0b82015-08-17 13:14:36 +020091 src/printer_yang.c
Michal Vaskoa63ca342016-02-05 14:29:19 +010092 src/printer_yin.c
Michal Vasko9b9e4a02015-08-21 09:11:14 +020093 src/printer_xml.c
Radek Krejci998a0b82015-08-17 13:14:36 +020094 src/printer_tree.c
95 src/printer_info.c
96 src/printer_json.c
Radek Krejcida04f4a2015-05-21 12:54:09 +020097 src/yang_types.c)
Radek Krejcie74ea8c2015-04-09 18:06:20 +020098
Radek Krejci5a065542015-05-22 15:02:07 +020099set(lintsrc
Michal Vaskof3e59f12015-06-18 11:53:56 +0200100 tools/lint/main.c
Radek Krejci797590b2016-10-10 16:23:04 +0200101 tools/lint/main_ni.c
Michal Vaskof3e59f12015-06-18 11:53:56 +0200102 tools/lint/commands.c
Michal Vaskoe0cb2522015-07-01 10:24:53 +0200103 tools/lint/completion.c
Michal Vasko203b4e72015-06-30 15:25:15 +0200104 linenoise/linenoise.c)
Radek Krejci5a065542015-05-22 15:02:07 +0200105
Michal Vasko9a542742016-06-23 13:04:51 +0200106set(yang2yinsrc
107 tools/yang2yin/main.c)
108
Tomas Cejka422c0072015-07-21 18:18:25 +0200109set(headers
110 src/libyang.h
Michal Vasko2d162e12015-09-24 14:33:29 +0200111 src/tree_schema.h
Radek Krejcic6704c82015-10-06 11:12:45 +0200112 src/tree_data.h
Radek Krejcia1a6b762016-11-14 09:53:38 +0900113 src/extensions.h
Radek Krejci41912fe2015-10-22 10:22:12 +0200114 src/xml.h
115 src/dict.h)
Tomas Cejka422c0072015-07-21 18:18:25 +0200116
Abhi Keshav7afa0812016-10-25 14:52:51 -0700117# create static libyang library
118if(STATIC)
119 add_library(yang_static STATIC ${libsrc})
120 set_target_properties(yang_static PROPERTIES OUTPUT_NAME yang)
Radek Krejcic43058d2017-01-04 16:29:58 +0100121 set_target_properties(yang_static PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
Abhi Keshav7afa0812016-10-25 14:52:51 -0700122 target_link_libraries(yang_static m)
Radek Krejci858ad952017-01-04 11:16:32 +0100123 target_link_libraries(yang_static ${CMAKE_DL_LIBS})
Abhi Keshav7afa0812016-10-25 14:52:51 -0700124 target_link_libraries(yang_static ${CMAKE_THREAD_LIBS_INIT})
125 target_link_libraries(yang_static ${PCRE_LIBRARIES})
Radek Krejci4c79e702016-11-06 13:00:20 +0100126 install(TARGETS yang_static DESTINATION ${CMAKE_INSTALL_LIBDIR})
Abhi Keshav7afa0812016-10-25 14:52:51 -0700127endif(STATIC)
128
Radek Krejci5708fe62016-09-14 12:25:12 +0200129set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
130add_library(yangobj OBJECT ${libsrc})
131add_library(yang SHARED $<TARGET_OBJECTS:yangobj>)
Radek Krejci8f7cf402015-08-13 08:05:48 +0200132set_target_properties(yang PROPERTIES VERSION ${LIBYANG_VERSION} SOVERSION ${LIBYANG_SOVERSION})
Radek Krejcic43058d2017-01-04 16:29:58 +0100133set_target_properties(yang PROPERTIES COMPILE_FLAGS "-fvisibility=hidden")
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200134
Radek Krejci89658b32016-02-08 11:14:46 +0100135# link math
136target_link_libraries(yang m)
137
Radek Krejci858ad952017-01-04 11:16:32 +0100138#link dl
139target_link_libraries(yang ${CMAKE_DL_LIBS})
140
Radek Krejci7d9f46a2016-01-29 13:53:18 +0100141# find pthreads
142set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
143find_package(Threads REQUIRED)
144target_link_libraries(yang ${CMAKE_THREAD_LIBS_INIT})
145
Michal Vaskoe4e8fbd2015-08-24 14:54:49 +0200146# find PCRE library
147find_package(PCRE REQUIRED)
148include_directories(${PCRE_INCLUDE_DIRS})
Radek Krejci1d949ec2015-10-02 13:35:05 +0200149target_link_libraries(yang ${PCRE_LIBRARIES})
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200150
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100151install(TARGETS yang DESTINATION ${CMAKE_INSTALL_LIBDIR})
152install(FILES ${headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libyang)
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200153
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100154find_package(PkgConfig)
155if(PKG_CONFIG_FOUND)
156 # generate and install pkg-config file
157 configure_file("libyang.pc.in" "libyang.pc" @ONLY)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100158 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libyang.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
Michal Vasko98ae1e12015-12-15 11:11:28 +0100159 # check that pkg-config includes the used path
160 execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --variable pc_path pkg-config RESULT_VARIABLE RETURN OUTPUT_VARIABLE PC_PATH ERROR_QUIET)
161 if(RETURN EQUAL 0)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100162 string(REGEX MATCH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig" SUBSTR "${PC_PATH}")
Michal Vasko33c001c2016-02-11 09:01:52 +0100163 string(LENGTH "${SUBSTR}" SUBSTR_LEN)
Michal Vaskob8d90a32016-02-09 13:59:42 +0100164 if(SUBSTR_LEN EQUAL 0)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100165 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 +0100166 endif()
167 endif()
Michal Vaskoe5cf9922015-11-11 11:06:40 +0100168endif()
169
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200170# generate doxygen documentation for libyang API
Radek Krejcie2626092015-10-08 11:25:44 +0200171find_package(Doxygen)
172if(DOXYGEN_FOUND)
173 set(DOXYGEN_SKIP_DOT TRUE)
174 add_custom_target(doc
175 COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile
176 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
177 configure_file(Doxyfile.in Doxyfile)
178endif()
Radek Krejcie74ea8c2015-04-09 18:06:20 +0200179
180# clean cmake cache
181add_custom_target(cclean
Radek Krejci25155ac2015-10-08 11:26:20 +0200182 COMMAND make clean
183 COMMAND find . -iname '*cmake*' -not -name CMakeLists.txt -not -path './CMakeModules*' -exec rm -rf {} +
184 COMMAND rm -rf Makefile Doxyfile
185 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
Radek Krejci2cb30542015-04-10 08:33:57 +0200186
Radek Krejcic43058d2017-01-04 16:29:58 +0100187# YANG extensions plugins
188add_subdirectory(src/extensions)
189
190# yanglint
Radek Krejci5a065542015-05-22 15:02:07 +0200191add_executable(yanglint ${lintsrc})
Radek Krejci1d949ec2015-10-02 13:35:05 +0200192target_link_libraries(yanglint yang)
Jan Kundrátbffd9e42016-11-04 16:33:49 +0100193install(TARGETS yanglint DESTINATION ${CMAKE_INSTALL_BINDIR})
194install(FILES ${PROJECT_SOURCE_DIR}/tools/lint/yanglint.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
Radek Krejci5a065542015-05-22 15:02:07 +0200195
Radek Krejcic43058d2017-01-04 16:29:58 +0100196# yang2yin
Michal Vasko9a542742016-06-23 13:04:51 +0200197add_executable(yang2yin ${yang2yinsrc})
198
Mislav Novakovic021b9a12015-10-01 20:50:21 +0200199if(ENABLE_VALGRIND_TESTS)
200 set(ENABLE_BUILD_TESTS ON)
201endif()
202
203if(ENABLE_BUILD_TESTS)
Radek Krejcie02ce0a2015-11-04 14:14:20 +0100204 find_package(CMocka 1.0.0)
205 if(CMOCKA_FOUND)
206 enable_testing()
207 add_subdirectory(tests)
208 endif(CMOCKA_FOUND)
209endif(ENABLE_BUILD_TESTS)
Mislav Novakovica827b5f2016-02-29 11:47:22 +0100210
211if(JAVASCRIPT_BINDING)
212 include(swig/javascript/CMakeLists.txt)
213endif()