gpt4 book ai didi

go - 从交叉编译的二进制文件构建RPM软件包Go app tarball

转载 作者:行者123 更新时间:2023-12-01 22:16:24 25 4
gpt4 key购买 nike

我正在尝试创建与Go交叉编译的第一个RPM软件包。

这是包含所有必需信息的Makefile:

APPNAME?=helloworld
VERSION?=v1.0.0

APPANDVER := ${APPNAME}-$(VERSION)

# Build flags
LDFLAGS := -ldflags "-s -w -X=main.VERSION=$(VERSION)"

# Temporary directory for common files when creating tarballs
RELEASETMPDIR := $(shell mktemp -d -t ${APPNAME}-${VERSION}-release-XXXXXX)

# Cross-compile to these CPUs
# https://golang.org/doc/install/source#environment
LINUX_ARCHS := amd64 arm arm64 ppc64 ppc64le

default: release-bin

main.go:
@echo 'package main' > $@
@echo 'import "fmt"' >> $@
@echo '// Replaced with version when building' >> $@
@echo 'var VERSION = "v0.0.0"' >> $@
@echo 'func main() {' >> $@
@echo ' fmt.Println("Hello, world!")' >> $@
@echo '}' >> $@
@go fmt $@

README.md:
@echo 'This is a README for $(APPNAME) $(VERSION)' > $@
@echo '$(APPNAME) simply prints "Hello, world!"' >> $@

LICENSE:
@echo 'You can do what ever you want with $(APPNAME).' > $@
@echo 'You have all the responsibility!' >> $@

# Build for all listed architectures
linux-build: main.go
@for arch in $(LINUX_ARCHS); do \
echo "GNU/Linux build... $$arch"; \
CGO_ENABLED=0 GOOS=linux GOARCH=$$arch go build $(LDFLAGS) -v -o ./bin/linux-$$arch/${APPNAME} . ; \
done

# Copy common files used in binary tarball releases
copycommon: README.md LICENSE
@echo "Copying common files to temporary release directory '$(RELEASETMPDIR)'.."
@mkdir "$(RELEASETMPDIR)/bin"
@cp -v "LICENSE" "$(RELEASETMPDIR)"
@cp -v "README.md" "$(RELEASETMPDIR)"
@mkdir --parents "$(PWD)/release/${VERSION}"

# Create binary release tarballs for each CPU architecture
compress-linux: linux-build
@for arch in $(LINUX_ARCHS); do \
echo "GNU/Linux tar... $$arch"; \
cp -v "$(PWD)/bin/linux-$$arch/${APPNAME}" "$(RELEASETMPDIR)/bin"; \
cd "$(RELEASETMPDIR)"; \
tar --numeric-owner --owner=0 --group=0 -zcvf "$(PWD)/release/${VERSION}/$(APPANDVER)-linux-$$arch.tar.gz" . ; \
rm "$(RELEASETMPDIR)/bin/${APPNAME}"; \
done

# Move all to temporary directory and compress with common files
compress-everything: copycommon compress-linux
@echo "$@ ..."
rm -rf "$(RELEASETMPDIR)/*"

# Create tarballs which has common files and different bin/${APPNAME} per CPU architecture
release-bin: linux-build compress-everything
@echo "release done..."

# Linux distributions
release-ldistros: ldistro-rpm
@echo "Linux distros release done..."

release/linux/rpm:
@mkdir --parents ./release/linux/rpm

# RPM spec file (probably wrong)
release/linux/rpm/package.spec: release/linux/rpm
@echo 'Name: ${APPNAME}' > $@
@echo 'Version: %{_version}' >> $@
@echo 'Release: 1%{?dist}' >> $@
@echo 'Summary: Hello world' >> $@
@echo 'URL: https://example.org/${APPANDVER}/' >> $@
@echo 'Group: Applications/Utilities' >> $@
@echo 'License: Apache-2.0' >> $@
@echo '%description' >> $@
@echo '${APPNAME} is a command line program which prints "Hello, world!"' >> $@
@echo '%setup -q' >> $@
@echo '%clean' >> $@
@echo '%files' >> $@
@echo '%license /usr/share/licenses/%{NAME}/LICENSE' >> $@
@echo '%doc /usr/share/doc/%{NAME}/README.md' >> $@
@echo '/usr/bin/${APPNAME}' >> $@
@echo '%install' >> $@
@echo 'install -Dm755 "usr/bin/%{NAME}" -t "/usr/bin"' >> $@

