Skip to content

Commit

Permalink
Updated to use wrap to build a static linked program if no pre-built …
Browse files Browse the repository at this point in the history
…dependency found.
  • Loading branch information
AlynxZhou committed Jun 6, 2021
1 parent 10cce34 commit 88a8a17
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 58 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ Just download file with `win` in its name from [lastest release page](https://gi

### From Source

Meson has a tool called wrap that can download and compile dependencies automatically on Windows, but SDL2 is always failed to build. So I cannot make a static linked program.
**NOTICE**: I saw a windows user says "This program has dlls in its folder so it's not simple!" and I got angry. This user knows nothing about compiling, linking and loading. It might be not so easy for some windows users to understand how complicated building static libraries is and what dynamically libraries are. Windows is a horrible platform for developers: no package manager for easy distribution, slowly visual studio, complicated tool chains. But thanks to Meson which handles all dirty things for me, it's SDL2 wrap works now and I managed to tweak it to build a static linked program automatically if no pre-built dependency found.

#### With Meson (Recommended)

1. Install Meson, Ninja, Visual Studio.
2. Download SDL2 and SDL2_ttf devel files and extract and rename. Please refer to [`deps/README.md`](deps/README.md) for links.
2. Create a prefix directory, for example `d:/flipclock-prefix`, program files will be installed into it.
3. Open `x64 Native Tools Command Prompt for VS 2019` from Start Menu, or other architectures you need.
4. Change dir to where you put this project. Run `mkdir build && cd build && meson setup --prefix=d:/ --buildtype=release . .. && meson compile && meson install`. You can change prefix to other path, but you need to use UNIX style slash instead of backslash because it's escape character in C.
5. Go to `flipclock` dir under your prefix, you can now find `flipclock.scr` and right click it to install it as a screensaver.
4. Change dir to where you put this project. Run `mkdir build && cd build && meson setup --prefix=d:/flipclock-prefix --buildtype=release . .. && meson compile && meson install`. You can change prefix argument to other path you created in Step 2, but you need to use UNIX style slash instead of backslash because it's escape character in C.
5. Go to `flipclock` dir under your prefix directory, you can now find `flipclock.scr` and right click it to install it as a screensaver.

#### With CMake

Expand Down
2 changes: 0 additions & 2 deletions dists/COPYING
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ FlipClock is distributed under the terms of the Apache-2.0 License.

FlipClock depends on SDL2 and SDL2_ttf, which are distributed under the terms of the Zlib License.

SDL2_ttf depends on Zlib, which is distributed under the terms of the Zlib License.

SDL2_ttf also depends on FreeType, which is distributed under the terms of the FreeType License.

flipclock.ttf is modified from GNU FreeSans, which is distributed under the terms of the GNU General Public License.
Expand Down
64 changes: 12 additions & 52 deletions meson.build
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# If we want to build static program with wrap on Windows,
# We need to set `default_library=static` here
# instead of setting it for each dependencies.
# Because though FlipClock does not link against freetype2 directly,
# sdl2_ttf needs to link against freetype2,
# and if we only pass `default_library=static` to sdl2_ttf,
# it still links against freetype2 dynamically.
project(
'flipclock',
'c',
version: '2.8.1',
license: 'Apache-2.0',
default_options: ['c_std=c11']
default_options: ['c_std=c11', 'default_library=static']
)

cc = meson.get_compiler('c')
Expand Down Expand Up @@ -32,41 +39,10 @@ sources = files(

dependencies = []
include_directories = []
if host_machine.system() == 'linux'
# Just simply use pkg-config.
sdl2 = dependency('sdl2', required: true)
sdl2_ttf = dependency('SDL2_ttf', required: true)
dependencies += [sdl2, sdl2_ttf]
# Deps from pkg-config don't need to set include_directories explicitly.
elif host_machine.system() == 'windows'
# I failed to build static SDL2 on Windows,
# so I just find downloaded prebuild libraries.
vc_path = 'x86'
if host_machine.cpu_family() == 'x86_64'
vc_path = 'x64'
endif
# Need absolute path for find_library().
sdl2 = cc.find_library(
'SDL2',
dirs: [meson.project_source_root() / 'deps' / 'SDL2' / 'lib' / vc_path],
required: true
)
sdl2main = cc.find_library(
'SDL2main',
dirs: [meson.project_source_root() / 'deps' / 'SDL2' / 'lib' / vc_path],
required: true
)
sdl2_ttf = cc.find_library(
'SDL2_ttf',
dirs: [meson.project_source_root() / 'deps' / 'SDL2_ttf' / 'lib' / vc_path],
required: true
)
# But relative path is acceptable for include_directories().
sdl2_inc = include_directories('deps' / 'SDL2' / 'include')
sdl2_ttf_inc = include_directories('deps' / 'SDL2_ttf' / 'include')
dependencies += [sdl2, sdl2main, sdl2_ttf]
include_directories += [sdl2_inc, sdl2_ttf_inc]
endif
# Wrap files will be used as fallback.
sdl2 = dependency('sdl2', required: true)
sdl2_ttf = dependency('SDL2_ttf', required: true)
dependencies += [sdl2, sdl2_ttf]

if host_machine.system() == 'linux'
executable(
Expand Down Expand Up @@ -138,20 +114,4 @@ elif host_machine.system() == 'windows'
'dists' / '请先读我.txt',
install_dir: get_option('prefix') / meson.project_name()
)
install_data(
'deps' / 'SDL2' / 'lib' / vc_path / 'SDL2.dll',
install_dir: get_option('prefix') / meson.project_name()
)
install_data(
'deps' / 'SDL2_ttf' / 'lib' / vc_path / 'SDL2_ttf.dll',
install_dir: get_option('prefix') / meson.project_name()
)
install_data(
'deps' / 'SDL2_ttf' / 'lib' / vc_path / 'libfreetype-6.dll',
install_dir: get_option('prefix') / meson.project_name()
)
install_data(
'deps' / 'SDL2_ttf' / 'lib' / vc_path / 'zlib1.dll',
install_dir: get_option('prefix') / meson.project_name()
)
endif
11 changes: 11 additions & 0 deletions subprojects/freetype2.wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[wrap-file]
directory = freetype-2.9.1
source_url = https://download.savannah.gnu.org/releases/freetype/freetype-2.9.1.tar.gz
source_filename = freetype-2.9.1.tar.gz
source_hash = ec391504e55498adceb30baceebd147a6e963f636eb617424bcfc47a169898ce
patch_url = https://wrapdb.mesonbuild.com/v1/projects/freetype2/2.9.1/2/get_zip
patch_filename = freetype2-2.9.1-2-wrap.zip
patch_hash = eb47e263df3f69281f96011b16442e7a1352ed5a241c1bdda2d9a86be71cf578

[provide]
freetype2 = freetype_dep
11 changes: 11 additions & 0 deletions subprojects/sdl2.wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[wrap-file]
directory = SDL2-2.0.12
source_url = https://www.libsdl.org/release/SDL2-2.0.12.tar.gz
source_filename = SDL2-2.0.12.tar.gz
source_hash = 349268f695c02efbc9b9148a70b85e58cefbbf704abd3e91be654db7f1e2c863
patch_url = https://wrapdb.mesonbuild.com/v1/projects/sdl2/2.0.12/2/get_zip
patch_filename = sdl2-2.0.12-2-wrap.zip
patch_hash = 50a1f974c4521f16f002a6c14d791d905727ba99927037587e6789750eecbac8

[provide]
sdl2 = sdl2_dep
11 changes: 11 additions & 0 deletions subprojects/sdl2_ttf.wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[wrap-file]
directory = SDL2_ttf-2.0.12
source_url = https://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-2.0.12.zip
source_filename = SDL2_ttf-2.0.12.zip
source_hash = eb909b4e5717b86e87dad7ee15adbbc5f26a33bd2dc22b93a77517bb0ba7fec8
patch_url = https://wrapdb.mesonbuild.com/v1/projects/sdl2_ttf/2.0.12/3/get_zip
patch_filename = sdl2_ttf-2.0.12-3-wrap.zip
patch_hash = 1113ee5af044f3549daa62047af4a3cb7ba2330acbd78932f4e9d206170073f4

[provide]
sdl2_ttf = sdl2_ttf_dep

0 comments on commit 88a8a17

Please sign in to comment.