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

Set up code build. #1

Merged
merged 12 commits into from
Jul 21, 2020
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
# Based on templates from https://github.com/github/gitignore.
build/*
bin/*
.cproject
.project
keys/*
install/*
**/__pycache__
*.unison*
.vscode/

# Ignore third party.
third-party/*/**
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
!third-party/seal/src/**
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
!third-party/*/CMakeLists.txt
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
cmake-build-debug

# Ignore CMake
# https://github.com/github/gitignore/blob/master/CMake.gitignore
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
111 changes: 31 additions & 80 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,39 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.12)
include(ExternalProject)

# include scripts used in this file
include(${CMAKE_CURRENT_SOURCE_DIR}/scripts/cmake/apply-git-patch.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/scripts/cmake/update-git-submodules.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/scripts/cmake/set-common-flags.cmake)

project(HELR VERSION 0.0.1 LANGUAGES CXX)

include(FindProtobuf)
include_directories(${PROTOBUF_INCLUDE_DIR})

include(FindBoost)
include_directories(${Boost_INCLUDE_DIRS})
project(homomorphic-implementors-toolkit VERSION 0.0.1 LANGUAGES CXX)

# Define global variables.
# Build in Release mode by default; otherwise use selected option
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE
STRING "Build type" FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
STRINGS "Release" "Debug" "MinSizeRel" "RelWithDebInfo")
endif()
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Release" "Debug" "MinSizeRel" "RelWithDebInfo")
endif ()
message(STATUS "Build type (CMAKE_BUILD_TYPE): ${CMAKE_BUILD_TYPE}")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_HOME_DIRECTORY}/bin/${CMAKE_BUILD_TYPE})

if(DEFINED ENV{NO_CHECKOUT_SEAL})
message(STATUS "Skipping submodule updates")
else()
# check out all git submodules at DETACHED HEAD
update_submodules()

