Skip to content

Commit

Permalink
feat: add emscripten CI
Browse files Browse the repository at this point in the history
* fix: detect cross compiler for em++
  • Loading branch information
abeimler committed Nov 20, 2022
1 parent 9d7d2ce commit a85248e
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 44 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/ci.emscripten.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: ci-cross-emscripten
on:
pull_request:
push:
branches:
- main
- master

jobs:
Test:
if: "!contains(github.event.head_commit.message, '[ci skip]')"
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-20.04
cmake:
- true
steps:
- uses: actions/checkout@v2
- name: Cache
uses: actions/cache@v2
with:
path: |
~/vcpkg
./build/vcpkg_installed
${{ env.HOME }}/.cache/vcpkg/archives
${{ env.XDG_CACHE_HOME }}/vcpkg/archives
${{ env.LOCALAPPDATA }}\vcpkg\archives
${{ env.APPDATA }}\vcpkg\archives
key: ${{ runner.os }}-emscripten-${{ env.BUILD_TYPE }}-${{ hashFiles('**/CMakeLists.txt') }}-${{ hashFiles('./vcpkg.json')}}-${{ matrix.cmake }}
restore-keys: |
${{ runner.os }}-${{ env.BUILD_TYPE }}-
- name: Setup Cpp
uses: aminya/setup-cpp@v1
with:
cmake: ${{ matrix.cmake }}
ninja: true
vcpkg: true
conan: true
cppcheck: true
clangtidy: true
task: true
doxygen: true

- name: Setup emscripten
uses: mymindstorm/setup-emsdk@v11

- name: Build
run: |
task build_emscripten
env:
CMAKE_GENERATOR: ${{ matrix.cmake_generator }}
4 changes: 4 additions & 0 deletions .github/workflows/ci.mingw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
- ubuntu-20.04
cmake:
- true
platform:
- x64
- x86
include:
- platform: x64
cross_cc: x86_64-w64-mingw32-gcc
Expand Down Expand Up @@ -51,6 +54,7 @@ jobs:
clangtidy: true
task: true
doxygen: true
powershell: true

- name: Setup MinGW
uses: egor-tensin/setup-mingw@v2
Expand Down
5 changes: 3 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ tasks:
- docker-compose down build-llvm test-llvm

test_docker_mingw:
- docker-compose up --build minimal-build-mingw-x64
- docker-compose up --build build-mingw-x64
- docker-compose up --build build-mingw-x86
- docker-compose down build-mingw-x64 build-mingw-x86
- docker-compose down minimal-build-mingw-x64 build-mingw-x64 build-mingw-x86

test_docker_emscripten:
- docker-compose up --build build-emscripten
Expand All @@ -66,7 +67,7 @@ tasks:

build_emscripten:
cmds:
- cmake ./test_emscripten -B ./test_emscripten/build -DCMAKE_BUILD_TYPE:STRING=Debug -G '{{.CMAKE_GENERATOR | default "Ninja Multi-Config"}}' -DENABLE_CROSS_COMPILING:BOOL=ON -DCMAKE_C_COMPILER=emcc -DCMAKE_CXX_COMPILER=em++
- cmake ./test_emscripten -B ./test_emscripten/build -DCMAKE_BUILD_TYPE:STRING=Debug -G '{{.CMAKE_GENERATOR | default "Ninja Multi-Config"}}' -DENABLE_CROSS_COMPILING:BOOL=ON -DDEFAULT_TRIPLET=wasm32-emscripten
- cmake --build ./test_emscripten/build --config Debug


Expand Down
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,43 @@ services:
args:
compiler: gcc
target: build
environment:
CMAKE_ARGS: -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++
test-gcc:
build:
context: .
dockerfile: ./docker/Dockerfile
args:
compiler: gcc
target: test
environment:
CMAKE_ARGS: -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++
build-llvm:
build:
context: .
dockerfile: ./docker/Dockerfile
args:
compiler: llvm
target: build
environment:
CMAKE_ARGS: -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
test-llvm:
build:
context: .
dockerfile: ./docker/Dockerfile
args:
compiler: llvm
target: test
environment:
CMAKE_ARGS: -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
minimal-build-mingw-x64:
build:
context: .
dockerfile: ./docker/Dockerfile.mingw
target: build-minimal
environment:
- CROSS_CC=x86_64-w64-mingw32-gcc
- CROSS_CXX=x86_64-w64-mingw32-g++
build-mingw-x64:
build:
context: .
Expand Down
8 changes: 4 additions & 4 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ RUN /setup_cpp_linux --compiler $compiler --llvm true --cmake true --ninja true
#RUN touch /root/.bashrc && cat /root/.cpprc >> /root/.bashrc

