+ 收藏我们

网站模板

网站模板搜索
404模板 营销型模板 外贸网站模板 单页模板 双语模板 标签大全
电话:18630701785
首页 > 站长学院 > 从交叉编译的二进制文件 Go 应用 tarball 构建 RPM 包 >

从交叉编译的二进制文件 Go 应用 tarball 构建 RPM 包

时间:2024-04-08 08:52:08

今天本篇文章带大家了解《从交叉编译的二进制文件 Go 应用 tarball 构建 RPM 包》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

问题内容
我正在尝试创建我的第一个与 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
创建二进制源 tarball 到 release/$version/$appname-$version-$os-$cpuarch.tar.gz:

% make compress-everything
在 tarball 内创建文件树结构:

bin/$appname (different for each architecture)
license
readme.md
此结构被锁定,因为也有不同的操作系统 tarball。出于可读性原因,在此最小化示例中删除了不同的操作系统构建目标。

为每个 cpu 架构生成 rpm:

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


解决方案

@echo 'install -Dm755 "usr/bin/%{NAME}" -t "/usr/bin"'
是您要安装到 /usr/bin 的位置。您应该安装到 $rpm_build_root/usr/bin。

以上就是本文的全部内容了,是否有顺利帮助你解决问题.

有问题可以加入网站技术QQ群一起交流学习

本站会员学习、解决问题QQ群(691961965)

客服微信号:lpf010888

Title