Skip to content

Commit b2396e1

Browse files
Build Static Linux Executable from macOS (#4852)
* Add a new script to build a linux static executable from macOS * add a new command to make file to build linux static executable from macOS * update the macos build command name and description in Makefile * refactor and optimize linux executable shell file for mocOS
1 parent 692cfc8 commit b2396e1

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ help:
3838
@echo "test-rosetta-attach - attach onto the rosetta testing docker container for inspection"
3939
@echo "linux_static - static build the harmony binary & bootnode along with the MCL & BLS libs (for linux)"
4040
@echo "linux_static_quick - static build the harmony binary & bootnode more quickly without recompiling dependencies (for linux)"
41+
@echo "linux_static_cross_build - cross-compile static Linux binaries for Harmony and Bootnode from macOS"
4142
@echo "rpm_init - prepare the RPM build environment by creating directories, copying files, and generating the spec file and source tarball"
4243
@echo "rpm_build - build an RPM package for x86_64 architecture using the spec file (<RPMBUILD>/SPECS/harmony.spec)"
4344
@echo "rpm - build a harmony RPM pacakge"
@@ -168,6 +169,9 @@ linux_static:
168169
linux_static_quick:
169170
bash ./scripts/go_executable_build.sh -s
170171

172+
linux_static_cross_build:
173+
bash ./scripts/linux_executable_from_macos.sh -s
174+
171175
deb_init:
172176
rm -rf $(DEBBUILD)
173177
mkdir -p $(DEBBUILD)/$(PKGNAME)-$(VERSION)-$(RELEASE)/{etc/systemd/system,usr/sbin,etc/sysctl.d,etc/harmony}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/bash
2+
set -e # Exit on first error
3+
4+
# Define paths
5+
SCRIPT_DIR=$(dirname "$0")
6+
PROJECT_ROOT=$(realpath "$SCRIPT_DIR/..")
7+
HARMONY_DIR="$PROJECT_ROOT"
8+
DOCKER_IMAGE="harmony-one" # Name of the Docker image
9+
OUTPUT_DIR="$HARMONY_DIR/bin/linux_static_bin" # Folder for generated binaries
10+
11+
echo "=== Paths ==="
12+
echo "SCRIPT_DIR: $SCRIPT_DIR"
13+
echo "PROJECT_ROOT: $PROJECT_ROOT"
14+
echo "HARMONY_DIR: $HARMONY_DIR"
15+
echo "OUTPUT_DIR: $OUTPUT_DIR"
16+
17+
# Ensure output directory exists and clean old binaries
18+
mkdir -p "$OUTPUT_DIR"
19+
rm -f "$OUTPUT_DIR/harmony" "$OUTPUT_DIR/bootnode"
20+
21+
# Check if Docker is running
22+
if ! docker info > /dev/null 2>&1; then
23+
echo "Docker is not running. Please start Docker and try again."
24+
exit 1
25+
fi
26+
27+
# Build Docker image if not found
28+
if ! docker image inspect "$DOCKER_IMAGE" > /dev/null 2>&1; then
29+
echo "Building Docker image '$DOCKER_IMAGE'..."
30+
pushd "$PROJECT_ROOT/scripts/macos_docker"
31+
docker build -t "$DOCKER_IMAGE" -f Dockerfile .
32+
popd
33+
else
34+
echo "Docker image '$DOCKER_IMAGE' already exists. Skipping build."
35+
fi
36+
37+
# Run the container in the background
38+
echo "Running Docker container..."
39+
CONTAINER_ID=$(docker run -d "$DOCKER_IMAGE" tail -f /dev/null)
40+
trap 'docker stop "$CONTAINER_ID" > /dev/null; docker rm "$CONTAINER_ID" > /dev/null' EXIT # Ensure cleanup on exit
41+
42+
# Copy project files into the container
43+
echo "Copying project files to Docker container..."
44+
docker cp "$HARMONY_DIR/." "$CONTAINER_ID:/root/go/src/github.com/harmony-one/tmp"
45+
46+
# Build binaries inside the container
47+
echo "Building Linux static binary..."
48+
docker exec "$CONTAINER_ID" bash -c "
49+
set -e
50+
cd /root/go/src/github.com/harmony-one
51+
echo 'Current Directory: ' && pwd
52+
rm -rf harmony
53+
mv tmp harmony
54+
cd harmony/bin && rm -rf * # Clear previous binaries
55+
cd ..
56+
git config --global --add safe.directory /root/go/src/github.com/harmony-one/harmony
57+
make linux_static
58+
"
59+
60+
# Copy built binaries back to host machine
61+
echo "Copying binaries to output directory..."
62+
docker cp "$CONTAINER_ID:/root/go/src/github.com/harmony-one/harmony/bin/harmony" "$OUTPUT_DIR"
63+
docker cp "$CONTAINER_ID:/root/go/src/github.com/harmony-one/harmony/bin/bootnode" "$OUTPUT_DIR"
64+
65+
# Validate build output
66+
if [ "$(ls -A "$OUTPUT_DIR")" ]; then
67+
echo "✅ Build completed successfully. Binaries are in '$OUTPUT_DIR'."
68+
else
69+
echo "❌ Build failed: No binaries found in '$OUTPUT_DIR'."
70+
exit 1
71+
fi

scripts/macos_docker/Dockerfile

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
FROM ubuntu:22.04
2+
3+
ARG TARGETARCH
4+
ARG GOLANG_VERSION="1.22.5"
5+
6+
SHELL ["/bin/bash", "-c"]
7+
8+
ENV GOPATH=/root/go
9+
ENV GO111MODULE=on
10+
ENV HMY_PATH=${GOPATH}/src/github.com/harmony-one
11+
ENV OPENSSL_DIR=/usr/lib/ssl
12+
ENV MCL_DIR=${HMY_PATH}/mcl
13+
ENV BLS_DIR=${HMY_PATH}/bls
14+
ENV CGO_CFLAGS="-I${BLS_DIR}/include -I${MCL_DIR}/include"
15+
ENV CGO_LDFLAGS="-L${BLS_DIR}/lib"
16+
ENV LD_LIBRARY_PATH=${BLS_DIR}/lib:${MCL_DIR}/lib
17+
ENV GIMME_GO_VERSION=${GOLANG_VERSION}
18+
ENV PATH="/root/bin:${PATH}"
19+
20+
RUN apt update && apt upgrade -y && \
21+
apt install libgmp-dev libssl-dev curl git \
22+
psmisc dnsutils jq make gcc g++ bash tig tree sudo vim \
23+
silversearcher-ag unzip emacs-nox nano bash-completion -y
24+
25+
RUN mkdir ~/bin && \
26+
curl -sL -o ~/bin/gimme \
27+
https://raw.githubusercontent.com/travis-ci/gimme/master/gimme && \
28+
chmod +x ~/bin/gimme
29+
30+
RUN eval "$(~/bin/gimme ${GIMME_GO_VERSION})"
31+
32+
#RUN git clone https://github.com/harmony-one/harmony.git ${HMY_PATH}/harmony
33+
34+
RUN git clone https://github.com/harmony-one/mcl.git ${HMY_PATH}/mcl && \
35+
echo "mcl repository cloned" && \
36+
ls -la ${HMY_PATH}/mcl # List the contents of the mcl directory
37+
38+
RUN git clone https://github.com/harmony-one/bls.git ${HMY_PATH}/bls && \
39+
echo "bls repository cloned" && \
40+
ls -la ${HMY_PATH}/bls # List the contents of the bls directory
41+
42+
RUN git clone https://github.com/harmony-one/go-sdk.git ${HMY_PATH}/go-sdk
43+
44+
RUN cd ${HMY_PATH}/bls && make -j8 BLS_SWAP_G=1
45+
46+
RUN touch /root/.bash_profile && \
47+
gimme ${GIMME_GO_VERSION} >> /root/.bash_profile && \
48+
echo "GIMME_GO_VERSION='${GIMME_GO_VERSION}'" >> /root/.bash_profile && \
49+
echo "GO111MODULE='on'" >> /root/.bash_profile && \
50+
echo ". ~/.bash_profile" >> /root/.profile && \
51+
echo ". ~/.bash_profile" >> /root/.bashrc
52+
53+
ENV PATH="/root/.gimme/versions/go${GIMME_GO_VERSION}.linux.${TARGETARCH:-amd64}/bin:${GOPATH}/bin:${PATH}"
54+
55+
RUN . ~/.bash_profile; \
56+
go install golang.org/x/tools/cmd/goimports; \
57+
go install golang.org/x/lint/golint ; \
58+
go install github.com/rogpeppe/godef ; \
59+
go install github.com/go-delve/delve/cmd/dlv; \
60+
go install github.com/golang/mock/mockgen; \
61+
go install github.com/stamblerre/gocode; \
62+
go install golang.org/x/tools/...; \
63+
go install honnef.co/go/tools/cmd/staticcheck/...
64+

0 commit comments

Comments
 (0)