Skip to content

Commit 86dfe62

Browse files
committed
Add backtrace module, improve debuginformation for allocations, improve symbols in RelWithDebInfo.
1 parent 715a2f5 commit 86dfe62

File tree

14 files changed

+483
-39
lines changed

14 files changed

+483
-39
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ COMPOSE_PROJECT_NAME='metacall'
2222

2323
# Configure default variables
2424
METACALL_PATH=/usr/local/metacall
25-
METACALL_BUILD_TYPE=release
25+
METACALL_BUILD_TYPE=relwithdebinfo
2626
METACALL_BASE_IMAGE=debian:bullseye-slim

cmake/CompileOptions.cmake

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ if(WIN32 AND MSVC)
110110
)
111111
endif()
112112

113-
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR MAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
113+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
114114
set(DEFAULT_COMPILE_DEFINITIONS
115115
${DEFAULT_COMPILE_DEFINITIONS}
116116
DEBUG
@@ -131,19 +131,19 @@ set(DEFAULT_COMPILE_OPTIONS)
131131

132132
if(WIN32 AND MSVC)
133133
# Build runtime as multithreaded shared library
134-
# if(${CMAKE_VERSION} VERSION_LESS "3.15")
135-
# set(COMPILER_FLAGS_ID
136-
# CMAKE_CXX_FLAGS
137-
# CMAKE_CXX_FLAGS_DEBUG
138-
# CMAKE_CXX_FLAGS_RELEASE
139-
# CMAKE_C_FLAGS
140-
# CMAKE_C_FLAGS_DEBUG
141-
# CMAKE_C_FLAGS_RELEASE
142-
# )
143-
# foreach(FLAG_ID ${COMPILER_FLAGS_ID})
144-
# string(REPLACE "/MD" "/MT" ${FLAG_ID} "${${FLAG_ID}}")
145-
# endforeach()
146-
# else()
134+
# if(${CMAKE_VERSION} VERSION_LESS "3.15")
135+
# set(COMPILER_FLAGS_ID
136+
# CMAKE_CXX_FLAGS
137+
# CMAKE_CXX_FLAGS_DEBUG
138+
# CMAKE_CXX_FLAGS_RELEASE
139+
# CMAKE_C_FLAGS
140+
# CMAKE_C_FLAGS_DEBUG
141+
# CMAKE_C_FLAGS_RELEASE
142+
# )
143+
# foreach(FLAG_ID ${COMPILER_FLAGS_ID})
144+
# string(REPLACE "/MD" "/MT" ${FLAG_ID} "${${FLAG_ID}}")
145+
# endforeach()
146+
# else()
147147
# set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
148148
# endif()
149149

@@ -156,7 +156,7 @@ if(WIN32 AND MSVC)
156156
#add_compile_options(/ZH:SHA_256) # use SHA256 for generating hashes of compiler processed source files.
157157

158158
# Release
159-
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
159+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
160160
# Disable optimizations
161161
add_compile_options(/Od)
162162
else()
@@ -173,6 +173,11 @@ if(WIN32 AND MSVC)
173173
add_compile_options(/O2)
174174
add_compile_options(/Oi)
175175
add_compile_options(/Oy)
176+
177+
if(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
178+
# Enable debug symbols
179+
add_compile_options(/Z7)
180+
endif()
176181
endif()
177182
endif()
178183

@@ -197,9 +202,14 @@ if (PROJECT_OS_FAMILY MATCHES "unix")
197202
add_compile_options(-Wall)
198203
add_compile_options(-Wextra)
199204

205+
# Debug symbols
200206
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
201207
add_compile_options(-g)
202-
else()
208+
add_compile_options(-rdynamic)
209+
endif()
210+
211+
# Optimizations
212+
if(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
203213
add_compile_options(-O3)
204214
endif()
205215

source/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ add_subdirectory(version)
9696
add_subdirectory(preprocessor)
9797
add_subdirectory(environment)
9898
add_subdirectory(format)
99+
add_subdirectory(backtrace)
99100
add_subdirectory(log)
100101
add_subdirectory(memory)
101102
add_subdirectory(portability)

source/backtrace/CMakeLists.txt

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
#
2+
# Library name and options
3+
#
4+
5+
# Target name
6+
set(target backtrace)
7+
8+
# Exit here if required dependencies are not met
9+
message(STATUS "Lib ${target}")
10+
11+
# Set API export file and macro
12+
string(TOUPPER ${target} target_upper)
13+
set(feature_file "include/${target}/${target}_features.h")
14+
set(export_file "include/${target}/${target}_api.h")
15+
set(export_macro "${target_upper}_API")
16+
17+
#
18+
# Compiler warnings
19+
#
20+
21+
include(Warnings)
22+
23+
#
24+
# Compiler security
25+
#
26+
27+
include(SecurityFlags)
28+
29+
#
30+
# Sources
31+
#
32+
33+
set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}")
34+
set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")
35+
36+
set(headers
37+
${include_path}/backtrace.h
38+
)
39+
40+
if(UNIX)
41+
set(BACKTRACE_IMPL unix)
42+
elseif(WIN32)
43+
set(BACKTRACE_IMPL win32)
44+
else()
45+
message(ERROR "Backtrace library not supported in this platform")
46+
endif()
47+
48+
set(sources
49+
${source_path}/backtrace.c
50+
${source_path}/backtrace_${BACKTRACE_IMPL}.c
51+
)
52+
53+
# Group source files
54+
set(header_group "Header Files (API)")
55+
set(source_group "Source Files")
56+
source_group_by_path(${include_path} "\\\\.h$|\\\\.hpp$"
57+
${header_group} ${headers})
58+
source_group_by_path(${source_path} "\\\\.cpp$|\\\\.c$|\\\\.h$|\\\\.hpp$"
59+
${source_group} ${sources})
60+
61+
#
62+
# Create library
63+
#
64+
65+
# Build library
66+
add_library(${target} OBJECT
67+
${sources}
68+
${headers}
69+
)
70+
71+
# Create namespaced alias
72+
add_library(${META_PROJECT_NAME}::${target} ALIAS ${target})
73+
74+
# Export library for downstream projects
75+
export(TARGETS ${target} NAMESPACE ${META_PROJECT_NAME}:: FILE ${PROJECT_BINARY_DIR}/cmake/${target}/${target}-export.cmake)
76+
77+
# Create feature detection header
78+
# Compilers: https://cmake.org/cmake/help/v3.1/variable/CMAKE_LANG_COMPILER_ID.html#variable:CMAKE_%3CLANG%3E_COMPILER_ID
79+
# Feature: https://cmake.org/cmake/help/v3.1/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html
80+
81+
# Check for availability of module; use pre-generated version if not found
82+
if (WriterCompilerDetectionHeaderFound)
83+
write_compiler_detection_header(
84+
FILE ${feature_file}
85+
PREFIX ${target_upper}
86+
COMPILERS AppleClang Clang GNU MSVC
87+
FEATURES cxx_alignas cxx_alignof cxx_constexpr cxx_final cxx_noexcept cxx_nullptr cxx_sizeof_member cxx_thread_local
88+
VERSION 3.2
89+
)
90+
else()
91+
file(
92+
COPY ${PROJECT_SOURCE_DIR}/codegeneration/${target}_features.h
93+
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/include/${target}
94+
USE_SOURCE_PERMISSIONS
95+
)
96+
endif()
97+
98+
# Create API export header
99+
generate_export_header(${target}
100+
EXPORT_FILE_NAME ${export_file}
101+
EXPORT_MACRO_NAME ${export_macro}
102+
)
103+
104+
#
105+
# Project options
106+
#
107+
108+
set_target_properties(${target}
109+
PROPERTIES
110+
${DEFAULT_PROJECT_OPTIONS}
111+
FOLDER "${IDE_FOLDER}"
112+
)
113+
114+
#
115+
# Include directories
116+
#
117+
118+
target_include_directories(${target}
119+
PRIVATE
120+
${PROJECT_BINARY_DIR}/source/include
121+
${CMAKE_CURRENT_SOURCE_DIR}/include
122+
${CMAKE_CURRENT_BINARY_DIR}/include
123+
124+
PUBLIC
125+
${DEFAULT_INCLUDE_DIRECTORIES}
126+
127+
INTERFACE
128+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
129+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
130+
$<INSTALL_INTERFACE:include>
131+
)
132+
133+
#
134+
# Libraries
135+
#
136+
137+
target_link_libraries(${target}
138+
PRIVATE
139+
${META_PROJECT_NAME}::version
140+
141+
PUBLIC
142+
${DEFAULT_LIBRARIES}
143+
144+
INTERFACE
145+
)
146+
147+
#
148+
# Compile definitions
149+
#
150+
151+
target_compile_definitions(${target}
152+
PRIVATE
153+
${target_upper}_EXPORTS # Export API
154+
155+
PUBLIC
156+
$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:${target_upper}_STATIC_DEFINE>
157+
${DEFAULT_COMPILE_DEFINITIONS}
158+
159+
INTERFACE
160+
)
161+
162+
#
163+
# Compile options
164+
#
165+
166+
target_compile_options(${target}
167+
PRIVATE
168+
169+
PUBLIC
170+
${DEFAULT_COMPILE_OPTIONS}
171+
172+
INTERFACE
173+
)
174+
175+
#
176+
# Linker options
177+
#
178+
179+
target_link_libraries(${target}
180+
PRIVATE
181+
182+
PUBLIC
183+
${DEFAULT_LINKER_OPTIONS}
184+
185+
INTERFACE
186+
)
187+
188+
#
189+
# Deployment
190+
#
191+
192+
# Export
193+
install(TARGETS ${target}
194+
EXPORT "${target}-export" COMPONENT dev
195+
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Backtrace Library by Parra Studios
3+
* A cross-platform library for supporting SEGV catching and backtracing.
4+
*
5+
* Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#ifndef BACKTRACE_H
22+
#define BACKTRACE_H 1
23+
24+
/* -- Headers -- */
25+
26+
#include <backtrace/backtrace_api.h>
27+
28+
#ifdef __cplusplus
29+
extern "C" {
30+
#endif
31+
32+
/* -- Methods -- */
33+
34+
BACKTRACE_API int backtrace_initialize(void);
35+
36+
BACKTRACE_API int backtrace_destroy(void);
37+
38+
BACKTRACE_API const char *backtrace_print_info(void);
39+
40+
#ifdef __cplusplus
41+
}
42+
#endif
43+
44+
#endif /* BACKTRACE_H */
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Backtrace Library by Parra Studios
3+
* A cross-platform library for supporting SEGV catching and backtracing.
4+
*
5+
* Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
/* -- Headers -- */
22+
23+
#include <metacall/metacall_version.h>
24+
25+
#include <backtrace/backtrace.h>
26+
27+
/* -- Methods -- */
28+
29+
const char *backtrace_print_info()
30+
{
31+
static const char backtrace_info[] =
32+
"Backtrace Library " METACALL_VERSION "\n"
33+
"Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia <[email protected]>\n"
34+
35+
#ifdef LOG_STATIC_DEFINE
36+
"Compiled as static library type\n"
37+
#else
38+
"Compiled as shared library type\n"
39+
#endif
40+
41+
"\n";
42+
43+
return backtrace_info;
44+
}

0 commit comments

Comments
 (0)