Skip to content

Commit 2156705

Browse files
committed
try to use vcpkg for the emscripten build
1 parent 27e40a8 commit 2156705

File tree

3 files changed

+164
-92
lines changed

3 files changed

+164
-92
lines changed

CMakeLists.txt

Lines changed: 88 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,119 +3,127 @@ project(oopetris)
33

44
set(CMAKE_CXX_STANDARD 23)
55

6+
set(TARGET_LIST oopetris)
7+
8+
if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
9+
set(CMAKE_HAVE_PTHREAD_H 1)
10+
endif()
11+
612
find_package(argparse CONFIG REQUIRED)
7-
find_package(SDL2 CONFIG REQUIRED)
8-
find_package(SDL2_ttf CONFIG REQUIRED)
9-
find_package(SDL2_mixer CONFIG REQUIRED)
1013
find_package(spdlog CONFIG REQUIRED)
1114
find_package(nlohmann_json CONFIG REQUIRED)
1215
find_package(magic_enum CONFIG REQUIRED)
1316
find_package(tl-optional CONFIG REQUIRED)
1417
find_package(tl-expected CONFIG REQUIRED)
1518

16-
set(TARGET_LIST oopetris SDL2::SDL2-static)
19+
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
20+
find_package(SDL2 CONFIG REQUIRED)
21+
find_package(SDL2_ttf CONFIG REQUIRED)
22+
find_package(SDL2_mixer CONFIG REQUIRED)
23+
set(TARGET_LIST oopetris SDL2::SDL2-static)
24+
endif()
1725

1826
add_executable(oopetris
19-
src/application.cpp
20-
src/application.hpp
21-
src/bag.cpp
22-
src/bag.hpp
23-
src/clock_source.cpp
24-
src/clock_source.hpp
25-
src/color.hpp
26-
src/command_line_arguments.hpp
27-
src/constants.hpp
28-
src/controls.hpp
29-
src/event_dispatcher.cpp
30-
src/event_dispatcher.hpp
31-
src/event_listener.hpp
32-
src/font.cpp
33-
src/font.hpp
34-
src/grid.cpp
35-
src/grid.hpp
36-
src/input_event.hpp
37-
src/input.cpp
38-
src/input.hpp
39-
src/key_codes.hpp
40-
src/magic_enum_wrapper.hpp
41-
src/main.cpp
42-
src/mino_stack.cpp
43-
src/mino_stack.hpp
44-
src/mino.cpp
45-
src/mino.hpp
46-
src/music_manager.cpp
47-
src/point.hpp
48-
src/random.cpp
49-
src/random.hpp
50-
src/recording.hpp
51-
src/rect.hpp
52-
src/renderer.cpp
53-
src/renderer.hpp
54-
src/sdl_context.cpp
55-
src/sdl_context.hpp
56-
src/settings.hpp
57-
src/static_string.hpp
58-
src/tetrion_snapshot.cpp
59-
src/tetrion_snapshot.hpp
60-
src/tetrion.cpp
61-
src/tetrion.hpp
62-
src/tetris_application.cpp
63-
src/tetris_application.hpp
64-
src/tetromino_type.cpp
65-
src/tetromino_type.hpp
66-
src/tetromino.hpp
67-
src/text.cpp
68-
src/text.hpp
69-
src/types.hpp
70-
src/utils.cpp
71-
src/utils.hpp
72-
src/window.cpp
73-
src/window.hpp
74-
)
27+
src/application.cpp
28+
src/application.hpp
29+
src/bag.cpp
30+
src/bag.hpp
31+
src/clock_source.cpp
32+
src/clock_source.hpp
33+
src/color.hpp
34+
src/command_line_arguments.hpp
35+
src/constants.hpp
36+
src/controls.hpp
37+
src/event_dispatcher.cpp
38+
src/event_dispatcher.hpp
39+
src/event_listener.hpp
40+
src/font.cpp
41+
src/font.hpp
42+
src/grid.cpp
43+
src/grid.hpp
44+
src/input_event.hpp
45+
src/input.cpp
46+
src/input.hpp
47+
src/key_codes.hpp
48+
src/magic_enum_wrapper.hpp
49+
src/main.cpp
50+
src/mino_stack.cpp
51+
src/mino_stack.hpp
52+
src/mino.cpp
53+
src/mino.hpp
54+
src/music_manager.cpp
55+
src/point.hpp
56+
src/random.cpp
57+
src/random.hpp
58+
src/recording.hpp
59+
src/rect.hpp
60+
src/renderer.cpp
61+
src/renderer.hpp
62+
src/sdl_context.cpp
63+
src/sdl_context.hpp
64+
src/settings.hpp
65+
src/static_string.hpp
66+
src/tetrion_snapshot.cpp
67+
src/tetrion_snapshot.hpp
68+
src/tetrion.cpp
69+
src/tetrion.hpp
70+
src/tetris_application.cpp
71+
src/tetris_application.hpp
72+
src/tetromino_type.cpp
73+
src/tetromino_type.hpp
74+
src/tetromino.hpp
75+
src/text.cpp
76+
src/text.hpp
77+
src/types.hpp
78+
src/utils.cpp
79+
src/utils.hpp
80+
src/window.cpp
81+
src/window.hpp
82+
)
7583

