Skip to content

Instantly share code, notes, and snippets.

@jschaf
Last active March 2, 2021 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jschaf/940e9c67c6ac8fc23b4d853ed379dc7d to your computer and use it in GitHub Desktop.
Save jschaf/940e9c67c6ac8fc23b4d853ed379dc7d to your computer and use it in GitHub Desktop.
Bazel openssl and postgres
#!/usr/bin/env bash
function symlink_to_dir() {
local target="$2"
mkdir -p "$target"
if [[ -f "$1" ]]; then
ln -s -f -t "$target" "$1"
elif [[ -L "$1" ]]; then
cp $1 $2
elif [[ -d "$1" ]]; then
local children=$(find -H "$1" -maxdepth 1 -mindepth 1)
local dirname=$(basename "$1")
mkdir -p "$target/$dirname"
for child in $children; do
symlink_to_dir $child $target/$dirname
done
else
echo "Can not copy $1"
fi
}
function children_to_path() {
if [ -d $EXT_BUILD_DEPS/bin ]; then
local tools=$(find $EXT_BUILD_DEPS/bin -maxdepth 1 -mindepth 1)
for tool in $tools; do
if [[ -d "$tool" ]] || [[ -L "$tool" ]]; then
export PATH=$PATH:$tool
fi
done
fi
}
function increment_pkg_config_path() {
local children=$(find $1 -mindepth 1 -name '*.pc')
# assume there is only one directory with pkg config
for child in $children; do
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$(dirname $child)"
return
done
}
function replace_in_files() {
if [ -d "$1" ]; then
find -L $1 -type f \( -name "*.pc" -or -name "*.la" -or -name "*-config" -or -name "*.cmake" \) -exec sed -i 's@'"$2"'@'"$3"'@g' {} ';'
fi
}
printf ""
printf "Bazel external C/C++ Rules #0.0.8. Building library 'postgres'\n"
printf ""
set -e
export EXT_BUILD_ROOT=$(pwd)
export BUILD_TMPDIR=$(mktemp -d)
export EXT_BUILD_DEPS=$(mktemp -d)
export INSTALLDIR=$EXT_BUILD_ROOT/bazel-out/k8-fastbuild/bin/third_party/postgres/postgres
export PATH="$EXT_BUILD_ROOT:$PATH"
mkdir -p $INSTALLDIR
printf "Environment:______________\n"
env
printf "__________________________\n"
symlink_to_dir $EXT_BUILD_ROOT/bazel-out/k8-fastbuild/bin/third_party/openssl/copy_openssl/openssl $EXT_BUILD_DEPS
children_to_path $EXT_BUILD_DEPS/bin
export PATH="$EXT_BUILD_DEPS/bin:$PATH"
cd $BUILD_TMPDIR
export INSTALL_PREFIX="postgres"
increment_pkg_config_path $EXT_BUILD_DEPS/openssl
echo "PKG_CONFIG_PATH=$PKG_CONFIG_PATH"
replace_in_files $EXT_BUILD_DEPS \${EXT_BUILD_DEPS} $EXT_BUILD_DEPS
CFLAGS="-U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\"redacted\" -D__TIMESTAMP__=\"redacted\" -D__TIME__=\"redacted\" -fPIC" \
CXXFLAGS="-U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\"redacted\" -D__TIMESTAMP__=\"redacted\" -D__TIME__=\"redacted\" -fPIC" \
ARFLAGS="rcsD" \
ASFLAGS="-U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\"redacted\" -D__TIMESTAMP__=\"redacted\" -D__TIME__=\"redacted\"" \
LDFLAGS="-fuse-ld=gold -Wl,-no-as-needed -Wl,-z,relro,-z,now -B/usr/bin -pass-exit-codes -lstdc++ -lm -L$EXT_BUILD_DEPS/openssl/lib" \
CC="/usr/bin/gcc" \
CXX="/usr/bin/gcc" \
AR="/usr/bin/ar" \
CPPFLAGS="-I$EXT_BUILD_DEPS/openssl/include" \
"$EXT_BUILD_ROOT/external/postgres/configure" \
--prefix=$BUILD_TMPDIR/$INSTALL_PREFIX \
CFLAGS='-Dredacted=\"redacted\"' \
--with-includes=${EXT_BUILD_DEPS}/openssl/include \
--with-openssl
make -j8
make install
cp -L -r --no-target-directory "$BUILD_TMPDIR/$INSTALL_PREFIX" "$INSTALLDIR"
replace_in_files $INSTALLDIR $BUILD_TMPDIR \${EXT_BUILD_DEPS}
replace_in_files $INSTALLDIR $EXT_BUILD_DEPS \${EXT_BUILD_DEPS}
mkdir -p $EXT_BUILD_ROOT/bazel-out/k8-fastbuild/bin/third_party/postgres/copy_postgres/postgres
cp -L -r --no-target-directory "$INSTALLDIR" "$EXT_BUILD_ROOT/bazel-out/k8-fastbuild/bin/third_party/postgres/copy_postgres/postgres"
touch $EXT_BUILD_ROOT/bazel-out/k8-fastbuild/bin/third_party/postgres/empty_postgres.txt
cd $EXT_BUILD_ROOT
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@rules_foreign_cc//tools/build_defs:configure.bzl", "configure_make")
"""An openssl build file based on a snippet found in the github issue:
https://github.com/bazelbuild/rules_foreign_cc/issues/337
"""
# Read https://wiki.openssl.org/index.php/Compilation_and_Installation
CONFIGURE_OPTIONS = [
"no-comp",
"no-idea",
"no-weak-ssl-ciphers",
]
configure_make(
name = "openssl",
configure_command = "config",
configure_env_vars = select({
"@platforms//os:macos": {"AR": ""},
"//conditions:default": {},
}),
configure_in_place = True,
configure_options = select({
"@platforms//os:macos": [
"ARFLAGS=r",
"no-afalgeng",
"no-asm",
"no-shared",
] + CONFIGURE_OPTIONS,
"//conditions:default": [
"no-shared",
] + CONFIGURE_OPTIONS,
}),
lib_source = "@openssl//:all_files",
make_commands = [
# Assume 8 cores. Parallel build support is difficult to customize based
# on number of cores.
# https://github.com/bazelbuild/rules_foreign_cc/issues/329
"make -j8 build_libs",
"make install_dev",
],
static_libraries = [
"libcrypto.a",
"libssl.a",
],
visibility = ["//visibility:public"],
)
filegroup(
name = "gen_dir",
srcs = [":openssl"],
output_group = "gen_dir",
visibility = ["//visibility:public"],
)
build_test(
name = "openssl_build_test",
targets = ["//third_party/openssl"],
)
all_files = """filegroup(name = "all_files", srcs = glob(["**"]), visibility = ["//visibility:public"])"""
http_archive(
name = "openssl",
sha256 = "aaf2fcb575cdf6491b98ab4829abf78a3dec8402b8b81efc8f23c00d443981bf",
build_file_content = all_files,
strip_prefix = "openssl-1.1.1j",
urls = [
"https://www.openssl.org/source/openssl-1.1.1j.tar.gz",
"https://github.com/openssl/openssl/archive/OpenSSL_1_1_1j.tar.gz",
],
)
load("@rules_foreign_cc//tools/build_defs:configure.bzl", "configure_make")
config_setting(
name = "darwin_build",
values = {"cpu": "darwin"},
)
configure_make(
name = "postgres",
binaries = [
"clusterdb",
"createdb",
"createuser",
"dropdb",
"dropuser",
"initdb",
"pg_archivecleanup",
"pg_basebackup",
"pg_checksums",
"pg_config",
"pg_controldata",
"pg_ctl",
"pg_dump",
"pg_dumpall",
"pg_isready",
"pg_receivewal",
"pg_recvlogical",
"pg_resetwal",
"pg_restore",
"pg_rewind",
"pg_test_fsync",
"pg_test_timing",
"pg_upgrade",
"pg_verifybackup",
"pg_waldump",
"pgbench",
"postgres",
"psql",
"reindexdb",
"vacuumdb",
],
configure_env_vars = select({
# Workaround for libtool no output files specified on MacOS.
# https://github.com/bazelbuild/rules_foreign_cc/issues/338
# https://github.com/bazelbuild/rules_foreign_cc/issues/254
":darwin_build": {
# Necessary to create a fully static linked library.
"CXXFLAGS": "-fPIC",
"CFLAGS": "-fPIC",
# OSX crosstools specific flags
"LDFLAGS": "-undefined error",
"AR": "",
},
"//conditions:default": {
# Necessary to create a fully static linked library.
"CXXFLAGS": "-fPIC",
"CFLAGS": "-fPIC",
},
}),
# Workaround for https://github.com/bazelbuild/rules_foreign_cc/issues/442.
configure_options = [
"CFLAGS='-Dredacted=\\\"redacted\\\"'",
"--with-includes=${EXT_BUILD_DEPS}/openssl/include",
"--with-openssl",
],
lib_source = "@postgres//:all_files",
# Assume 8 cores. Parallel build support is difficult to customize based
# on number of cores.
# https://github.com/bazelbuild/rules_foreign_cc/issues/329
make_commands = [
"make -j8",
"make install",
],
shared_libraries = select({
# Workaround for libtool no output files specified on MacOS.
# https://github.com/bazelbuild/rules_foreign_cc/issues/338
# https://github.com/bazelbuild/rules_foreign_cc/issues/254
":darwin_build": [
"plpgsql.so",
"libpq.dylib",
"libpgtypes.dylib",
],
"//conditions:default": [
"plpgsql.so",
"libpq.so",
"libpgtypes.so",
],
}),
visibility = ["//visibility:public"],
deps = [
"//third_party/openssl",
],
)
all_files = """filegroup(name = "all_files", srcs = glob(["**"]), visibility = ["//visibility:public"])"""
http_archive(
name = "postgres",
build_file_content = all_files,
strip_prefix = "postgresql-13.1",
urls = [
"https://ftp.postgresql.org/pub/source/v13.1/postgresql-13.1.tar.bz2",
],
sha256 = "12345c83b89aa29808568977f5200d6da00f88a035517f925293355432ffe61f",
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment