test
[anna.git] / CMakeLists.txt
1 # See more at: https://cmake.org/Wiki/CMake_Useful_Variables
2
3 # Stop if cmake version below 2.8
4 cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
5
6 # Project name
7 project(anna)
8
9 # Enable c++ language
10 enable_language(C CXX)
11
12 # Project version
13 set(VERSION_MAJOR 1)
14 set(VERSION_MINOR 0)
15 set(VERSION_PATCH 0)
16
17 # Dynamic libraries not linked to build tree:
18 set(CMAKE_SKIP_RPATH TRUE)
19
20 # Build type:
21 if(NOT CMAKE_BUILD_TYPE)
22   message(WARNING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel.")
23   set(CMAKE_BUILD_TYPE "Release" CACHE STRING
24       "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
25 endif(NOT CMAKE_BUILD_TYPE)
26 message(STATUS "CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}")
27
28 # Build output directory:
29 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY build/${CMAKE_BUILD_TYPE}/bin)
30 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY build/${CMAKE_BUILD_TYPE}/lib)
31 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY build/${CMAKE_BUILD_TYPE}/lib)
32 message(STATUS "CMAKE_RUNTIME_OUTPUT_DIRECTORY is ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
33 message(STATUS "CMAKE_LIBRARY_OUTPUT_DIRECTORY is ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
34 message(STATUS "CMAKE_ARCHIVE_OUTPUT_DIRECTORY is ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}")
35
36 # http://stackoverflow.com/questions/6594796/how-do-i-make-cmake-output-into-a-bin-dir
37 #set_target_properties( targets...
38 #    PROPERTIES
39 #    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
40 #    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
41 #    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
42 #)
43
44 # Location of additional cmake modules
45 set(CMAKE_MODULE_PATH
46     ${CMAKE_MODULE_PATH}
47     ${CMAKE_CURRENT_SOURCE_DIR}/cmake
48     )
49
50 # Detect operating system
51 message(STATUS "This is a ${CMAKE_SYSTEM_NAME} system")
52 if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
53     add_definitions(-DSYSTEM_LINUX)
54 endif()
55 if(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
56     add_definitions(-DSYSTEM_DARWIN)
57 endif()
58 if(${CMAKE_SYSTEM_NAME} STREQUAL "AIX")
59     add_definitions(-DSYSTEM_AIX)
60 endif()
61
62 # Detect host processor
63 message(STATUS "The host processor is ${CMAKE_HOST_SYSTEM_PROCESSOR}")
64 message(STATUS "The c compiler is ${CMAKE_C_COMPILER}")
65 message(STATUS "The c++ compiler is ${CMAKE_CXX_COMPILER}")
66 message(STATUS "The build type is ${CMAKE_BUILD_TYPE}")
67
68 # Example how to set c++ compiler flags for GNU
69 message(STATUS "CMAKE_CXX_COMPILER_ID = ${CMAKE_CXX_COMPILER_ID}")
70 if(CMAKE_CXX_COMPILER_ID MATCHES GNU)
71     #execute_process(COMMAND g++ --version >/dev/null 2>/dev/null)
72     set(CMAKE_CXX_COMPILER             "/usr/bin/g++")
73     set(CMAKE_CXX_FLAGS                "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wno-deprecated -Wwrite-strings -Wno-unknown-pragmas -Wno-sign-compare -Wno-maybe-uninitialized -Wno-unused -Wno-reorder")
74     set(CMAKE_CXX_FLAGS_DEBUG          "-O0 -g3")
75     set(CMAKE_CXX_FLAGS_MINSIZEREL     "-Os -DNDEBUG")
76     set(CMAKE_CXX_FLAGS_RELEASE        "-O3")
77     set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
78
79 elseif(CMAKE_CXX_COMPILER_ID MATCHES Clang)
80     #execute_process(COMMAND clang++ --version >/dev/null 2>/dev/null)
81     add_definitions(-DIS_CLANG)
82     set(CMAKE_CXX_COMPILER             "/usr/bin/clang++")
83     set(CMAKE_CXX_FLAGS                "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wno-deprecated -Wwrite-strings -Wno-unknown-pragmas -Wno-sign-compare -Wno-maybe-uninitialized -Wno-unused -Wno-reorder -Wno-parentheses-equality")
84     set(CMAKE_CXX_FLAGS_DEBUG          "-O0 -g3")
85     set(CMAKE_CXX_FLAGS_MINSIZEREL     "-Os -DNDEBUG")
86     set(CMAKE_CXX_FLAGS_RELEASE        "-O3")
87     set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
88 endif()
89
90
91 if (CMAKE_BUILD_TYPE STREQUAL Debug)
92     set(BUILD_POSTFIX "_d")
93 else(CMAKE_BUILD_TYPE STREQUAL Debug)
94     set(BUILD_POSTFIX "")
95 endif(CMAKE_BUILD_TYPE STREQUAL Debug)
96
97
98 # Macro to get children directories:
99 MACRO(SUBDIRLIST result curdir)
100   FILE(GLOB children RELATIVE ${curdir} ${curdir}/*)
101   SET(dirlist "")
102   FOREACH(child ${children})
103     IF(IS_DIRECTORY ${curdir}/${child})
104       LIST(APPEND dirlist ${child})
105     ENDIF()
106   ENDFOREACH()
107   SET(${result} ${dirlist})
108 ENDMACRO()
109
110 # Definitions (any way to apply only at ldap context ?):
111 add_definitions(-DLDAP_DEPRECATED)
112
113 # General includes:
114 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
115 include_directories(/usr/include/libxml2)
116 include_directories(/usr/lib/oracle/12.1/client64/include)
117
118 # <source>
119 SUBDIRLIST(SUBDIRS ${CMAKE_CURRENT_SOURCE_DIR}/source)
120 FOREACH(subdir ${SUBDIRS})
121   message(STATUS "Processing library at: source/${subdir}")
122   file(GLOB_RECURSE SRCS source/${subdir}/*.cpp)
123   add_library(anna_${subdir}_static STATIC ${SRCS})
124   add_library(anna_${subdir}_shared SHARED ${SRCS})
125 ENDFOREACH()
126
127 # <dynamic>
128 file(GLOB_RECURSE DYNAMIC_PROCEDURES ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/launcher/*.cpp)
129 FOREACH(procedure ${DYNAMIC_PROCEDURES})
130   get_filename_component(dirname ${procedure} DIRECTORY)
131   file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR}/dynamic/launcher ${dirname})
132   # convert "/" to "_"
133   string(REGEX REPLACE "/" "_" libpath ${rel})
134
135   message(STATUS "Processing dynamic library at: dynamic/launcher/${rel}")
136   file(GLOB_RECURSE SRCS dynamic/launcher/${rel}/*.cpp)
137   add_library(anna_launcher_procedure_${libpath}_shared SHARED ${SRCS})
138
139   set(target_dirname build/${CMAKE_BUILD_TYPE}/lib/dynamic/launcher/${rel})
140
141   set(target_basename anna_launcher_procedure_${libpath}_shared)
142   set_target_properties(${target_basename} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${target_dirname})
143   #file(RENAME ${target_dirname}/lib${target_basename}.so ${target_dirname}/libprocedure.so)
144 ENDFOREACH()
145
146 # Generalization:
147 # <example>
148 SUBDIRLIST(MODULES ${CMAKE_CURRENT_SOURCE_DIR}/example)
149 FOREACH(module ${MODULES})
150   SUBDIRLIST(SUBDIRS ${CMAKE_CURRENT_SOURCE_DIR}/example/${module})
151   FOREACH(subdir ${SUBDIRS})
152     message(STATUS "Processing executable at: example/${module}/${subdir}")
153
154     # Sources
155     file(GLOB_RECURSE SRCS ${CMAKE_CURRENT_SOURCE_DIR}/example/${module}/${subdir}/*.cpp)
156
157     # Executable
158     set(target "anna_${module}_${subdir}")
159     add_executable(${target} ${SRCS})
160     set_target_properties(${target} PROPERTIES LINKER_LANGUAGE CXX)
161     set(libraries_file "${CMAKE_CURRENT_SOURCE_DIR}/example/${module}/${subdir}/libraries.txt")
162     set(includes_file "${CMAKE_CURRENT_SOURCE_DIR}/example/${module}/${subdir}/includes.txt")
163
164     if(EXISTS "${includes_file}")
165       file (STRINGS "${includes_file}" HDRS)
166       # Include own example directory:
167       target_include_directories(${target} PUBLIC
168         $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/example/${module}/${subdir}>
169       )
170       FOREACH(HDR ${HDRS})
171         #message(STATUS "Header directory: ${HDR}")
172         target_include_directories(${target} PUBLIC
173           $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${HDR}>
174         )
175       ENDFOREACH()
176     endif()
177
178     if(EXISTS "${libraries_file}")
179       file (STRINGS "${libraries_file}" LIBS)
180       #message(STATUS "Libraries: ${LIBS}")
181       target_link_libraries(${target} ${LIBS})
182     endif()
183
184   ENDFOREACH()
185 ENDFOREACH()
186
187
188 # <test>
189 find_package(GTest)
190 IF(GTest_FOUND)
191
192   enable_testing()
193   include_directories(${GTEST_INCLUDE_DIRS})
194
195   SUBDIRLIST(MODULES ${CMAKE_CURRENT_SOURCE_DIR}/test)
196   FOREACH(module ${MODULES})
197
198     message(STATUS "Processing basic test at: test/${module}")
199
200     # Sources
201     file(GLOB_RECURSE SRCS ${CMAKE_CURRENT_SOURCE_DIR}/test/${module}/*.cpp)
202
203     # Executable
204     set(target "anna_test_${module}")
205     add_executable(${target} ${SRCS})
206     target_link_libraries(${target} ${GTEST_BOTH_LIBRARIES} pthread)
207     set(libraries_file "${CMAKE_CURRENT_SOURCE_DIR}/test/${module}/libraries.txt")
208
209     if(EXISTS "${libraries_file}")
210       file (STRINGS "${libraries_file}" LIBS)
211       #message(STATUS "Libraries: ${LIBS}")
212       target_link_libraries(${target} ${GTEST_BOTH_LIBRARIES} pthread ${LIBS})
213     endif()
214
215     add_test(${target} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${target})
216
217   ENDFOREACH()
218 ENDIF(GTest_FOUND)
219
220
221 # Install
222 SET(MY_OWN_INSTALL_PREFIX "/opt/anna" CACHE PATH "Prefix prepended to install directories")
223 SET(CMAKE_INSTALL_PREFIX "${MY_OWN_INSTALL_PREFIX}" CACHE INTERNAL "Prefix prepended to install directories" FORCE)
224
225 message(STATUS "CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}")
226 # Binaries:
227 file(GLOB_RECURSE BINARIES ${CMAKE_CURRENT_SOURCE_DIR}/build/${CMAKE_BUILD_TYPE}/bin/*)
228 install(PROGRAMS ${BINARIES} DESTINATION bin)
229 # Headers:
230 install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/anna DESTINATION include)
231 # Libraries:
232 file(GLOB_RECURSE LIBRARIES ${CMAKE_CURRENT_SOURCE_DIR}/build/${CMAKE_BUILD_TYPE}/lib/*)
233 install(FILES ${LIBRARIES} DESTINATION lib)
234
235
236 # Add target for API documentation with Doxygen
237 find_package(Doxygen)
238 IF(Doxygen_FOUND)
239   add_custom_target(doc
240     ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/docs/doxygen/Doxyfile
241     WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/docs/doxygen
242     COMMENT "Generating API documentation with Doxygen" VERBATIM
243   )
244 ENDIF(Doxygen_FOUND)
245