76-
foreach (target ${TARGET_LIST})
84+
foreach(target ${TARGET_LIST})
7785
# set warning levels
78-
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
86+
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
7987
message("MSVC build")
80-
if (CMAKE_BUILD_TYPE STREQUAL "Release")
88+
89+
if(CMAKE_BUILD_TYPE STREQUAL "Release")
8190
target_compile_options(${target} INTERFACE /W4 /WX /permissive-)
82-
else ()
91+
else()
8392
target_compile_options(${target} INTERFACE /W4 /WX /permissive-)
84-
endif ()
85-
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
93+
endif()
94+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
8695
message("GCC build")
87-
if (CMAKE_BUILD_TYPE STREQUAL "Release")
96+
97+
if(CMAKE_BUILD_TYPE STREQUAL "Release")
8898
target_compile_options(${target} INTERFACE -Wall -Wextra -Werror -pedantic -Wconversion)
89-
else ()
99+
else()
90100
target_compile_options(${target} INTERFACE -Wall -Wextra -Werror -pedantic -Wconversion)
91-
endif ()
92-
endif ()
101+
endif()
102+
endif()
93103

94104
# define DEBUG_BUILD
95105
target_compile_definitions(${target} INTERFACE "$<$<CONFIG:DEBUG>:DEBUG_BUILD>")
96106