# update vcpkg
WORKDIR /root/vcpkg
RUN git pull origin master
RUN ./vcpkg update
WORKDIR /
#WORKDIR /root/vcpkg
#RUN git pull origin master
#RUN ./vcpkg update
#WORKDIR /

COPY ./docker/entrypoint.sh /docker-entrypoint.sh
ENTRYPOINT [ "/docker-entrypoint.sh" ]
Expand Down
12 changes: 6 additions & 6 deletions docker/Dockerfile.emscripten
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:22.04 AS base
FROM ubuntu:20.04 AS base

ARG setup_cpp_linux_version="0.22.0"

Expand All @@ -11,14 +11,14 @@ RUN chmod +x /setup_cpp_linux
FROM base AS setup

# install cmake, ninja, and ccache
RUN /setup_cpp_linux --llvm true --cmake true --ninja true --ccache true --doxygen true --cppcheck true --vcpkg true --conan true --task true
RUN /setup_cpp_linux --cmake true --ninja true --ccache true --doxygen true --cppcheck true --vcpkg true --conan true --task true
#RUN touch /root/.bashrc && cat /root/.cpprc >> /root/.bashrc

# update vcpkg
WORKDIR /root/vcpkg
RUN git pull origin master
RUN ./vcpkg update
WORKDIR /
#WORKDIR /root/vcpkg
#RUN git pull origin master
#RUN ./vcpkg update
#WORKDIR /


# install emscripten
Expand Down
18 changes: 12 additions & 6 deletions docker/Dockerfile.mingw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:22.04 AS base
FROM ubuntu:20.04 AS base

ARG setup_cpp_linux_version="0.22.0"

Expand All @@ -11,7 +11,7 @@ RUN chmod +x /setup_cpp_linux
FROM base AS setup

# install cmake, ninja, and ccache
RUN /setup_cpp_linux --llvm true --cmake true --ninja true --ccache true --doxygen true --cppcheck true --vcpkg true --conan true --task true
RUN /setup_cpp_linux --cmake true --ninja true --ccache true --doxygen true --cppcheck true --vcpkg true --conan true --task true --powershell true
#RUN touch /root/.bashrc && cat /root/.cpprc >> /root/.bashrc

