From cf1dbdc07ab428e645fa0e5c057e4b02cf719302 Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Mon, 8 Dec 2025 16:19:56 +0800 Subject: [PATCH] add autoscaler and build for go code --- src/autoscaler/meson.build | 2 + src/autoscaler/providers/meson.build | 34 +++++++++ src/meson.build | 6 ++ src/tools/go_build | 102 +++++++++++++++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 src/autoscaler/providers/meson.build create mode 100755 src/tools/go_build diff --git a/src/autoscaler/meson.build b/src/autoscaler/meson.build index ea12a1b..d47d734 100644 --- a/src/autoscaler/meson.build +++ b/src/autoscaler/meson.build @@ -22,3 +22,5 @@ libscaler = static_library('scaler', include_directories: [incdir], dependencies: libscaler_deps, install: true) + +subdir('providers') diff --git a/src/autoscaler/providers/meson.build b/src/autoscaler/providers/meson.build new file mode 100644 index 0000000..2ad7e28 --- /dev/null +++ b/src/autoscaler/providers/meson.build @@ -0,0 +1,34 @@ + +go_env = environment() +go_env.set('BUILDTYPE', get_option('buildtype')) +go_env.set('FIX_ENVARS','CGO_CFLAGS CGO_LDFLAGS') +go_env.set('DEPENDENCIES','thread math') +go_env.set('ENVARS', 'CURRENT_SOURCE_DIR BUILD_ROOT') + +apiserver = custom_target('bcctool', + input: 'baidu/src/bcctool/main.go', + output: 'bcctool', + command: [env_program, + 'CURRENT_SOURCE_DIR="@CURRENT_SOURCE_DIR@"', + 'BUILD_ROOT="@BUILD_ROOT@"', + go_build_program, + '@INPUT@', '@OUTPUT@'], + env: go_env, + install: true, + install_dir: get_option('prefix') / 'bin') + +scripts = [ + './baidu/scripts/getReturnRequests.sh', + './baidu/scripts/getAvailableTemplates.sh', + './baidu/scripts/getRequestStatus.sh', + './baidu/scripts/requestReturnMachines.sh', + './baidu/scripts/getAvailableMachines.sh', + './baidu/scripts/requestMachines.sh', + './baidu/postprovision/lsf/example_user_data.sh', +] + +install_data(scripts, + install_dir: 'share/bcctool/scripts') + +# FIXME: support installation of configuration +# file later diff --git a/src/meson.build b/src/meson.build index 65deebc..79f1f41 100644 --- a/src/meson.build +++ b/src/meson.build @@ -31,6 +31,12 @@ else wrap_link_args = [] endif +go_compiler = find_program('go') +pkgconfig = find_program('pkg-config') +go_build = files('tools/go_build') +go_build_program = find_program('go_build', go_build) +env_program = find_program('env') + include_dir = include_directories('include', 'include/xce') cc = meson.get_compiler('c') diff --git a/src/tools/go_build b/src/tools/go_build new file mode 100755 index 0000000..c6c011b --- /dev/null +++ b/src/tools/go_build @@ -0,0 +1,102 @@ +#!/bin/sh + +if [ "x$DEBUG" = "x" ]; then + : +else + set -x +fi + +if [ "x$BUILDTYPE" = "xdebug" ]; then + CGO_CFLAGS="$CGO_CFLAGS -DWITH_DEBUG=1 -Og -ggdb " + CGO_LDFLAGS="$CGO_LDFLAGS -Og -ggdb " + GO_FLAGS=' -gcflags "all=-N -l"' + + export CGO_CFLAGS CGO_LDFLAGS GO_FLAGS +fi + +fix_env() { + local _n _x _cmd _v + + _n="$1" + shift + _cmd="sed " + for _x in $*; do + _v=$(eval $(echo "echo \$$_x") | sed -s 's@"@@g') + _cmd="$_cmd -e s|@$_x@|$_v|g " + done + _v=$(eval $(echo "echo \$$_n")) + _cmd="$_n=\"$(echo $_v | $_cmd)\"; export $_n" + eval "$_cmd" +} + +# fix value of environment variable by realpath +# of it +fix_input_envs() { + local _n _v _cmd + + for _n in CURRENT_SOURCE_DIR BUILD_ROOT; do + _v=$(eval $(echo "echo \$$_n") | sed -e 's@"@@g') + _v=$(realpath $_v) + _cmd="$_n=$_v; export $_n" + eval "$_cmd" + done +} + +# fix dependencies, by: +# for math just add -lmath +# for thread by add -pthread +# otherwise using +# pkg-config --cflags/ pkg-config --libs +fix_dependencies() { + local _x _cflags _ldflags + + for _x in $DEPENDENCIES; do + case "$_x" in + math) + _cflags="" + _ldflags="-lm" + ;; + thread) + _cflags="-pthread" + _ldflags="-pthread" + ;; + *) + _cflags=$(pkg-config --cflags "$_x") + _ldflags=$(pkg-config --libs "$_x") + ;; + esac + + CGO_CFLAGS="$CGO_CFLAGS $_cflags" + CGO_LDFLAGS="$CGO_LDFLAGS $_ldflags" + done + + export CGO_CFLAGS CGO_LDFLAGS +} + +if [ "$#" -ne 2 ]; then + echo go_build sfile dest + exit 1 +fi + +_sfile=$(realpath "$1") +_sdir=$(dirname "$_sfile") +_dfile=$(realpath "$2") + +fix_input_envs +fix_dependencies + +if [ -z "$HOME" -o ! -w "$HOME" ]; then + HOME=$TMPDIR + export HOME +fi + +export GOCACHE=$TMPDIR/go-cache +export GOPATH="$TMPDIR/go" + +for _n in $FIX_ENVARS; do + fix_env $_n $ENVARS; +done + +export GOPROXY=https://goproxy.cn + +(cd $_sdir; go build -gcflags "all=-N -l" -o "$_dfile" "$_sfile")