# Create RPM package for each CPU architecture from tarballs (probably wrong)
ldistro-rpm: "release/linux/rpm/package.spec
@for arch in $(LINUX_ARCHS); do \
echo "Generating RPM... $$arch"; \
tempdir=$$(mktemp -d -t $(APPANDVER)-rpm-XXXXXX) ; \
echo " >> Using temporary directory $$tempdir" ; \
cd "$$tempdir" ; \
mkdir --parents {SOURCES,RPMS,SPECS,SRPMS,BUILD,tmp} ; \
cp "$(PWD)/release/linux/rpm/package.spec" "SPECS/${APPNAME}" ; \
cd "BUILD"; \
echo " >> Extracting source binary package.." ; \
tar -xzf "$(PWD)/release/${VERSION}/$(APPANDVER)-linux-$$arch.tar.gz" . ; \
echo " >> Generating directory structure in temp dir.." ; \
mkdir --parents ./usr/bin/ ; \
mv ./bin/${APPNAME} ./usr/bin/ ; \
rm -rf ./bin ; \
mkdir --parents ./usr/share/licenses/${APPNAME}/ ; \
mv LICENSE ./usr/share/licenses/${APPNAME} ; \
mkdir --parents ./usr/share/doc/${APPNAME}/ ; \
mv README.md ./usr/share/doc/${APPNAME} ; \
cd .. ; \
echo " >> Building RPM package.." ; \
sudo rpmbuild -vv --nosignature --nodebuginfo --dbpath "$$tempdir" --root "$$tempdir" --buildroot "./BUILD" --target $$arch --define "_tmppath /tmp" --define "_topdir ." --define "_version ${VERSION}" --define "_buildhost localhost" --define "_rpmfilename $(APPANDVER)-$$arch.rpm" -bb "SPECS/${APPNAME}" && \
rpm -qlp --info "./RPMS/$(APPANDVER)-$$arch.rpm" && \
cp "./RPMS/$(APPANDVER)-$$arch.rpm" "$(PWD)/release/${VERSION}/" ; \
echo "------------------------------------------------------------"; \
done

创建 bin/$os-$cpuarch/$appname二进制文件:
% make linux-build

将二进制源压缩文件创建为 release/$version/$appname-$version-$os-$cpuarch.tar.gz:
% make compress-everything

在tarball中创建文件树结构:
bin/$appname (different for each architecture)
LICENSE
README.md

由于也存在不同的OS压缩包,因此该结构被锁定。由于可读性原因,在此最小化的示例中删除了不同的OS构建目标。

为每种CPU架构生成RPM:
% make ldistro-rpm

当前的问题是,在RPM构建期间,实际的可执行文件正在安装到正在运行的系统中。 AFAIK这不应该发生。我可能在规范文件或 ldistro-rpm目标中缺少什么?另外,一些 rpmbuild示例似乎仅使用 -bb参数,但是我找不到示例或弄清楚如何修改 spec文件,从而可以正常工作。 ldistro-rpm目标似乎过于复杂。规范文件的 %install%prep等中应该包含一些命令吗?您能以某种方式使用 Source0文件中的 spec并将其指向tarball,而不是在 ldistro-rpm目标中生成目录结构吗?

最佳答案

@echo 'install -Dm755 "usr/bin/%{NAME}" -t "/usr/bin"'

是要安装到 /usr/bin的位置,应该改为安装到 $RPM_BUILD_ROOT/usr/bin

关于go - 从交叉编译的二进制文件构建RPM软件包Go app tarball,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59776997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com