Skip to content

Commit

Permalink
[krabby] add bazel config of sdl2
Browse files Browse the repository at this point in the history
  • Loading branch information
PikachuHy committed Feb 2, 2024
1 parent fedf1fd commit 4d39ade
Show file tree
Hide file tree
Showing 8 changed files with 312 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ build:macos --define client_type=macos
build:macos --cxxopt=-std=c++20 --host_cxxopt=-std=c++20
build:android --define client_type=android
common --noenable_bzlmod
build:wasm --copt=-Wno-unused-variable --copt=-Wno-unused-but-set-variable --copt=-Wno-unused-function
build:wasm --copt=-Wno-int-conversion
build:wasm --copt=-Wno-incompatible-pointer-types
7 changes: 7 additions & 0 deletions .github/workflows/macos_bazel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,10 @@ jobs:
with:
name: macos-pscm-wasm
path: bazel-bin/wasm/

- name: Build krabby
run: bazel build //project/krabby:krabby -c opt

- name: Build krabby wasm
run: bazel build //project/krabby:krabby-wasm -c opt --config=wasm

114 changes: 114 additions & 0 deletions 3rd/freetype.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

licenses(["notice"])

# We can't just glob *.c, since Freetype has .c files that include other .c
# files all over the place. In order to simplify the process of listing them
# manually, we patch out the non-TrueType drivers from the configuration file,
# since the only font we ship is TrueType.
sources = [
"src/autofit/afangles.c",
"src/autofit/afblue.c",
"src/autofit/afcjk.c",
"src/autofit/afdummy.c",
"src/autofit/afglobal.c",
"src/autofit/afhints.c",
"src/autofit/afindic.c",
"src/autofit/aflatin.c",
"src/autofit/afloader.c",
"src/autofit/afmodule.c",
"src/autofit/afpic.c",
"src/autofit/afranges.c",
"src/autofit/afshaper.c",
"src/autofit/afwarp.c",
"src/base/basepic.c",
"src/base/ftadvanc.c",
"src/base/ftapi.c",
"src/base/ftbbox.c",
"src/base/ftbdf.c",
"src/base/ftbitmap.c",
"src/base/ftcalc.c",
"src/base/ftcid.c",
"src/base/ftdbgmem.c",
"src/base/ftdebug.c",
"src/base/ftfntfmt.c",
"src/base/ftfstype.c",
"src/base/ftgasp.c",
"src/base/ftgloadr.c",
"src/base/ftglyph.c",
"src/base/ftgxval.c",
"src/base/fthash.c",
"src/base/ftinit.c",
"src/base/ftlcdfil.c",
"src/base/ftmm.c",
"src/base/ftobjs.c",
"src/base/ftotval.c",
"src/base/ftoutln.c",
"src/base/ftpatent.c",
"src/base/ftpfr.c",
"src/base/ftpic.c",
"src/base/ftrfork.c",
"src/base/ftsnames.c",
"src/base/ftstream.c",
"src/base/ftstroke.c",
"src/base/ftsynth.c",
"src/base/ftsystem.c",
"src/base/fttrigon.c",
"src/base/fttype1.c",
"src/base/ftutil.c",
"src/base/ftwinfnt.c",
"src/gzip/ftgzip.c",
"src/raster/ftraster.c",
"src/raster/ftrend1.c",
"src/raster/rastpic.c",
"src/sfnt/pngshim.c",
"src/sfnt/sfdriver.c",
"src/sfnt/sfntpic.c",
"src/sfnt/sfobjs.c",
"src/sfnt/ttbdf.c",
"src/sfnt/ttcmap.c",
"src/sfnt/ttkern.c",
"src/sfnt/ttload.c",
"src/sfnt/ttmtx.c",
"src/sfnt/ttpost.c",
"src/sfnt/ttsbit.c",
"src/smooth/ftgrays.c",
"src/smooth/ftsmooth.c",
"src/smooth/ftspic.c",
"src/truetype/ttdriver.c",
"src/truetype/ttgload.c",
"src/truetype/ttgxvar.c",
"src/truetype/ttinterp.c",
"src/truetype/ttobjs.c",
"src/truetype/ttpic.c",
"src/truetype/ttpload.c",
"src/truetype/ttsubpix.c",
]

cc_library(
name = "freetype",
srcs = sources,
hdrs = glob([
"src/**/*.h",
"include/**/*.h",
]),
copts = [
"-DFT2_BUILD_LIBRARY",
"-UDEBUG",
],
includes = ["include"],
textual_hdrs = glob(["src/**/*.c"]),
visibility = ["//visibility:public"],
)
26 changes: 26 additions & 0 deletions 3rd/freetype_config.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
diff --git a/include/freetype/config/ftmodule.h b/include/freetype/config/ftmodule.h
index 76d271a..c5950fe 100644
--- a/include/freetype/config/ftmodule.h
+++ b/include/freetype/config/ftmodule.h
@@ -12,21 +12,10 @@

