CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. # Main CMake file for compiling the library itself, examples and tests.
  2. #
  3. # Copyright (c) 2012-2016 Sebastien Rombauts (sebastien.rombauts@gmail.com)
  4. #
  5. # Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
  6. # or copy at http://opensource.org/licenses/MIT)
  7. cmake_minimum_required(VERSION 2.8.12) # first version with add_compile_options()
  8. project(SQLiteCpp)
  9. # Define useful variables to handle OS differences:
  10. if (WIN32)
  11. set(DEV_NULL "NUL")
  12. else (WIN32) # UNIX
  13. set(DEV_NULL "/dev/null")
  14. endif (WIN32)
  15. # then Compiler/IDE differences:
  16. if (MSVC)
  17. set(CPPLINT_ARG_OUTPUT "--output=vs7")
  18. set(CPPCHECK_ARG_TEMPLATE "--template=vs")
  19. # disable Visual Studio warnings for fopen() used in the example
  20. add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  21. # Flags for linking with multithread static C++ runtime, required by googletest
  22. if (SQLITECPP_BUILD_TESTS)
  23. message(STATUS "Linking against multithread static C++ runtime for unit tests with googletest")
  24. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
  25. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
  26. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
  27. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
  28. endif (SQLITECPP_BUILD_TESTS)
  29. else (MSVC)
  30. set(CPPLINT_ARG_OUTPUT "--output=eclipse")
  31. set(CPPCHECK_ARG_TEMPLATE "--template=gcc")
  32. # Useful compile flags and extra warnings
  33. add_compile_options(-fstack-protector -Wall -Winit-self -Wswitch-enum -Wshadow -Winline)
  34. if (CMAKE_COMPILER_IS_GNUCXX)
  35. # GCC flags
  36. if (SQLITECPP_USE_GCOV AND CMAKE_COMPILER_IS_GNUCXX)
  37. if (CMAKE_BUILD_TYPE STREQUAL "Debug")
  38. message (STATUS "Using GCov instrumentation")
  39. else ()
  40. message (WARNING "GCov instrumentation works best in Debug mode")
  41. endif ()
  42. add_compile_options (-coverage) # NOTE would be usefull but not working with current google test and gcc 4.8 -fkeep-inline-functions
  43. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -coverage")
  44. endif ()
  45. endif (CMAKE_COMPILER_IS_GNUCXX)
  46. endif (MSVC)
  47. # and then common variables
  48. set(CPPLINT_ARG_VERBOSE "--verbose=3")
  49. set(CPPLINT_ARG_LINELENGTH "--linelength=120")
  50. # Print CXX compiler information
  51. message (STATUS "CMAKE_CXX_COMPILER '${CMAKE_CXX_COMPILER}' '${CMAKE_CXX_COMPILER_ID}' '${CMAKE_CXX_COMPILER_VERSION}'")
  52. # Print CXX FLAGS
  53. message (STATUS "CMAKE_CXX_FLAGS '${CMAKE_CXX_FLAGS}'")
  54. if (MSVC)
  55. message (STATUS "CMAKE_CXX_FLAGS_DEBUG '${CMAKE_CXX_FLAGS_DEBUG}'")
  56. message (STATUS "CMAKE_CXX_FLAGS_RELEASE '${CMAKE_CXX_FLAGS_RELEASE}'")
  57. message (STATUS "CMAKE_CXX_FLAGS_RELWITHDEBINFO '${CMAKE_CXX_FLAGS_RELWITHDEBINFO}'")
  58. message (STATUS "CMAKE_CXX_FLAGS_MINSIZEREL '${CMAKE_CXX_FLAGS_MINSIZEREL}'")
  59. else (NOT MSVC)
  60. if (CMAKE_BUILD_TYPE STREQUAL Debug)
  61. message (STATUS "CMAKE_CXX_FLAGS_DEBUG '${CMAKE_CXX_FLAGS_DEBUG}'")
  62. elseif (CMAKE_BUILD_TYPE STREQUAL RelWithDebInfo)
  63. message (STATUS "CMAKE_CXX_FLAGS_RELWITHDEBINFO '${CMAKE_CXX_FLAGS_RELWITHDEBINFO}'")
  64. elseif (CMAKE_BUILD_TYPE STREQUAL MinSizeRel)
  65. message (STATUS "CMAKE_CXX_FLAGS_MINSIZEREL '${CMAKE_CXX_FLAGS_MINSIZEREL}'")
  66. else ()
  67. message (STATUS "CMAKE_CXX_FLAGS_RELEASE '${CMAKE_CXX_FLAGS_RELEASE}'")
  68. endif ()
  69. endif ()
  70. # Options relative to SQLite and SQLiteC++ functions
  71. option(SQLITE_ENABLE_COLUMN_METADATA "Enable Column::getColumnOriginName(). Require support from sqlite3 library." ON)
  72. if (SQLITE_ENABLE_COLUMN_METADATA)
  73. # Enable the use of SQLite column metadata and Column::getColumnOriginName() method,
  74. # Require that the sqlite3 library is also compiled with this flag (default under Debian/Ubuntu, but not on Mac OS X).
  75. add_definitions(-DSQLITE_ENABLE_COLUMN_METADATA)
  76. endif (SQLITE_ENABLE_COLUMN_METADATA)
  77. option(SQLITE_ENABLE_ASSERT_HANDLER "Enable the user defintion of a assertion_failed() handler." OFF)
  78. if (SQLITE_ENABLE_ASSERT_HANDLER)
  79. # Enable the user defintion of a assertion_failed() handler (default to false, easier to handler for begginers).
  80. add_definitions(-DSQLITECPP_ENABLE_ASSERT_HANDLER)
  81. endif (SQLITE_ENABLE_ASSERT_HANDLER)
  82. ## Build the C++ Wrapper ##
  83. # adding a new file require explicittly modifing the CMakeLists.txt
  84. # so that CMake knows that it should rebuild the project (it is best practice)
  85. # list of sources files of the library
  86. set(SQLITECPP_SRC
  87. ${PROJECT_SOURCE_DIR}/src/Backup.cpp
  88. ${PROJECT_SOURCE_DIR}/src/Column.cpp
  89. ${PROJECT_SOURCE_DIR}/src/Database.cpp
  90. ${PROJECT_SOURCE_DIR}/src/Exception.cpp
  91. ${PROJECT_SOURCE_DIR}/src/Statement.cpp
  92. ${PROJECT_SOURCE_DIR}/src/Transaction.cpp
  93. )
  94. source_group(src FILES ${SQLITECPP_SRC})
  95. # list of header files of the library
  96. set(SQLITECPP_INC
  97. ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/SQLiteCpp.h
  98. ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Assertion.h
  99. ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Backup.h
  100. ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Column.h
  101. ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Database.h
  102. ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Exception.h
  103. ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Statement.h
  104. ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Transaction.h
  105. ${PROJECT_SOURCE_DIR}/include/SQLiteCpp/VariadicBind.h
  106. )
  107. source_group(inc FILES ${SQLITECPP_INC})
  108. # list of test files of the library
  109. set(SQLITECPP_TESTS
  110. tests/Column_test.cpp
  111. tests/Database_test.cpp
  112. tests/Statement_test.cpp
  113. tests/Backup_test.cpp
  114. tests/Transaction_test.cpp
  115. tests/VariadicBind_test.cpp
  116. )
  117. source_group(tests FILES ${SQLITECPP_TESTS})
  118. # list of example files of the library
  119. set(SQLITECPP_EXAMPLES
  120. examples/example1/main.cpp
  121. )
  122. source_group(example1 FILES ${SQLITECPP_EXAMPLES})
  123. # list of doc files of the library
  124. set(SQLITECPP_DOC
  125. README.md
  126. LICENSE.txt
  127. CHANGELOG.txt
  128. TODO.txt
  129. )
  130. source_group(doc FILES ${SQLITECPP_DOC})
  131. # list of script files of the library
  132. set(SQLITECPP_SCRIPT
  133. .travis.yml
  134. appveyor.yml
  135. build.bat
  136. build.sh
  137. cpplint.py
  138. Doxyfile
  139. FindSQLiteCpp.cmake
  140. )
  141. source_group(scripts FILES ${SQLITECPP_SCRIPT})
  142. # All includes are relative to the "include" directory
  143. include_directories("${PROJECT_SOURCE_DIR}/include")
  144. # add sources of the wrapper as a "SQLiteCpp" static library
  145. add_library(SQLiteCpp ${SQLITECPP_SRC} ${SQLITECPP_INC} ${SQLITECPP_DOC} ${SQLITECPP_SCRIPT})
  146. target_include_directories(SQLiteCpp PUBLIC "${PROJECT_SOURCE_DIR}/include")
  147. if (UNIX AND (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang"))
  148. set_target_properties(SQLiteCpp PROPERTIES COMPILE_FLAGS "-fPIC")
  149. endif (UNIX AND (CMAKE_COMPILER_IS_GNUCXX OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang"))
  150. ## Build provided copy of SQLite3 C library ##
  151. option(SQLITECPP_INTERNAL_SQLITE "Add the internal SQLite3 source to the project." ON)
  152. if (SQLITECPP_INTERNAL_SQLITE)
  153. # build the SQLite3 C library (for ease of use/compatibility) versus Linux sqlite3-dev package
  154. add_subdirectory(sqlite3)
  155. include_directories("${PROJECT_SOURCE_DIR}/sqlite3")
  156. target_include_directories(sqlite3 PUBLIC "${PROJECT_SOURCE_DIR}/sqlite3")
  157. endif (SQLITECPP_INTERNAL_SQLITE)
  158. # Optional additional targets:
  159. option(SQLITECPP_RUN_CPPLINT "Run cpplint.py tool for Google C++ StyleGuide." ON)
  160. if (SQLITECPP_RUN_CPPLINT)
  161. find_package(PythonInterp)
  162. if (PYTHONINTERP_FOUND)
  163. # add a cpplint target to the "all" target
  164. add_custom_target(SQLiteCpp_cpplint
  165. ALL
  166. COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/cpplint.py ${CPPLINT_ARG_OUTPUT} ${CPPLINT_ARG_VERBOSE} ${CPPLINT_ARG_LINELENGTH} ${SQLITECPP_SRC} ${SQLITECPP_INC}
  167. )
  168. endif (PYTHONINTERP_FOUND)
  169. else (SQLITECPP_RUN_CPPLINT)
  170. message(STATUS "SQLITECPP_RUN_CPPLINT OFF")
  171. endif (SQLITECPP_RUN_CPPLINT)
  172. option(SQLITECPP_RUN_CPPCHECK "Run cppcheck C++ static analysis tool." ON)
  173. if (SQLITECPP_RUN_CPPCHECK)
  174. find_program(CPPCHECK_EXECUTABLE NAMES cppcheck)
  175. if (CPPCHECK_EXECUTABLE)
  176. # add a cppcheck target to the "all" target
  177. add_custom_target(SQLiteCpp_cppcheck
  178. ALL
  179. COMMAND ${CPPCHECK_EXECUTABLE} -j 8 cppcheck --enable=style --quiet ${CPPCHECK_ARG_TEMPLATE} ${PROJECT_SOURCE_DIR}/src
  180. )
  181. execute_process(COMMAND "${CPPCHECK_EXECUTABLE}" --version OUTPUT_VARIABLE CPPCHECK_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE)
  182. message(STATUS "Found Cppcheck: ${CPPCHECK_EXECUTABLE} ${CPPCHECK_VERSION}")
  183. else (CPPCHECK_EXECUTABLE)
  184. message(STATUS "Could NOT find cppcheck")
  185. endif (CPPCHECK_EXECUTABLE)
  186. else (SQLITECPP_RUN_CPPCHECK)
  187. message(STATUS "SQLITECPP_RUN_CPPCHECK OFF")
  188. endif (SQLITECPP_RUN_CPPCHECK)
  189. option(SQLITECPP_RUN_DOXYGEN "Run Doxygen C++ documentation tool." OFF)
  190. if (SQLITECPP_RUN_DOXYGEN)
  191. find_package(Doxygen)
  192. if (DOXYGEN_FOUND)
  193. # add a Doxygen target to the "all" target
  194. add_custom_target(SQLiteCpp_doxygen
  195. ALL
  196. COMMAND doxygen Doxyfile > ${DEV_NULL}
  197. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  198. )
  199. endif (DOXYGEN_FOUND)
  200. else (SQLITECPP_RUN_DOXYGEN)
  201. message(STATUS "SQLITECPP_RUN_DOXYGEN OFF")
  202. endif (SQLITECPP_RUN_DOXYGEN)
  203. option(SQLITECPP_BUILD_EXAMPLES "Build examples." OFF)
  204. if (SQLITECPP_BUILD_EXAMPLES)
  205. # add the basic example executable
  206. add_executable(SQLiteCpp_example1 ${SQLITECPP_EXAMPLES})
  207. target_link_libraries(SQLiteCpp_example1 SQLiteCpp sqlite3)
  208. # Link target with pthread and dl for linux
  209. if (UNIX)
  210. target_link_libraries(SQLiteCpp_example1 pthread)
  211. if (NOT APPLE)
  212. target_link_libraries(SQLiteCpp_example1 dl)
  213. endif ()
  214. elseif (MSYS OR MINGW)
  215. target_link_libraries(SQLiteCpp_example1 ssp)
  216. endif ()
  217. else (SQLITECPP_BUILD_EXAMPLES)
  218. message(STATUS "SQLITECPP_BUILD_EXAMPLES OFF")
  219. endif (SQLITECPP_BUILD_EXAMPLES)
  220. option(SQLITECPP_BUILD_TESTS "Build and run tests." OFF)
  221. if (SQLITECPP_BUILD_TESTS)
  222. # deactivate some warnings for compiling the gtest library
  223. if (NOT MSVC)
  224. add_compile_options(-Wno-variadic-macros -Wno-long-long -Wno-switch-enum -Wno-float-equal -Wno-conversion-null -Wno-switch-default -Wno-pedantic)
  225. endif (NOT MSVC)
  226. # add the subdirectory containing the CMakeLists.txt for the gtest library
  227. # TODO: under Linux, uses libgtest-dev if found
  228. # TODO: move to the new googletest Github repository
  229. if (NOT EXISTS "${PROJECT_SOURCE_DIR}/googletest/CMakeLists.txt")
  230. message(FATAL_ERROR "Missing 'googletest' submodule! Either use 'git init submodule' and 'git update submodule' to get googletest according to the README, or deactivate unit tests with -DSQLITECPP_BUILD_TESTS=OFF")
  231. endif ()
  232. add_subdirectory(googletest)
  233. include_directories("${PROJECT_SOURCE_DIR}/googletest/googletest/include")
  234. # add the unit test executable
  235. add_executable(SQLiteCpp_tests ${SQLITECPP_TESTS})
  236. target_link_libraries(SQLiteCpp_tests gtest_main SQLiteCpp sqlite3)
  237. # Link target with dl for linux
  238. if (UNIX AND NOT APPLE)
  239. target_link_libraries(SQLiteCpp_tests dl)
  240. endif ()
  241. # add a "test" target:
  242. enable_testing()
  243. # does the tests pass?
  244. add_test(UnitTests SQLiteCpp_tests)
  245. if (SQLITECPP_BUILD_EXAMPLES)
  246. # does the example1 runs successfully?
  247. add_test(Example1Run SQLiteCpp_example1)
  248. endif (SQLITECPP_BUILD_EXAMPLES)
  249. else (SQLITECPP_BUILD_TESTS)
  250. message(STATUS "SQLITECPP_BUILD_TESTS OFF")
  251. endif (SQLITECPP_BUILD_TESTS)