Skip to content

Make namespaces configurable #64

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

Open
wants to merge 3 commits into
base: main
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
23 changes: 21 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ option(TESTING "Build tests" OFF)
option(CLANG_TIDY "Perform linting with clang-tidy" OFF)
option(SANITIZERS "Enable sanitizers" OFF)
option(NO_ALLOC "Build without needing an allocator" OFF)
option(NAMESPACE_SUFFIX "Namespace Suffix for CXX and CMake Export")

if(NAMESPACE_SUFFIX)
set(SFRAME_CXX_NAMESPACE "sframe_${NAMESPACE_SUFFIX}" CACHE STRING "Top-level Namespace for CXX")
set(SFRAME_EXPORT_NAMESPACE "SFrame${NAMESPACE_SUFFIX}" CACHE STRING "Namespace for CMake Export")
else()
set(SFRAME_CXX_NAMESPACE "sframe" CACHE STRING "Top-level Namespace for CXX")
set(SFRAME_EXPORT_NAMESPACE "SFrame" CACHE STRING "Namespace for CMake Export")
endif()
message(STATUS "CXX Namespace: ${SFRAME_CXX_NAMESPACE}")
message(STATUS "CMake Export Namespace: ${SFRAME_EXPORT_NAMESPACE}")

# Use -DCRYPTO=(OPENSSL_1_1 | OPENSSL_3 | BORINGSSL) to configure crypto
if(NOT DEFINED CRYPTO)
Expand All @@ -20,6 +31,12 @@ endif()
###
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/namespace.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/include/namespace.h"
@ONLY
)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
Expand Down Expand Up @@ -87,13 +104,15 @@ endif()
set(LIB_NAME "${PROJECT_NAME}")

