Allow Statically Linking in Go (#2067)

* unset -L and -rpath from CGO_LDFLAGS

Signed-off-by: Pedro Tôrres <t0rr3sp3dr0@gmail.com>

* allow go statically linking

Signed-off-by: Pedro Tôrres <t0rr3sp3dr0@gmail.com>

* fix setup.py

---------

Signed-off-by: Pedro Tôrres <t0rr3sp3dr0@gmail.com>
Co-authored-by: mio <mio@lazym.io>
This commit is contained in:
Pedro Tôrres
2024-12-20 19:02:28 -08:00
committed by GitHub
parent 7737e7b436
commit 2899088844
7 changed files with 33 additions and 76 deletions

View File

@@ -1515,6 +1515,7 @@ if(UNICORN_INSTALL AND NOT MSVC)
)
endif()
install(FILES $<TARGET_FILE:unicorn_archive> DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES $<TARGET_FILE_DIR:unicorn_archive>/$<TARGET_PROPERTY:unicorn_archive,SYMLINK_NAME> DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES ${UNICORN_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/unicorn)
if (ATOMIC_LINKAGE_FIX)
set(ATOMIC_LINK_PKG_CONFIG " -latomic")

View File

@@ -0,0 +1,4 @@
package unicorn
// #cgo CFLAGS: -I../../../include -O3 -Wall -Werror
import "C"

View File

@@ -0,0 +1,6 @@
//go:build !static
package unicorn
// #cgo LDFLAGS: -lunicorn
import "C"

View File

@@ -0,0 +1,7 @@
//go:build static
package unicorn
// #cgo !darwin LDFLAGS: -lunicorn -lpthread -lm -latomic
// #cgo darwin LDFLAGS: -lunicorn.o
import "C"

View File

@@ -7,9 +7,6 @@ import (
)
/*
#cgo CFLAGS: -O3 -Wall -Werror -I../../../include
#cgo LDFLAGS: -L../../../ -lunicorn -Wl,-rpath,${SRCDIR}/../../../
#cgo linux LDFLAGS: -L../../../ -lunicorn -lrt -Wl,-rpath,${SRCDIR}/../../../
#include <unicorn/unicorn.h>
#include "uc.h"
*/

View File

@@ -113,7 +113,7 @@ def build_libraries():
obj_dir = os.path.join(BUILD_DIR, conf)
shutil.copy(os.path.join(obj_dir, LIBRARY_FILE), LIBS_DIR)
shutil.copy(os.path.join(BUILD_DIR, STATIC_LIBRARY_FILE), LIBS_DIR)
shutil.copy(os.path.join(obj_dir, STATIC_LIBRARY_FILE), LIBS_DIR)
else:
cmake_args = ["cmake", '-B', BUILD_DIR, '-S', UC_DIR, "-DCMAKE_BUILD_TYPE=" + conf]
if os.getenv("TRACE"):

View File

@@ -40,76 +40,18 @@ function(bundle_static_library tgt_name bundled_tgt_name library_name)
list(REMOVE_DUPLICATES static_libs)
list(REMOVE_DUPLICATES dep_libs)
set(bundled_tgt_full_name
${CMAKE_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${library_name}${CMAKE_STATIC_LIBRARY_SUFFIX})
if (APPLE)
find_program(lib_tool libtool REQUIRED)
foreach(tgt IN LISTS static_libs)
list(APPEND static_libs_objects $<TARGET_OBJECTS:${tgt}>)
endforeach()
foreach(tgt IN LISTS static_libs)
list(APPEND static_libs_full_names $<TARGET_FILE:${tgt}>)
endforeach()
add_custom_command(
COMMAND ${lib_tool} -static -o ${bundled_tgt_full_name} ${static_libs_full_names}
OUTPUT ${bundled_tgt_full_name}
COMMENT "Bundling ${bundled_tgt_name}"
VERBATIM)
elseif(UNIX OR MINGW)
file(WRITE ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar.in
"CREATE ${bundled_tgt_full_name}\n" )
foreach(tgt IN LISTS static_libs)
file(APPEND ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar.in
"ADDLIB $<TARGET_FILE:${tgt}>\n")
endforeach()
file(APPEND ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar.in "SAVE\n")
file(APPEND ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar.in "END\n")
file(GENERATE
OUTPUT ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar
INPUT ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar.in)
set(ar_tool ${CMAKE_AR})
if (CMAKE_INTERPROCEDURAL_OPTIMIZATION)
set(ar_tool ${CMAKE_CXX_COMPILER_AR})
endif()
add_custom_command(
COMMAND ${ar_tool} -M < ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar
OUTPUT ${bundled_tgt_full_name}
COMMENT "Bundling ${bundled_tgt_name}"
VERBATIM)
elseif(WIN32)
# https://stackoverflow.com/a/38096930/1806760
get_filename_component(vs_bin_path "${CMAKE_LINKER}" DIRECTORY)
find_program(lib_tool lib HINTS "${vs_bin_path}" REQUIRED)
foreach(tgt IN LISTS static_libs)
list(APPEND static_libs_full_names $<TARGET_FILE:${tgt}>)
endforeach()
add_custom_command(
COMMAND ${lib_tool} /NOLOGO /OUT:${bundled_tgt_full_name} ${static_libs_full_names}
OUTPUT ${bundled_tgt_full_name}
COMMENT "Bundling ${bundled_tgt_name}"
VERBATIM)
else()
message(FATAL_ERROR "Unknown bundle scenario!")
endif()
add_custom_target(bundling_target ALL DEPENDS ${bundled_tgt_full_name})
add_dependencies(bundling_target ${tgt_name})
add_library(${bundled_tgt_name} STATIC IMPORTED)
set_target_properties(${bundled_tgt_name}
PROPERTIES
IMPORTED_LOCATION ${bundled_tgt_full_name}
INTERFACE_INCLUDE_DIRECTORIES $<TARGET_PROPERTY:${tgt_name},INTERFACE_INCLUDE_DIRECTORIES>
INTERFACE_LINK_LIBRARIES "${dep_libs}")
#IMPORTED_LINK_INTERFACE_LIBRARIES "${dep_libs}") # Deprecated
add_dependencies(${bundled_tgt_name} bundling_target)
endfunction()
add_library(${bundled_tgt_name} STATIC ${static_libs_objects})
set_target_properties(${bundled_tgt_name} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES $<TARGET_PROPERTY:${tgt_name},INTERFACE_INCLUDE_DIRECTORIES>
INTERFACE_LINK_LIBRARIES "${dep_libs}"
OUTPUT_NAME "${library_name}"
SYMLINK_NAME "${library_name}.o"
)
add_custom_command(TARGET ${bundled_tgt_name} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink "$<TARGET_FILE_NAME:${bundled_tgt_name}>" "$<TARGET_FILE_DIR:${bundled_tgt_name}>/$<TARGET_PROPERTY:${bundled_tgt_name},SYMLINK_NAME>"
)
endfunction()