Reorganize as modern CMake with an interface target

pull/247/head
Oliver Smith 2 years ago
parent dde4aaeb2f
commit 8deeab8465
  1. 60
      CMakeLists.txt
  2. 6
      cmake/PeglibCXXStandard.cmake
  3. 15
      cmake/PeglibOptions.cmake
  4. 13
      cmake/PeglibThreads.cmake
  5. 14
      example/CMakeLists.txt
  6. 5
      lint/CMakeLists.txt
  7. 5
      pl0/CMakeLists.txt
  8. 3
      test/CMakeLists.txt

@ -1,32 +1,60 @@
# Peglib is a header-only library with zero dependencies (pthreads on Linux).
#
# To build with it, add the CppPegLib INTERFACE in your cmake lists, this will
# provide the neccessary include paths, linkage, etc.
#
# Then your code simply needs to #include <peglib.h>
cmake_minimum_required(VERSION 3.14)
project(cpp-peglib)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)
project(cpp-peglib DESCRIPTION "C++17 header-only PEG library" LANGUAGES CXX)
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus /utf-8 /D_CRT_SECURE_NO_DEPRECATE")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
endif()
include(cmake/PeglibCXXStandard.cmake)
include(cmake/PeglibOptions.cmake)
add_library(
CppPegLib
INTERFACE
peglib.h
)
# Tag the library with the path to the top of it's source tree.
set_target_properties(CppPegLib PROPERTIES SOURCE_FOLDER ${CMAKE_CURRENT_LIST_DIR})
option(PEGLIB_BUILD_TESTS "Build cpp-peglib tests" ON)
# Include cmake helpers that attach other properties to the target.
include(cmake/PeglibCompileOptions.cmake)
include(cmake/PeglibIncludePaths.cmake)
include(cmake/PeglibThreads.cmake)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(add_link_deps Threads::Threads)
if(PEGLIB_BUILD_LINTER)
add_subdirectory(lint)
else()
message(STATUS "PEGLIB_BUILD_LINTER is OFF: Not building linter")
endif()
add_subdirectory(lint)
add_subdirectory(example)
if(PEGLIB_BUILD_EXAMPLES)
add_subdirectory(example)
else()
message(STATUS "PEGLIB_BUILD_EXAMPLES is OFF: Not building examples")
endif ()
# add_subdirectory(cymbol)
if (${PEGLIB_BUILD_TESTS})
if (PEGLIB_BUILD_TESTS)
include(CTest)
add_subdirectory(test)
enable_testing()
else()
message(STATUS "PEGLIB_BUILD_TESTS is OFF: Skipping tests")
endif()
# This facilitates users who want to install this system-wide, but they'll have the
# complication of needing to set the compile flags and potentially link flag.
# Might be worth telling CMake how to generate a config when installing.
install(FILES peglib.h DESTINATION include)

@ -0,0 +1,6 @@
include_guard(GLOBAL)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

@ -0,0 +1,15 @@
include_guard(GLOBAL)
# Only default ot building tests and examples when building standalone, which
# we can determine by whether the top of the build tree is this folder or not.
if(CMAKE_SOURCE_DIR STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
set(_standalone_build ON)
else()
set(_standalone_build OFF)
endif()
option(PEGLIB_BUILD_LINTER "Build peglib's linter" ${_standalone_build})
option(PEGLIB_BUILD_TESTS "Build cpp-peglib tests" ${_standalone_build})
option(PEGLIB_BUILD_EXAMPLES "Build example parsers" ${_standalone_build})
unset(_standalone_build)

@ -0,0 +1,13 @@
include_guard(GLOBAL)
# We don't really have any dependencies, except needing to link against the
# pthreads library on linu.x
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_compile_options(CppPegLib "-pthread")
target_link_libraries(CppPegLib Threads::Threads)
endif()

@ -1,22 +1,20 @@
cmake_minimum_required(VERSION 3.14)
project(example)
include_directories(..)
add_executable(calc calc.cc)
target_link_libraries(calc ${add_link_deps})
target_link_libraries(calc CppPegLib)
add_executable(calc2 calc2.cc)
target_link_libraries(calc2 ${add_link_deps})
target_link_libraries(calc2 CppPegLib)
add_executable(calc3 calc3.cc)
target_link_libraries(calc3 ${add_link_deps})
target_link_libraries(calc3 CppPegLib)
add_executable(calc4 calc4.cc)
target_link_libraries(calc4 ${add_link_deps})
target_link_libraries(calc4 CppPegLib)
add_executable(calc5 calc5.cc)
target_link_libraries(calc5 ${add_link_deps})
target_link_libraries(calc5 CppPegLib)
add_executable(indent indent.cc)
target_link_libraries(indent ${add_link_deps})
target_link_libraries(indent CppPegLib)

@ -1,7 +1,6 @@
cmake_minimum_required(VERSION 3.14)
project(peglint)
include_directories(..)
add_executable(peglint peglint.cc)
target_link_libraries(peglint ${add_link_deps})
target_link_libraries(peglint CppPegLib)

@ -1,10 +1,7 @@
/Users/yhirose/Projects/cpp-peglib/.travis.yml
project(pl0)
include_directories(..)
add_executable(pl0 pl0.cc)
find_package(LLVM REQUIRED CONFIG)
set(add_link_deps ${add_link_deps} LLVM)
target_include_directories(pl0 PUBLIC ${LLVM_INCLUDE_DIRS})
target_link_libraries(pl0 ${add_link_deps})
target_link_libraries(pl0 CppPegLib LLVM)

@ -14,8 +14,7 @@ enable_testing()
add_executable(test-main test1.cc test2.cc test3.cc)
target_include_directories(test-main PRIVATE ..)
include(GoogleTest)
gtest_discover_tests(test-main)
target_link_libraries(test-main PUBLIC CppPegLib)
target_link_libraries(test-main PRIVATE gtest_main)

Loading…
Cancel
Save