file(GLOB_RECURSE LIB_HEADERS CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
file(GLOB_RECURSE LIB_GENERATED_HEADERS CONFIGURE_DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/include/*.h")
file(GLOB_RECURSE LIB_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")

add_library(${LIB_NAME} ${LIB_HEADERS} ${LIB_SOURCES})
add_library(${LIB_NAME} ${LIB_HEADERS} ${LIB_GENERATED_HEADERS} ${LIB_SOURCES})
target_link_libraries(${LIB_NAME} PRIVATE ${CRYPTO_LIB})
target_include_directories(${LIB_NAME}
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
)

Expand Down
4 changes: 4 additions & 0 deletions cmake/namespace.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

// Configurable top-level namespace
#define SFRAME_NAMESPACE @SFRAME_CXX_NAMESPACE@
9 changes: 5 additions & 4 deletions include/sframe/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <sframe/vector.h>

namespace sframe {
namespace SFRAME_NAMESPACE {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we include #include <namespace.h> ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We get it via vector.h. (That's why it passes CI)


template<typename K, typename V, size_t N>
class map : private vector<std::optional<std::pair<K, V>>, N>
Expand Down Expand Up @@ -69,13 +69,14 @@ class map : private vector<std::optional<std::pair<K, V>>, N>
}
};

} // namespace sframe
} // namespace SFRAME_NAMESPACE

#else // ifdef NO_ALLOC

#include <map>
#include <namespace.h>

namespace sframe {
namespace SFRAME_NAMESPACE {

// NOTE: NOT RECOMMENDED FOR USE OUTSIDE THIS LIBRARY
//
Expand Down Expand Up @@ -106,6 +107,6 @@ class map : public std::map<K, V>
}
};

} // namespace sframe
} // namespace SFRAME_NAMESPACE

#endif // def NO_ALLOC
6 changes: 4 additions & 2 deletions include/sframe/sframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <sframe/map.h>
#include <sframe/vector.h>

#include <namespace.h>

// These constants define the size of certain internal data structures if
// we are configured not to depend on dynamic allocations, i.e., if the NO_ALLOC
// flag is set. If you are using an allocator, you can ignore them.
Expand All @@ -22,7 +24,7 @@
#define SFRAME_EPOCH_BITS 4
#endif

namespace sframe {
namespace SFRAME_NAMESPACE {

struct crypto_error : std::runtime_error
{
Expand Down Expand Up @@ -202,4 +204,4 @@ class MLSContext : protected Context
vector<std::optional<EpochKeys>, max_epochs> epoch_cache;
};

} // namespace sframe
} // namespace SFRAME_NAMESPACE
9 changes: 5 additions & 4 deletions include/sframe/vector.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include <gsl/gsl-lite.hpp>
#include <namespace.h>

#ifdef NO_ALLOC

namespace sframe {
namespace SFRAME_NAMESPACE {

template<typename T, size_t N>
class vector
Expand Down Expand Up @@ -86,13 +87,13 @@ class vector
operator gsl::span<T>() { return gsl::span(_data).first(_size); }
};

} // namespace sframe
} // namespace SFRAME_NAMESPACE

#else // ifdef NO_ALLOC

#include <vector>

namespace sframe {
namespace SFRAME_NAMESPACE {

// NOTE: NOT RECOMMENDED FOR USE OUTSIDE THIS LIBRARY
//
Expand Down Expand Up @@ -137,6 +138,6 @@ class vector : public std::vector<T>
}
};

} // namespace sframe
} // namespace SFRAME_NAMESPACE

#endif // def NO_ALLOC
4 changes: 2 additions & 2 deletions src/crypto.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "crypto.h"
#include "header.h"

namespace sframe {
namespace SFRAME_NAMESPACE {

size_t
cipher_digest_size(CipherSuite suite)
Expand Down Expand Up @@ -94,4 +94,4 @@ cipher_overhead(CipherSuite suite)
}
}

} // namespace sframe
} // namespace SFRAME_NAMESPACE
4 changes: 2 additions & 2 deletions src/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <sframe/sframe.h>

namespace sframe {
namespace SFRAME_NAMESPACE {

size_t
cipher_digest_size(CipherSuite suite);
Expand Down Expand Up @@ -48,4 +48,4 @@ open(CipherSuite suite,
input_bytes aad,
input_bytes ct);

} // namespace sframe
} // namespace SFRAME_NAMESPACE
4 changes: 2 additions & 2 deletions src/crypto_boringssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <openssl/hmac.h>
#include <openssl/mem.h>

namespace sframe {
namespace SFRAME_NAMESPACE {

///
/// Convert between native identifiers / errors and OpenSSL ones
Expand Down Expand Up @@ -428,6 +428,6 @@ open(CipherSuite suite,
throw unsupported_ciphersuite_error();
}

} // namespace sframe
} // namespace SFRAME_NAMESPACE

#endif // defined(OPENSSL_3)
4 changes: 2 additions & 2 deletions src/crypto_openssl11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <openssl/evp.h>
#include <openssl/hmac.h>

namespace sframe {
namespace SFRAME_NAMESPACE {

///
/// Scoped pointers for OpenSSL objects
Expand Down Expand Up @@ -466,6 +466,6 @@ open(CipherSuite suite,
throw unsupported_ciphersuite_error();
}

} // namespace sframe
} // namespace SFRAME_NAMESPACE

#endif // defined(OPENSSL_1_1)
4 changes: 2 additions & 2 deletions src/crypto_openssl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <openssl/kdf.h>
#include <openssl/params.h>

namespace sframe {
namespace SFRAME_NAMESPACE {

///
/// Convert between native identifiers / errors and OpenSSL ones
Expand Down Expand Up @@ -468,6 +468,6 @@ open(CipherSuite suite,
throw unsupported_ciphersuite_error();
}

} // namespace sframe
} // namespace SFRAME_NAMESPACE

#endif // defined(OPENSSL_3)
4 changes: 2 additions & 2 deletions src/header.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "header.h"

namespace sframe {
namespace SFRAME_NAMESPACE {

static size_t
uint_size(uint64_t val)
Expand Down Expand Up @@ -200,4 +200,4 @@ Header::encode(output_bytes buffer) const
}
#endif

} // namespace sframe
} // namespace SFRAME_NAMESPACE
4 changes: 2 additions & 2 deletions src/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <sframe/sframe.h>

namespace sframe {
namespace SFRAME_NAMESPACE {

void
encode_uint(uint64_t val, output_bytes buffer);
Expand Down Expand Up @@ -31,4 +31,4 @@ class Header
Header(KeyID key_id_in, Counter counter_in, input_bytes encoded_in);
};

} // namespace sframe
} // namespace SFRAME_NAMESPACE
4 changes: 2 additions & 2 deletions src/sframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "crypto.h"
#include "header.h"

namespace sframe {
namespace SFRAME_NAMESPACE {

///
/// Errors
Expand Down Expand Up @@ -378,4 +378,4 @@ MLSContext::ensure_key(KeyID key_id, KeyUsage usage)
return;
}

} // namespace sframe
} // namespace SFRAME_NAMESPACE
2 changes: 1 addition & 1 deletion test/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ from_hex(const std::string& hex)
}

std::string
to_hex(const sframe::input_bytes data)
to_hex(const SFRAME_NAMESPACE::input_bytes data)
{
std::stringstream hex(std::ios_base::out);
hex.flags(std::ios::hex);
Expand Down
3 changes: 2 additions & 1 deletion test/common.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <namespace.h>
#include <sframe/sframe.h>

#include <string>
Expand All @@ -8,7 +9,7 @@ using bytes = std::vector<uint8_t>;
bytes
from_hex(const std::string& hex);
std::string
to_hex(const sframe::input_bytes data);
to_hex(const SFRAME_NAMESPACE::input_bytes data);

template<typename T>
bytes
Expand Down
2 changes: 1 addition & 1 deletion test/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <map> // for map
#include <stdexcept> // for invalid_argument

using namespace sframe;
using namespace SFRAME_NAMESPACE;

TEST_CASE("Header Known-Answer")
{
Expand Down
2 changes: 1 addition & 1 deletion test/sframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <stdexcept> // for invalid_argument
#include <string> // for basic_string, operator==

using namespace sframe;
using namespace SFRAME_NAMESPACE;

TEST_CASE("SFrame Round-Trip")
{
Expand Down
2 changes: 1 addition & 1 deletion test/vectors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "common.h"

using namespace sframe;
using namespace SFRAME_NAMESPACE;
using nlohmann::json;

struct HexBytes
Expand Down
Loading