Skip to content

Commit

Permalink
Custom build func to Windows/ARM64 assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
justsmth committed Jul 10, 2024
1 parent 00fcba4 commit 7ab4142
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ if(MSVC)
set(CMAKE_GENERATOR_CC cl)
endif()

if(ARCH STREQUAL "aarch64" AND CMAKE_GENERATOR MATCHES "Visual Studio" AND NOT "${CMAKE_VS_PLATFORM_TOOLSET}" MATCHES "ClangCL")
message(FATAL_ERROR "AWS-LC Windows/ARM64 assembly code requires ClangCL. Current toolset: ${CMAKE_VS_PLATFORM_TOOLSET}")
endif()

include(sources.cmake)
include(TestBigEndian)

Expand Down Expand Up @@ -366,10 +370,10 @@ if(GCC OR CLANG)
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra")
set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wall -fvisibility=hidden -fno-common")
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wno-newline-eof")
endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wunused -Wcomment -Wchar-subscripts -Wuninitialized -Wshadow")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wwrite-strings -Wformat-security -Wunused-result -Wno-overlength-strings")
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wno-newline-eof")
set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-c11-extensions -Wvla -Wtype-limits -Wno-unused-parameter")
endif()
set(C_CXX_FLAGS "${C_CXX_FLAGS} -Werror -Wformat=2 -Wsign-compare -Wmissing-field-initializers -Wwrite-strings")
Expand Down
49 changes: 49 additions & 0 deletions crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@

# Function to handle assembly files for ARM64 targets when using MSBuild (the "Visual Studio" generator)
# This function is necessary because MSBuild ignores ARM64 assembly file dependencies
function(msbuild_aarch64_asm)
set(options "")
set(oneValueArgs TARGET OUTPUT_OBJECTS)
set(multiValueArgs ASM_FILES)

cmake_parse_arguments(MSBUILD_AARCH64_ASM "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGV})

# Set the output directory for object files
set(OBJ_DIR "${CMAKE_CURRENT_BINARY_DIR}/${MSBUILD_AARCH64_ASM_TARGET}.dir/$<CONFIG>")

# Initialize list to store assembled object files
set(ASSEMBLED_OBJECTS "")

foreach(ASM_FILE ${MSBUILD_AARCH64_ASM_ASM_FILES})
# Get the filename without extension
get_filename_component(ASM_NAME ${ASM_FILE} NAME_WE)
set(OBJ_FILE "${OBJ_DIR}/${ASM_NAME}.obj")

add_custom_command(
OUTPUT ${OBJ_FILE}
COMMAND "${CMAKE_ASM_COMPILER}"
--target=arm64-pc-windows-msvc
/c
/I "${PROJECT_SOURCE_DIR}/include"
/o "${OBJ_FILE}"
"${ASM_FILE}"
DEPENDS ${ASM_FILE}
COMMENT "Assembling ${ASM_FILE}"
)

# Mark the generated object file as an external object
set_source_files_properties(${OBJ_FILE} PROPERTIES EXTERNAL_OBJECT TRUE)

list(APPEND ASSEMBLED_OBJECTS ${OBJ_FILE})
endforeach()

# Set the output variable in the parent scope
set(${MSBUILD_AARCH64_ASM_OUTPUT_OBJECTS} ${ASSEMBLED_OBJECTS} PARENT_SCOPE)
endfunction()

if(NOT OPENSSL_NO_ASM)
if(UNIX)
if(ARCH STREQUAL "aarch64")
Expand Down Expand Up @@ -281,6 +324,11 @@ if(ENABLE_DILITHIUM)
)
endif()

set(CRYPTO_ARCH_OBJECTS "")
if (ARCH STREQUAL "aarch64" AND CMAKE_GENERATOR MATCHES "Visual Studio")
msbuild_aarch64_asm(TARGET crypto_objects ASM_FILES ${CRYPTO_ARCH_SOURCES} OUTPUT_OBJECTS CRYPTO_ARCH_OBJECTS)
endif()

add_library(
crypto_objects
OBJECT
Expand Down Expand Up @@ -532,6 +580,7 @@ add_library(
decrepit/x509/x509_decrepit.c

${CRYPTO_ARCH_SOURCES}
${CRYPTO_ARCH_OBJECTS}
)

target_compile_definitions(crypto_objects PRIVATE BORINGSSL_IMPLEMENTATION)
Expand Down
7 changes: 7 additions & 0 deletions crypto/fipsmodule/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,11 @@ elseif(FIPS_SHARED)
set(BCM_NAME ${BCM_NAME} PARENT_SCOPE)

else()
set(BCM_ASM_OBJECTS "")
if (ARCH STREQUAL "aarch64" AND CMAKE_GENERATOR MATCHES "Visual Studio")
msbuild_aarch64_asm(TARGET fipsmodule ASM_FILES ${BCM_ASM_SOURCES} OUTPUT_OBJECTS BCM_ASM_OBJECTS)
endif()

add_library(
fipsmodule

Expand All @@ -563,10 +568,12 @@ else()
cpucap/cpucap.c

${BCM_ASM_SOURCES}
${BCM_ASM_OBJECTS}
)
target_compile_definitions(fipsmodule PRIVATE BORINGSSL_IMPLEMENTATION)

add_dependencies(fipsmodule boringssl_prefix_symbols)
target_include_directories(fipsmodule BEFORE PRIVATE ${PROJECT_BINARY_DIR}/symbol_prefix_include)
target_include_directories(fipsmodule PRIVATE ${PROJECT_SOURCE_DIR}/include)

endif()
2 changes: 1 addition & 1 deletion util/build_compilation_database.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ mkdir -p "${AWS_LC_BUILD}"

cmake "${BASE_DIR}" -B "${AWS_LC_BUILD}" ${MY_CMAKE_FLAGS[@]} "${@}"

cmake --build "${AWS_LC_BUILD}" --target all
cmake --build "${AWS_LC_BUILD}" -j 4 --target all_tests

cp "${AWS_LC_BUILD}"/compile_commands.json "${BASE_DIR}"/

0 comments on commit 7ab4142

Please sign in to comment.