Skip to content

Commit

Permalink
feat(next-swc): embed target triple string (#35416)
Browse files Browse the repository at this point in the history
This PR straightly port over swc's target triple metadata (swc-project/swc#4058) into next-swc, mainly for the tracking purpose of usages.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
  • Loading branch information
kwonoj committed Mar 19, 2022
1 parent 73b83a0 commit 9be56aa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
18 changes: 18 additions & 0 deletions packages/next-swc/crates/napi/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
use std::{
env,
fs::File,
io::{BufWriter, Write},
path::Path,
};

extern crate napi_build;

fn main() {
let out_dir = env::var("OUT_DIR").expect("Outdir should exist");
let dest_path = Path::new(&out_dir).join("triple.txt");
let mut f =
BufWriter::new(File::create(&dest_path).expect("Failed to create target triple text"));
write!(
f,
"{}",
env::var("TARGET").expect("Target should be specified")
)
.expect("Failed to write target triple text");

napi_build::setup();
}
2 changes: 2 additions & 0 deletions packages/next-swc/crates/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ fn init(mut exports: JsObject) -> napi::Result<()> {

exports.create_named_method("parse", parse::parse)?;

exports.create_named_method("getTargetTriple", util::get_target_triple)?;

Ok(())
}

Expand Down
9 changes: 8 additions & 1 deletion packages/next-swc/crates/napi/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ DEALINGS IN THE SOFTWARE.
*/

use anyhow::{Context, Error};
use napi::{CallContext, JsBuffer, Status};
use napi::{CallContext, Env, JsBuffer, JsString, Status};
use serde::de::DeserializeOwned;
use std::any::type_name;

static TARGET_TRIPLE: &str = include_str!(concat!(env!("OUT_DIR"), "/triple.txt"));

#[contextless_function]
pub fn get_target_triple(env: Env) -> napi::ContextlessResult<JsString> {
env.create_string(TARGET_TRIPLE).map(Some)
}

pub trait MapErr<T>: Into<Result<T, anyhow::Error>> {
fn convert_err(self) -> napi::Result<T> {
self.into()
Expand Down
7 changes: 7 additions & 0 deletions packages/next/build/swc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,10 @@ export async function parse(src, options) {
let parserOptions = getParserOptions(options)
return bindings.parse(src, parserOptions).then((astStr) => JSON.parse(astStr))
}

export function getBinaryMetadata() {
let bindings = loadBindingsSync()
return {
target: bindings.getTargetTriple(),
}
}

0 comments on commit 9be56aa

Please sign in to comment.