Skip to content

Commit

Permalink
feat: Add cmake.py for easier location of the cmake helper files.
Browse files Browse the repository at this point in the history
  • Loading branch information
BraynStorm committed Apr 25, 2022
1 parent f09160c commit 0d438fe
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 9 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
**/.mypy_cache
**/__pycache__
**/build
ccgen.*
dist
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ pip install -U git+https://github.com/BraynStorm/ccgen
```

## Usage
Set your CMake Module Path like this:
```
-DCMAKE_MODULE_PATH=%LOCALAPPDATA%\Python\PythonXXX\Lib\site-packages\ccgen\share\cmake
Use the following snippet in your top-level CMakeLists.txt
```cmake
find_package(Python3 REQUIRED COMPONENTS Interpreter)
execute_process(COMMAND ${Python3_EXECUTABLE} -m ccgen.cmake OUTPUT_VARIABLE CCGEN_CMAKE OUTPUT_STRIP_TRAILING_WHITESPACE)
list(APPEND CMAKE_MODULE_PATH "${CCGEN_CMAKE}")
include(ccgen)
```
on Windows or the equivalent on your operating system.
## Example CMake project
See the examples/ folder for full examples.

Expand All @@ -44,10 +46,13 @@ enums:
### CMakeLists.txt
```cmake
cmake_minimum_required(VERSION 3.15)
cmake_minimum_required(VERSION 3.23)

project(MyProject)

find_package(Python3 REQUIRED COMPONENTS Interpreter)
execute_process(COMMAND ${Python3_EXECUTABLE} -m ccgen.cmake OUTPUT_VARIABLE CCGEN_CMAKE OUTPUT_STRIP_TRAILING_WHITESPACE)
list(APPEND CMAKE_MODULE_PATH "${CCGEN_CMAKE}")
include(ccgen)

# Important: Name your YAML files *.ccgen.yaml
Expand Down
3 changes: 3 additions & 0 deletions ccgen/cmake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from pathlib import Path

print(Path(__file__).parent / "share" / "cmake")
39 changes: 39 additions & 0 deletions ccgen/share/cmake/ccgen.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Auxiliary file for CCGEN in Cmake
#
# Provides a ccgen(...) function to compile all *.ccgen.yaml files to a .c/.h pair
# Usage: after you define your target do:
# include(ccgen.cmake)
# ...
# add_executable(MY_TARGET src1.c, src2.c, src3.yaml)
# ...
# ccgen(MY_TARGET)
# target
#
# To use, feel free to copy this file to your CMakeLists.txt directory.

function(ccgen target)
get_target_property(sources ${target} SOURCES)
foreach(yaml_file in ${sources})
cmake_path(GET yaml_file EXTENSION ext)
if("${ext}" MATCHES ".*\.ccgen\.yaml$")
cmake_path(GET yaml_file STEM gen)
add_custom_command(
COMMAND ${Python3_EXECUTABLE} -m ccgen "${yaml_file}" -o "${CMAKE_BINARY_DIR}"
OUTPUT
"${CMAKE_BINARY_DIR}/${gen}.ccgen.c"
"${CMAKE_BINARY_DIR}/${gen}.ccgen.h"
DEPENDS
"${yaml_file}"
COMMENT "CCGen - genetate files."
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
target_sources(
${target}
PRIVATE
"${CMAKE_BINARY_DIR}/${gen}.ccgen.c"
"${CMAKE_BINARY_DIR}/${gen}.ccgen.h"
)
endif()
endforeach()
target_include_directories(${target} PUBLIC "${CMAKE_BINARY_DIR}")
endfunction()
8 changes: 6 additions & 2 deletions examples/01_poc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
cmake_minimum_required(VERSION 3.23)
project(MyProject)

include(ccgen.cmake)
find_package(Python3 REQUIRED COMPONENTS Interpreter)

add_executable(MyExec main.c enums.ccgen.yaml)
execute_process(COMMAND ${Python3_EXECUTABLE} -m ccgen.cmake OUTPUT_VARIABLE CCGEN_CMAKE OUTPUT_STRIP_TRAILING_WHITESPACE)
list(APPEND CMAKE_MODULE_PATH "${CCGEN_CMAKE}")

include(ccgen)

add_executable(MyExec main.c enums.ccgen.yaml)
ccgen(MyExec)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "ccgen"
description = "Simple C code generator for enums."
version = "0.1.1"
version = "0.1.2"
classifiers = [
"License :: OSI Approved :: MIT License",
]
Expand Down

0 comments on commit 0d438fe

Please sign in to comment.