From 657b599b5202796b8cbbd6ecb174d14a2e669c59 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 5 Jun 2017 11:22:09 -0400 Subject: [PATCH] Cmake cleanup (#21) * Fixes for #8, vars hidden and findPython used * Adding compat with default python, better defaults --- CMakeLists.txt | 11 +++++++++-- cmake/AddGoogletest.cmake | 2 ++ scripts/MakeSingleHeader.py | 16 ++++++++-------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 38955915..f8b8e0b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,11 +53,18 @@ add_library(CLI11 INTERFACE) target_include_directories(CLI11 INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include") # Single file test -option(CLI_SINGLE_FILE "Generate a single header file (and test)" ${CUR_PROJ}) +find_package(PythonInterp) +if(CUR_PROJ AND PYTHONINTERP_FOUND) + set(CLI_SINGLE_FILE_DEFAULT ON) +else() + set(CLI_SINGLE_FILE_DEFAULT OFF) +endif() +option(CLI_SINGLE_FILE "Generate a single header file (and test)" ${CLI_SINGLE_FILE_DEFAULT}) if(CLI_SINGLE_FILE) + find_package(PythonInterp REQUIRED) file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/include") add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp" - COMMAND python "${CMAKE_CURRENT_SOURCE_DIR}/scripts/MakeSingleHeader.py" "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp" + COMMAND "${PYTHON_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/scripts/MakeSingleHeader.py" "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp" DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/CLI/CLI.hpp" ${CLI_headers} ) add_custom_target(generate_cli_single_file ALL diff --git a/cmake/AddGoogletest.cmake b/cmake/AddGoogletest.cmake index e18ce48a..5e026cca 100644 --- a/cmake/AddGoogletest.cmake +++ b/cmake/AddGoogletest.cmake @@ -46,6 +46,8 @@ gtest_build_tests gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols +BUILD_GMOCK +BUILD_GTEST ) set_target_properties(gtest gtest_main gmock gmock_main diff --git a/scripts/MakeSingleHeader.py b/scripts/MakeSingleHeader.py index 15760929..23c2de3f 100755 --- a/scripts/MakeSingleHeader.py +++ b/scripts/MakeSingleHeader.py @@ -4,24 +4,24 @@ from __future__ import print_function, unicode_literals +import os import re import argparse -from pathlib import Path from subprocess import check_output includes_local = re.compile(r"""^#include "(.*)"$""", re.MULTILINE) includes_system = re.compile(r"""^#include \<(.*)\>$""", re.MULTILINE) -DIR = Path(__file__).resolve().parent -BDIR = DIR.parent / 'include' +DIR = os.path.dirname(os.path.abspath(__file__)) # Path(__file__).resolve().parent +BDIR = os.path.join(os.path.dirname(DIR), 'include') # DIR.parent / 'include' print("Git directory:", DIR) TAG = check_output(['git', 'describe', '--tags', '--always'], cwd=str(DIR)).decode("utf-8") def MakeHeader(out): - main_header = BDIR / 'CLI/CLI.hpp' - with main_header.open() as f: + main_header = os.path.join(BDIR, 'CLI', 'CLI.hpp') + with open(main_header) as f: header = f.read() include_files = includes_local.findall(header) @@ -29,7 +29,7 @@ def MakeHeader(out): headers = set() output = '' for inc in include_files: - with (BDIR / inc).open() as f: + with open(os.path.join(BDIR, inc)) as f: inner = f.read() headers |= set(includes_system.findall(inner)) output += '\n// From {inc}\n\n'.format(inc=inc) @@ -50,7 +50,7 @@ def MakeHeader(out): {header_list} {output}'''.format(header_list=header_list, output=output, tag=TAG) - with Path(out).open('w') as f: + with open(out, 'w') as f: f.write(output) print("Created {out}".format(out=out)) @@ -58,6 +58,6 @@ def MakeHeader(out): if __name__ == '__main__': parser = argparse.ArgumentParser() - parser.add_argument("output", nargs='?', default=BDIR / 'CLI11.hpp') + parser.add_argument("output", nargs='?', default=os.path.join(BDIR, 'CLI11.hpp')) args = parser.parse_args() MakeHeader(args.output)