Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn project into a CMake project. #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*.swp
test-base64
.wsjcpp/*
/build
.wsjcpp/*
123 changes: 123 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
cmake_minimum_required(VERSION 3.8)
project(cpp-base64 CXX)

include(CheckCXXCompilerFlag)

option(BASE64_ENABLE_STATIC_LIB "Build static library" ON)
option(BASE64_ENABLE_TESTS "Build test executables and libraries" OFF)
option(BASE64_WARNINGS_AS_ERRORS "Treat warnings as errors" OFF)

# Require C++11 but use C++17 if possible
set(CMAKE_CXX_STANDARD_REQUIRED 11)
set(CMAKE_CXX_STANDARD 17)

# Collect enabled warnings
set(WARNING_FLAGS
-Wall
-Wextra
-pedantic
-Wcast-align
-Wcast-qual
-Wctor-dtor-privacy
-Wdisabled-optimization
-Wformat=2
-Winit-self
-Wlogical-op
-Wmissing-include-dirs
-Wnoexcept
-Wold-style-cast
-Woverloaded-virtual
-Wredundant-decls
-Wshadow
-Wsign-promo
-Wstrict-null-sentinel
-Wstrict-overflow=5
-Wundef
-Wno-unused
-Wno-variadic-macros
-Wno-parentheses
-fdiagnostics-show-option
)
if(${BASE64_WARNINGS_AS_ERRORS})
list(APPEND WARNING_FLAGS -Werror)
endif()

# Main library target
add_library(base64 SHARED
src/base64.cpp
)
set(TARGETS base64)
add_library(base64::base64 ALIAS base64)

# Static library target
if(${BASE64_ENABLE_STATIC_LIB})
add_library(base64_static STATIC
src/base64.cpp
)
set_property(TARGET base64_static PROPERTY POSITION_INDEPENDENT_CODE ON)
list(APPEND TARGETS base64_static)
add_library(base64::base64_static ALIAS base64_static)
endif()

if (${BASE64_ENABLE_TESTS})
enable_testing()

# CXX11 version of library
add_library(base64_cxx11 SHARED
src/base64.cpp
)
set_property(TARGET base64_cxx11 PROPERTY CXX_STANDARD 11)

# CXX11 version of test
add_executable(base64_test_cxx11
src/test.cpp
)
target_link_libraries(base64_test_cxx11
PUBLIC
base64_cxx11
)
set_property(TARGET base64_test_cxx11 PROPERTY CXX_STANDARD 11)

add_test(NAME cxx11 COMMAND base64_test_cxx11)

# CXX17 version of test
add_executable(base64_test_cxx17
src/test.cpp
)
target_link_libraries(base64_test_cxx17
PUBLIC
base64
)
set_property(TARGET base64_test_cxx17 PROPERTY CXX_STANDARD 17)
set_property(TARGET base64_test_cxx17 PROPERTY CXX_STANDARD_REQUIRED 17)

add_test(NAME cxx17 COMMAND base64_test_cxx17)

list(APPEND TARGETS base64_test_cxx17 base64_cxx11 base64_test_cxx11)
endif(${BASE64_ENABLE_TESTS})

# Find supported warning flags
foreach(FLAG ${WARNING_FLAGS})
string(REPLACE "-" "_" VAR "HAVE${FLAG}")
check_cxx_compiler_flag("-Werror ${FLAG}" ${VAR})
if(${${VAR}})
list(APPEND _WARNING_FLAGS ${FLAG})
endif()
endforeach()

foreach(TARGET ${TARGETS})
# Set warning flags on all targets (but don't export them)
target_compile_options(${TARGET}
PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/W4 /WX>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:${_WARNING_FLAGS}>
)

# Set include directories
target_include_directories(
${TARGET}
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
endforeach()
48 changes: 0 additions & 48 deletions Makefile

This file was deleted.

25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

Base64 encoding and decoding with c++

## Using with CMake

The easiest way to use this project with CMake is by adding it as subfolder. To that aim, first either copy the files of this project or add it as git submodule to your project like this:

```bash
git submodule add -f https://github.com/eneNyffenegger/cpp-base64.git 3rdparty/cpp-base64
```

Then make the following modifications to your `CMakeLists.txt`:

```cmake
# Pick this line for an out-of-source submodule
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../3rdparty/cpp-base64/" cpp-base64)

# Pick this line instead if the library is inside your own source folder
add_subdirectory(cpp-base64)

# Add a dependency for any target that uses the library
target_link_libraries(your_target
PRIVATE # Add it to the PUBLIC section instead if you
# include base64.h from a public header file
base64::base64
)
```

## See also

https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp
7 changes: 5 additions & 2 deletions compile-and-run-test
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
g++ test.cpp base64.cpp -o test-base64
./test-base64
mkdir build
cd build
cmake .. -DBASE64_ENABLE_TESTS=ON -DBASE64_WARNINGS_AS_ERRORS=ON
make
make test
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions wsjcpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ keywords:
- "base64"

distribution:
- source-file: "base64.cpp"
- source-file: "src/base64.cpp"
target-file: "base64.cpp"
type: "source-code"
- source-file: "base64.h"
- source-file: "include/base64.h"
target-file: "base64.h"
type: "source-code"