CMake: simplify generator and cmakelists

parent f8dc8e77
......@@ -9,18 +9,24 @@ include(CheckCXXCompilerFlag)
include(sanitizer) # sanitizer -DSANITIZE=<sanitizer>
find_package(Backtrace)
# warn if neither clang nor gnu
message("[Current compiler] ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
if (NOT ${CMAKE_CXX_COMPILER_ID} MATCHES "(Clang|GNU)")
message(WARNING "Compiler ${CMAKE_CXX_COMPILER_ID} is not officially supported.")
endif ()
message("[Current compiler] ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
# set cxx standard
set (CMAKE_CXX_STANDARD 17)
set (CXX_STANDARD_REQUIRED ON)
# set build flags
# set -fPIC
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# add build flags
add_compile_options(-Wall -pedantic -pipe -Wextra -Werror -Wshadow -Wpointer-arith -Wcast-qual -Wdelete-non-virtual-dtor -Wredundant-decls)
# set definitions
add_definitions(-DQT_NO_FOREACH)
if(BACKTRACE_EXISTS)
add_definitions(-DBACKTRACE)
endif()
......
......@@ -69,3 +69,6 @@ find: Readline REQUIRED
[CMake:Deps:tclap]
include: ${TCLAP_INCLUDE_DIR}
find: Tclap REQUIRED
[CMake:Deps:stdc++fs]
link: stdc++fs
......@@ -203,18 +203,14 @@ class Generator:
def generate_root(cls, project, dry_run, projects):
with SmartOpen("CMakeLists.txt", 'w', directory=ALIB_DIRECTORY, dry_run=dry_run) as f:
f.write(CATEGORIES['root'].format(
alib_modules_lib=Helpers.join([p.name for p in projects if p.category == "library"]),
alib_modules_exe=Helpers.join([p.name for p in projects if p.category == "executable"]),
alib_modules_test=Helpers.join([p.name for p in projects if p.category == "testing"]),
alib_versioning_major=Config.get(CONFIG, 'Versioning', 'major'),
alib_versioning_minor=Config.get(CONFIG, 'Versioning', 'minor'),
alib_versioning_patch=Config.get(CONFIG, 'Versioning', 'patch'),
alib_modules=Helpers.join([p.name for p in projects]),
alib_version=ALIB_VERSION,
))
@classmethod
def generate_library(cls, project, dry_run):
sys_includes, sys_libs, finds = cls._handle_system_deps(project, project.system_deps)
test_sys_includes, test_sys_libs, test_finds = cls._handle_system_deps(project, project.system_deps_test)
sys_includes, sys_libs, finds = cls._handle_system_deps(project.system_deps)
test_sys_includes, test_sys_libs, test_finds = cls._handle_system_deps(project.system_deps_test)
with SmartOpen("CMakeLists.txt", 'w', directory=project.path, dry_run=dry_run) as f:
f.write(CATEGORIES[project.category].format(
......@@ -229,14 +225,12 @@ class Generator:
include_files=Helpers.join(project.find_install_sources()),
find_packages=Helpers.join(map(lambda p: "find_package({})".format(p), finds), "\n"),
find_packages_tests=Helpers.join(map(lambda p: "find_package({})".format(p), test_finds), "\n"),
alib_versioning_major=Config.get(CONFIG, 'Versioning', 'major'),
alib_versioning_minor=Config.get(CONFIG, 'Versioning', 'minor'),
alib_versioning_patch=Config.get(CONFIG, 'Versioning', 'patch'),
alib_version=ALIB_VERSION,
))
@classmethod
def generate_testing(cls, project, dry_run):
test_sys_includes, test_sys_libs, test_finds = cls._handle_system_deps(project, project.system_deps_test)
test_sys_includes, test_sys_libs, test_finds = cls._handle_system_deps(project.system_deps_test)
with SmartOpen("CMakeLists.txt", 'w', directory=project.path, dry_run=dry_run) as f:
f.write(CATEGORIES[project.category].format(
......@@ -246,14 +240,12 @@ class Generator:
cmake_options=Helpers.join(["set({} {})".format(k.upper(), v) for k, v in project.cmake_additional_set], sep='\n'),
source_files_test=Helpers.join(project.find_sources_test()),
find_packages_tests=Helpers.join(map(lambda p: "find_package({})".format(p), test_finds), "\n"),
alib_versioning_major=Config.get(CONFIG, 'Versioning', 'major'),
alib_versioning_minor=Config.get(CONFIG, 'Versioning', 'minor'),
alib_versioning_patch=Config.get(CONFIG, 'Versioning', 'patch'),
alib_version=ALIB_VERSION,
))
@classmethod
def generate_executable(cls, project, dry_run):
sys_includes, sys_libs, finds = cls._handle_system_deps(project, project.system_deps)
sys_includes, sys_libs, finds = cls._handle_system_deps(project.system_deps)
with SmartOpen("CMakeLists.txt", 'w', directory=project.path, dry_run=dry_run) as f:
f.write(CATEGORIES[project.category].format(
......@@ -263,36 +255,22 @@ class Generator:
cmake_options=Helpers.join(["set({} {})".format(k.upper(), v) for k, v in project.cmake_additional_set], sep='\n'),
source_files=Helpers.join(project.find_sources()),
find_packages=Helpers.join(map(lambda p: "find_package({})".format(p), finds), "\n"),
alib_versioning_major=Config.get(CONFIG, 'Versioning', 'major'),
alib_versioning_minor=Config.get(CONFIG, 'Versioning', 'minor'),
alib_versioning_patch=Config.get(CONFIG, 'Versioning', 'patch'),
alib_version=ALIB_VERSION,
))
@staticmethod
def _handle_system_deps(project, system_deps):
def _handle_system_deps(system_deps):
libs, incl, find = [], [], []
def update(cfg, dep, incl, libs, find):
# try access the section, raises ConfigException if not present
Config.get_section(cfg, 'CMake:Deps:{}'.format(dep))
incl += Config.get_array(cfg, 'CMake:Deps:{}'.format(dep), 'include', fallback=[])
libs += Config.get_array(cfg, 'CMake:Deps:{}'.format(dep), 'link', fallback=[])
f = Config.get(CONFIG, 'CMake:Deps:{}'.format(dep), 'find', fallback="")
if f is not "":
find.append(f)
for dep in system_deps:
try:
# first, try to find CMake:Deps:{} section in projects's conf
update(project._config, dep, incl, libs, find)
incl += Config.get_array(CONFIG, 'CMake:Deps:{}'.format(dep), 'include', fallback=[])
libs += Config.get_array(CONFIG, 'CMake:Deps:{}'.format(dep), 'link', fallback=[])
f = Config.get(CONFIG, 'CMake:Deps:{}'.format(dep), 'find', fallback="")
if f is not "":
find.append(f)
except ConfigException:
try:
# then in main conf
update(CONFIG, dep, incl, libs, find)
except ConfigException:
# warning!
print("Warning: System dependency '{}' is not configured".format(dep), file=sys.stderr)
print("Warning: System dependency '{}' is not configured".format(dep), file=sys.stderr)
return incl, libs, find
......@@ -303,6 +281,8 @@ SCRIPT_DIRECTORY = Helpers.path(os.path.dirname(__file__))
CONFIG = configparser.ConfigParser(allow_no_value=True)
CONFIG.read(Helpers.path(SCRIPT_DIRECTORY, 'generate.conf'))
ALIB_DIRECTORY = Helpers.path(SCRIPT_DIRECTORY, Config.get(CONFIG, 'General', 'PathToSources'))
ALIB_VERSION = "{}.{}.{}".format(Config.get(CONFIG, 'Versioning', 'major'), Config.get(CONFIG, 'Versioning', 'minor'),
Config.get(CONFIG, 'Versioning', 'patch'))
PROJECTS = {
project: Project(project, Config.get(CONFIG, 'General', 'ProjectConfFile'))
for project in Helpers.find_dirs_with_file(ALIB_DIRECTORY, Config.get(CONFIG, 'General', 'ProjectConfFile'))
......
project({project_name}
VERSION {alib_versioning_major}.{alib_versioning_minor}.{alib_versioning_patch}
VERSION {alib_version}
LANGUAGES CXX)
set(PROJECT_NAME {project_name})
......@@ -16,22 +16,11 @@ add_executable(${{PROJECT_NAME}} ${{SOURCE_FILES}})
target_include_directories(${{PROJECT_NAME}}
PUBLIC ${{CMAKE_CURRENT_SOURCE_DIR}}/src # anything that depends on this should include src also, hence public
{include_paths}
${{CMAKE_BINARY_DIR}}/generated
)
# Target link libraries
target_link_libraries(${{PROJECT_NAME}} {target_libs})
set_target_properties(${{PROJECT_NAME}} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
LINKER_LANGUAGE CXX
INTERFACE_POSITION_INDEPENDENT_CODE ON
)
# if (CMAKE_BUILD_TYPE STREQUAL "Release")
# set_property ( TARGET ${{PROJECT_NAME}} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE )
# endif ()
# Install
install(TARGETS ${{PROJECT_NAME}} RUNTIME DESTINATION bin)
project({project_name}
VERSION {alib_versioning_major}.{alib_versioning_minor}.{alib_versioning_patch}
VERSION {alib_version}
LANGUAGES CXX)
set(PROJECT_NAME_TEST test-{project_name})
......@@ -17,24 +17,16 @@ configure_file (
)
add_executable(${{PROJECT_NAME_TEST}} ${{SOURCE_FILES_TEST}})
set_target_properties(${{PROJECT_NAME_TEST}} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
)
target_link_libraries(${{PROJECT_NAME_TEST}} {target_test_libs} catch2)
# Include dependencies directories
target_include_directories(${{PROJECT_NAME_TEST}}
PUBLIC ${{CMAKE_CURRENT_SOURCE_DIR}}/test-src # anything that depends on this should include src also, hence public
${{CMAKE_BINARY_DIR}}/generated # testfiles header
{include_test_paths}
${{CMAKE_BINARY_DIR}}/generated
)
# if (CMAKE_BUILD_TYPE STREQUAL "Release")
# set_property ( TARGET ${{PROJECT_NAME_TEST}} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE )
# endif ()
include(Catch)
catch_discover_tests(
${{PROJECT_NAME_TEST}}
......
project({project_name}
VERSION {alib_versioning_major}.{alib_versioning_minor}.{alib_versioning_patch}
VERSION {alib_version}
LANGUAGES CXX)
set(PROJECT_NAME {project_name})
......@@ -21,23 +21,14 @@ target_link_libraries(${{PROJECT_NAME}} LINK_PUBLIC {target_libs})
target_include_directories(${{PROJECT_NAME}}
PUBLIC ${{CMAKE_CURRENT_SOURCE_DIR}}/src # anything that depends on this should include src also, hence public
{include_paths}
${{CMAKE_BINARY_DIR}}/generated
)
set_target_properties(${{PROJECT_NAME}} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
LINKER_LANGUAGE CXX
INTERFACE_POSITION_INDEPENDENT_CODE ON
VERSION ${{PROJECT_VERSION}}
SOVERSION ${{PROJECT_VERSION_MAJOR}}
)
# if (CMAKE_BUILD_TYPE STREQUAL "Release")
# set_property ( TARGET ${{PROJECT_NAME}} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE )
# endif ()
# Install
install(TARGETS ${{PROJECT_NAME}} LIBRARY DESTINATION lib)
......@@ -62,10 +53,6 @@ set(SOURCE_FILES_TEST
)
add_executable(${{PROJECT_NAME_TEST}} ${{SOURCE_FILES_TEST}})
set_target_properties(${{PROJECT_NAME_TEST}} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
)
target_link_libraries(${{PROJECT_NAME_TEST}} ${{PROJECT_NAME}} {target_test_libs} catch2)
......@@ -75,10 +62,6 @@ target_include_directories(${{PROJECT_NAME_TEST}}
{include_test_paths}
)
# if (CMAKE_BUILD_TYPE STREQUAL "Release")
# set_property ( TARGET ${{PROJECT_NAME_TEST}} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE )
# endif ()
enable_testing()
add_test(NAME "[unit]${{PROJECT_NAME}}"
COMMAND $<TARGET_FILE:${{PROJECT_NAME_TEST}}>
......
cmake_minimum_required(VERSION 3.7)
project(alib2
VERSION {alib_versioning_major}.{alib_versioning_minor}.{alib_versioning_patch}
VERSION {alib_version}
LANGUAGES CXX)
set(CMAKE_MODULE_PATH ${{CMAKE_SOURCE_DIR}}/CMake/Modules)
......@@ -47,18 +47,6 @@ include(Install)
enable_testing()
add_subdirectory(contrib/catch2)
# list projects
set(ALIB_MODULES_LIB
{alib_modules_lib}
)
set(ALIB_MODULES_EXE
{alib_modules_exe}
)
set(ALIB_MODULES_TEST
{alib_modules_test}
)
foreach (module ${{ALIB_MODULES_LIB}} ${{ALIB_MODULES_EXE}} ${{ALIB_MODULES_TEST}})
foreach (module {alib_modules})
add_subdirectory(${{module}})
endforeach ()
......@@ -4,6 +4,3 @@ category: library
[Dependencies]
project: alib2xml alib2common alib2abstraction alib2measure alib2std
system: xml2 stdc++fs
[CMake:Deps:stdc++fs]
link: stdc++fs
......@@ -5,6 +5,3 @@ groups: cli
[TestDependencies]
project: alib2cli alib2str alib2std alib2common alib2algo alib2data alib2elgo alib2aux
system: stdc++fs
[CMake:Deps:stdc++fs]
link: stdc++fs
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment