Skip to content

Instantly share code, notes, and snippets.

@jschaf
Last active January 18, 2024 01:10
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/0b9c716ee759e02df82399da90b6ec8c to your computer and use it in GitHub Desktop.
Save jschaf/0b9c716ee759e02df82399da90b6ec8c to your computer and use it in GitHub Desktop.
Heremetic Postgres

Old rules we used to use build Postgres with Bazel.

We stopped and used precompiled binaries because it was much less effort. Bazel also had a bad habit of recompiling Postgres and OpenSSL often.

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",
"shared",
] + CONFIGURE_OPTIONS,
"//conditions:default": [
] + 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",
],
shared_libraries = select({
"@platforms//os:macos": [
"libssl.dylib",
"libcrypto.dylib",
],
"//conditions:default": [
"libssl.so",
"libcrypto.so",
],
}),
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"],
)
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-libraries=$EXT_BUILD_DEPS/openssl/lib",
"--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",
],
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
all_files = """filegroup(name = "all_files", srcs = glob(["**"]), visibility = ["//visibility:public"])"""
def init_repos_openssl():
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("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
all_files = """filegroup(name = "all_files", srcs = glob(["**"]), visibility = ["//visibility:public"])"""
def init_repos_postgres():
# Use 'postgres' for current target. Use versions for any other target.
http_archive(
name = "postgres",
build_file_content = all_files,
strip_prefix = "postgresql-13.3",
urls = [
"https://ftp.postgresql.org/pub/source/v13.3/postgresql-13.3.tar.bz2",
],
sha256 = "3cd9454fa8c7a6255b6743b767700925ead1b9ab0d7a0f9dcb1151010f8eb4a1",
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment