From d9a37a6358cee4a1c44ece3cf2fa1f71c84c0d34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mateos?= Date: Mon, 5 Aug 2024 17:28:14 +0200 Subject: [PATCH 1/3] Allow to pass multiple lib dirs split by a colon i.e: --with-or-tools-lib=/usr/local/lib:/usr/local/lib64 --- ext/or-tools/extconf.rb | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/ext/or-tools/extconf.rb b/ext/or-tools/extconf.rb index 42e1a50..cc6865f 100644 --- a/ext/or-tools/extconf.rb +++ b/ext/or-tools/extconf.rb @@ -9,7 +9,27 @@ if inc || lib inc ||= "/usr/local/include" lib ||= "/usr/local/lib" - rpath = lib + + lib_dirs = lib.split(':') + rpath = lib_dirs.join(':') + + $INCFLAGS << " -I#{inc}" + + lib_dirs.each do |lib_dir| + $LDFLAGS.prepend("-L#{lib_dir} ") + end + + # Add rpath for all lib directories + $LDFLAGS.prepend("-Wl,-rpath,#{rpath} ") + + # Check for libprotobuf.a in any of the lib directories + libprotobuf_found = lib_dirs.any? { |dir| File.exist?("#{dir}/libprotobuf.a") } + raise "libprotobuf.a not found" unless libprotobuf_found + + # Add libprotobuf.a to LDFLAGS + $LDFLAGS << " #{lib_dirs.find { |dir| File.exist?("#{dir}/libprotobuf.a") }}/libprotobuf.a" + + raise "OR-Tools not found" unless have_library("ortools") else # download require_relative "vendor" @@ -21,15 +41,10 @@ # use double dollar sign and single quotes to escape properly rpath_prefix = RbConfig::CONFIG["host_os"] =~ /darwin/ ? "@loader_path" : "$$ORIGIN" rpath = "'#{rpath_prefix}/../../tmp/or-tools/lib'" + + $INCFLAGS << " -I#{inc}" + $LDFLAGS.prepend("-Wl,-rpath,#{rpath} -L#{lib} #{lib}/libprotobuf.a ") end -# find_header and find_library first check without adding path -# which can cause them to find system library -$INCFLAGS << " -I#{inc}" -# could support shared libraries for protobuf and abseil -# but keep simple for now -raise "libprotobuf.a not found" unless File.exist?("#{lib}/libprotobuf.a") -$LDFLAGS.prepend("-Wl,-rpath,#{rpath} -L#{lib} #{lib}/libprotobuf.a ") -raise "OR-Tools not found" unless have_library("ortools") create_makefile("or_tools/ext") From 4035081f3b0d9f12508f17d9366bce407be941c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mateos?= Date: Tue, 6 Aug 2024 10:04:17 +0200 Subject: [PATCH 2/3] Clean up --- ext/or-tools/extconf.rb | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/ext/or-tools/extconf.rb b/ext/or-tools/extconf.rb index cc6865f..621a291 100644 --- a/ext/or-tools/extconf.rb +++ b/ext/or-tools/extconf.rb @@ -13,23 +13,18 @@ lib_dirs = lib.split(':') rpath = lib_dirs.join(':') - $INCFLAGS << " -I#{inc}" - - lib_dirs.each do |lib_dir| - $LDFLAGS.prepend("-L#{lib_dir} ") - end - - # Add rpath for all lib directories - $LDFLAGS.prepend("-Wl,-rpath,#{rpath} ") + # Find the first lib directory containing libprotobuf.a + libprotobuf_dir = lib_dirs.find { |dir| File.exist?("#{dir}/libprotobuf.a") } + raise "libprotobuf.a not found" unless libprotobuf_dir - # Check for libprotobuf.a in any of the lib directories - libprotobuf_found = lib_dirs.any? { |dir| File.exist?("#{dir}/libprotobuf.a") } - raise "libprotobuf.a not found" unless libprotobuf_found + raise "OR-Tools not found" unless have_library("ortools") - # Add libprotobuf.a to LDFLAGS - $LDFLAGS << " #{lib_dirs.find { |dir| File.exist?("#{dir}/libprotobuf.a") }}/libprotobuf.a" + # -L flags for each lib directory + lib_dirs_flags = lib_dirs.map { |lib_dir| "-L#{lib_dir} " }.join - raise "OR-Tools not found" unless have_library("ortools") + $INCFLAGS << " -I#{inc}" + ld_flags = "-Wl,-rpath,#{rpath} #{lib_dirs_flags} #{libprotobuf_dir}/libprotobuf.a " + $LDFLAGS.prepend(ld_flags) else # download require_relative "vendor" From ab867a84a0634a7c14abcb6421258d9ec6795f3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mateos?= Date: Tue, 6 Aug 2024 13:54:22 +0200 Subject: [PATCH 3/3] Fix library not linking --- ext/or-tools/extconf.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ext/or-tools/extconf.rb b/ext/or-tools/extconf.rb index 621a291..79b1c9d 100644 --- a/ext/or-tools/extconf.rb +++ b/ext/or-tools/extconf.rb @@ -17,8 +17,6 @@ libprotobuf_dir = lib_dirs.find { |dir| File.exist?("#{dir}/libprotobuf.a") } raise "libprotobuf.a not found" unless libprotobuf_dir - raise "OR-Tools not found" unless have_library("ortools") - # -L flags for each lib directory lib_dirs_flags = lib_dirs.map { |lib_dir| "-L#{lib_dir} " }.join @@ -41,5 +39,5 @@ $LDFLAGS.prepend("-Wl,-rpath,#{rpath} -L#{lib} #{lib}/libprotobuf.a ") end - +raise "OR-Tools not found" unless have_library("ortools") create_makefile("or_tools/ext")