diff --git a/.gitignore b/.gitignore index 94428a7..1cf2e80 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ *.swp -test-base64 -.wsjcpp/* \ No newline at end of file +/build +.wsjcpp/* diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..33eaeed --- /dev/null +++ b/CMakeLists.txt @@ -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() diff --git a/Makefile b/Makefile deleted file mode 100644 index f0e9acc..0000000 --- a/Makefile +++ /dev/null @@ -1,48 +0,0 @@ -WARNINGS= \ - -Werror \ - -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 - -test: base64-test-11 base64-test-17 - base64-test-11 - base64-test-17 - -base64-test-11: base64-11.o test-11.o - g++ base64-11.o test-11.o -o $@ - -base64-test-17: base64-17.o test-17.o - g++ base64-17.o test-17.o -o $@ - -base64-11.o: base64.cpp base64.h - g++ -std=c++11 $(WARNINGS) -c base64.cpp -o base64-11.o - -base64-17.o: base64.cpp base64.h - g++ -std=c++17 $(WARNINGS) -c base64.cpp -o base64-17.o - -test-11.o: test.cpp - g++ -std=c++11 $(WARNINGS) -c test.cpp -o test-11.o - -test-17.o: test.cpp - g++ -std=c++17 $(WARNINGS) -c test.cpp -o test-17.o diff --git a/README.md b/README.md index dd4b0f4..3f36c6e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/compile-and-run-test b/compile-and-run-test index 1ab5985..41f13e6 100755 --- a/compile-and-run-test +++ b/compile-and-run-test @@ -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 diff --git a/base64.h b/include/base64.h similarity index 100% rename from base64.h rename to include/base64.h diff --git a/base64.cpp b/src/base64.cpp similarity index 100% rename from base64.cpp rename to src/base64.cpp diff --git a/measure-time.cpp b/src/measure-time.cpp similarity index 100% rename from measure-time.cpp rename to src/measure-time.cpp diff --git a/test-google.cpp b/src/test-google.cpp similarity index 100% rename from test-google.cpp rename to src/test-google.cpp diff --git a/test.cpp b/src/test.cpp similarity index 100% rename from test.cpp rename to src/test.cpp diff --git a/wsjcpp.yml b/wsjcpp.yml index a847f38..db3d6c1 100644 --- a/wsjcpp.yml +++ b/wsjcpp.yml @@ -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"