Skip to content

Commit ec24b34

Browse files
committed
[cmake] Use CMake configurations for compiler flags + generate version number from main CMakeLists.txt
1 parent 5edc25c commit ec24b34

File tree

10 files changed

+47
-30
lines changed

10 files changed

+47
-30
lines changed

.github/workflows/c-cpp.yml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,21 @@ jobs:
1313

1414
steps:
1515
- uses: actions/checkout@v2
16-
- name: make gcc
17-
run: make gcc-native
18-
- name: unit tests gcc
16+
- name: make gcc Debug
17+
run: make gcc-native BUILD_TYPE=Debug
18+
- name: unit tests gcc Debug
1919
run: make tests-gcc-native
20-
- name: make clang
21-
run: make clang-native
20+
- name: make clang Debug
21+
run: make clang-native Debug
2222
- name: unit tests clang
23-
run: make tests-clang-native
23+
run: make tests-clang-native BUILD_TYPE=Debug
24+
- name: clean
25+
run: make clean
26+
- name: make gcc Release
27+
run: make gcc-native BUILD_TYPE=Release
28+
- name: unit tests gcc Release
29+
run: make tests-gcc-native
30+
- name: make clang Debug
31+
run: make clang-native Release
32+
- name: unit tests clang
33+
run: make tests-clang-native BUILD_TYPE=Release

3rdparty/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ add_library(doctest INTERFACE)
88
target_include_directories(doctest INTERFACE doctest/doctest)
99

1010
# Use default flags for the libwebsockets library
11-
set(CMAKE_C_FLAGS "-g -ggdb")
12-
set(CMAKE_CXX_FLAGS "-g -ggdb")
11+
set(CMAKE_C_FLAGS_DEBUG "-O0 -g3 -ggdb3")
12+
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g3 -ggdb3")
13+
set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
14+
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
1315

1416
add_subdirectory(libwebsockets)

CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ project(OpenOCPP DESCRIPTION "Open Source C++ implementation of the OCPP 1.6 pro
88
VERSION 0.5.0
99
)
1010

11+
# Definitions for Version.h file
12+
add_definitions(-DLIBOPENOCPP_MAJOR="${PROJECT_VERSION_MAJOR}")
13+
add_definitions(-DLIBOPENOCPP_MINOR="${PROJECT_VERSION_MINOR}")
14+
add_definitions(-DLIBOPENOCPP_FIX="${PROJECT_VERSION_PATCH}")
15+
1116
# Build options
1217
include(CMakeLists_Options.txt)
1318

@@ -118,15 +123,15 @@ set(PKG_CONFIG_CFLAGS "-I\${includedir}")
118123