97107
# static runtime library
98108
set_property(TARGET ${target} PROPERTY
99-
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
100-
endforeach ()
109+
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
110+
endforeach()
101111

102-
target_link_libraries(oopetris
112+
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
113+
target_link_libraries(oopetris
103114
PRIVATE
104115
$<TARGET_NAME_IF_EXISTS:SDL2::SDL2main>
105116
$<IF:$<TARGET_EXISTS:SDL2::SDL2>,SDL2::SDL2,SDL2::SDL2-static>
106-
)
117+
)
118+
target_link_libraries(oopetris PRIVATE $<IF:$<TARGET_EXISTS:SDL2_ttf::SDL2_ttf>,SDL2_ttf::SDL2_ttf,SDL2_ttf::SDL2_ttf-static>)
119+
target_link_libraries(oopetris PRIVATE $<IF:$<TARGET_EXISTS:SDL2_mixer::SDL2_mixer>,SDL2_mixer::SDL2_mixer,SDL2_mixer::SDL2_mixer-static>)
120+
endif()
107121

108122
target_link_libraries(oopetris PRIVATE argparse::argparse)
109123
target_link_libraries(oopetris PRIVATE spdlog::spdlog spdlog::spdlog_header_only)
110124
target_link_libraries(oopetris PRIVATE nlohmann_json::nlohmann_json)
111125
target_link_libraries(oopetris PRIVATE magic_enum::magic_enum)
112-
target_link_libraries(oopetris PRIVATE $<IF:$<TARGET_EXISTS:SDL2_ttf::SDL2_ttf>,SDL2_ttf::SDL2_ttf,SDL2_ttf::SDL2_ttf-static>)
113-
target_link_libraries(oopetris PRIVATE $<IF:$<TARGET_EXISTS:SDL2_mixer::SDL2_mixer>,SDL2_mixer::SDL2_mixer,SDL2_mixer::SDL2_mixer-static>)
114126
target_link_libraries(oopetris PRIVATE tl::optional)
115127
target_link_libraries(oopetris PRIVATE tl::expected)
116128

117129
target_compile_definitions(oopetris PUBLIC AUDIO_WITH_FLAC_SUPPORT AUDIO_WITH_MP3_SUPPORT AUDIO_PREFER_FLAC)
118-
119-
# static runtime library
120-
set_property(TARGET ${target} PROPERTY
121-
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

android/run-web.sh

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@ if [ ! -d "emsdk" ]; then
99
./emsdk activate latest
1010

1111
fi
12-
source "./emsdk/emsdk_env.sh"
12+
source "./emsdk/emsdk_env.sh" >/dev/null
1313

1414
export BUILD_DIR="build-emcc"
1515

1616
export CC=emcc
1717
export CXX=em++
18-
# export LD=llvm-ld
19-
# export AS=llvm-as
2018
export AR=emar
21-
# export RANLIB=llvm-ranlib
22-
# export STRIP=llvm-strip
19+
export RANLIB=emranlib
20+
export STRIP=emstrip
2321
unset PKG_CONFIG
2422

25-
cat <<EOF >"./android/crossbuilt-web.ini"
23+
export BUILD_SYSTEM="cmake"
24+
25+
if [ $BUILD_SYSTEM = "meson" ]; then
26+
27+
cat <<EOF >"./android/crossbuilt-web.ini"
2628
[host_machine]
2729
system = 'emscripten'
2830
cpu_family = 'wasm32'
@@ -53,10 +55,41 @@ cpp_std = 'c++20'
5355
5456
EOF
5557

56-
meson setup "$BUILD_DIR" \
57-
--cross-file "./android/crossbuilt-web.ini" \
58-
-Dcpp_args=-DAUDIO_PREFER_MP3 \
59-
-Ddefault_library=static \
60-
-Dfreetype2:zlib=disabled
58+
meson setup "$BUILD_DIR" \
59+
--cross-file "./android/crossbuilt-web.ini" \
60+
-Dcpp_args=-DAUDIO_PREFER_MP3 \
61+
-Ddefault_library=static \
62+
-Dfreetype2:zlib=disabled
63+
64+
meson compile -C "$BUILD_DIR"
65+
66+
else
67+
68+
if [ ! -d "vcpkg" ]; then
69+
70+
git clone https://github.com/Microsoft/vcpkg.git
71+
./vcpkg/bootstrap-vcpkg.sh
72+
73+
fi
74+
75+
export EMSCRIPTEN_ARGS=("USE_PTHREADS=1" "USE_SDL=2" "USE_SDL_MIXER=2" "USE_VORBIS=1" "USE_LIBPNG" "USE_MPG123=1" "USE_ZLIB=1" "SDL2_MIXER_FORMATS=['wav','flac','mp3']" "USE_SDL_TTF=2")
6176

62-
meson compile -C "$BUILD_DIR"
77+
export C_FLAGS=""
78+
79+
for ARG in ${EMSCRIPTEN_ARGS[@]}; do
80+
81+
export C_FLAGS="$C_FLAGS -s \"$ARG\" "
82+
done
83+
84+
export C_FLAGS="$C_FLAGS -pthread"
85+
86+
emcmake cmake -B "$BUILD_DIR" -DCMAKE_BUILD_TYPE=Release \
87+
-DCMAKE_TOOLCHAIN_FILE=./vcpkg/scripts/buildsystems/vcpkg.cmake \
88+
-DVCPKG_MANIFEST_DIR=android \
89+
"-DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=$PWD/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake" \
90+
"-DVCPKG_CXX_FLAGS=$C_FLAGS" "-DVCPKG_C_FLAGS=$C_FLAGS" \
91+
"-DCMAKE_CXX_FLAGS=$C_FLAGS" "-DCMAKE_C_FLAGS=$C_FLAGS"
92+
93+
cmake --build "$BUILD_DIR" -j 1 --config Release
94+
95+
fi

android/vcpkg.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "oopetris-web",
3+
"version-string": "1.0.0",
4+
"builtin-baseline": "ffe7360e0f5ba62f98b620575294ecb9d8a02980",
5+
"dependencies": [
6+
{
7+
"name": "tl-optional",
8+
"version>=": "2021-05-02"
9+
},
10+
{
11+
"name": "nlohmann-json",
12+
"version>=": "3.11.2"
13+
},
14+
{
15+
"name": "magic-enum",
16+
"version>=": "0.8.2"
17+
},
18+
{
19+
"name": "spdlog",
20+
"version>=": "1.11.0"
21+
},
22+
{
23+
"name": "argparse",
24+
"version>=": "2.9"
25+
},
26+
{
27+
"name": "tl-expected",
28+
"version>=": "2022-11-24"
29+
}
30+
]
31+
}

0 commit comments

Comments
 (0)