Skip to content

Commit ab60859

Browse files
committed
add Cmake
1 parent e2248fa commit ab60859

14 files changed

+667
-148
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
config.log
55

66
config.status
7+
compile_commands.json
8+
build/
79

810
Makefile
911

.ycm_extra_conf.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import os
2+
import ycm_core
3+
4+
from clang_helpers import PrepareClangFlags
5+
6+
def DirectoryOfThisScript():
7+
return os.path.dirname(os.path.abspath(__file__))
8+
9+
# This is the single most important line in this script. Everything else is just nice to have but
10+
# not strictly necessary.
11+
compilation_database_folder = DirectoryOfThisScript()
12+
13+
# This provides a safe fall-back if no compilation commands are available. You could also add a
14+
# includes relative to your project directory, for example.
15+
flags = [
16+
'-Wall',
17+
'-std=c++14',
18+
'-stdlib=libc++',
19+
'-x',
20+
'c++',
21+
'-I',
22+
'.',
23+
'-isystem', '/usr/local/include',
24+
'-isystem', '/usr/include',
25+
'-I.',
26+
]
27+
28+
if compilation_database_folder:
29+
database = ycm_core.CompilationDatabase(compilation_database_folder)
30+
else:
31+
database = None
32+
33+
SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
34+
35+
def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
36+
if not working_directory:
37+
return list( flags )
38+
new_flags = []
39+
make_next_absolute = False
40+
path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
41+
for flag in flags:
42+
new_flag = flag
43+
44+
if make_next_absolute:
45+
make_next_absolute = False
46+
if not flag.startswith( '/' ):
47+
new_flag = os.path.join( working_directory, flag )
48+
49+
for path_flag in path_flags:
50+
if flag == path_flag:
51+
make_next_absolute = True
52+
break
53+
54+
if flag.startswith( path_flag ):
55+
path = flag[ len( path_flag ): ]
56+
new_flag = path_flag + os.path.join( working_directory, path )
57+
break
58+
59+
if new_flag:
60+
new_flags.append( new_flag )
61+
return new_flags
62+
63+
64+
def IsHeaderFile( filename ):
65+
extension = os.path.splitext( filename )[ 1 ]
66+
return extension in [ '.h', '.hxx', '.hpp', '.hh' ]
67+
68+
69+
def GetCompilationInfoForFile( filename ):
70+
# The compilation_commands.json file generated by CMake does not have entries
71+
# for header files. So we do our best by asking the db for flags for a
72+
# corresponding source file, if any. If one exists, the flags for that file
73+
# should be good enough.
74+
if IsHeaderFile( filename ):
75+
basename = os.path.splitext( filename )[ 0 ]
76+
for extension in SOURCE_EXTENSIONS:
77+
replacement_file = basename + extension
78+
if os.path.exists( replacement_file ):
79+
compilation_info = database.GetCompilationInfoForFile(
80+
replacement_file )
81+
if compilation_info.compiler_flags_:
82+
return compilation_info
83+
return None
84+
return database.GetCompilationInfoForFile( filename )
85+
86+
87+
def FlagsForFile( filename, **kwargs ):
88+
if database:
89+
# Bear in mind that compilation_info.compiler_flags_ does NOT return a
90+
# python list, but a "list-like" StringVec object
91+
compilation_info = GetCompilationInfoForFile( filename )
92+
if not compilation_info:
93+
return None
94+
95+
final_flags = MakeRelativePathsInFlagsAbsolute(
96+
compilation_info.compiler_flags_,
97+
compilation_info.compiler_working_dir_ )
98+
99+
else:
100+
relative_to = DirectoryOfThisScript()
101+
final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
102+
103+
return {
104+
'flags': final_flags,
105+
'do_cache': True
106+
}