119124
set(LIB_NAME "open-ocpp")
120125
configure_file(
121-
"${CMAKE_CURRENT_SOURCE_DIR}/libopen-ocpp.pc.in"
126+
"${CMAKE_CURRENT_SOURCE_DIR}/deploy/libopen-ocpp.pc.in"
122127
"${CMAKE_CURRENT_BINARY_DIR}/libopen-ocpp.pc"
123128
)
124129
install(FILES "${CMAKE_BINARY_DIR}/libopen-ocpp.pc" DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
125130

126131
if(${BUILD_STATIC_LIBRARY})
127132
set(LIB_NAME "open-ocpp_static")
128133
configure_file(
129-
"${CMAKE_CURRENT_SOURCE_DIR}/libopen-ocpp.pc.in"
134+
"${CMAKE_CURRENT_SOURCE_DIR}/deploy/libopen-ocpp.pc.in"
130135
"${CMAKE_CURRENT_BINARY_DIR}/libopen-ocpp_static.pc"
131136
)
132137
install(FILES "${CMAKE_BINARY_DIR}/libopen-ocpp_static.pc" DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)

CMakeLists_Options.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ add_compile_definitions(LOG_LEVEL=${LOG_LEVEL})
1010

1111
# Static library
1212
if(NOT DEFINED BUILD_STATIC_LIBRARY)
13-
set(BUILD_STATIC_LIBRARY OFF)
13+
set(BUILD_STATIC_LIBRARY ON)
1414
endif()
1515

1616
# Unit tests

CMakeLists_native.txt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22
# Native target toolchain definition and specific compiler flags #
33
#################################################################################
44

5-
# Optimization flags
6-
if (DEBUG)
7-
set(OPTIMIZATION_FLAGS "-O0 -g -ggdb")
8-
else()
9-
set(OPTIMIZATION_FLAGS "-O2")
10-
endif()
11-
125
# Warnings
136
set(WARNING_FLAGS "-Wall -Wextra -Werror")
147

@@ -17,5 +10,7 @@ set(C_FLAGS "-std=c11")
1710
set(CXX_FLAGS "-std=c++17")
1811

1912
# Set compiler flags
20-
set(CMAKE_C_FLAGS "${C_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
21-
set(CMAKE_CXX_FLAGS "${CXX_FLAGS} ${WARNING_FLAGS} ${OPTIMIZATION_FLAGS}")
13+
set(CMAKE_C_FLAGS_DEBUG "${C_FLAGS} ${WARNING_FLAGS} -O0 -g3 -ggdb3")
14+
set(CMAKE_CXX_FLAGS_DEBUG "${CXX_FLAGS} ${WARNING_FLAGS} -O0 -g3 -ggdb3")
15+
set(CMAKE_C_FLAGS_RELEASE "${C_FLAGS} ${WARNING_FLAGS} -O2 -DNDEBUG")
16+
set(CMAKE_CXX_FLAGS_RELEASE "${CXX_FLAGS} ${WARNING_FLAGS} -O2 -DNDEBUG")

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,13 @@ The build is based on CMake, the following definitions must be passed to the CMa
309309

310310
* **TARGET** : Allow to load the appropriate *CMakeLists_TARGET.txt* file
311311
* **BIN_DIR** : Output directory for the generated binaries
312-
* **DEBUG** : If set to ON, use DEBUG compilation flag, otherwise use release compilation flags
312+
* **CMAKE_BUILD_TYPE** : Can be set to either Debug or Release (Release build produces optimized stripped binaries)
313313

314314
Additionnaly, the **CMakeLists_Options.txt** contains several options that can be switched on/off.
315315

316316
An helper makefile is available at project's level to simplify the use of CMake. Just use the one of the following commands to build using gcc or gcc without cross compilation :
317317

318-
```make gcc-native``` or ```make clang-native```
318+
```make gcc-native``` or ```make clang-native``` or ```make gcc-native BUILD_TYPE=Debug``` or ```make clang-native BUILD_TYPE=Debug```
319319

320320
This makefile also contains the corresponding cleaning targets :
321321

File renamed without changes.

examples/remote_chargepoint/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ int main(int argc, char* argv[])
175175
while (true)
176176
{
177177
// Check connector state
178-
unsigned int connector_id;
178+
unsigned int connector_id = 0;
179179
std::string id_tag;
180180
bool restore = false;
181181
for (unsigned int i = 1; i <= config.ocppConfig().numberOfConnectors(); i++)

makefile

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ BIN_DIR:=$(ROOT_DIR)/bin
1010

1111
# Make options
1212
#VERBOSE="VERBOSE=1"
13-
PARALLEL_BUILD:=-j 4
14-
DEBUG:=ON
13+
PARALLEL_BUILD?=-j 4
14+
15+
# Build type can be either Debug or Release
16+
BUILD_TYPE?=Release
1517

1618
# Default target
1719
default: gcc-native
1820

19-
# Silent makefile if not verbose mode
20-
$(VERBOSE).SILENT:
21+
# Silent makefile
22+
.SILENT:
2123

2224
# Install prefix
2325
ifneq ($(strip $(INSTALL_PREFIX)),)
@@ -57,7 +59,7 @@ $(GCC_NATIVE_BUILD_DIR)/Makefile:
5759
@echo "Generating gcc-native makefiles..."
5860
@mkdir -p $(GCC_NATIVE_BUILD_DIR)
5961
@mkdir -p $(GCC_NATIVE_BIN_DIR)
60-
@cd $(GCC_NATIVE_BUILD_DIR) && export CC=gcc && export CXX=g++ && cmake -D TARGET=native -D BIN_DIR=$(GCC_NATIVE_BIN_DIR) -D DEBUG=$(DEBUG) $(CMAKE_INSTALL_PREFIX) $(ROOT_DIR)
62+
@cd $(GCC_NATIVE_BUILD_DIR) && export CC=gcc && export CXX=g++ && cmake -D CMAKE_BUILD_TYPE=$(BUILD_TYPE) -D TARGET=native -D BIN_DIR=$(GCC_NATIVE_BIN_DIR) $(CMAKE_INSTALL_PREFIX) $(ROOT_DIR)
6163

6264

6365
# Targets for clang-native build
@@ -87,4 +89,4 @@ $(CLANG_NATIVE_BUILD_DIR)/Makefile:
8789
@echo "Generating clang-native makefiles..."
8890
@mkdir -p $(CLANG_NATIVE_BUILD_DIR)
8991
@mkdir -p $(CLANG_NATIVE_BIN_DIR)
90-
@cd $(CLANG_NATIVE_BUILD_DIR) && export CC=clang && export CXX=clang++ && cmake -D TARGET=native -D _CMAKE_TOOLCHAIN_PREFIX=llvm- -D BIN_DIR=$(CLANG_NATIVE_BIN_DIR) -D DEBUG=$(DEBUG) $(CMAKE_INSTALL_PREFIX) $(ROOT_DIR)
92+
@cd $(CLANG_NATIVE_BUILD_DIR) && export CC=clang && export CXX=clang++ && cmake -D CMAKE_BUILD_TYPE=$(BUILD_TYPE) -D TARGET=native -D _CMAKE_TOOLCHAIN_PREFIX=llvm- -D BIN_DIR=$(CLANG_NATIVE_BIN_DIR) $(CMAKE_INSTALL_PREFIX) $(ROOT_DIR)

src/version/Version.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ along with OpenOCPP. If not, see <http://www.gnu.org/licenses/>.
2323
* XX = Major version number (increase = API compatibility broken and/or major feature addition)
2424
* YY = Minor version number (increase = Minor feature addition)
2525
* ZZ = Fix version number (increase = Bug fix(es))
26+
*
27+
* Stack version number is defined in the main CMakeLists.txt file of
28+
* the build tree
2629
*/
27-
#define OPEN_OCPP_VERSION "0.5.0"
30+
#define OPEN_OCPP_VERSION LIBOPENOCPP_MAJOR "." LIBOPENOCPP_MINOR "." LIBOPENOCPP_FIX
2831

2932
/** @brief Return the version string of the library */
3033
extern "C" const char* OPEN_OCPP_Version();

0 commit comments

Comments
 (0)