From ebc4f236db18e8e840ba4792b345b3f1685dc5a1 Mon Sep 17 00:00:00 2001 From: Michael Uleysky Date: Sun, 27 Sep 2015 11:57:21 +1000 Subject: [PATCH] CMake build system --- CMakeLists.txt | 44 +++++++++++++++++++++++++++++++++ cmake/modules/BuildModule.cmake | 11 +++++++++ src/CMakeLists.txt | 43 ++++++++++++++++++++++++++++++++ src/Makefile | 4 +-- src/parser/grammatical.y | 2 -- src/parser/lexical.l | 2 -- 6 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 cmake/modules/BuildModule.cmake create mode 100644 src/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c2c527a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,44 @@ +cmake_minimum_required(VERSION 2.8.5) + +project(gmt_makemap CXX) + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo" FORCE) +endif() + +set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules/" CACHE INTERNAL "Location of our custom CMake modules." FORCE) + +include_directories(include) +set(default_options -std=gnu++11 -flto -fvisibility=hidden -Wl,--export-dynamic -Wall) +add_compile_options(${default_options}) + +file(GLOB modules modules/*) +file(GLOB extramodules extramodules/*) + +set(STATIC_MODULES "") + +foreach(ext ${modules}) + if(EXISTS ${ext}/CMakeLists.txt) + string(REGEX REPLACE .*/ "" modname ${ext}) + option(MODULE_${modname}_BUILD "Build module ${modname}" ON) + option(MODULE_${modname}_STATIC "Link module ${modname} statically in gmt_makemap" ON) + mark_as_advanced(FORCE MODULE_${modname}_STATIC) + if(MODULE_${modname}_BUILD) + add_subdirectory(modules/${modname}) + endif() + endif() +endforeach(ext) + +foreach(ext ${extramodules}) + if(EXISTS ${ext}/CMakeLists.txt) + string(REGEX REPLACE .*/ "" modname ${ext}) + option(MODULE_${modname}_BUILD "Build module ${modname}" ON) + option(MODULE_${modname}_STATIC "Link module ${modname} statically in gmt_makemap" ON) + mark_as_advanced(FORCE MODULE_${modname}_STATIC) + if(MODULE_${modname}_BUILD) + add_subdirectory(extramodules/${modname}) + endif() + endif() +endforeach(ext) + +add_subdirectory(src) diff --git a/cmake/modules/BuildModule.cmake b/cmake/modules/BuildModule.cmake new file mode 100644 index 0000000..bf19be3 --- /dev/null +++ b/cmake/modules/BuildModule.cmake @@ -0,0 +1,11 @@ +set(moddir ${CMAKE_CURRENT_SOURCE_DIR}) +include(${moddir}/ModuleSetup.cmake OPTIONAL) +file(GLOB srcs *.cpp) +if(MODULE_${modname}_STATIC) + set(STATIC_MODULES ${STATIC_MODULES} ${CMAKE_CURRENT_SOURCE_DIR} PARENT_SCOPE) +else() + include_directories(${MODULE_ADDITIONAL_INCLUDES}) + add_library(${modname} MODULE ${srcs} ${MODULE_ADDITIONAL_SOURCES}) + target_link_libraries(${modname} ${MODULE_ADDITIONAL_LIBRARIES}) +endif() + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..9c44e10 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,43 @@ +cmake_minimum_required(VERSION 2.8.5) + +set(EXENAME makemap) + +find_package(FLEX REQUIRED) +find_package(BISON REQUIRED) + +find_library(dl dl) +find_library(pthread pthread) + +if(NOT dl) + message(FATAL_ERROR "libdl not found!") +endif() + +if(NOT pthread) + message(FATAL_ERROR "libpthread not found!") +endif() + +file(GLOB srcs *.cpp) +BISON_TARGET(GParser parser/grammatical.y ${CMAKE_CURRENT_BINARY_DIR}/grammatical.cpp) +FLEX_TARGET(LScanner parser/lexical.l ${CMAKE_CURRENT_BINARY_DIR}/lexical.cpp COMPILE_FLAGS "--header-file=${CMAKE_CURRENT_BINARY_DIR}/lexical.h") +set_property(DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES lexical.h) + +set(MODULES_SOURCES "") +set(MODULES_LIBS "") + +foreach(moddir ${STATIC_MODULES}) + include(${moddir}/ModuleSetup.cmake OPTIONAL) + file(GLOB modsrcs ${moddir}/*.cpp) + set(MODULES_SOURCES ${MODULES_SOURCES} ${modsrcs} ${MODULE_ADDITIONAL_SOURCES}) + set(MODULES_LIBS ${MODULES_LIBS} ${MODULE_ADDITIONAL_LIBRARIES}) + include_directories(${MODULE_ADDITIONAL_INCLUDES}) +endforeach() + +include_directories(${CMAKE_CURRENT_BINARY_DIR}) +add_executable(${EXENAME} ${srcs} ${FLEX_LScanner_OUTPUTS} ${BISON_GParser_OUTPUTS} ${MODULES_SOURCES}) + +# Workaround. BISON_TARGET makes header with extension .hpp, but we use extension .h +set_source_files_properties(init.cpp PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/grammatical.h) +add_custom_command(OUTPUT grammatical.h COMMAND cp grammatical.hpp grammatical.h DEPENDS grammatical.hpp WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + +target_link_libraries(${EXENAME} ${dl} ${pthread} ${MODULES_LIBS}) +set_target_properties(${EXENAME} PROPERTIES POSITION_INDEPENDENT_CODE ON) diff --git a/src/Makefile b/src/Makefile index a33b399..c512e8f 100644 --- a/src/Makefile +++ b/src/Makefile @@ -31,11 +31,11 @@ $(DEPENDS): ../include/grammatical.h ../include/lexical.h parser/grammatical.d: ../include/lexical.h parser/lexical.cpp: parser/lexical.l - cd parser && flex lexical.l + flex -o $@ --header-file=../include/lexical.h $< [ -f ../include/lexical.h ] && touch ../include/lexical.h parser/grammatical.cpp: parser/grammatical.y - cd parser && bison grammatical.y + bison -o $@ --defines=../include/grammatical.h $< [ -f ../include/grammatical.h ] && touch ../include/grammatical.h ../include/grammatical.h: parser/grammatical.y parser/grammatical.cpp diff --git a/src/parser/grammatical.y b/src/parser/grammatical.y index 8f6ba18..2ade5c6 100644 --- a/src/parser/grammatical.y +++ b/src/parser/grammatical.y @@ -1,6 +1,4 @@ %language "c" -%output "grammatical.cpp" -%defines "../../include/grammatical.h" %param {yyscan_t scanner} %define api.prefix {conf} %define api.pure full diff --git a/src/parser/lexical.l b/src/parser/lexical.l index 22dfd9a..0adcbc8 100644 --- a/src/parser/lexical.l +++ b/src/parser/lexical.l @@ -2,8 +2,6 @@ %option warn %option yylineno %option noyywrap -%option header-file="../../include/lexical.h" -%option outfile="lexical.cpp" %option prefix="conf" %option extra-type="struct lexical_extra*" %option bison-bridge