Skip to content

Commit

Permalink
Fixed cross-compiling of AREG SDK. binaries. Updated documents.
Browse files Browse the repository at this point in the history
  • Loading branch information
aregtech committed Nov 11, 2024
1 parent fb086ea commit 33ced59
Show file tree
Hide file tree
Showing 20 changed files with 643 additions and 202 deletions.
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,14 @@ git clone https://github.com/aregtech/areg-sdk.git

### Build Instructions

The **AREG SDK** is written in **C++17** and supports multiple platforms and compilers:
- **Supported platforms:** Linux, Windows, Cygwin and macOS.
- **Supported compilers:** GNU, Clang, MSVC and Cygwin GNU.
- **Supported CPU:** x86, x86_64, arm, and aarch64.
The **AREG SDK** is written in **C++17**, supports multiple platforms, processors and compilers:

The following build tools are supported:

| Tool | Solution | Platforms | API | Quick actions to compile |
|-------------|------------------|------------------------|--------------|------------------------------------|
| **CMake** | `CMakeLists.txt` | Linux, Windows, Cygwin | POSIX, Win32 | Build with CMake, VSCode, or MSVS. |
| **MSBuild** | `areg-sdk.sln` | Windows | Win32 | Build with MSBuild or MSVS. |
| Compiler | Platforms | Tools | API | CPU Architecture |
|-----------|---------------|---------------|---------------|---------------------------|
| GNU | Linux, macOS | CMake | POSIX | x86, x86_64, ARM, AARCH64 |
| Clang | Linux, Windows| CMake, MSVS | POSIX, Win32 | x86, x86_64 |
| MSVC | Windows | CMake, MSVS | Win32 | x86, x86_64 |
| Cygwin GNU| Windows | CMake | POSIX | x86, x86_64 |

For detailed build instructions, check the **[Building AREG SDK with CMake](./docs/wiki/01b-cmake-build.md)** or **[Building the AREG SDK with Microsoft Visual Studio and MSBuild](./docs/wiki/01c-msvc-build.md)** pages.

