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

cmake standalone build #284

Merged
merged 2 commits into from
Nov 30, 2022
Merged
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
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ set(hacl_VERSION_TWEAK "")
# This file must be generated before running cmake with ./mach.py --configure
# If the build is invoked through ./mach.py, a separate configuration is not
# needed.
include(build/config.cmake)
# If the file is not present, i.e. cmake was invoked directly, we copy the default
# config from config/default_config.cmake
if(NOT EXISTS ${PROJECT_SOURCE_DIR}/build/config.cmake)
configure_file(${PROJECT_SOURCE_DIR}/config/default_config.cmake ${PROJECT_SOURCE_DIR}/build/config.cmake COPYONLY)
endif()

# Now include the config.
include(${PROJECT_SOURCE_DIR}/build/config.cmake)

# Constants used throughout hacl and the build.
include(config/constants.cmake)
Expand Down
1 change: 1 addition & 0 deletions examples/cmake/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
25 changes: 25 additions & 0 deletions examples/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.10)

include(FetchContent)
FetchContent_Declare(hacl
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
GIT_REPOSITORY https://github.com/cryspen/hacl-packages/
GIT_TAG 88560a8b5bae5db890e11f1ea71376adfa767baf
)
FetchContent_MakeAvailable(hacl)

project(hacl-blake-example)
set(CMAKE_OSX_ARCHITECTURES arm64)

add_executable(example blake-example.cc)

# Add includes from HACL
target_include_directories(example PRIVATE
${hacl_SOURCE_DIR}/include
${hacl_SOURCE_DIR}/build
${hacl_SOURCE_DIR}/karamel/include
${hacl_SOURCE_DIR}/karamel/krmllib/dist/minimal
${hacl_SOURCE_DIR}/vale/include
)
# Link the HACL library
target_link_libraries(example PRIVATE hacl)
12 changes: 12 additions & 0 deletions examples/cmake/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# CMake Example

This folder contains an example for how to use the HACL Packages C library in a
CMake project.

```sh
mkdir build
cd build
cmake ..
cmake --build .
./example
```
44 changes: 44 additions & 0 deletions examples/cmake/blake-example.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2022 Cryspen Sarl
*
* Licensed under the Apache License, Version 2.0 or MIT.
* - http://www.apache.org/licenses/LICENSE-2.0
* - http://opensource.org/licenses/MIT
*/
#include "Hacl_Hash_Blake2.h"

using namespace std;

void print_hex_ln(size_t bytes_len, uint8_t *bytes)
{
for (int i = 0; i < bytes_len; ++i)
{
printf("%02x", bytes[i]);
}

printf("\n");
}

int main(int argc, char const *argv[])
{
// Reserve memory for a 64 byte digest, i.e.,
// for a BLAKE2B run with full 512-bit output.
franziskuskiefer marked this conversation as resolved.
Show resolved Hide resolved
uint32_t output_len = 64;
uint8_t output[64];

// The message we want to hash.
const char *message = "Hello, HACL Packages!";
uint32_t message_len = strlen(message);

// BLAKE2B can be used as an HMAC, i.e., with a key.
// We don't want to use a key here and thus provide a zero-sized key.
uint32_t key_len = 0;
uint8_t *key = 0;

Hacl_Blake2b_32_blake2b(
output_len, output, message_len, (uint8_t *)message, key_len, key);

print_hex_ln(output_len, output);

return 0;
}