Skip to content

Commit

Permalink
Merge pull request #197 from zombodb/oh-no-mac-resolution
Browse files Browse the repository at this point in the history
Enable new SQL generator on Mac
  • Loading branch information
Hoverbear authored Sep 21, 2021
2 parents 34fe6f1 + 0301e27 commit 6d9c826
Show file tree
Hide file tree
Showing 54 changed files with 301 additions and 147 deletions.
8 changes: 0 additions & 8 deletions .cargo/config

This file was deleted.

23 changes: 15 additions & 8 deletions cargo-pgx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,22 @@ The flags are typically set by a linker script:
```bash
#! /usr/bin/env bash
# Auto-generated by pgx. You may edit this, or delete it to have a new one created.
if [[ $CARGO_BIN_NAME == "sql-generator" ]]; then
gcc -Wl,-undefined,dynamic_lookup,-dynamic-list=$CARGO_MANIFEST_DIR/.cargo/pgx-dynamic-list.txt $@
else
UNAME=$(uname)
if [[ $UNAME == "Darwin" ]]; then
TEMP=$(mktemp pgx-XXX)
echo "*_pgx_internals_*" > ${TEMP}
gcc -exported_symbols_list ${TEMP} $@
rm -rf ${TEMP}
else
TEMP=$(mktemp pgx-XXX)
echo "{ __pgx_internals_*; };" > ${TEMP}
gcc -Wl,-dynamic-list=${TEMP} $@
rm -rf ${TEMP}
fi
else
gcc -Wl,-undefined,dynamic_lookup $@
fi
```
Expand All @@ -571,12 +584,6 @@ Which would be configured in `.cargo/config` for supported targets:
linker = "./.cargo/linker-script.sh"
```
The content of `.cargo/pgx-dynamic-list.txt` would typically be:
```
{ __pgx_internals_*; };
```
Then, a `src/bin/sql-generator.rs` binary would exist with the following:
```rust
Expand Down
8 changes: 4 additions & 4 deletions cargo-pgx/src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub(crate) fn install_extension(
additional_features,
&extdir,
&base_directory,
);
)?;

println!("{} installing {}", " Finished".bold().green(), extname);
Ok(())
Expand Down Expand Up @@ -130,7 +130,7 @@ fn copy_sql_files(
additional_features: Vec<&str>,
extdir: &PathBuf,
base_directory: &PathBuf,
) {
) -> Result<(), std::io::Error> {
let mut dest = base_directory.clone();
dest.push(extdir);

Expand All @@ -147,8 +147,7 @@ fn copy_sql_files(
None,
false,
true,
)
.unwrap();
)?;
let written = std::fs::read_to_string(&dest).unwrap();
let written = filter_contents(written);
std::fs::write(&dest, written).unwrap();
Expand All @@ -169,6 +168,7 @@ fn copy_sql_files(
}
}
}
Ok(())
}