# TODO: install cross-compiler with setup_cpp_linux
Expand All @@ -21,10 +21,10 @@ RUN apt-get update && apt-get install -y \
&& rm -rf /var/lib/apt/lists/*

# update vcpkg
WORKDIR /root/vcpkg
RUN git pull origin master
RUN ./vcpkg update
WORKDIR /
#WORKDIR /root/vcpkg
#RUN git pull origin master
#RUN ./vcpkg update
#WORKDIR /

COPY ./docker/entrypoint.sh /docker-entrypoint.sh
ENTRYPOINT [ "/docker-entrypoint.sh" ]
Expand All @@ -34,3 +34,9 @@ FROM setup AS build
COPY . /home/project_options
WORKDIR /home/project_options
CMD ["/bin/bash", "-c", "task build_mingw"]


FROM setup AS build-minimal
COPY . /home/project_options
WORKDIR /home/project_options
CMD ["/bin/bash", "-c", "task build_minimal_mingw"]
76 changes: 59 additions & 17 deletions src/CrossCompiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,38 @@ macro(enable_cross_compiler)

# detect_compiler()
set(_cc ${CMAKE_C_COMPILER})
if("${_cc}" STREQUAL "")
set(_cc $ENV{CC})
endif()
set(_cxx ${CMAKE_CXX_COMPILER})
if("${_cxx}" STREQUAL "")
set(_cxx $ENV{CXX})

# detect compiler by triplet
if("${DEFAULT_TRIPLET}" STREQUAL "x64-mingw-dynamic" OR "${DEFAULT_TRIPLET}" STREQUAL "x64-mingw-static")
if("${_cc}" STREQUAL "")
set(_cc "x86_64-w64-mingw32-gcc")
endif()
if("${_cxx}" STREQUAL "")
set(_cxx "x86_64-w64-mingw32-g++")
endif()
set(TARGET_ARCHITECTURE "x64")
elseif("${DEFAULT_TRIPLET}" STREQUAL "x86-mingw-dynamic" OR "${DEFAULT_TRIPLET}" STREQUAL "x86-mingw-static")
if("${_cc}" STREQUAL "")
set(_cc "i686-w64-mingw32-gcc")
endif()
if("${_cxx}" STREQUAL "")
set(_cxx "i686-w64-mingw32-g++")
endif()
set(TARGET_ARCHITECTURE "x86")
elseif("${DEFAULT_TRIPLET}" STREQUAL "wasm32-emscripten")
set(_cc "emcc")
set(_cxx "em++")
set(TARGET_ARCHITECTURE "wasm32-emscripten")
endif()
set(CMAKE_C_COMPILER ${_cc})
set(CMAKE_CXX_COMPILER ${_cxx})

if(NOT DEFINED TARGET_ARCHITECTURE)
if("${TARGET_ARCHITECTURE}" STREQUAL "")
if("${_cc}" STREQUAL "")
set(_cc $ENV{CC})
endif()
if("${_cxx}" STREQUAL "")
set(_cxx $ENV{CXX})
endif()
if(_cc MATCHES "x86_64(-w64)?-mingw32-[gc]..?" OR _cxx MATCHES "x86_64(-w64)?-mingw32-[gc]..?")
set(TARGET_ARCHITECTURE "x64")
elseif(_cc MATCHES "i686(-w64)?-mingw32-[gc]..?" OR _cxx MATCHES "i686(-w64)?-mingw32-[gc]..?")
Expand Down Expand Up @@ -44,15 +65,24 @@ macro(enable_cross_compiler)
if(_cc MATCHES "(x86_64|i686)(-w64)?-mingw32-[gc]..?" OR _cxx MATCHES "(x86_64|i686)(-w64)?-mingw32-[gc]..?")
set(MINGW TRUE)
set(USE_CROSSCOMPILER_MINGW TRUE)
elseif(_cc MATCHES "emcc" OR _xx MATCHES "em\\+\\+")
elseif(_cc MATCHES "emcc" OR _cxx MATCHES "em\\+\\+")
set(USE_CROSSCOMPILER_EMSCRIPTEN TRUE)
endif()

set(LIBRARY_LINKAGE)
if(BUILD_SHARED_LIBS)
set(LIBRARY_LINKAGE "dynamic")
if("${TRIPLET}" STREQUAL "x64-mingw-static" OR "${TRIPLET}" STREQUAL "x86-mingw-static")
message(WARNING "cross-compiler triplet is set to 'static' but BUILD_SHARED_LIBS is enabled")
endif()
else()
set(LIBRARY_LINKAGE "static")
if("${TRIPLET}" STREQUAL "x64-mingw-dynamic" OR "${TRIPLET}" STREQUAL "x86-mingw-dynamic")
set(LIBRARY_LINKAGE "dynamic")
elseif("${TRIPLET}" STREQUAL "x64-mingw-static" OR "${TRIPLET}" STREQUAL "x86-mingw-static")
set(LIBRARY_LINKAGE "static")
else()
set(LIBRARY_LINKAGE "static")
endif()
endif()

if(NOT DEFINED CROSS_ROOT)
Expand All @@ -65,11 +95,12 @@ macro(enable_cross_compiler)
endif()

if(USE_CROSSCOMPILER_EMSCRIPTEN)
if($ENV{EMSCRIPTEN})
if(NOT "$ENV{EMSCRIPTEN}" STREQUAL "")
set(EMSCRIPTEN_ROOT $ENV{EMSCRIPTEN})
else()
if(NOT DEFINED EMSCRIPTEN_ROOT)
include(FetchContent)
message(STATUS "fetch emscripten repo. ...")
FetchContent_Declare(
emscripten
GIT_REPOSITORY https://github.com/emscripten-core/emscripten
Expand All @@ -78,14 +109,18 @@ macro(enable_cross_compiler)
FetchContent_Populate(emscripten)
set(EMSCRIPTEN_ROOT "${emscripten_SOURCE_DIR}")
endif()
if($ENV{EMSDK})
set(EMSCRIPTEN_PREFIX "$ENV{EMSDK}/upstream/emscripten")
endif()
endif()
endif()
if(NOT "$ENV{EMSDK}" STREQUAL "")
set(EMSCRIPTEN_PREFIX "$ENV{EMSDK}/upstream/emscripten")
set(EMSCRIPTEN_ROOT_PATH "$ENV{EMSDK}/upstream/emscripten")
endif()
if(NOT DEFINED CMAKE_CROSSCOMPILING_EMULATOR)
set(CMAKE_CROSSCOMPILING_EMULATOR "$ENV{EMSDK_NODE};--experimental-wasm-threads")
endif()
else()
set(CMAKE_C_COMPILER ${_cc})
set(CMAKE_CXX_COMPILER ${_cxx})
endif()

set(_toolchain_file)
Expand All @@ -94,17 +129,24 @@ macro(enable_cross_compiler)
set(CROSSCOMPILING TRUE)

message(STATUS "enable cross-compiling")
#message(STATUS "use CMAKE_C_COMPILER: ${CMAKE_C_COMPILER}")
#message(STATUS "use CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}")
if(USE_CROSSCOMPILER_MINGW)
message(STATUS "use MINGW cross-compiling")
message(STATUS "use ROOT_PATH: ${CROSS_ROOT}")
elseif(USE_CROSSCOMPILER_EMSCRIPTEN)
message(STATUS "use emscripten cross-compiling")
message(STATUS "use emscripten root: ${EMSCRIPTEN_ROOT}")
#message(STATUS "use emscripten root (path): ${EMSCRIPTEN_ROOT_PATH}")
#message(STATUS "EMSCRIPTEN: $ENV{EMSCRIPTEN}")
#message(STATUS "EMSDK_NODE: $ENV{EMSDK_NODE}")
#message(STATUS "EMSDK: $ENV{EMSDK}")
message(STATUS "use emscripten cross-compiler emulator: ${CMAKE_CROSSCOMPILING_EMULATOR}")
endif()
message(STATUS "Target Architecture: ${TARGET_ARCHITECTURE}")
if (DEFAULT_TRIPLET)
message(STATUS "Default Triplet: ${DEFAULT_TRIPLET}")
endif()
message(STATUS "Host Triplet: ${HOST_TRIPLET}")
message(STATUS "Toolchain File: ${CMAKE_TOOLCHAIN_FILE}")
endmacro()
Expand All @@ -118,14 +160,14 @@ function(get_toolchain_file value)
if("${_arch}" MATCHES "x64")
set(_arch "x86_64")
elseif("${_arch}" MATCHES "x86")
set(_arch "x86_64")
set(_arch "i686")
endif()

if(MINGW)
if(USE_CROSSCOMPILER_MINGW)
set(${value}
${ProjectOptions_SRC_DIR}/toolchains/${_arch}-w64-mingw32.toolchain.cmake
PARENT_SCOPE)
elseif(EMSCRIPTEN)
elseif(USE_CROSSCOMPILER_EMSCRIPTEN)
if(EMSCRIPTEN_ROOT)
set(${value}
${EMSCRIPTEN_ROOT}/cmake/Modules/Platform/Emscripten.cmake
Expand Down
12 changes: 5 additions & 7 deletions src/MinGW.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ include_guard()

# detect mingw
function(is_mingw value)
if(CROSSCOMPILING)
if(MINGW)
set(${value}
ON
PARENT_SCOPE)
return()
endif()
if(USE_CROSSCOMPILER_MINGW)
set(${value}
ON
PARENT_SCOPE)
return()
else()
if(NOT WIN32 OR MSVC)
set(${value}
Expand Down
Loading

0 comments on commit a85248e

Please sign in to comment.