Skip to content

Commit 9339a5e

Browse files
committed
Updated CMake support.
1 parent e18a85a commit 9339a5e

6 files changed

Lines changed: 562 additions & 8 deletions

File tree

lib/ruby-bindgen/visitors/cmake/cmake.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
module RubyBindgen
22
module Visitors
33
class CMake
4-
attr_reader :project, :outputter, :include_dirs
4+
attr_reader :outputter, :config
55

6-
def initialize(outputter, project = nil, include_dirs: [])
7-
@project = project&.gsub(/-/, '_')
6+
def initialize(outputter, config)
87
@outputter = outputter
9-
@include_dirs = include_dirs
8+
@config = config
9+
end
10+
11+
def project
12+
config[:extension]&.gsub(/-/, '_')
13+
end
14+
15+
def include_dirs
16+
config[:include_dirs] || []
1017
end
1118

1219
def render_template(template, local_variables = {})
@@ -59,9 +66,15 @@ def create_directories(path)
5966
end
6067
end
6168

69+
def create_presets
70+
content = render_template("presets")
71+
self.outputter.write("CMakePresets.json", content)
72+
end
73+
6274
def visit_start
6375
pathname = Pathname.new(self.outputter.base_path)
6476
create_project(pathname)
77+
create_presets
6578
create_directories(pathname)
6679
end
6780

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
{
2+
"version": 6,
3+
"configurePresets": [
4+
{
5+
"name": "base",
6+
"hidden": true,
7+
"generator": "Ninja",
8+
"binaryDir": "${sourceDir}/build/${presetName}",
9+
"installDir": "${sourceDir}/install/${presetName}",
10+
"cacheVariables": {
11+
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
12+
"CMAKE_CXX_FLAGS": "-Wall -ftemplate-backtrace-limit=0 -fvisibility=hidden -fvisibility-inlines-hidden"
13+
}
14+
},
15+
{
16+
"name": "linux-debug",
17+
"inherits": "base",
18+
"displayName": "Linux Debug",
19+
"cacheVariables": {
20+
"CMAKE_BUILD_TYPE": "Debug",
21+
"CMAKE_CXX_FLAGS_DEBUG": "-ggdb3 -O0 -fno-omit-frame-pointer -fno-inline -fno-optimize-sibling-calls"
22+
},
23+
"condition": {
24+
"type": "equals",
25+
"lhs": "${hostSystemName}",
26+
"rhs": "Linux"
27+
}
28+
},
29+
{
30+
"name": "linux-release",
31+
"inherits": "base",
32+
"displayName": "Linux Release",
33+
"cacheVariables": {
34+
"CMAKE_BUILD_TYPE": "Release",
35+
"CMAKE_CXX_FLAGS_RELEASE": "-O3 -DNDEBUG",
36+
"CMAKE_INTERPROCEDURAL_OPTIMIZATION": "ON",
37+
"CMAKE_SHARED_LINKER_FLAGS_RELEASE": "-Wl,--exclude-libs,ALL -Wl,--strip-all"
38+
},
39+
"condition": {
40+
"type": "equals",
41+
"lhs": "${hostSystemName}",
42+
"rhs": "Linux"
43+
}
44+
},
45+
{
46+
"name": "macos-debug",
47+
"inherits": "base",
48+
"displayName": "macOS Debug",
49+
"cacheVariables": {
50+
"CMAKE_BUILD_TYPE": "Debug",
51+
"CMAKE_CXX_FLAGS": "-Wall -Wno-unused-private-field -ftemplate-backtrace-limit=0 -fvisibility=hidden -fvisibility-inlines-hidden",
52+
"CMAKE_CXX_FLAGS_DEBUG": "-g3 -Og -fno-omit-frame-pointer -fno-inline -gsplit-dwarf"
53+
},
54+
"condition": {
55+
"type": "equals",
56+
"lhs": "${hostSystemName}",
57+
"rhs": "Darwin"
58+
}
59+
},
60+
{
61+
"name": "macos-release",
62+
"inherits": "base",
63+
"displayName": "macOS Release",
64+
"cacheVariables": {
65+
"CMAKE_BUILD_TYPE": "Release",
66+
"CMAKE_CXX_FLAGS": "-Wall -Wno-unused-private-field -ftemplate-backtrace-limit=0 -fvisibility=hidden -fvisibility-inlines-hidden",
67+
"CMAKE_CXX_FLAGS_RELEASE": "-O3 -DNDEBUG",
68+
"CMAKE_INTERPROCEDURAL_OPTIMIZATION": "ON",
69+
"CMAKE_SHARED_LINKER_FLAGS_RELEASE": "-Wl,-dead_strip -Wl,-x"
70+
},
71+
"condition": {
72+
"type": "equals",
73+
"lhs": "${hostSystemName}",
74+
"rhs": "Darwin"
75+
}
76+
},
77+
{
78+
"name": "mingw-debug",
79+
"inherits": "base",
80+
"displayName": "Mingw x64 Debug",
81+
"cacheVariables": {
82+
"CMAKE_BUILD_TYPE": "Debug",
83+
"CMAKE_CXX_COMPILER": "g++.exe",
84+
"CMAKE_CXX_FLAGS": "-Wall -ftemplate-backtrace-limit=0 -Wa,-mbig-obj -fvisibility=hidden -fvisibility-inlines-hidden",
85+
"CMAKE_CXX_FLAGS_DEBUG": "-g3 -Og -fno-omit-frame-pointer -fno-inline -gsplit-dwarf"
86+
},
87+
"condition": {
88+
"type": "equals",
89+
"lhs": "${hostSystemName}",
90+
"rhs": "Windows"
91+
}
92+
},
93+
{
94+
"name": "mingw-release",
95+
"inherits": "base",
96+
"displayName": "Mingw x64 Release",
97+
"cacheVariables": {
98+
"CMAKE_BUILD_TYPE": "Release",
99+
"CMAKE_CXX_COMPILER": "g++.exe",
100+
"CMAKE_CXX_FLAGS": "-Wall -ftemplate-backtrace-limit=0 -Wa,-mbig-obj -fvisibility=hidden -fvisibility-inlines-hidden",
101+
"CMAKE_CXX_FLAGS_RELEASE": "-O3 -DNDEBUG",
102+
"CMAKE_SHARED_LINKER_FLAGS_RELEASE": "-Wl,--exclude-all-symbols"
103+
},
104+
"condition": {
105+
"type": "equals",
106+
"lhs": "${hostSystemName}",
107+
"rhs": "Windows"
108+
}
109+
},
110+
{
111+
"name": "windows-base",
112+
"hidden": true,
113+
"generator": "Ninja",
114+
"binaryDir": "${sourceDir}/build/${presetName}",
115+
"installDir": "${sourceDir}/install/${presetName}",
116+
"toolchainFile": "$env{VCPKG_ROOT}\\scripts\\buildsystems\\vcpkg.cmake",
117+
"cacheVariables": {
118+
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
119+
"CMAKE_CXX_FLAGS": "/EHs /W4 /bigobj /utf-8 /D_CRT_SECURE_NO_DEPRECATE /D_CRT_NONSTDC_NO_DEPRECATE"
120+
},
121+
"condition": {
122+
"type": "equals",
123+
"lhs": "${hostSystemName}",
124+
"rhs": "Windows"
125+
}
126+
},
127+
{
128+
"name": "msvc-debug",
129+
"inherits": "windows-base",
130+
"displayName": "MSVC x64 Debug",
131+
"cacheVariables": {
132+
"CMAKE_BUILD_TYPE": "Debug",
133+
"CMAKE_CXX_COMPILER": "cl.exe",
134+
"CMAKE_CXX_FLAGS_DEBUG": "/Od",
135+
"CMAKE_MSVC_DEBUG_INFORMATION_FORMAT": "ProgramDatabase"
136+
}
137+
},
138+
{
139+
"name": "msvc-release",
140+
"inherits": "windows-base",
141+
"displayName": "MSVC x64 Release",
142+
"cacheVariables": {
143+
"CMAKE_BUILD_TYPE": "Release",
144+
"CMAKE_CXX_COMPILER": "cl.exe",
145+
"CMAKE_CXX_FLAGS_RELEASE": "/O2 /DNDEBUG",
146+
"CMAKE_INTERPROCEDURAL_OPTIMIZATION": "ON",
147+
"CMAKE_MSVC_DEBUG_INFORMATION_FORMAT": "ProgramDatabase"
148+
}
149+
},
150+
{
151+
"name": "clang-windows-debug",
152+
"inherits": "windows-base",
153+
"displayName": "Clang Windows Debug",
154+
"cacheVariables": {
155+
"CMAKE_BUILD_TYPE": "Debug",
156+
"CMAKE_CXX_COMPILER": "clang-cl.exe",
157+
"CMAKE_CXX_FLAGS": "/EHs /W4 /bigobj /utf-8 /D_CRT_SECURE_NO_DEPRECATE /D_CRT_NONSTDC_NO_DEPRECATE /clang:-Wno-unused-private-field",
158+
"CMAKE_CXX_FLAGS_DEBUG": "/Od /Zi"
159+
}
160+
},
161+
{
162+
"name": "clang-windows-release",
163+
"inherits": "windows-base",
164+
"displayName": "Clang Windows Release",
165+
"cacheVariables": {
166+
"CMAKE_BUILD_TYPE": "Release",
167+
"CMAKE_CXX_COMPILER": "clang-cl.exe",
168+
"CMAKE_CXX_FLAGS": "/EHs /W4 /bigobj /utf-8 /D_CRT_SECURE_NO_DEPRECATE /D_CRT_NONSTDC_NO_DEPRECATE /clang:-Wno-unused-private-field",
169+
"CMAKE_CXX_FLAGS_RELEASE": "/O2 /DNDEBUG",
170+
"CMAKE_INTERPROCEDURAL_OPTIMIZATION": "ON"
171+
}
172+
}
173+
],
174+
"buildPresets": [
175+
{
176+
"name": "linux-debug",
177+
"displayName": "Build Linux Debug",
178+
"configurePreset": "linux-debug",
179+
"jobs": 6
180+
},
181+
{
182+
"name": "linux-release",
183+
"displayName": "Build Linux Release",
184+
"configurePreset": "linux-release",
185+
"jobs": 6
186+
},
187+
{
188+
"name": "macos-debug",
189+
"displayName": "Build macOS Debug",
190+
"configurePreset": "macos-debug"
191+
},
192+
{
193+
"name": "macos-release",
194+
"displayName": "Build macOS Release",
195+
"configurePreset": "macos-release"
196+
},
197+
{
198+
"name": "msvc-debug",
199+
"displayName": "Build MSVC x64 Debug",
200+
"configurePreset": "msvc-debug"
201+
},
202+
{
203+
"name": "msvc-release",
204+
"displayName": "Build MSVC x64 Release",
205+
"configurePreset": "msvc-release"
206+
},
207+
{
208+
"name": "clang-windows-debug",
209+
"displayName": "Build Clang Windows Debug",
210+
"configurePreset": "clang-windows-debug"
211+
},
212+
{
213+
"name": "clang-windows-release",
214+
"displayName": "Build Clang Windows Release",
215+
"configurePreset": "clang-windows-release"
216+
},
217+
{
218+
"name": "mingw-debug",
219+
"displayName": "Build Mingw x64 Debug",
220+
"configurePreset": "mingw-debug"
221+
},
222+
{
223+
"name": "mingw-release",
224+
"displayName": "Build Mingw x64 Release",
225+
"configurePreset": "mingw-release"
226+
}
227+
]
228+
}