Expand All @@ -175,7 +172,8 @@ cmake -B ./build
cmake --build ./build -j 20
```

For custom builds, pass the options via the command line or configure the options listed in **[user.cmake](./docs/wiki/02a-cmake-config.md)** in CMake script.
> [!TIP]
> For **custom builds and cross-compiling** see **[Building AREG SDK with CMake](./docs/wiki/01b-cmake-build.md)**.
#### Build with Microsoft Visual Studio

Expand Down
2 changes: 2 additions & 0 deletions areg-sdk.sln
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "wiki", "wiki", "{C33D0DF1-A
docs\wiki\04d-logcollector.md = docs\wiki\04d-logcollector.md
docs\wiki\05a-mcrouter.md = docs\wiki\05a-mcrouter.md
docs\wiki\06a-persistence-syntax.md = docs\wiki\06a-persistence-syntax.md
docs\wiki\07a-troubleshooting-wsl-update.md = docs\wiki\07a-troubleshooting-wsl-update.md
docs\wiki\07b-troubleshooting-cmake-linux-builds.md = docs\wiki\07b-troubleshooting-cmake-linux-builds.md
docs\wiki\README.md = docs\wiki\README.md
EndProjectSection
EndProject
Expand Down
24 changes: 21 additions & 3 deletions conf/cmake/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ endif()
# Identify compiler short name
if ("${AREG_COMPILER_FAMILY}" STREQUAL "")

macro_setup_compilers_data("${CMAKE_CXX_COMPILER}" AREG_COMPILER_FAMILY AREG_COMPILER_SHORT AREG_CXX_COMPILER AREG_C_COMPILER _compiler_found)
macro_setup_compilers_data("${CMAKE_CXX_COMPILER}" AREG_COMPILER_FAMILY AREG_COMPILER_SHORT AREG_CXX_COMPILER AREG_C_COMPILER AREG_PROCESSOR AREG_BITNESS _compiler_found)
if (_compiler_found)
message(STATUS "AREG: >>> Use system default settings:")
message(STATUS "AREG: ... Compiler family = \'${AREG_COMPILER_FAMILY}\'")
Expand All @@ -24,14 +24,16 @@ if ("${AREG_COMPILER_FAMILY}" STREQUAL "")

elseif("${AREG_COMPILER_SHORT}" STREQUAL "")

message(FATAL_ERROR "AREG: >>> The cmake file \'${AREG_CMAKE_CONFIG_DIR}/setup.cmake\' should be included before \'common.cmake\', fix it and retry again.")
message(FATAL_ERROR "AREG: >>> The file \'${AREG_CMAKE_CONFIG_DIR}/setup.cmake\' should be included before \'common.cmake\', fix it and retry again.")

endif()

# Identify the OS
set(AREG_OS ${CMAKE_SYSTEM_NAME})
# Identify CPU platform
set(AREG_PROCESSOR ${CMAKE_SYSTEM_PROCESSOR})
if ("${AREG_PROCESSOR}" STREQUAL "")
set(AREG_PROCESSOR ${CMAKE_SYSTEM_PROCESSOR})
endif()

# -----------------------------------------------------
# areg specific internal variable settings
Expand Down Expand Up @@ -63,11 +65,21 @@ option(AREG_GTEST_FOUND "GTest package found flag" FALSE)
add_definitions(-DUNICODE -D_UNICODE)
remove_definitions(-D_MBCS -DMBCS)

# ########################################################
# Warnings to disable
# ########################################################

# Disable common warnings for all projects
set(AREG_OPT_DISABLE_WARN_COMMON)
# Disable warnings only for areg framework
set(AREG_OPT_DISABLE_WARN_FRAMEWORK)
# Disable warnings only for tools
set(AREG_OPT_DISABLE_WARN_TOOLS)
# Disable warnings only for examples
set(AREG_OPT_DISABLE_WARN_EXAMPLES)
# Disable warnings only for generated codes
set(AREG_OPT_DISABLE_WARN_CODEGEN)
# Disable warnings only for thirdparty projects
set(AREG_OPT_DISABLE_WARN_THIRDPARTY)

# Checking Compiler for adding corresponded tweaks and flags
Expand Down Expand Up @@ -106,6 +118,12 @@ else()
add_definitions(-DAREG_LOGS=0)
endif()

if (AREG_BITNESS EQUAL 32)
add_definitions(-DBIT32)
else()
add_definitions(-DBIT64)
endif()

# -------------------------------------------------------
# Setup product paths
# -------------------------------------------------------
Expand Down
85 changes: 69 additions & 16 deletions conf/cmake/functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,51 @@ macro(macro_parse_arguments res_sources res_libs res_resources)
endforeach()
endmacro(macro_parse_arguments)

# ---------------------------------------------------------------------------
# Macro ......: macro_guess_processor_architecture
# Purpose ....: Tries to guess the CPU architecture and bitness from the given cross-compiler.
# For example, if cross-compiler has "arm" in the name, it could mean that
# it targets 32-bit ARM processor.
# Parameters .: ${compiler_path} [in] -- Path of the cross-compiler.
# ${target_processor} [out] -- Name of variable to store the CPU type.
# ${target_bitness} [out] -- Name of variable to store bitness.
#
# Usage ......: macro_guess_processor_architecture(<compiler-path> <processor-var> <bitness-var>)
# Example ....: macro_guess_processor_architecture("arm-linux-gnueabihf-g++" cpu_architect cpu_bitness)
# ---------------------------------------------------------------------------
macro(macro_guess_processor_architecture compiler_path target_processor target_bitness)
foreach(_proc "arm;32;arm" "aarch64;64;arm64")
list(GET _proc 0 _arch)
list(GET _proc 1 _bits)
list(GET _proc 2 _name)
string(FIND "${compiler_path}" ${_arch} _proc_pos)
if (_proc_pos GREATER -1)
set(${target_processor} ${_name})
set(${target_bitness} ${_bits})
break()
endif()
endforeach()
endmacro(macro_guess_processor_architecture)

# ---------------------------------------------------------------------------
# Macro ......: macro_system_bitness
# Purpose ....: Extracts the system default bitness.
# Parameters : ${var_bitness} -- The name of variable to set the bitness.
# Usage ......: macro_system_bitness(<var-name>)
# Example ....: macro_system_bitness(_sys_bitness)
# ---------------------------------------------------------------------------
macro(macro_system_bitness var_bitness)
# Detect and set bitness here
# 8 bytes ==> 64-bits (x64) and 4 bytes ==> 32-nit (x86)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(${var_bitness} 64)
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(${var_bitness} 32)
else()
set(${var_bitness} 0)
endif()
endmacro(macro_system_bitness)

# ---------------------------------------------------------------------------
# Macro ......: macro_setup_compilers_data
# Purpose ....: Identifies and configures compiler family, short names, and paths.
Expand All @@ -181,13 +226,21 @@ endmacro(macro_parse_arguments)
# AREG_COMPILER_FAMILY
# AREG_COMPILER_SHORT
# AREG_CXX_COMPILER
# AREG_C_COMPILER
# AREG_C_COMPILER
# AREG_PROCESSOR
# AREG_BITNESS
# _compiler_supports
# )
# ---------------------------------------------------------------------------
macro(macro_setup_compilers_data compiler_path compiler_family compiler_short compiler_cxx compiler_c is_identified)
macro(macro_setup_compilers_data compiler_path compiler_family compiler_short compiler_cxx compiler_c sys_platform sys_bitness is_identified)

set(${is_identified} FALSE)
if (DEFINED AREG_PROCESSOR)
set(${sys_platform} AREG_PROCESSOR)
endif()
if (DEFINED AREG_BITNESS)
set(${sys_bitness} AREG_BITNESS)
endif()

# Iterate over known compilers to identify the compiler type
foreach(_entry "clang-cl;llvm;clang-cl" "clang++;llvm;clang" "clang;llvm;clang" "g++;gnu;gcc" "gcc;gnu;gcc" "c++;gnu;cc" "cc;gnu;cc" "cl;msvc;cl")
Expand All @@ -196,11 +249,15 @@ macro(macro_setup_compilers_data compiler_path compiler_family compiler_short co
list(GET _entry 2 _c_compiler)

# Check if the provided compiler matches the known C++ compiler
string(FIND "${compiler_path}" "${_cxx_compiler}" _found_pos REVERSE)
string(TOLOWER "${compiler_path}" _comp_path)
string(FIND "${_comp_path}" "${_cxx_compiler}" _found_pos REVERSE)
if (_found_pos GREATER -1)
# Handle special case for CYGWIN and GNU family compilers
if (CYGWIN AND ("${_family}" STREQUAL "gnu"))
set(${compiler_family} "cygwin")
elseif("${_family}" STREQUAL "gnu")
macro_guess_processor_architecture("${_comp_path}" ${sys_platform} ${sys_bitness})
set(${compiler_family} "${_family}")
else()
set(${compiler_family} "${_family}")
endif()
Expand Down Expand Up @@ -234,7 +291,7 @@ endmacro(macro_setup_compilers_data)
# - ${compiler_cxx} -- Output: Variable to hold the C++ compiler name.
# - ${compiler_c} -- Output: Variable to hold the corresponding C compiler name.
# - ${is_identified} -- Output: Name of variable to hold Boolean indicating successful identification..
# Usage ......: macro_setup_compilers_data(<compiler> <family-var> <short-var> <CXX-compiler-var> <C-compiler-var> <identified-var>)
# Usage ......: macro_setup_compilers_data_by_family(<compiler-family> <short-var> <CXX-compiler-var> <C-compiler-var> <identified-var>)
# Example ....: macro_setup_compilers_data_by_family("gnu"
# AREG_COMPILER_SHORT
# AREG_CXX_COMPILER
Expand All @@ -258,6 +315,14 @@ macro(macro_setup_compilers_data_by_family compiler_family compiler_short compil
set(${compiler_short} "clang-cl")
set(${compiler_cxx} "clang-cl")
set(${compiler_c} "clang-cl")
elseif (AREG_PROCESSOR STREQUAL "arm" AND "${_family}" STREQUAL "gnu")
set(${compiler_short} "g++")
set(${compiler_cxx} "arm-linux-gnueabihf-g++")
set(${compiler_c} "arm-linux-gnueabihf-gcc")
elseif (AREG_PROCESSOR STREQUAL "aarch64" AND "${_family}" STREQUAL "gnu")
set(${compiler_short} "g++")
set(${compiler_cxx} "aarch64-linux-gnu-g++")
set(${compiler_c} "aarch64-linux-gnu-gcc")
else()
set(${compiler_short} "${_compiler}")
set(${compiler_cxx} "${_compiler}")
Expand Down Expand Up @@ -840,15 +905,3 @@ function(printAregConfigStatus var_make_print var_prefix var_header var_footer)
message(STATUS "=======================================================================================")

endfunction(printAregConfigStatus)

macro(macro_system_bitness var_bitness)
# Detect and set bitness here
# 8 bytes ==> 64-bits (x64) and 4 bytes ==> 32-nit (x86)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(${var_bitness} 64)
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(${var_bitness} 32)
else()
set(${var_bitness} 0)
endif()
endmacro(macro_system_bitness)
18 changes: 12 additions & 6 deletions conf/cmake/gnu.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ else()
list(APPEND AREG_COMPILER_OPTIONS -O0 -g3)
endif()

if(${AREG_BITNESS} EQUAL 32)
list(APPEND AREG_COMPILER_OPTIONS -m32)
list(APPEND AREG_LDFLAGS -m32)
else()
list(APPEND AREG_COMPILER_OPTIONS -m64)
list(APPEND AREG_LDFLAGS -m64)
if (NOT ${AREG_PROCESSOR} STREQUAL "arm" AND NOT ${AREG_PROCESSOR} STREQUAL "aarch64")
if(${AREG_BITNESS} EQUAL 32)
list(APPEND AREG_COMPILER_OPTIONS -m32)
list(APPEND AREG_LDFLAGS -m32)
else()
list(APPEND AREG_COMPILER_OPTIONS -m64)
list(APPEND AREG_LDFLAGS -m64)
endif()
endif()

# Linker flags (-l is not necessary)
Expand All @@ -46,3 +48,7 @@ list(APPEND AREG_OPT_DISABLE_WARN_THIRDPARTY
-Wno-everything
-Wno-unused-function
)

list(APPEND AREG_OPT_DISABLE_WARN_COMMON
-Wno-psabi
)
8 changes: 4 additions & 4 deletions conf/cmake/msvc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ message(STATUS "AREG: >>> Preparing settings for MSVC compiler under \'${AREG_OS

# Visual Studio C++, Windows / Win32 API
set(AREG_DEVELOP_ENV "Win32")

add_definitions(-DWINDOWS -D_WINDOWS -DWIN32 -D_WIN32)
if (${AREG_BITNESS} EQUAL 64)
add_definitions(-DWIN64 -D_WIN64)
endif()

if(NOT CMAKE_BUILD_TYPE MATCHES Release)
list(APPEND AREG_COMPILER_OPTIONS -Od -RTC1 -c)
endif()

if (${AREG_BITNESS} EQUAL 64)
add_definitions(-DWIN64 -D_WIN64)
endif()

# Linker flags (-l is not necessary)
list(APPEND AREG_LDFLAGS advapi32 psapi shell32 ws2_32)
set(AREG_LDFLAGS_STR "-ladvapi32 -lpsapi -lshell32 -lws2_32")
Loading

0 comments on commit 33ced59

Please sign in to comment.