CMakeLists.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
# Creates the file compile_commands.json in the build directory.
4+
SET( CMAKE_EXPORT_COMPILE_COMMANDS ON )
5+
set (CMAKE_CXX_STANDARD 14)
6+
7+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
8+
include("cmake/ParseAndAddCatchTests.cmake")
9+
include("cmake/DownloadProject.CMake")
10+
11+
project(SqliteModernCpp)
12+
13+
find_package(sqlite3 REQUIRED)
14+
download_project(
15+
PROJ catch
16+
GIT_REPOSITORY [email protected]:catchorg/Catch2.git
17+
GIT_TAG v2.1.0
18+
UPDATE_DISCONNECTED 1
19+
)
20+
21+
set(CATCH_INCLUDE_DIR ${CMAKE_BINARY_DIR}/catch-src/single_include/)
22+
set(TEST_SOURCE_DIR ${CMAKE_SOURCE_DIR}/tests)
23+
#file(GLOB TEST_SOURCES ${TEST_SOURCE_DIR}/*.cc)
24+
set(TEST_SOURCES
25+
${TEST_SOURCE_DIR}/readme_example.cc
26+
${TEST_SOURCE_DIR}/blob_example.cc
27+
${TEST_SOURCE_DIR}/error_log.cc
28+
${TEST_SOURCE_DIR}/error_log2.cc
29+
${TEST_SOURCE_DIR}/exception_dont_execute.cc
30+
${TEST_SOURCE_DIR}/exception_dont_execute_nested.cc
31+
)
32+
33+
enable_testing()
34+
35+
add_library (sqlite_modern_cpp INTERFACE)
36+
target_include_directories(sqlite_modern_cpp INTERFACE hdr/)
37+
38+
add_library (Catch INTERFACE)
39+
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})
40+
41+
add_executable(tests ${TEST_SOURCES})
42+
target_include_directories(tests INTERFACE ${SQLITE3_INCLUDE_DIRS})
43+
target_link_libraries(tests Catch sqlite_modern_cpp sqlite3)
44+
45+
ParseAndAddCatchTests(tests)
46+
47+
# Place the file in the source directory, permitting us to place a single configuration file for YCM there.
48+
# YCM is the code-completion engine for (neo)vim https://github.com/Valloric/YouCompleteMe
49+
IF(EXISTS "${CMAKE_BINARY_DIR}/compile_commands.json")
50+
EXECUTE_PROCESS( COMMAND ${CMAKE_COMMAND} -E copy_if_different
51+
${CMAKE_BINARY_DIR}/compile_commands.json
52+
${CMAKE_SOURCE_DIR}/compile_commands.json
53+
)
54+
ENDIF()

cmake/.DS_Store

6 KB
Binary file not shown.

