Skip to content

Commit 1bc752d

Browse files
Support --features trusted on macOS (#334)
With this PR, PL/Rust now supports installation as a Trusted Procedural Language on macOS. It also supports cross compiling between x86_64 and aarch64 on macOS. We do **NOT** support cross-compiling between operating systems. * Adds macos support to CI nightly --------- Co-authored-by: Brady Bonnette <b_bonnette@tcdi.com>
1 parent 6a56c65 commit 1bc752d

File tree

8 files changed

+240
-70
lines changed

8 files changed

+240
-70
lines changed
File renamed without changes.

.github/workflows/ci.yml

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
run: |
5959
cd $WORK_DIR
6060
shopt -s globstar
61-
checksum=$(cat **/Cargo.lock **/rust-toolchain.toml .github/workflows/ci.yml | sha256sum | awk '{print $1}')
61+
checksum=$(cat **/Cargo.lock **/rust-toolchain.toml .github/workflows/ci.yml .cargo/config | sha256sum | awk '{print $1}')
6262
echo "CACHE_KEY_CHECKSUM=$checksum" >> $GITHUB_ENV
6363
6464
- name: Set up (Linux) prerequisites and environment
@@ -267,54 +267,21 @@ jobs:
267267
cargo --version
268268
echo ""
269269
270-
- name: Set up (Mac) prerequisites and environment
271-
if: matrix.os == 'macos-11'
272-
run: |
273-
echo ""
274-
275-
# https://stackoverflow.com/questions/57968497/how-do-i-set-an-env-var-with-a-bash-expression-in-github-actions/57969570#57969570
276-
echo "----- Getting pre-installed Postgres major version -----"
277-
PG_VER=$(pg_config --version | awk '{split($2,a,"."); print a[1]}')
278-
echo "PG_VER=$PG_VER" >> $GITHUB_ENV
279-
cat $GITHUB_ENV
280-
281-
echo "----- Installing yq so .toml files can be parsed and queried -----"
282-
pip3 install yq
283-
echo ""
284-
285-
echo "----- Setting up RUSTFLAGS found in .cargo/config -----"
286-
echo "RUSTFLAGS=$RUSTFLAGS $(tomlq --raw-output '.build.rustflags | join(" ")' .cargo/config)" >> $GITHUB_ENV
287-
cat $GITHUB_ENV
288-
echo ""
289-
290-
echo "----- Set up Postgres permissions -----"
291-
sudo chmod a+rwx `$(which pg_config) --pkglibdir` `$(which pg_config) --sharedir`/extension
292-
ls -lath `$(which pg_config) --pkglibdir` `$(which pg_config) --sharedir`/extension
293-
echo ""
294-
295-
echo "----- Output Cargo version -----"
296-
cargo --version
297-
echo ""
298-
299-
echo "----- Outputting env -----"
300-
env
301-
echo ""
302-
303270
- name: Cache cargo registry
304271
uses: actions/cache@v3
305272
continue-on-error: false
306273
with:
307274
path: |
308275
/home/runner/.cargo
309-
key: v0-plrust-x86_64-cargo-${{ runner.os }}-${{ hashFiles('**/Cargo.lock', '**/rust-toolchain.toml', 'plrustc/.cargo/config.toml', '.github/workflows/ci.yml') }}
276+
key: v0-plrust-x86_64-cargo-${{ runner.os }}-${{ hashFiles('**/Cargo.lock', '**/rust-toolchain.toml', 'plrustc/.cargo/config.toml', '.github/workflows/ci.yml', '.cargo/config') }}
310277

311278
- name: Cache sccache directory
312279
uses: actions/cache@v3
313280
continue-on-error: false
314281
with:
315282
path: |
316283
/home/runner/.cache/sccache
317-
key: v0-plrust-x86_64-sccache-${{ matrix.target }}-${{ runner.os }}-${{ hashFiles('**/Cargo.lock', '**/rust-toolchain.toml', 'plrustc/.cargo/config.toml', '.github/workflows/ci.yml') }}
284+
key: v0-plrust-x86_64-sccache-${{ matrix.target }}-${{ runner.os }}-${{ hashFiles('**/Cargo.lock', '**/rust-toolchain.toml', 'plrustc/.cargo/config.toml', '.github/workflows/ci.yml', '.cargo/config') }}
318285

319286
- name: Start sccache server
320287
run: sccache --start-server && sccache --show-stats

.github/workflows/nightly.yml

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ jobs:
2525
CI: true
2626

2727
steps:
28+
# If this workflow is being called from a schedule/cron job, then let's
29+
# force the "develop" branch. Otherwise, use whatever is passed in via
30+
# GITHUB_HEAD_REF. The result of this will be used below in the
31+
# actions/checkout@vX step. Note that at the time of this writing, Github
32+
# Actions does not allow us to specify which branch to run a schedule from
33+
# (it always runs from the default branch, which in this case is master).
34+
- name: Set up correct branch environment variable
35+
run: |
36+
if [ $GITHUB_EVENT_NAME == "schedule" ]; then
37+
echo "Running via schedule, so using branch develop"
38+
echo "NIGHTLY_BUILD_REF=develop" >> $GITHUB_ENV
39+
else
40+
echo "Not running via schedule, so using branch $GITHUB_HEAD_REF"
41+
echo "NIGHTLY_BUILD_REF=$GITHUB_HEAD_REF" >> $GITHUB_ENV
42+
fi
43+
2844
- uses: actions/checkout@v3
2945

3046
# The default installation of Docker on Github Actions runners are pretty outdated, as the artifact builder
@@ -185,6 +201,22 @@ jobs:
185201
CI: true
186202

187203
steps:
204+
# If this workflow is being called from a schedule/cron job, then let's
205+
# force the "develop" branch. Otherwise, use whatever is passed in via
206+
# GITHUB_HEAD_REF. The result of this will be used below in the
207+
# actions/checkout@vX step. Note that at the time of this writing, Github
208+
# Actions does not allow us to specify which branch to run a schedule from
209+
# (it always runs from the default branch, which in this case is master).
210+
- name: Set up correct branch environment variable
211+
run: |
212+
if [ $GITHUB_EVENT_NAME == "schedule" ]; then
213+
echo "Running via schedule, so using branch develop"
214+
echo "NIGHTLY_BUILD_REF=develop" >> $GITHUB_ENV
215+
else
216+
echo "Not running via schedule, so using branch $GITHUB_HEAD_REF"
217+
echo "NIGHTLY_BUILD_REF=$GITHUB_HEAD_REF" >> $GITHUB_ENV
218+
fi
219+
188220
- uses: actions/checkout@v3
189221

190222
# The default installation of Docker on Github Actions runners are pretty outdated, as the artifact builder
@@ -239,3 +271,93 @@ jobs:
239271
sleep 30
240272
psql -h 127.0.0.1 -U postgres -c 'CREATE FUNCTION plrust.one() RETURNS INT LANGUAGE plrust AS $$ Ok(Some(1)) $$;'
241273
psql -h 127.0.0.1 -U postgres -c 'SELECT * FROM plrust.one()'
274+
275+
plrust_macos_x86_64:
276+
name: macos x86_64 tests
277+
runs-on: ${{ matrix.os }}
278+
if: "!contains(github.event.head_commit.message, 'nogha')"
279+
280+
env:
281+
PLRUST_TRUSTED_PGRX_OVERRIDE: "pgrx = { path = '/Users/runner/work/plrust/plrust/plrust-trusted-pgrx', package='plrust-trusted-pgrx' }"
282+
283+
strategy:
284+
matrix:
285+
version: ["pg14"]
286+
os: ["macos-12"]
287+
target: ["host", "postgrestd"]
288+
fail-fast: false
289+
290+
steps:
291+
# If this workflow is being called from a schedule/cron job, then let's
292+
# force the "develop" branch. Otherwise, use whatever is passed in via
293+
# GITHUB_HEAD_REF. The result of this will be used below in the
294+
# actions/checkout@vX step. Note that at the time of this writing, Github
295+
# Actions does not allow us to specify which branch to run a schedule from
296+
# (it always runs from the default branch, which in this case is master).
297+
- name: Set up correct branch environment variable
298+
run: |
299+
if [ $GITHUB_EVENT_NAME == "schedule" ]; then
300+
echo "Running via schedule, so using branch develop"
301+
echo "NIGHTLY_BUILD_REF=develop" >> $GITHUB_ENV
302+
else
303+
echo "Not running via schedule, so using branch $GITHUB_HEAD_REF"
304+
echo "NIGHTLY_BUILD_REF=$GITHUB_HEAD_REF" >> $GITHUB_ENV
305+
fi
306+
307+
- uses: actions/checkout@v3
308+
309+
- name: Set up (Mac) prerequisites and environment
310+
run: |
311+
echo ""
312+
313+
echo "----- Getting pre-installed Postgres major version -----"
314+
PG_VER=$(pg_config --version | awk '{split($2,a,"."); print a[1]}')
315+
echo "PG_VER=$PG_VER" >> $GITHUB_ENV
316+
cat $GITHUB_ENV
317+
318+
echo "----- Set up Postgres permissions -----"
319+
sudo chmod a+rwx `$(which pg_config) --pkglibdir` `$(which pg_config) --sharedir`/extension
320+
ls -lath `$(which pg_config) --pkglibdir` `$(which pg_config) --sharedir`/extension
321+
echo ""
322+
323+
echo "----- Output Cargo version -----"
324+
cargo --version
325+
echo ""
326+
327+
echo "----- Outputting env -----"
328+
env
329+
echo ""
330+
331+
# See <plrust-root>/.github/scripts/install_cargo_pgrx.sh for more details
332+
- name: Install cargo-pgrx defined in plrust/Cargo.toml
333+
run: |
334+
. $GITHUB_WORKSPACE/.github/scripts/install_cargo_pgrx.sh
335+
install_cargo_pgrx
336+
337+
- name: Install llvm-tools-preview
338+
run: rustup component add llvm-tools-preview rustc-dev
339+
340+
- name: Create protected files
341+
run: |
342+
sudo mkdir -p /var/ci-stuff/secret_rust_files
343+
sudo echo "const FOO:i32 = 7;" /var/ci-stuff/secret_rust_files/const_foo.rs
344+
sudo echo "const BAR:i32 = 8;" /var/ci-stuff/const_bar.rs
345+
sudo chmod -R 600 /var/ci-stuff/secret_rust_files
346+
if: matrix.target == 'postgrestd'
347+
348+
- name: Test plrustc
349+
run: cd plrustc && cargo test -p plrustc
350+
351+
- name: Install plrustc
352+
run: cd plrustc && ./build.sh && cp ../build/bin/plrustc ~/.cargo/bin
353+
354+
- name: Run 'cargo pgrx init' against system-level ${{ matrix.version }}
355+
run: cargo pgrx init --pg$PG_VER $(which pg_config)
356+
357+
- name: Test PL/rust as "untrusted"
358+
if: matrix.target == 'host'
359+
run: cargo test --all --features "pg$PG_VER" --no-default-features
360+
361+
- name: Test PL/rust as "trusted" (inc. postgrestd)
362+
if: matrix.target == 'postgrestd'
363+
run: cd plrust && STD_TARGETS="x86_64-apple-darwin-postgres" ./build && cargo test --verbose --no-default-features --features "pg$PG_VER trusted"

doc/src/config-pg.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,20 @@ This is the name of the linker `rustc` should use on for cross-compile.
107107
The architecture linker names have sensible defaults and shouldn't need to be be
108108
changed (unless the host is some esoteric Linux distribution we have not encountered yet).
109109

110+
Linux defaults:
111+
110112
```bash
111113
plrust.x86_64_linker = 'x86_64_linux_gnu_gcc'
112114
plrust.aarch64_linker = 'aarch64_linux_gnu_gcc'
113115
```
114116

117+
macOS defaults:
118+
119+
```bash
120+
plrust.x86_64_linker = 'cc'
121+
plrust.aarch64_linker = 'cc'
122+
```
123+
115124

116125

117126
#### `plrust.{arch}_pgrx_bindings_path` (string)

doc/src/install-plrust.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,20 @@ uses `x86_64` and ensures the target is installed. If you are using `aarch64`,
3939
update the command accordingly.
4040

4141

42+
Linux:
43+
4244
```bash
4345
rustup component add llvm-tools-preview rustc-dev
4446
rustup target install x86_64-unknown-linux-gnu
4547
```
4648

49+
macOS:
50+
51+
```bash
52+
rustup component add llvm-tools-preview rustc-dev
53+
rustup target install x86_64-apple-darwin
54+
```
55+
4756
Change into the `plrust/plrustc` directory to build `plrustc`.
4857
Move the generated binary into `~/.cargo/bin/`.
4958

@@ -115,6 +124,8 @@ Adding cross compilation support to PL/Rust requires a few minor changes to the
115124
the changes to make for cross compile support, not the full process.
116125

117126

127+
#### Linux
128+
118129
As a Linux user with `sudo` access, install these additional prerequisites.
119130

120131

@@ -132,14 +143,15 @@ rustup target install x86_64-unknown-linux-gnu
132143
```
133144

134145

135-
Update the `STD_TARGETS` used when building `postgrestd` to include both architectures.
136-
This step will take longer with cross compilation then only one architectures, as
137-
it is required to double some of the work.
146+
#### macOS
147+
148+
The normal trusted install uses `rustup` to install one architecture target.
149+
Cross compilation support requires both.
138150

139151
```bash
140-
PG_VER=15 \
141-
STD_TARGETS="x86_64-postgres-linux-gnu aarch64-postgres-linux-gnu" \
142-
./build
152+
rustup component add llvm-tools-preview rustc-dev
153+
rustup target install aarch64-apple-darwin
154+
rustup target install x86_64-apple-darwin
143155
```
144156

145157
> The above environment variables are the default... you can just run `./build`. `PG_VER=15` currently represents the latest released PostgreSQL version.

plrust/build

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,48 @@ set -xe
33

44
if [ -z "$STD_TARGETS" ]; then
55
# if none specified, always build for these two targets
6-
STD_TARGETS="x86_64-postgres-linux-gnu aarch64-postgres-linux-gnu"
6+
if [ `uname` == "Darwin" ]; then
7+
STD_TARGETS="x86_64-apple-darwin-postgres aarch64-apple-darwin-postgres"
8+
else
9+
STD_TARGETS="x86_64-postgres-linux-gnu aarch64-postgres-linux-gnu"
10+
fi
711
fi
812

913
# and depending on the platform we're building on, we need to set a linker flag for the other
1014
# this'll get hairy when we support more platforms and we should port this script to Rust
11-
if [ `uname -m` == "x86_64" ]; then
12-
if [[ -z "$CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER" ]] && [[ -z "$CARGO_TARGET_AARCH64_POSTGRES_LINUX_GNU_LINKER" ]]; then
13-
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
14-
export CARGO_TARGET_AARCH64_POSTGRES_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
15-
fi
16-
elif [ `uname -m` == "aarch64" ]; then
17-
if [[ -z "$CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER" ]] && [[ -z "$CARGO_TARGET_X86_64_POSTGRES_LINUX_GNU_LINKER" ]]; then
18-
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc
19-
export CARGO_TARGET_X86_64_POSTGRES_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc
20-
fi
15+
16+
if [ `uname` == "Darwin" ]; then
17+
if [ `uname -m` == "arm64" ]; then
18+
if [[ -z "$CARGO_TARGET_AARCH64_APPLE_DARWIN_LINKER" ]] && [[ -z "$CARGO_TARGET_AARCH64_APPLE_DARWIN_POSTGRES_LINKER" ]]; then
19+
export CARGO_TARGET_AARCH64_APPLE_DARWIN_LINKER=cc
20+
export CARGO_TARGET_AARCH64_APPLE_DARWIN_POSTGRES_LINKER=cc
21+
fi
22+
elif [ `uname -m` == "x86_64" ]; then
23+
if [[ -z "$CARGO_TARGET_X86_64_APPLE_DARWIN_LINKER" ]] && [[ -z "$CARGO_TARGET_X86_64_APPLE_DARWIN_POSTGRES_LINKER" ]]; then
24+
export CARGO_TARGET_X86_64_APPLE_DARWIN_LINKER=cc
25+
export CARGO_TARGET_X86_64_APPLE_DARWIN_POSTGRES_LINKER=cc
26+
fi
27+
28+
else
29+
echo unsupported macos build platform: $(uname -m)
30+
exit 1
31+
fi
32+
2133
else
22-
echo unsupported build platform: $(uname -m)
23-
exit 1
34+
if [ `uname -m` == "x86_64" ]; then
35+
if [[ -z "$CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER" ]] && [[ -z "$CARGO_TARGET_AARCH64_POSTGRES_LINUX_GNU_LINKER" ]]; then
36+
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
37+
export CARGO_TARGET_AARCH64_POSTGRES_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
38+
fi
39+
elif [ `uname -m` == "aarch64" ]; then
40+
if [[ -z "$CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER" ]] && [[ -z "$CARGO_TARGET_X86_64_POSTGRES_LINUX_GNU_LINKER" ]]; then
41+
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc
42+
export CARGO_TARGET_X86_64_POSTGRES_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc
43+
fi
44+
else
45+
echo unsupported build platform: $(uname -m)
46+
exit 1
47+
fi
2448
fi
2549

2650
# Make sure the tip of pgrx's develop branch is used,
@@ -45,7 +69,7 @@ fi
4569
git clone https://github.com/tcdi/postgrestd.git --branch "rust-1.70.0" --recurse-submodules
4670
cd ./postgrestd
4771
fi
48-
rm --force rust-toolchain.toml
72+
rm -f rust-toolchain.toml
4973
STD_TARGETS="$STD_TARGETS" ./run clean
5074
STD_TARGETS="$STD_TARGETS" ./run install
5175
)

plrust/src/lib.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ Use of this source code is governed by the PostgreSQL license that can be found
1212

1313
#[cfg(all(
1414
feature = "trusted",
15-
not(all(
16-
target_os = "linux",
17-
any(target_arch = "x86_64", target_arch = "aarch64")
18-
))
15+
not(any(
16+
all(
17+
target_os = "linux",
18+
any(target_arch = "x86_64", target_arch = "aarch64")
19+
),
20+
all(
21+
target_os = "macos",
22+
any(target_arch = "x86_64", target_arch = "aarch64")
23+
)
24+
)
25+
)
1926
))]
2027
compile_error!("This platform does not support the 'trusted' version of plrust");
2128

0 commit comments

Comments
 (0)