# double check that submodule updates were successful
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/external/SEAL/CMakeLists.txt")
message(FATAL_ERROR "The SEAL submodule was not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()

if(NOT EXISTS "${PROJECT_SOURCE_DIR}/external/googletest/CMakeLists.txt")
message(FATAL_ERROR "The GoogleTest submodule was not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()

# Set the version of SEAL that we want to depend on
set(SEAL_VERSION "3.5.1")

# Checkout the correct branch of SEAL
execute_process(COMMAND ${GIT_EXECUTABLE} checkout ${SEAL_VERSION}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/external/SEAL
RESULT_VARIABLE GIT_SUBMOD_RESULT
OUTPUT_QUIET)
if(NOT GIT_SUBMOD_RESULT EQUAL "0")
message(FATAL_ERROR "Could not checkout SEAL branch ${SEAL_VERSION}")
endif()
endif()

message(STATUS "Searching for dependencies...")
find_package(Protobuf REQUIRED)
find_package(Boost 1.65 REQUIRED COMPONENTS program_options)

# Import Microsoft SEAL prior to compiling helib
message(STATUS "Compiling SEAL...")
add_subdirectory(external/SEAL)

message(STATUS "Compiling HELib...")
add_subdirectory(helib)

enable_testing()

message(STATUS "Compiling GoogleTest...")
add_subdirectory(external/googletest)

if(NOT HELR_ROOT_DIR)
set(HELR_ROOT_DIR "${PROJECT_SOURCE_DIR}/")
endif()

add_executable(evaldemo examples/evaluator_demo.cpp)
set_common_flags(evaldemo)

target_link_libraries(evaldemo helib)

add_custom_target(
run_evaldemo
COMMAND bin/${CMAKE_BUILD_TYPE}/evaldemo
WORKING_DIRECTORY ${HELR_ROOT_DIR}
DEPENDS evaldemo
USES_TERMINAL)
# Include customized cmake functions.
include(cmake/set-common-flags.cmake)
include(cmake/download-external-project.cmake)

# Find third party from CMake modules.
set(HIT_THIRD_PARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third-party)
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
find_package(BoostLib REQUIRED)
find_package(ProtobufLib REQUIRED)
find_package(SealLib REQUIRED)

# Build HIT source code.
# TODO: uncomment below when source code is imported.
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
# add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src)

# Build HIT test code if enabled.
option(BUILD_TESTING "Build the unit tests." ON)
if (BUILD_TESTING)
enable_testing()
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
# include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
# Install test dependency - Google Test.
find_package(GoogleTestLib REQUIRED)
# TODO: uncomment below when test code is imported.
# add_subdirectory(tests)
endif ()
25 changes: 25 additions & 0 deletions cmake/download-external-project.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

function(download_external_project project_dir)
message(STATUS "Downloading ${project_dir}.")
set(EXTERNAL_PROJECT_CMAKE_CACHE_FILE ${HIT_THIRD_PARTY_DIR}/${project_dir}/CMakeCache.txt)
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
if(EXISTS ${EXTERNAL_PROJECT_CMAKE_CACHE_FILE})
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
message(STATUS "Removing old ${EXTERNAL_PROJECT_CMAKE_CACHE_FILE}")
file(REMOVE ${EXTERNAL_PROJECT_CMAKE_CACHE_FILE})
endif()
set(COMMAND_WORK_DIR ${HIT_THIRD_PARTY_DIR}/${project_dir})
execute_process(
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${COMMAND_WORK_DIR})
if(result)
message(FATAL_ERROR "Failed to download (${result}).")
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
RESULT_VARIABLE result
WORKING_DIRECTORY ${COMMAND_WORK_DIR})
if(result)
message(FATAL_ERROR "Failed to build (${result}).")
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
endif()
endfunction()
49 changes: 49 additions & 0 deletions cmake/modules/FindBoostLib.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

message(STATUS "Searching Boost lib.")
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
find_package(Boost 1.65 COMPONENTS program_options)
if (Boost_FOUND)
message(STATUS "Boost is found. Skip downloading source code.")
else ()
message(STATUS "Downloading and installing Boost since it is not found.")
# Variables
set(BOOST_USE_STATIC_LIBS false)
# Set the library prefix and library suffix properly.
if (BOOST_USE_STATIC_LIBS)
set(LIB_PREFIX ${CMAKE_STATIC_LIBRARY_PREFIX})
set(LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX})
else ()
set(LIB_PREFIX ${CMAKE_SHARED_LIBRARY_PREFIX})
set(LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
endif ()
# Download source code.
download_external_project("boost")
set(BOOST_CONTENT_DIR ${HIT_THIRD_PARTY_DIR}/boost/src)
set(BOOST_BUILD_DIR ${HIT_THIRD_PARTY_DIR}/boost/build)
# Build project.
# https://github.com/boostorg/wiki/wiki/Getting-Started%3A-Overview
file(REMOVE_RECURSE ${BOOST_BUILD_DIR})
file(MAKE_DIRECTORY ${BOOST_BUILD_DIR})
execute_process(
COMMAND "./bootstrap.sh" "--prefix=${BOOST_BUILD_DIR}"
RESULT_VARIABLE result
WORKING_DIRECTORY ${BOOST_CONTENT_DIR})
if (result)
message(FATAL_ERROR "Failed to bootstrap boost build. Error code: (${result}).")
endif ()
execute_process(
COMMAND ./b2 --prefix=${BOOST_BUILD_DIR} --with-program_options install
RESULT_VARIABLE result
WORKING_DIRECTORY ${BOOST_CONTENT_DIR})
if (result)
message(FATAL_ERROR "Failed to build boost. Error code: (${result}).")
endif ()
set(Boost_INCLUDE_DIRS ${BOOST_BUILD_DIR}/include)
set(Boost_LIBRARY_DIRS ${BOOST_BUILD_DIR}/lib/${LIB_PREFIX}boost_${component}${LIB_SUFFIX})
endif ()
message(STATUS "Boost version : ${Boost_VERSION}")
message(STATUS "Boost_INCLUDE_DIRS : ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARY_DIRS : ${Boost_LIBRARY_DIRS}")
include_directories(${Boost_INCLUDE_DIRS})
include_directories(${Boost_LIBRARY_DIRS})
13 changes: 13 additions & 0 deletions cmake/modules/FindGoogleTestLib.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

message(STATUS "Downloading and installing GTest.")
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
# Download and install test dependency - Google Test.
# https://github.com/google/googletest/blob/release-1.10.0/googletest/README.md.
download_external_project("googletest")
# Add googletest directly to our build.
# This defines the gtest and gtest_main targets.
add_subdirectory(
${HIT_THIRD_PARTY_DIR}/googletest/src
${HIT_THIRD_PARTY_DIR}/googletest/build
EXCLUDE_FROM_ALL)
52 changes: 52 additions & 0 deletions cmake/modules/FindProtobufLib.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

message(STATUS "Searching Protobuf.")
find_package(Protobuf "3.0.0")
if (Protobuf_FOUND)
message(STATUS "Protobuf is found.")
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
else ()
message(STATUS "Downloading and installing Protobuf since it is not found.")
# Download source code.
download_external_project("protobuf")
# Build and install Protobuf project.
# https://github.com/protocolbuffers/protobuf/blob/master/src/README.md
set(PROTOBUF_CONTENT_DIR ${HIT_THIRD_PARTY_DIR}/protobuf/src)
set(PROTOBUF_BUILD_DIR ${HIT_THIRD_PARTY_DIR}/protobuf/build)
file(REMOVE_RECURSE ${PROTOBUF_BUILD_DIR})
file(MAKE_DIRECTORY ${PROTOBUF_BUILD_DIR})
execute_process(
COMMAND ./configure --prefix ${PROTOBUF_BUILD_DIR}
RESULT_VARIABLE result
WORKING_DIRECTORY ${PROTOBUF_CONTENT_DIR})
if (result)
message(FATAL_ERROR "Failed to configure Protobuf. Error code: (${result}).")
endif ()
execute_process(
COMMAND make -j
RESULT_VARIABLE result
WORKING_DIRECTORY ${PROTOBUF_CONTENT_DIR})
if (result)
message(FATAL_ERROR "Failed to build Protobuf. Error code: (${result}).")
endif ()
execute_process(
COMMAND make install
RESULT_VARIABLE result
WORKING_DIRECTORY ${PROTOBUF_CONTENT_DIR})
if (result)
message(FATAL_ERROR "Failed to install Protobuf. Error code: (${result}).")
endif ()
set(PROTOBUF_USE_STATIC_LIBS false)
# Set the library prefix and library suffix properly.
if (PROTOBUF_USE_STATIC_LIBS)
set(LIB_PREFIX ${CMAKE_STATIC_LIBRARY_PREFIX})
set(LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX})
else ()
set(LIB_PREFIX ${CMAKE_SHARED_LIBRARY_PREFIX})
set(LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
endif ()
set(Protobuf_INCLUDE_DIRS ${PROTOBUF_BUILD_DIR}/include)
set(Protobuf_LIBRARIES "${PROTOBUF_BUILD_DIR}/lib/${LIB_PREFIX}protobuf${LIB_SUFFIX}")
endif ()
message(STATUS "Protobuf include dir : ${Protobuf_INCLUDE_DIRS}")
message(STATUS "Protobuf libraries : ${Protobuf_LIBRARIES}")
9 changes: 9 additions & 0 deletions cmake/modules/FindSealLib.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

message(STATUS "Searching Microsoft SEAL.")

# SEAL is a `git subtree` of this repository because some tweaks are made based on our needs.
# The subtree is under ${HIT_THIRD_PARTY_DIR}/seal/src
# These tweaks may be contributed back to SEAL git repo by opening issues.
add_subdirectory(${HIT_THIRD_PARTY_DIR}/seal/src)
14 changes: 14 additions & 0 deletions cmake/set-common-flags.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

function(set_common_flags target_name)
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
target_compile_options(${target_name} PUBLIC -std=c++17 -Wall -Werror -Wformat=2 -Wwrite-strings -Wvla
-fvisibility=hidden -fno-common -funsigned-char -Wextra -Wunused -Wcomment -Wchar-subscripts -Wuninitialized
-Wunused-result -Wfatal-errors)
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(${target_name} PUBLIC -Wmissing-declarations -Wmissing-field-initializers -Wshadow
-Wpedantic)
endif()
#-Wextra turns on sign-compare which is strict on comparing loop indexes (int) with size_t from vector length
target_compile_options(${target_name} PUBLIC -Wno-sign-compare)
endfunction()
8 changes: 8 additions & 0 deletions scripts/codebuild/run_unit_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash -ex
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

if [ -d build ]; then rm -Rf build; fi
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
mkdir -p build && cd build
cmake -GNinja -DCMAKE_BUILD_TYPE=Release ../
ninja -j $(nproc)
24 changes: 24 additions & 0 deletions third-party/boost/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.12)

project(BOOST_DOWNLOAD VERSION 1.73.0)

message(STATUS "Downloading Boost in ${CMAKE_CURRENT_LIST_DIR}/src.")

string(REPLACE "." "_" BOOST_SUFFIX ${PROJECT_VERSION})

include(ExternalProject)
# TODO: check if the download URL needs to be changed to github release.
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
# https://github.com/boostorg/boost/archive/boost-1.73.0.zip
ExternalProject_Add(EP_BOOST
URL https://downloads.sourceforge.net/project/boost/boost/${PROJECT_VERSION}/boost_${BOOST_SUFFIX}.zip
DOWNLOAD_DIR ${CMAKE_CURRENT_LIST_DIR}/src
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/src
CMAKE_ARGS ""
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
25 changes: 25 additions & 0 deletions third-party/googletest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.12)

project(GTEST_DOWNLOAD VERSION 1.10.0)

message(STATUS "Downloading GoogleTest in ${CMAKE_CURRENT_LIST_DIR}.")

include(ExternalProject)
ExternalProject_Add(EP_GTEST
TMP_DIR ${CMAKE_CURRENT_LIST_DIR}/tmp
STAMP_DIR ${CMAKE_CURRENT_LIST_DIR}/stamp
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
DOWNLOAD_DIR ""
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/src
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
BINARY_DIR ${CMAKE_CURRENT_LIST_DIR}/build
bryce-shang marked this conversation as resolved.
Show resolved Hide resolved
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-${PROJECT_VERSION}
GIT_CONFIG advice.detachedHead=false
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
Loading