cmake/DownloadProject.CMake

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# Distributed under the OSI-approved MIT License. See accompanying
2+
# file LICENSE or https://github.com/Crascit/DownloadProject for details.
3+
#
4+
# MODULE: DownloadProject
5+
#
6+
# PROVIDES:
7+
# download_project( PROJ projectName
8+
# [PREFIX prefixDir]
9+
# [DOWNLOAD_DIR downloadDir]
10+
# [SOURCE_DIR srcDir]
11+
# [BINARY_DIR binDir]
12+
# [QUIET]
13+
# ...
14+
# )
15+
#
16+
# Provides the ability to download and unpack a tarball, zip file, git repository,
17+
# etc. at configure time (i.e. when the cmake command is run). How the downloaded
18+
# and unpacked contents are used is up to the caller, but the motivating case is
19+
# to download source code which can then be included directly in the build with
20+
# add_subdirectory() after the call to download_project(). Source and build
21+
# directories are set up with this in mind.
22+
#
23+
# The PROJ argument is required. The projectName value will be used to construct
24+
# the following variables upon exit (obviously replace projectName with its actual
25+
# value):
26+
#
27+
# projectName_SOURCE_DIR
28+
# projectName_BINARY_DIR
29+
#
30+
# The SOURCE_DIR and BINARY_DIR arguments are optional and would not typically
31+
# need to be provided. They can be specified if you want the downloaded source
32+
# and build directories to be located in a specific place. The contents of
33+
# projectName_SOURCE_DIR and projectName_BINARY_DIR will be populated with the
34+
# locations used whether you provide SOURCE_DIR/BINARY_DIR or not.
35+
#
36+
# The DOWNLOAD_DIR argument does not normally need to be set. It controls the
37+
# location of the temporary CMake build used to perform the download.
38+
#
39+
# The PREFIX argument can be provided to change the base location of the default
40+
# values of DOWNLOAD_DIR, SOURCE_DIR and BINARY_DIR. If all of those three arguments
41+
# are provided, then PREFIX will have no effect. The default value for PREFIX is
42+
# CMAKE_BINARY_DIR.
43+
#
44+
# The QUIET option can be given if you do not want to show the output associated
45+
# with downloading the specified project.
46+
#
47+
# In addition to the above, any other options are passed through unmodified to
48+
# ExternalProject_Add() to perform the actual download, patch and update steps.
49+
# The following ExternalProject_Add() options are explicitly prohibited (they
50+
# are reserved for use by the download_project() command):
51+
#
52+
# CONFIGURE_COMMAND
53+
# BUILD_COMMAND
54+
# INSTALL_COMMAND
55+
# TEST_COMMAND
56+
#
57+
# Only those ExternalProject_Add() arguments which relate to downloading, patching
58+
# and updating of the project sources are intended to be used. Also note that at
59+
# least one set of download-related arguments are required.
60+
#
61+
# If using CMake 3.2 or later, the UPDATE_DISCONNECTED option can be used to
62+
# prevent a check at the remote end for changes every time CMake is run
63+
# after the first successful download. See the documentation of the ExternalProject
64+
# module for more information. It is likely you will want to use this option if it
65+
# is available to you. Note, however, that the ExternalProject implementation contains
66+
# bugs which result in incorrect handling of the UPDATE_DISCONNECTED option when
67+
# using the URL download method or when specifying a SOURCE_DIR with no download
68+
# method. Fixes for these have been created, the last of which is scheduled for
69+
# inclusion in CMake 3.8.0. Details can be found here:
70+
#
71+
# https://gitlab.kitware.com/cmake/cmake/commit/bdca68388bd57f8302d3c1d83d691034b7ffa70c
72+
# https://gitlab.kitware.com/cmake/cmake/issues/16428
73+
#
74+
# If you experience build errors related to the update step, consider avoiding
75+
# the use of UPDATE_DISCONNECTED.
76+
#
77+
# EXAMPLE USAGE:
78+
#
79+
# include(DownloadProject)
80+
# download_project(PROJ googletest
81+
# GIT_REPOSITORY https://github.com/google/googletest.git
82+
# GIT_TAG master
83+
# UPDATE_DISCONNECTED 1
84+
# QUIET
85+
# )
86+
#
87+
# add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR})
88+
#
89+
#========================================================================================
90+
91+
92+
set(_DownloadProjectDir "${CMAKE_CURRENT_LIST_DIR}")
93+
94+
include(CMakeParseArguments)
95+
96+
function(download_project)
97+
98+
set(options QUIET)
99+
set(oneValueArgs
100+
PROJ
101+
PREFIX
102+
DOWNLOAD_DIR
103+
SOURCE_DIR
104+
BINARY_DIR
105+
# Prevent the following from being passed through
106+
CONFIGURE_COMMAND
107+
BUILD_COMMAND
108+
INSTALL_COMMAND
109+
TEST_COMMAND
110+
)
111+
set(multiValueArgs "")
112+
113+
cmake_parse_arguments(DL_ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
114+
115+
# Hide output if requested
116+
if (DL_ARGS_QUIET)
117+
set(OUTPUT_QUIET "OUTPUT_QUIET")
118+
else()
119+
unset(OUTPUT_QUIET)
120+
message(STATUS "Downloading/updating ${DL_ARGS_PROJ}")
121+
endif()
122+
123+
# Set up where we will put our temporary CMakeLists.txt file and also
124+
# the base point below which the default source and binary dirs will be.
125+
# The prefix must always be an absolute path.
126+
if (NOT DL_ARGS_PREFIX)
127+
set(DL_ARGS_PREFIX "${CMAKE_BINARY_DIR}")
128+
else()
129+
get_filename_component(DL_ARGS_PREFIX "${DL_ARGS_PREFIX}" ABSOLUTE
130+
BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
131+
endif()
132+
if (NOT DL_ARGS_DOWNLOAD_DIR)
133+
set(DL_ARGS_DOWNLOAD_DIR "${DL_ARGS_PREFIX}/${DL_ARGS_PROJ}-download")
134+
endif()
135+
136+
# Ensure the caller can know where to find the source and build directories
137+
if (NOT DL_ARGS_SOURCE_DIR)
138+
set(DL_ARGS_SOURCE_DIR "${DL_ARGS_PREFIX}/${DL_ARGS_PROJ}-src")
139+
endif()
140+
if (NOT DL_ARGS_BINARY_DIR)
141+
set(DL_ARGS_BINARY_DIR "${DL_ARGS_PREFIX}/${DL_ARGS_PROJ}-build")
142+
endif()
143+
set(${DL_ARGS_PROJ}_SOURCE_DIR "${DL_ARGS_SOURCE_DIR}" PARENT_SCOPE)
144+
set(${DL_ARGS_PROJ}_BINARY_DIR "${DL_ARGS_BINARY_DIR}" PARENT_SCOPE)
145+
146+
# The way that CLion manages multiple configurations, it causes a copy of
147+
# the CMakeCache.txt to be copied across due to it not expecting there to
148+
# be a project within a project. This causes the hard-coded paths in the
149+
# cache to be copied and builds to fail. To mitigate this, we simply
150+
# remove the cache if it exists before we configure the new project. It
151+
# is safe to do so because it will be re-generated. Since this is only
152+
# executed at the configure step, it should not cause additional builds or
153+
# downloads.
154+
file(REMOVE "${DL_ARGS_DOWNLOAD_DIR}/CMakeCache.txt")
155+
156+
# Create and build a separate CMake project to carry out the download.
157+
# If we've already previously done these steps, they will not cause
158+
# anything to be updated, so extra rebuilds of the project won't occur.
159+
# Make sure to pass through CMAKE_MAKE_PROGRAM in case the main project
160+
# has this set to something not findable on the PATH.
161+
configure_file("${_DownloadProjectDir}/DownloadProject.CMakeLists.cmake.in"
162+
"${DL_ARGS_DOWNLOAD_DIR}/CMakeLists.txt")
163+
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}"
164+
-D "CMAKE_MAKE_PROGRAM:FILE=${CMAKE_MAKE_PROGRAM}"
165+
.
166+
RESULT_VARIABLE result
167+
${OUTPUT_QUIET}
168+
WORKING_DIRECTORY "${DL_ARGS_DOWNLOAD_DIR}"
169+
)
170+
if(result)
171+
message(FATAL_ERROR "CMake step for ${DL_ARGS_PROJ} failed: ${result}")
172+
endif()
173+
execute_process(COMMAND ${CMAKE_COMMAND} --build .
174+
RESULT_VARIABLE result
175+
${OUTPUT_QUIET}
176+
WORKING_DIRECTORY "${DL_ARGS_DOWNLOAD_DIR}"
177+
)
178+
if(result)
179+
message(FATAL_ERROR "Build step for ${DL_ARGS_PROJ} failed: ${result}")
180+
endif()
181+
182+
endfunction()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Distributed under the OSI-approved MIT License. See accompanying
2+
# file LICENSE or https://github.com/Crascit/DownloadProject for details.
3+
4+
cmake_minimum_required(VERSION 2.8.2)
5+
6+
project(${DL_ARGS_PROJ}-download NONE)
7+
8+
include(ExternalProject)
9+
ExternalProject_Add(${DL_ARGS_PROJ}-download
10+
${DL_ARGS_UNPARSED_ARGUMENTS}
11+
SOURCE_DIR "${DL_ARGS_SOURCE_DIR}"
12+
BINARY_DIR "${DL_ARGS_BINARY_DIR}"
13+
CONFIGURE_COMMAND ""
14+
BUILD_COMMAND ""
15+
INSTALL_COMMAND ""
16+
TEST_COMMAND ""
17+
)

0 commit comments

Comments
 (0)