This repository has been archived by the owner on Apr 7, 2019. It is now read-only.
forked from shadowmint/go-static-linking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
134 lines (99 loc) · 4.13 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Copyright 2012 Douglas Linder
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Modified by Don Bright 2015
# This Cmake file intends to help linking Go code statically with C code
# Please see the README.md file for a fuller explanation of how this process
# works.
# This is a hybrid build system generator. After Cmake is run:
#
# 1. running 'make' will take care of checking C dependencies and
# building the .c code. It doesn't deal with .go
#
# 2. running 'make' will also run 'go install', but it will be run every time.
# make itself will not check go dependencies... this is left to 'go' itself.
cmake_minimum_required (VERSION 2.8)
project (hello)
####
#### C Library section
####
set(C_INCLUDE_PATH ${PROJECT_SOURCE_DIR}/src/c)
set(C_LIBRARY_NAME world)
set(CSOURCES src/c/world.c)
# Make sure C library files (.a/.lib files) are built under 'lib' subdir
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_BINARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
include_directories(${C_INCLUDE_PATH})
add_library( ${C_LIBRARY_NAME} ${CSOURCES} )
####
#### Go language section
####
# Create bridge.go bridge file using 'go.in' template
set(INFILE ${PROJECT_SOURCE_DIR}/src/bridge/bridge.go.in)
set(OUTFILE ${PROJECT_BINARY_DIR}/src/bridge/bridge.go)
set(C_LIBRARY_PATH ${PROJECT_BINARY_DIR}/lib)
set(CFLAGS "-I${C_INCLUDE_PATH}")
set(LDFLAGS "-L${C_LIBRARY_PATH} -l${C_LIBRARY_NAME}")
message(STATUS "Creating .go bridge file")
message(STATUS "Input template:${INFILE}" )
message(STATUS "Output file :${OUTFILE}")
message(STATUS "Output file CFLAGS : ${CFLAGS}")
message(STATUS "Output file LDFLAGS: ${LDFLAGS}")
configure_file( ${INFILE} ${OUTFILE} )
# Call 'go build' to generate executable file
# GOPATH / GOBIN
# see https://golang.org/doc/articles/go_command.html
# note: order matters... binary must come first so bridge.go can be found.
set(GOPATH ${PROJECT_BINARY_DIR}:${PROJECT_SOURCE_DIR})
set(GOBIN "${PROJECT_BINARY_DIR}/bin")
message(STATUS "GOPATH=${GOPATH}")
message(STATUS "GOBIN=${GOBIN}")
message(STATUS "executable file: ${GOBIN}/${CMAKE_PROJECT_NAME}")
# create shell programs to help user set GOPATH and GOBIN
set(SHFILE ${PROJECT_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/setenv.sh)
file(WRITE ${SHFILE} "")
file(APPEND ${SHFILE} "GOPATH=${GOPATH}\n")
file(APPEND ${SHFILE} "GOBIN=${GOBIN}\n")
file(APPEND ${SHFILE} "export GOPATH\n")
file(APPEND ${SHFILE} "export GOBIN\n")
file(APPEND ${SHFILE} "echo GOPATH=$GOPATH\n")
file(APPEND ${SHFILE} "echo GOBIN=$GOBIN\n")
set(CSHFILE ${PROJECT_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/setenv.csh)
file(WRITE ${CSHFILE} "")
file(APPEND ${CSHFILE} "setenv GOPATH ${GOPATH}\n")
file(APPEND ${CSHFILE} "setenv GOBIN ${GOBIN}\n")
file(APPEND ${CSHFILE} "echo GOPATH=$GOPATH\n")
file(APPEND ${CSHFILE} "echo GOBIN=$GOBIN\n")
# Setup 'go install' for Makefile.
# To dump alot more debug info with go build '-x', set the
# environment variable VERBOSE=1 before running cmake
# (for example, VERBOSE=1 cmake)
if(DEFINED ENV{VERBOSE})
set(GOFLAGS "-v" "-x" "--ldflags" "'-extldflags" "\"-static\"'")
else()
set(GOFLAGS "--ldflags" "'-extldflags" "\"-static\"'")
endif()
#set(CUSTCMD strace -f go install -v hello)
#set(CUSTCMD go build hello)
set(GOCMD go install ${GOFLAGS} ${CMAKE_PROJECT_NAME})
message(STATUS "gocmd ${GOCMD}")
set(GOFULLCMD GOBIN=${GOBIN} GOPATH=${GOPATH} ${GOCMD})
# the args to add_custom_target have ';' but i want to print with spaces ' '
# so pretty print here
string(REPLACE ";" " " GOCMDPP "${GOFULLCMD}")
add_custom_target( goinstall ALL
${GOFULLCMD}
COMMENT "Running ${GOCMDPP}"
DEPENDS ${C_LIBRARY_NAME}
)