From 139ec8b964850336ae33f07d40756b53ebb5f681 Mon Sep 17 00:00:00 2001 From: John Anderson Date: Sat, 4 May 2024 09:41:36 -0400 Subject: [PATCH] define for relevant simd --- build.rs | 21 +++++++++++++++++++++ src/wrapper.cc | 6 +++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/build.rs b/build.rs index f9bf781..68755c1 100644 --- a/build.rs +++ b/build.rs @@ -13,13 +13,34 @@ fn main() { std::process::exit(1) } + let wrapper_defs_dir = &env::var_os("OUT_DIR").unwrap(); + let wrapper_defs_dir = Path::new(&wrapper_defs_dir); + let dest_path = wrapper_defs_dir.join("wrapper_defs.h"); + use std::env; + use std::path::Path; + let target_env = std::env::var("TARGET"); + let define_value = match target_env { + // srsly. There must be a better way. + Ok(s) if s == "x86_64-unknown-linux-gnu" => "#define RAPIDJSON_SSE42".into(), + Ok(s) if s == "x86_64-pc-windows-gnu" => "#define RAPIDJSON_SSE42".into(), + Ok(s) if s == "arm64e-apple-darwin"=> "#define RAPIDJSON_NEON".into(), + wut => format!("// whaddya mean {:?}", wut), + }; + if option_env!("RAPIDJSON_SSE42").is_some() { + std::fs::write(&dest_path, define_value).unwrap(); + } else { + std::fs::write(&dest_path, "").unwrap(); + } + cxx_build::bridge("src/rapid.rs") .include(rapidjson_include) + .include(wrapper_defs_dir) .file("src/wrapper.cc") .cpp(true) .std("c++20") .compile("rapid"); + println!("cargo::rerun-if-changed=build.rs"); println!("cargo::rerun-if-changed=src/rapid.rs"); println!("cargo::rerun-if-changed=src/wrapper.cc"); println!("cargo::rerun-if-changed=src/wrapper.h"); diff --git a/src/wrapper.cc b/src/wrapper.cc index e2c56c9..e49c2b3 100644 --- a/src/wrapper.cc +++ b/src/wrapper.cc @@ -5,10 +5,10 @@ // NOTE jch is just what cxx-build wants to call it, because of the cargo package name. #include "jch/src/rapid.rs.h" -// turn on simd instructions -// #define RAPIDJSON_SSE42 +// Turn on simd instructions for the architecture. +// The file is generated by build.rs +#include "wrapper_defs.h" -// TODO vendor or git-submodule this into the build tree #include "rapidjson/fwd.h" #include "rapidjson/rapidjson.h" #include "rapidjson/reader.h"