Skip to content

Commit 460814a

Browse files
committed
feat: include .so into lib folder (linux)
1 parent 82d60e5 commit 460814a

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

CMakeLists.txt

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ if(APPLE)
101101

102102
# Chercher les libs à runtime dans Contents/Frameworks
103103
set(CMAKE_MACOSX_RPATH ON)
104-
set_target_properties(mario_isn PROPERTIES
104+
set_target_properties(mario_isn PROPERTIES
105105
BUILD_RPATH "@executable_path/../Frameworks"
106106
INSTALL_RPATH "@executable_path/../Frameworks"
107107
)
@@ -121,15 +121,15 @@ if(APPLE)
121121
$<TARGET_BUNDLE_CONTENT_DIR:mario_isn>/Resources/bin
122122
)
123123

124-
# --- Copie/réécriture des dylibs dans Contents/Frameworks via fixup_bundle ---
124+
# --- Copie/réécriture des dylibs dans Contents/Frameworks via macos_fixup ---
125125
add_custom_command(TARGET mario_isn POST_BUILD
126126
COMMAND ${CMAKE_COMMAND} -E make_directory
127127
$<TARGET_BUNDLE_CONTENT_DIR:mario_isn>/Frameworks
128128
# On passe au script: l'app et une liste de dossiers où chercher les .dylib
129129
COMMAND ${CMAKE_COMMAND}
130130
-DAPP="$<TARGET_BUNDLE_DIR:mario_isn>"
131131
-DSEARCH_DIRS="$<TARGET_FILE_DIR:SDL2::SDL2>;$<TARGET_FILE_DIR:SDL2_image::SDL2_image>;$<TARGET_FILE_DIR:SDL2_mixer::SDL2_mixer>;/opt/homebrew/lib;/opt/homebrew/opt"
132-
-P "${CMAKE_SOURCE_DIR}/cmake/fixup_bundle.cmake"
132+
-P "${CMAKE_SOURCE_DIR}/cmake/macos_fixup.cmake"
133133
)
134134

135135
add_custom_command(TARGET mario_isn POST_BUILD
@@ -152,6 +152,12 @@ elseif(WIN32)
152152
)
153153

154154
elseif(UNIX)
155+
# --- RPATH: l’exe cherchera d’abord ses libs dans ./lib (relatif à l’exe) ---
156+
set_target_properties(mario_isn PROPERTIES
157+
BUILD_RPATH "\$ORIGIN/lib"
158+
INSTALL_RPATH "\$ORIGIN/lib"
159+
)
160+
155161
# On Linux, copy assets next to the executable
156162
add_custom_command(TARGET mario_isn POST_BUILD
157163
COMMAND ${CMAKE_COMMAND} -E copy_directory
@@ -164,4 +170,20 @@ elseif(UNIX)
164170
${CMAKE_SOURCE_DIR}/ressources/bin
165171
$<TARGET_FILE_DIR:mario_isn>/bin
166172
)
173+
174+
# --- Post-build : créer ./lib et copier les .so nécessaires ---
175+
# Cas 1 : on a des cibles importées (CONFIG) -> on peut copier directement
176+
if (TARGET SDL2::SDL2 AND TARGET SDL2_image::SDL2_image AND TARGET SDL2_mixer::SDL2_mixer)
177+
add_custom_command(TARGET mario_isn POST_BUILD
178+
COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:mario_isn>/lib
179+
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:SDL2::SDL2> $<TARGET_FILE_DIR:mario_isn>/lib/ || true
180+
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:SDL2_image::SDL2_image> $<TARGET_FILE_DIR:mario_isn>/lib/ || true
181+
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:SDL2_mixer::SDL2_mixer> $<TARGET_FILE_DIR:mario_isn>/lib/ || true
182+
)
183+
else()
184+
# Cas 2 : fallback pkg-config -> on scanne l’exe avec ldd et on copie les libs utiles
185+
add_custom_command(TARGET mario_isn POST_BUILD
186+
COMMAND ${CMAKE_COMMAND} -DAPP_DIR="$<TARGET_FILE_DIR:mario_isn>" -P "${CMAKE_SOURCE_DIR}/cmake/linux_fixup.cmake"
187+
)
188+
endif()
167189
endif()

cmake/linux_fixup.cmake

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# cmake/linux_fixup.cmake
2+
cmake_minimum_required(VERSION 3.20)
3+
4+
if(NOT DEFINED APP_DIR)
5+
message(FATAL_ERROR "linux_fixup: APP_DIR not set")
6+
endif()
7+
8+
# Dossier de sortie des .so embarqués
9+
set(outdir "${APP_DIR}/lib")
10+
file(MAKE_DIRECTORY "${outdir}")
11+
12+
# Exécutable principal
13+
set(exe "${APP_DIR}/mario_isn")
14+
15+
# Liste de libs qu’on veut embarquer (SDL2 + deps image/son courantes)
16+
# Ajustable si besoin.
17+
set(regex "lib(SDL2(_image|_mixer)?|png|jpeg|jxl|webp|sharpyuv|tiff|ogg|vorbis|opus|FLAC)\\.so")
18+
19+
# Utilise ldd pour trouver les .so résolus et copie ceux qui matchent
20+
# (on passe par /bin/sh pour utiliser grep/awk/xargs proprement)
21+
execute_process(
22+
COMMAND /bin/sh -lc
23+
"ldd \"${exe}\" \
24+
| awk '/=>/ {print $3}' \
25+
| grep -E '${regex}' \
26+
| xargs -r -I{} cp -n {} \"${outdir}\""
27+
RESULT_VARIABLE rc
28+
)
29+
30+
if(NOT rc EQUAL 0)
31+
message(WARNING "linux_fixup: ldd copy step returned ${rc}")
32+
endif()
File renamed without changes.

0 commit comments

Comments
 (0)