Skip to content

Commit 585a321

Browse files
committed
build: add CMakeLists.txt
1 parent 72f4f90 commit 585a321

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

CMakeLists.txt

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(libipfs_go)
3+
4+
if(APPLE)
5+
set(GOOS "darwin")
6+
elseif(UNIX)
7+
if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
8+
set(GOOS "freebsd")
9+
else()
10+
set(GOOS "linux")
11+
endif()
12+
elseif(WIN32)
13+
set(GOOS "windows")
14+
else()
15+
message(FATAL_ERROR "Unsupported OS")
16+
endif()
17+
18+
if(NOT DEFINED ARCH_ID)
19+
message(FATAL_ERROR "ARCH_ID must be passed from parent")
20+
endif()
21+
22+
if(DEFINED ARCH_ID)
23+
message(STATUS "Using Scala ARCH_ID: ${ARCH_ID}")
24+
set(GOARCH_FROM_ARCH_ID ON)
25+
if(ARCH_ID STREQUAL "x86_64")
26+
set(GOARCH "amd64")
27+
elseif(ARCH_ID MATCHES "^(aarch64|arm64|armv8-a)$")
28+
set(GOARCH "arm64")
29+
elseif(ARCH_ID STREQUAL "riscv64")
30+
set(GOARCH "riscv64")
31+
else()
32+
message("Unsupported ARCH_ID: ${ARCH_ID} DEFAULTING TO amd64")
33+
set(GOARCH "amd64")
34+
endif()
35+
else()
36+
message(STATUS "Deriving GOARCH from CMAKE_SYSTEM_PROCESSOR")
37+
string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" SYSTEM_ARCH)
38+
message(STATUS "Detected system arch: ${SYSTEM_ARCH}")
39+
40+
if(SYSTEM_ARCH STREQUAL "x86_64" OR SYSTEM_ARCH STREQUAL "amd64")
41+
set(GOARCH "amd64")
42+
elseif(SYSTEM_ARCH MATCHES "^(aarch64|arm64|armv8-a)$")
43+
set(GOARCH "arm64")
44+
elseif(SYSTEM_ARCH STREQUAL "riscv64")
45+
set(GOARCH "riscv64")
46+
else()
47+
message(FATAL_ERROR "Unsupported system processor architecture: ${SYSTEM_ARCH}")
48+
endif()
49+
endif()
50+
51+
set(GO_OUT libipfs-${GOOS}-${GOARCH}.a)
52+
set(GO_HEADER libipfs.h)
53+
54+
add_custom_command(
55+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${GO_OUT} ${CMAKE_CURRENT_BINARY_DIR}/${GO_HEADER}
56+
COMMAND ${CMAKE_COMMAND} -E env
57+
CGO_ENABLED=1
58+
GOOS=${GOOS}
59+
GOARCH=${GOARCH}
60+
go build
61+
-buildmode=c-archive
62+
-trimpath
63+
-ldflags=-s\ -w
64+
-o ${CMAKE_CURRENT_BINARY_DIR}/${GO_OUT}
65+
${CMAKE_CURRENT_SOURCE_DIR}/libipfs.go
66+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
67+
${CMAKE_CURRENT_BINARY_DIR}/libipfs-${GOOS}-${GOARCH}.h
68+
${CMAKE_CURRENT_BINARY_DIR}/libipfs.h
69+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
70+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/libipfs.go
71+
COMMENT "Building Go IPFS library (${GOOS}/${GOARCH})"
72+
)
73+
74+
75+
add_custom_target(libipfs_build ALL
76+
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${GO_OUT}
77+
)
78+
79+
add_library(libipfs STATIC IMPORTED GLOBAL)
80+
set_target_properties(libipfs PROPERTIES
81+
IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${GO_OUT}
82+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_BINARY_DIR}
83+
)
84+
add_dependencies(libipfs libipfs_build)

0 commit comments

Comments
 (0)