test/bindings/cpp/CMakeLists.txt

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
cmake_minimum_required(VERSION 3.26)
2+
3+
set(CMAKE_CXX_STANDARD 17)
4+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
5+
6+
project("test_project" LANGUAGES CXX)
7+
8+
# Create the Extension Library
9+
if (MSVC)
10+
add_library(${CMAKE_PROJECT_NAME} SHARED)
11+
else ()
12+
add_library(${CMAKE_PROJECT_NAME} MODULE)
13+
endif ()
14+
15+
# Fetch Rice from GitHub
16+
include(FetchContent)
17+
18+
FetchContent_Declare(
19+
rice
20+
GIT_REPOSITORY https://github.com/ruby-rice/rice.git
21+
GIT_TAG master
22+
)
23+
FetchContent_MakeAvailable(rice)
24+
25+
# Configure Ruby Detection
26+
list(PREPEND CMAKE_MODULE_PATH "${rice_SOURCE_DIR}")
27+
28+
# Find Ruby
29+
find_package(Ruby REQUIRED)
30+
31+
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE
32+
Ruby::Module
33+
)
34+
35+
# Link to Rice
36+
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE
37+
Rice::Rice
38+
)
39+
40+
# Include Directories
41+
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
42+
"${CMAKE_CURRENT_SOURCE_DIR}/../../headers/cpp"
43+
)
44+
45+
# Configure Extension Output
46+
get_target_property(RUBY_EXT_SUFFIX Ruby::Ruby INTERFACE_RUBY_EXTENSION_SUFFIX)
47+
48+
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES
49+
PREFIX ""
50+
SUFFIX "${RUBY_EXT_SUFFIX}"
51+
OUTPUT_NAME "test_project"
52+
CXX_VISIBILITY_PRESET hidden
53+
VISIBILITY_INLINES_HIDDEN ON
54+
WINDOWS_EXPORT_ALL_SYMBOLS OFF
55+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/../lib/${Ruby_VERSION_MAJOR}.${Ruby_VERSION_MINOR}"
56+
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/../lib"
57+
)
58+
59+
# Subdirectories
60+
61+
# Sources
62+
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
63+
"buffers-rb.cpp"
64+
"classes-rb.cpp"
65+
"constructors-rb.cpp"
66+
"cross_file_derived-rb.cpp"
67+
"default_values-rb.cpp"
68+
"enums-rb.cpp"
69+
"filtering-rb.cpp"
70+
"functions-rb.cpp"
71+
"incomplete_types-rb.cpp"
72+
"inheritance-rb.cpp"
73+
"iterators-rb.cpp"
74+
"operators-rb.cpp"
75+
"overloads-rb.cpp"
76+
"template_defaults-rb.cpp"
77+
"template_inheritance-rb.cpp"
78+
"templates-rb.cpp"
79+
)

0 commit comments

Comments
 (0)