Skip to content

Commit

Permalink
Allow Statically Linking in Go (#2067)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
t0rr3sp3dr0 and wtdcode authored Dec 21, 2024
1 parent 7737e7b commit 2899088
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 77 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions bindings/go/unicorn/cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package unicorn

// #cgo CFLAGS: -I../../../include -O3 -Wall -Werror
import "C"
6 changes: 6 additions & 0 deletions bindings/go/unicorn/cgo_dynamic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build !static

package unicorn

// #cgo LDFLAGS: -lunicorn
import "C"
7 changes: 7 additions & 0 deletions bindings/go/unicorn/cgo_static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build static

package unicorn

// #cgo !darwin LDFLAGS: -lunicorn -lpthread -lm -latomic
// #cgo darwin LDFLAGS: -lunicorn.o
import "C"
3 changes: 0 additions & 3 deletions bindings/go/unicorn/unicorn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
*/
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
88 changes: 15 additions & 73 deletions cmake/bundle_static.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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_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()
foreach(tgt IN LISTS static_libs)
list(APPEND static_libs_objects $<TARGET_OBJECTS:${tgt}>)
endforeach()

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()

0 comments on commit 2899088

Please sign in to comment.