pub(crate) fn find_library_file(extname: &str, is_release: bool) -> PathBuf {
Expand Down
48 changes: 46 additions & 2 deletions cargo-pgx/src/commands/new.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Copyright 2020 ZomboDB, LLC <[email protected]>. All rights reserved. Use of this source code is
// governed by the MIT license that can be found in the LICENSE file.

use std::io::Write;
use std::path::PathBuf;
use std::{
io::Write,
path::PathBuf,
os::unix::fs::PermissionsExt,
};

pub(crate) fn create_crate_template(
path: PathBuf,
Expand All @@ -13,6 +16,8 @@ pub(crate) fn create_crate_template(
create_control_file(&path, name)?;
create_cargo_toml(&path, name)?;
create_dotcargo_config(&path, name)?;
create_linker_script(&path, name)?;
create_sql_generator_binary(&path, name)?;
create_lib_rs(&path, name, is_bgworker)?;
create_git_ignore(&path, name)?;

Expand Down Expand Up @@ -68,6 +73,45 @@ fn create_dotcargo_config(path: &PathBuf, _name: &str) -> Result<(), std::io::Er
Ok(())
}

fn create_linker_script(path: &PathBuf, _name: &str) -> Result<(), std::io::Error> {
let mut filename = path.clone();

filename.push(".cargo");

std::fs::create_dir_all(&filename)?;

filename.push("pgx-linker-script.sh");
let mut file = std::fs::File::create(&filename)?;

file.write_all(include_bytes!("../templates/pgx-linker-script.sh"))?;
drop(file);

let mut perms = std::fs::metadata(&filename)?.permissions();
perms.set_mode(0o755);
std::fs::set_permissions(&filename, perms)?;

Ok(())
}

fn create_sql_generator_binary(path: &PathBuf, name: &str) -> Result<(), std::io::Error> {
let mut filename = path.clone();

filename.push("src");
filename.push("bin");

std::fs::create_dir_all(&filename)?;

filename.push("sql-generator.rs");
let mut file = std::fs::File::create(filename)?;

file.write_all(format!("\
/* Auto-generated by pgx. You may edit this, or delete it to have a new one created. */\n\
pgx::pg_binary_magic!({});\n\
", name).as_bytes())?;

Ok(())
}

fn create_lib_rs(path: &PathBuf, name: &str, is_bgworker: bool) -> Result<(), std::io::Error> {
let mut filename = path.clone();

Expand Down
19 changes: 11 additions & 8 deletions cargo-pgx/src/commands/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ pub(crate) fn generate_schema(
std::fs::Permissions::from_mode(0o755),
)
.unwrap();
let expected_dynamic_list = include_str!("../templates/pgx-dynamic-list.txt");
check_templated_file(
".cargo/pgx-dynamic-list.txt",
expected_dynamic_list.to_string(),
force_default,
)?;
let expected_cargo_config = include_str!("../templates/cargo_config");
check_templated_file(
".cargo/config",
Expand Down Expand Up @@ -146,7 +140,7 @@ pub(crate) fn generate_schema(
println!("{} SQL entities", " Discovering".bold().green(),);
let dsym_path = sql_gen_path.resolve_dsym();
let buffer = ByteView::open(dsym_path.as_deref().unwrap_or(&sql_gen_path))?;
let archive = Archive::parse(&buffer).unwrap();
let archive = Archive::parse(&buffer).expect("Could not parse archive");

let mut fns_to_call = Vec::new();
for object in archive.objects() {
Expand All @@ -161,7 +155,16 @@ pub(crate) fn generate_schema(
}
}
}
_ => panic!("Unable to parse non-ELF symbols. (Please report this, we can probably fix this!)"),
SymbolIterator::MachO(iter) => {
for symbol in iter {
if let Some(name) = symbol.name {
if name.starts_with("__pgx_internals") {
fns_to_call.push(name);
}
}
}
}
_ => panic!("Unable to parse non-ELF or Mach0 symbols. (Please report this, we can probably fix this!)"),
},
Err(e) => {
panic!("Got error inspecting objects: {}", e);
Expand Down
1 change: 1 addition & 0 deletions cargo-pgx/src/templates/lib_rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn hello_{name}() -> &'static str {{
}}

#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {{
use pgx::*;

Expand Down
1 change: 0 additions & 1 deletion cargo-pgx/src/templates/pgx-dynamic-list.txt

This file was deleted.

15 changes: 13 additions & 2 deletions cargo-pgx/src/templates/pgx-linker-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
# Auto-generated by pgx. You may edit this, or delete it to have a new one created.

if [[ $CARGO_BIN_NAME == "sql-generator" ]]; then
gcc -Wl,-undefined,dynamic_lookup,-dynamic-list=$CARGO_MANIFEST_DIR/.cargo/pgx-dynamic-list.txt $@
else
UNAME=$(uname)
if [[ $UNAME == "Darwin" ]]; then
TEMP=$(mktemp pgx-XXX)
echo "*_pgx_internals_*" > ${TEMP}
gcc -exported_symbols_list ${TEMP} $@
rm -rf ${TEMP}
else
TEMP=$(mktemp pgx-XXX)
echo "{ __pgx_internals_*; };" > ${TEMP}
gcc -Wl,-dynamic-list=${TEMP} $@
rm -rf ${TEMP}
fi
else
gcc -Wl,-undefined,dynamic_lookup $@
fi
1 change: 0 additions & 1 deletion pgx-examples/aggregate/.cargo/pgx-dynamic-list.txt

This file was deleted.

15 changes: 13 additions & 2 deletions pgx-examples/aggregate/.cargo/pgx-linker-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
# Auto-generated by pgx. You may edit this, or delete it to have a new one created.

if [[ $CARGO_BIN_NAME == "sql-generator" ]]; then
gcc -Wl,-undefined,dynamic_lookup,-dynamic-list=$CARGO_MANIFEST_DIR/.cargo/pgx-dynamic-list.txt $@
else
UNAME=$(uname)
if [[ $UNAME == "Darwin" ]]; then
TEMP=$(mktemp pgx-XXX)
echo "*_pgx_internals_*" > ${TEMP}
gcc -exported_symbols_list ${TEMP} $@
rm -rf ${TEMP}
else
TEMP=$(mktemp pgx-XXX)
echo "{ __pgx_internals_*; };" > ${TEMP}
gcc -Wl,-dynamic-list=${TEMP} $@
rm -rf ${TEMP}
fi
else
gcc -Wl,-undefined,dynamic_lookup $@
fi
1 change: 0 additions & 1 deletion pgx-examples/arrays/.cargo/pgx-dynamic-list.txt

This file was deleted.

15 changes: 13 additions & 2 deletions pgx-examples/arrays/.cargo/pgx-linker-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
# Auto-generated by pgx. You may edit this, or delete it to have a new one created.

if [[ $CARGO_BIN_NAME == "sql-generator" ]]; then
gcc -Wl,-undefined,dynamic_lookup,-dynamic-list=$CARGO_MANIFEST_DIR/.cargo/pgx-dynamic-list.txt $@
else
UNAME=$(uname)
if [[ $UNAME == "Darwin" ]]; then
TEMP=$(mktemp pgx-XXX)
echo "*_pgx_internals_*" > ${TEMP}
gcc -exported_symbols_list ${TEMP} $@
rm -rf ${TEMP}
else
TEMP=$(mktemp pgx-XXX)
echo "{ __pgx_internals_*; };" > ${TEMP}
gcc -Wl,-dynamic-list=${TEMP} $@
rm -rf ${TEMP}
fi
else
gcc -Wl,-undefined,dynamic_lookup $@
fi
1 change: 0 additions & 1 deletion pgx-examples/bad_ideas/.cargo/pgx-dynamic-list.txt

This file was deleted.

15 changes: 13 additions & 2 deletions pgx-examples/bad_ideas/.cargo/pgx-linker-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
# Auto-generated by pgx. You may edit this, or delete it to have a new one created.

if [[ $CARGO_BIN_NAME == "sql-generator" ]]; then
gcc -Wl,-undefined,dynamic_lookup,-dynamic-list=$CARGO_MANIFEST_DIR/.cargo/pgx-dynamic-list.txt $@
else
UNAME=$(uname)
if [[ $UNAME == "Darwin" ]]; then
TEMP=$(mktemp pgx-XXX)
echo "*_pgx_internals_*" > ${TEMP}
gcc -exported_symbols_list ${TEMP} $@
rm -rf ${TEMP}
else
TEMP=$(mktemp pgx-XXX)
echo "{ __pgx_internals_*; };" > ${TEMP}
gcc -Wl,-dynamic-list=${TEMP} $@
rm -rf ${TEMP}
fi
else
gcc -Wl,-undefined,dynamic_lookup $@
fi
1 change: 0 additions & 1 deletion pgx-examples/bgworker/.cargo/pgx-dynamic-list.txt

This file was deleted.

15 changes: 13 additions & 2 deletions pgx-examples/bgworker/.cargo/pgx-linker-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
# Auto-generated by pgx. You may edit this, or delete it to have a new one created.

if [[ $CARGO_BIN_NAME == "sql-generator" ]]; then
gcc -Wl,-undefined,dynamic_lookup,-dynamic-list=$CARGO_MANIFEST_DIR/.cargo/pgx-dynamic-list.txt $@
else
UNAME=$(uname)
if [[ $UNAME == "Darwin" ]]; then
TEMP=$(mktemp pgx-XXX)
echo "*_pgx_internals_*" > ${TEMP}
gcc -exported_symbols_list ${TEMP} $@
rm -rf ${TEMP}
else
TEMP=$(mktemp pgx-XXX)
echo "{ __pgx_internals_*; };" > ${TEMP}
gcc -Wl,-dynamic-list=${TEMP} $@
rm -rf ${TEMP}
fi
else
gcc -Wl,-undefined,dynamic_lookup $@
fi
1 change: 0 additions & 1 deletion pgx-examples/bytea/.cargo/pgx-dynamic-list.txt

This file was deleted.

15 changes: 13 additions & 2 deletions pgx-examples/bytea/.cargo/pgx-linker-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
# Auto-generated by pgx. You may edit this, or delete it to have a new one created.

if [[ $CARGO_BIN_NAME == "sql-generator" ]]; then
gcc -Wl,-undefined,dynamic_lookup,-dynamic-list=$CARGO_MANIFEST_DIR/.cargo/pgx-dynamic-list.txt $@
else
UNAME=$(uname)
if [[ $UNAME == "Darwin" ]]; then
TEMP=$(mktemp pgx-XXX)
echo "*_pgx_internals_*" > ${TEMP}
gcc -exported_symbols_list ${TEMP} $@
rm -rf ${TEMP}
else
TEMP=$(mktemp pgx-XXX)
echo "{ __pgx_internals_*; };" > ${TEMP}
gcc -Wl,-dynamic-list=${TEMP} $@
rm -rf ${TEMP}
fi
else
gcc -Wl,-undefined,dynamic_lookup $@
fi
1 change: 0 additions & 1 deletion pgx-examples/custom_sql/.cargo/pgx-dynamic-list.txt

This file was deleted.

15 changes: 13 additions & 2 deletions pgx-examples/custom_sql/.cargo/pgx-linker-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
# Auto-generated by pgx. You may edit this, or delete it to have a new one created.

if [[ $CARGO_BIN_NAME == "sql-generator" ]]; then
gcc -Wl,-undefined,dynamic_lookup,-dynamic-list=$CARGO_MANIFEST_DIR/.cargo/pgx-dynamic-list.txt $@
else
UNAME=$(uname)
if [[ $UNAME == "Darwin" ]]; then
TEMP=$(mktemp pgx-XXX)
echo "*_pgx_internals_*" > ${TEMP}
gcc -exported_symbols_list ${TEMP} $@
rm -rf ${TEMP}
else
TEMP=$(mktemp pgx-XXX)
echo "{ __pgx_internals_*; };" > ${TEMP}
gcc -Wl,-dynamic-list=${TEMP} $@
rm -rf ${TEMP}
fi
else
gcc -Wl,-undefined,dynamic_lookup $@
fi
Loading

0 comments on commit 6d9c826

Please sign in to comment.