From f25ca44f03cef8667052fb2f0a284fde471045e2 Mon Sep 17 00:00:00 2001 From: Jordon Brooks Date: Mon, 27 May 2024 19:09:17 +0100 Subject: [PATCH] Linux compile working --- .gitignore | 4 ++ Compile.bat | 68 ++++++++++++++++++++++++++++++++ Compile.sh | 73 +++++++++++++++++++++++++++++++++++ HarmonyLinkLib/CMakeLists.txt | 16 +++++++- HarmonyLinkLib/include/Core.h | 12 +++++- 5 files changed, 169 insertions(+), 4 deletions(-) create mode 100644 Compile.bat create mode 100644 Compile.sh diff --git a/.gitignore b/.gitignore index 2e489f4..a7fdac2 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,7 @@ build/** linuxbuild/ build/ .idea/ +winbuild/ + +!Compile.sh +!Compile.bat diff --git a/Compile.bat b/Compile.bat new file mode 100644 index 0000000..ccad60c --- /dev/null +++ b/Compile.bat @@ -0,0 +1,68 @@ +@echo off +setlocal enabledelayedexpansion + +REM Clear the screen +cls + +REM Define the color codes +set GREEN= +set NC= + +REM Prompt the user to choose a compiler +echo %GREEN%Select the compiler to use:%NC% +echo 1^ ) MSBuild (default) +echo 2^ ) MinGW +echo 3^ ) Ninja +set /p choice=Enter the number of your choice: + +REM Set the generator and compiler based on the user's choice +if "%choice%"=="2" ( + set "GENERATOR=MinGW Makefiles" + set "COMPILER_OPTION=-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++" +) else if "%choice%"=="3" ( + set "GENERATOR=Ninja" + set "COMPILER_OPTION=" +) else ( + set "GENERATOR=Visual Studio 17 2022" + set "COMPILER_OPTION=-A x64" + set "choice=1" +) + +echo Using generator: %GENERATOR% + +REM Prompt the user to choose a build type +echo %GREEN%Select the build type:%NC% +echo 1^ ) Release (default) +echo 2^ ) Debug +set /p build_choice=Enter the number of your choice: + +REM Set the build type based on the user's choice +if "%build_choice%"=="2" ( + set "BUILD_TYPE=Debug" +) else ( + set "BUILD_TYPE=Release" +) + +echo Build type: %BUILD_TYPE% + +REM Create the build directory if it doesn't exist +if not exist "winbuild" ( + mkdir winbuild +) + +cd winbuild + +REM Get the number of processors +for /f "tokens=2 delims==" %%a in ('wmic cpu get NumberOfLogicalProcessors /value') do set "NUM_PROCESSORS=%%a" + +REM Run CMake with the selected generator and build type +cmake -G "%GENERATOR%" %COMPILER_OPTION% -DCMAKE_BUILD_TYPE=%BUILD_TYPE% .. + +REM Build the project +if "%choice%"=="1" ( + cmake --build . --config %BUILD_TYPE% -- /m:%NUM_PROCESSORS% +) else ( + cmake --build . --config %BUILD_TYPE% -- -j %NUM_PROCESSORS% +) + +cd .. diff --git a/Compile.sh b/Compile.sh new file mode 100644 index 0000000..c8d82ea --- /dev/null +++ b/Compile.sh @@ -0,0 +1,73 @@ +#!/bin/sh + +set -e + +clear + +# Define the color codes +GREEN='\033[0;32m' +NC='\033[0m' # No Color + +# Prompt the user to choose a compiler +echo "${GREEN}Select the compiler to use:${NC}" +echo "1) g++ (default)" +echo "2) clang++" +echo "3) clang++ 15" +echo "4) clang++ 16" +read -p "Enter the number of your choice: " choice + +# Set the compiler based on the user's choice +case $choice in + 2) + C_COMPILER=clang + CXX_COMPILER=clang++ + ;; + 3) + C_COMPILER=clang-15 + CXX_COMPILER=clang++-15 + ;; + 4) + C_COMPILER=clang-16 + CXX_COMPILER=clang++-16 + ;; + *) + C_COMPILER=gcc + CXX_COMPILER=g++ + ;; +esac + +echo "Using C compiler: $C_COMPILER" +echo "Using C++ compiler: $CXX_COMPILER" + +# Prompt the user to choose a build type +echo "${GREEN}Select the build type:${NC}" +echo "1) Release (default)" +echo "2) Debug" +read -p "Enter the number of your choice: " build_choice + +# Set the build type based on the user's choice +case $build_choice in + 2) + BUILD_TYPE=Debug + ;; + *) + BUILD_TYPE=Release + ;; +esac + +echo "Build type: $BUILD_TYPE" + +# Create the build directory if it doesn't exist +if [ ! -d "linuxbuild" ]; then + mkdir linuxbuild +fi + +cd linuxbuild + +# Run CMake with the selected compiler and build type +cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_C_COMPILER=$C_COMPILER -DCMAKE_CXX_COMPILER=$CXX_COMPILER .. + +# Build the project +cmake --build . --config $BUILD_TYPE --clean-first -j 15 --verbose + +cd .. diff --git a/HarmonyLinkLib/CMakeLists.txt b/HarmonyLinkLib/CMakeLists.txt index 7146d8e..eba9442 100644 --- a/HarmonyLinkLib/CMakeLists.txt +++ b/HarmonyLinkLib/CMakeLists.txt @@ -120,6 +120,18 @@ set(MAC_INCLUDES "src/Platform/Unix/UnixUtilities.h" ) +# Platform-specific definitions +if(WIN32) + add_definitions(-DBUILD_WINDOWS) +elseif(UNIX) + if(APPLE) + add_definitions(-DBUILD_MACOS) + else() + add_definitions(-DBUILD_LINUX) + endif() + add_definitions(-DBUILD_UNIX) +endif() + # Platform-specific definitions if(WIN32) message(STATUS "Compiling for Windows...") @@ -140,7 +152,7 @@ elseif(UNIX) endif() # Create the shared library -add_library(HarmonyLinkLibShared SHARED ${LIB_SOURCES} ${SHARED_SOURCES}) +add_library(HarmonyLinkLibShared SHARED ${LIB_SOURCES} ${SHARED_SOURCES} ${LIB_INCLUDES}) target_include_directories(HarmonyLinkLibShared PRIVATE "${PROJECT_SOURCE_DIR}/src" @@ -151,7 +163,7 @@ target_include_directories(HarmonyLinkLibShared target_compile_definitions(HarmonyLinkLibShared PRIVATE HARMONYLINKLIB_SHARED) # Create the static library -add_library(HarmonyLinkLibStatic STATIC ${LIB_SOURCES}) +add_library(HarmonyLinkLibStatic STATIC ${LIB_SOURCES} ${LIB_INCLUDES}) target_include_directories(HarmonyLinkLibStatic PRIVATE "${PROJECT_SOURCE_DIR}/src" diff --git a/HarmonyLinkLib/include/Core.h b/HarmonyLinkLib/include/Core.h index 94a32d2..90babed 100644 --- a/HarmonyLinkLib/include/Core.h +++ b/HarmonyLinkLib/include/Core.h @@ -15,7 +15,7 @@ #pragma once // Use a preprocessor definition to switch between export and import declarations -#ifdef _WIN32 +#ifdef BUILD_WINDOWS #ifdef HARMONYLINKLIB_STATIC #define HARMONYLINKLIB_API #else @@ -26,5 +26,13 @@ #endif #endif #else - #define HARMONYLINKLIB_API + #ifdef HARMONYLINKLIB_SHARED + #ifdef __clang__ + #define HARMONYLINKLIB_API __attribute__((visibility("default"))) + #else + #define HARMONYLINKLIB_API + #endif + #else + #define HARMONYLINKLIB_API + #endif #endif