@@ -149,98 +149,73 @@ install_deps_linux() {
149149 return 0
150150}
151151
152- # Generate CGO_LDFLAGS
153- # Note: Even though compression is disabled, RocksDB's compressed_secondary_cache still needs bzip2 and zlib
152+ # Find static library file by checking common paths
153+ # Arguments: library name (e.g., "bz2" or "z"), library directory
154+ # Returns: full path to static library if found, empty string otherwise
155+ find_static_library () {
156+ local lib_name=$1
157+ local lib_dir=$2
158+ local static_paths=(
159+ " ${lib_dir} /lib${lib_name} .a"
160+ " /usr/lib/x86_64-linux-gnu/lib${lib_name} .a"
161+ " /usr/lib64/lib${lib_name} .a"
162+ " /usr/lib/lib${lib_name} .a"
163+ )
164+
165+ for path in " ${static_paths[@]} " ; do
166+ if [ -f " $path " ]; then
167+ echo " $path "
168+ return 0
169+ fi
170+ done
171+
172+ return 1
173+ }
174+
175+ # Generate CGO_LDFLAGS for linking RocksDB and dependencies
176+ # Note: RocksDB's compressed_secondary_cache requires bzip2 and zlib
154177# These libraries are statically linked to avoid runtime dependencies
155178generate_cgo_ldflags () {
156179 local os=$( detect_os)
157180 local rocksdb_lib_dir=$1
158181 local output_file=$2
159- local lib_dir= " "
182+ local rocksdb_static= " ${rocksdb_lib_dir} /librocksdb.a "
160183
161184 if [ " $os " = " darwin" ]; then
162- # macOS: Statically link rocksdb, link bz2 and z as system libraries
163- # Note: Using grocksdb_no_link tag, so we need to provide all necessary libraries
164- # Note: macOS system libraries (bz2, z) are available on all macOS systems, so dynamic linking is acceptable
165- # Note: RocksDB is statically linked by directly specifying the .a file
166- # Note: Using -Wl,-force_load to ensure all symbols from static library are included
167- local rocksdb_static=" ${rocksdb_lib_dir} /librocksdb.a"
185+ # macOS: Statically link RocksDB, dynamically link system libraries
186+ # Using grocksdb_no_link tag, so we provide all necessary libraries
187+ # macOS system libraries (bz2, z) are available on all macOS systems
168188 if [ -f " $rocksdb_static " ]; then
169189 # Force load static library to ensure all symbols are included
170- # Link system libraries (bz2, z) - these are available on all macOS systems
171- # Use -Wl,-dead_strip to remove unused code
172190 echo " -Wl,-force_load,${rocksdb_static} -pthread -lstdc++ -ldl -lbz2 -lz" > " $output_file "
173191 else
174192 # Fallback: use library path (should not happen if setup-rocksdb completed)
175193 echo " -L${rocksdb_lib_dir} -lrocksdb -pthread -lstdc++ -ldl -lbz2 -lz" > " $output_file "
176194 fi
177195 elif [ " $os " = " linux" ]; then
178- # Linux: Use static linking for bz2 and z, keep rocksdb as static library
179- # Use -static flag to force static linking of system libraries
180- lib_dir=$( detect_linux_lib_dir)
181- # Try to find static library paths
182- local bz2_static=" "
183- local z_static=" "
184-
185- # Find static library paths
186- if [ -f " ${lib_dir} /libbz2.a" ]; then
187- bz2_static=" ${lib_dir} /libbz2.a"
188- elif [ -f " /usr/lib/x86_64-linux-gnu/libbz2.a" ]; then
189- bz2_static=" /usr/lib/x86_64-linux-gnu/libbz2.a"
190- elif [ -f " /usr/lib64/libbz2.a" ]; then
191- bz2_static=" /usr/lib64/libbz2.a"
192- elif [ -f " /usr/lib/libbz2.a" ]; then
193- bz2_static=" /usr/lib/libbz2.a"
194- fi
195-
196- if [ -f " ${lib_dir} /libz.a" ]; then
197- z_static=" ${lib_dir} /libz.a"
198- elif [ -f " /usr/lib/x86_64-linux-gnu/libz.a" ]; then
199- z_static=" /usr/lib/x86_64-linux-gnu/libz.a"
200- elif [ -f " /usr/lib64/libz.a" ]; then
201- z_static=" /usr/lib64/libz.a"
202- elif [ -f " /usr/lib/libz.a" ]; then
203- z_static=" /usr/lib/libz.a"
204- fi
196+ # Linux: Fully static link all libraries for maximum portability
197+ local lib_dir=$( detect_linux_lib_dir)
198+ local bz2_static
199+ local z_static
205200
206- # Force static linking of rocksdb by directly specifying the static library file
207- local rocksdb_static=" ${rocksdb_lib_dir} /librocksdb.a"
201+ # Try to find static library files
202+ bz2_static=$( find_static_library " bz2" " $lib_dir " 2> /dev/null || echo " " )
203+ z_static=$( find_static_library " z" " $lib_dir " 2> /dev/null || echo " " )
208204
209- # Build link flags with static libraries if available, otherwise use -static flag
210- # Note: Using grocksdb_no_link tag, so we need to provide all necessary libraries
211- # Note: On Linux, we can fully static link all libraries for portability
212- if [ -n " $bz2_static " ] && [ -n " $z_static " ]; then
213- # Use direct static library paths for all libraries - fully static binary
214- if [ -f " $rocksdb_static " ]; then
215- # Directly link all static library files
216- # This creates a fully static binary that can run on any Linux system
217- # Add necessary system libraries: pthread, stdc++, dl
218- echo " ${rocksdb_static} ${bz2_static} ${z_static} -static -pthread -lstdc++ -ldl" > " $output_file "
219- else
220- # RocksDB static library not found, use library path with static linking
221- if [ -d " $lib_dir " ] && [ " $lib_dir " != " /usr/lib" ]; then
222- echo " -L${rocksdb_lib_dir} -L${lib_dir} -Wl,-Bstatic -lrocksdb ${bz2_static} ${z_static} -Wl,-Bdynamic -static -pthread -lstdc++ -ldl" > " $output_file "
223- else
224- echo " -L${rocksdb_lib_dir} -Wl,-Bstatic -lrocksdb ${bz2_static} ${z_static} -Wl,-Bdynamic -static -pthread -lstdc++ -ldl" > " $output_file "
225- fi
226- fi
205+ # Build link flags based on available static libraries
206+ if [ -n " $bz2_static " ] && [ -n " $z_static " ] && [ -f " $rocksdb_static " ]; then
207+ # Best case: all static libraries found, create fully static binary
208+ echo " ${rocksdb_static} ${bz2_static} ${z_static} -static -pthread -lstdc++ -ldl" > " $output_file "
209+ elif [ -f " $rocksdb_static " ]; then
210+ # RocksDB static found, but system libs not found - use -static flag
211+ local lib_search=" "
212+ [ -d " $lib_dir " ] && [ " $lib_dir " != " /usr/lib" ] && lib_search=" -L${lib_dir} "
213+ echo " ${rocksdb_static} ${lib_search} -static -lbz2 -lz -pthread -lstdc++ -ldl" > " $output_file "
227214 else
228- # Static libraries not found, use -static flag for full static linking
229- if [ -f " $rocksdb_static " ]; then
230- # RocksDB is static, use -static for system libraries (fully static binary)
231- if [ -d " $lib_dir " ] && [ " $lib_dir " != " /usr/lib" ]; then
232- echo " ${rocksdb_static} -L${lib_dir} -static -lbz2 -lz -pthread -lstdc++ -ldl" > " $output_file "
233- else
234- echo " ${rocksdb_static} -static -lbz2 -lz -pthread -lstdc++ -ldl" > " $output_file "
235- fi
236- else
237- # Fall back to -static flag for static linking (fully static binary)
238- if [ -d " $lib_dir " ] && [ " $lib_dir " != " /usr/lib" ]; then
239- echo " -L${rocksdb_lib_dir} -L${lib_dir} -Wl,-Bstatic -lrocksdb -lbz2 -lz -Wl,-Bdynamic -static -pthread -lstdc++ -ldl" > " $output_file "
240- else
241- echo " -L${rocksdb_lib_dir} -Wl,-Bstatic -lrocksdb -lbz2 -lz -Wl,-Bdynamic -static -pthread -lstdc++ -ldl" > " $output_file "
242- fi
243- fi
215+ # Fallback: use library paths with static linking flags
216+ local lib_search=" -L${rocksdb_lib_dir} "
217+ [ -d " $lib_dir " ] && [ " $lib_dir " != " /usr/lib" ] && lib_search=" ${lib_search} -L${lib_dir} "
218+ echo " ${lib_search} -Wl,-Bstatic -lrocksdb -lbz2 -lz -Wl,-Bdynamic -static -pthread -lstdc++ -ldl" > " $output_file "
244219 fi
245220 else
246221 echo_error " Unsupported operating system: $os "
0 commit comments