FT_USE_MODULE( FT_Module_Class, autofit_module_class )
FT_USE_MODULE( FT_Driver_ClassRec, tt_driver_class )
-FT_USE_MODULE( FT_Driver_ClassRec, t1_driver_class )
-FT_USE_MODULE( FT_Driver_ClassRec, cff_driver_class )
-FT_USE_MODULE( FT_Driver_ClassRec, t1cid_driver_class )
-FT_USE_MODULE( FT_Driver_ClassRec, pfr_driver_class )
-FT_USE_MODULE( FT_Driver_ClassRec, t42_driver_class )
-FT_USE_MODULE( FT_Driver_ClassRec, winfnt_driver_class )
-FT_USE_MODULE( FT_Driver_ClassRec, pcf_driver_class )
-FT_USE_MODULE( FT_Module_Class, psaux_module_class )
-FT_USE_MODULE( FT_Module_Class, psnames_module_class )
-FT_USE_MODULE( FT_Module_Class, pshinter_module_class )
FT_USE_MODULE( FT_Renderer_Class, ft_raster1_renderer_class )
FT_USE_MODULE( FT_Module_Class, sfnt_module_class )
FT_USE_MODULE( FT_Renderer_Class, ft_smooth_renderer_class )
FT_USE_MODULE( FT_Renderer_Class, ft_smooth_lcd_renderer_class )
FT_USE_MODULE( FT_Renderer_Class, ft_smooth_lcdv_renderer_class )
-FT_USE_MODULE( FT_Driver_ClassRec, bdf_driver_class )

/* EOF */
80 changes: 80 additions & 0 deletions 3rd/sdl2.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

licenses(["notice"])

# Common include paths
sdl_includes = [
"include",
"src/video/khronos",
]

objc_library(
name = "sdl2_objc",
srcs = glob([
"src/**/*.h",
"include/*.h",
]),
includes = sdl_includes,
non_arc_srcs = glob([
"src/audio/coreaudio/*.m",
"src/file/cocoa/*.m",
"src/filesystem/cocoa/*.m",
"src/render/metal/*.m",
"src/video/cocoa/*.m",
]),
sdk_frameworks = [
"AudioToolbox",
"Carbon",
"CoreAudio",
"CoreVideo",
"Cocoa",
"ForceFeedback",
"IOKit",
"OpenGL",
"Metal",
],
alwayslink = 1,
)

sdl_srcs = glob(
include = [
"src/**/*.c",
"src/**/*.h",
],
exclude = [
"src/video/qnx/**",
"src/haptic/windows/**",
"src/test/*.c",
"src/thread/generic/*.c",
"src/core/linux/*.c",
],
)

# In general, we bundle SDL2. However, on Linux that would require a header
# generated by --configure, and a lot of buildflags and installing dev headers
# is in general less painful, so we just link it dynamically.
cc_library(
name = "sdl2",
srcs = sdl_srcs,
hdrs = glob(["include/*.h"]),
copts = ["-Wno-error"],
includes = sdl_includes,
textual_hdrs = glob(["src/thread/generic/*.c"]),
visibility = ["//visibility:public"],
deps = select({
"@platforms//cpu:wasm32": [],
"//conditions:default": [":sdl2_objc"],
}),
)
26 changes: 26 additions & 0 deletions 3rd/sdl2_ttf.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cc_library(
name = "sdl2_ttf",
srcs = ["SDL_ttf.c"],
hdrs = ["SDL_ttf.h"],
copts = ["-Wno-error"],
includes = ["."],
visibility = ["//visibility:public"],
deps = [
"@freetype",
"@sdl2",
],
)
27 changes: 27 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,30 @@ maybe(
"https://github.com/facebook/zstd/releases/download/v1.5.2/zstd-1.5.2.tar.gz",
],
)

http_archive(
name = "sdl2",
build_file = "@dev_pscm//3rd:sdl2.BUILD",
sha256 = "e6a7c71154c3001e318ba7ed4b98582de72ff970aca05abc9f45f7cbdc9088cb",
strip_prefix = "SDL2-2.0.8",
urls = ["https://www.libsdl.org/release/SDL2-2.0.8.zip"],
)

http_archive(
name = "sdl2_ttf",
build_file = "@dev_pscm//3rd:sdl2_ttf.BUILD",
sha256 = "ad7a7d2562c19ad2b71fa4ab2e76f9f52b3ee98096c0a7d7efbafc2617073c27",
strip_prefix = "SDL2_ttf-2.0.14",
urls = ["https://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-2.0.14.zip"],
)

http_archive(
name = "freetype",
build_file = "@dev_pscm//3rd:freetype.BUILD",
# We patch out some modules we don't use from freetype config file.
patch_args = ["-p1"],
patches = ["@dev_pscm//3rd:freetype_config.patch"],
sha256 = "bf380e4d7c4f3b5b1c1a7b2bf3abb967bda5e9ab480d0df656e0e08c5019c5e6",
strip_prefix = "freetype-2.9",
urls = ["https://download.savannah.gnu.org/releases/freetype/freetype-2.9.tar.gz"],
)
44 changes: 29 additions & 15 deletions project/krabby/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")

# FIXME: -sUSE_SDL=2 -sUSE_SDL_TTF=2 not work with bazel
# cc_binary(
# name = "krabby",
# srcs = ["src/main.cpp"],
# copts = [
# "-std=c++20",
# ],
# )

# wasm_cc_binary(
# name = "krabby-wasm",
# cc_target = ":krabby",
# outputs = [
# "krabby.js",
# "krabby.wasm",
# ],
# )
filegroup(
name = "asset",
srcs = glob([
"asset/font/*",
"asset/audio/*",
]),
)

cc_binary(
name = "krabby",
srcs = ["src/main.cpp"],
additional_linker_inputs = [":asset"],
copts = [
"-std=c++20",
],
data = [":asset"],
# linkopts = ["--preload-file $(locations asset)"],
deps = ["@sdl2_ttf"],
)

wasm_cc_binary(
name = "krabby-wasm",
cc_target = ":krabby",
outputs = [
"krabby.js",
"krabby.wasm",
],
threads = "emscripten",
)

0 comments on commit 4d39ade

Please sign in to comment.