Skip to content

Commit

Permalink
feat(cxx): introduce custom cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
pplmx committed Nov 18, 2024
1 parent 86bd489 commit 0b7add8
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 7 deletions.
22 changes: 15 additions & 7 deletions template/cxx/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import os
import shutil
import subprocess

def remove_file_if_exists(file_path):
if os.path.exists(file_path):
os.remove(file_path)

def remove_path_if_exists(path):
if os.path.exists(path):
if os.path.isfile(path):
os.remove(path)
elif os.path.isdir(path):
shutil.rmtree(path)


cxx_build_tool = "{{cookiecutter.cxx_build_tool}}"

cmake_root = "CMakeLists.txt"
cmake_test = "tests/CMakeLists.txt"
cmake_custom = "cmake"
xmake_root = "xmake.lua"
xmake_test = "tests/xmake.lua"

if cxx_build_tool == "cmake":
remove_file_if_exists(xmake_root)
remove_file_if_exists(xmake_test)
remove_path_if_exists(xmake_root)
remove_path_if_exists(xmake_test)
elif cxx_build_tool == "xmake":
remove_file_if_exists(cmake_root)
remove_file_if_exists(cmake_test)
remove_path_if_exists(cmake_root)
remove_path_if_exists(cmake_test)
remove_path_if_exists(cmake_custom)
else:
raise ValueError(f"Unknown cxx_build_tool: {cxx_build_tool}")

Expand Down
3 changes: 3 additions & 0 deletions template/cxx/{{cookiecutter.project_slug}}/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ project({{cookiecutter.project_slug}}

# ===================== Build Settings =====================

# Add custom cmake modules path
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)

# Set C++ standards
set(CMAKE_CXX_STANDARD {{cookiecutter.cxx_standard_version}})
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down
78 changes: 78 additions & 0 deletions template/cxx/{{cookiecutter.project_slug}}/cmake/FindXXX.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# FindXXX.cmake - Locate XXX library and headers
#
# This module defines the following variables:
# XXX_FOUND - True if the XXX library and headers are found
# XXX_INCLUDE_DIRS - The include directories for XXX
# XXX_LIBRARIES - The libraries to link against for XXX
# XXX_VERSION - The version string of XXX (if available)

# Define search paths
set(XXX_SEARCH_PATHS
${CMAKE_PREFIX_PATH}
/usr/local/xxx
/usr/local
/usr
/opt/xxx
/opt
)

# Locate the header files
find_path(XXX_INCLUDE_DIR NAMES xxx.h
HINTS
${XXX_ROOT}
$ENV{XXX_ROOT}
PATHS
${XXX_SEARCH_PATHS}
PATH_SUFFIXES include
)

# Define library components
set(XXX_LIB_COMPONENTS component1 component2 component3)

# Locate the libraries
set(XXX_LIBRARIES)
foreach(_comp ${XXX_LIB_COMPONENTS})
find_library(XXX_${_comp}_LIBRARY
NAMES ${_comp}
HINTS
${XXX_ROOT}
$ENV{XXX_ROOT}
PATHS
${XXX_SEARCH_PATHS}
PATH_SUFFIXES lib lib64
)
if(XXX_${_comp}_LIBRARY)
list(APPEND XXX_LIBRARIES ${XXX_${_comp}_LIBRARY})
endif()
endforeach()

# Set include directories
set(XXX_INCLUDE_DIRS ${XXX_INCLUDE_DIR})

# Version check (adapted it with your own library structure)
if(XXX_INCLUDE_DIR AND EXISTS "${XXX_INCLUDE_DIR}/../release-xxx.txt")
get_filename_component(XXX_PARENT_DIR ${XXX_INCLUDE_DIR} DIRECTORY)
set(RELEASE_FILE "${XXX_PARENT_DIR}/release-xxx.txt")
if(EXISTS "${RELEASE_FILE}")
file(READ "${RELEASE_FILE}" XXX_VERSION_CONTENTS LIMIT_COUNT 1)
string(REGEX MATCH "XXX SDK ([0-9]+\\.[0-9]+\\.[0-9]+)" XXX_VERSION_MATCH "${XXX_VERSION_CONTENTS}")
if(XXX_VERSION_MATCH)
string(REGEX REPLACE "XXX SDK ([0-9]+\\.[0-9]+\\.[0-9]+)" "\\1" XXX_VERSION "${XXX_VERSION_MATCH}")
else()
message(WARNING "Failed to detect XXX version from ${RELEASE_FILE}")
endif()
endif()
endif()

# Handle the QUIETLY and REQUIRED arguments, set XXX_FOUND to TRUE if all listed variables are TRUE
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(XXX
REQUIRED_VARS XXX_LIBRARIES XXX_INCLUDE_DIRS
VERSION_VAR XXX_VERSION
)

# Mark variables as advanced
mark_as_advanced(XXX_INCLUDE_DIR)
foreach(_comp ${XXX_LIB_COMPONENTS})
mark_as_advanced(XXX_${_comp}_LIBRARY)
endforeach()

0 comments on commit 0b7add8

Please sign in to comment.