Skip to content

Commit

Permalink
compute RUST_CRATES_SOURCES using code
Browse files Browse the repository at this point in the history
  • Loading branch information
declantsien committed Apr 10, 2024
1 parent 3fa4e2d commit 322b9db
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 43 deletions.
17 changes: 12 additions & 5 deletions .envrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
watch_file nix/* flake.nix
{
mkdir -p "$(direnv_layout_dir)"
eval "$(nix print-dev-env --no-update-lock-file --no-write-lock-file --profile $(direnv_layout_dir)/flake-profile)"
} || use nix
export GUILE_LOAD_PATH="$HOME/src/guix-channel/:$GUILE_LOAD_PATH"
use guix

export RUST_SRC_PATH="$HOME/src/rust/library"

export LD_LIBRARY_PATH=$LIBRARY_PATH
rm /tmp/cc
ln -s $(guix build clang)/bin/clang /tmp/cc
export PATH=$PATH:/tmp
export LIBCLANG_PATH=$(guix build clang)/lib
unset EMACSLOADPATH
unset EMACSNATIVELOADPATH
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ nix/_sources/.shake*
nix/cachix-key.secrets
build-aux/config.guess
build-aux/config.sub
src/.RUST_CRATES_SOURCES

# Seccomp filter files.
lib-src/seccomp-filter.bpf
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ libc = "0.2"
regex = "1.10"
cargo_metadata = { version = "0.18", optional = false }
cargo-files-core = { version = "0.2", optional = false }
thiserror = "1.0"

[lib]
path = "lib.rs"

[[bin]]
name = "print-source"
path = "bin/print_source.rs"
Expand Down
20 changes: 7 additions & 13 deletions crates/codegen/bin/print_source.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
use cargo_metadata::Package;
use codegen::packages_source;
use codegen::with_enabled_crates;
use codegen::with_enabled_crates_all;
use codegen::BuildError;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;

/// First we have to generate the include file for the main crate which
/// will be stored in OUT_DIR. It only contains the rust_init_syms
/// that runs the crates *_init_syms functions.
pub fn generate_source_list(packages: Vec<&Package>) -> Result<(), BuildError> {
let out_file = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?)
.join("..")
.join("..")
.join("src")
.join("libemacsng_source");
let mut out_file = File::create(out_file)?;

for file in packages_source(packages) {
write!(out_file, "{} ", file.display())?;
write!(std::io::stdout(), "{} ", file.display())?;
}
Ok(())
}
Expand All @@ -29,7 +19,11 @@ pub fn generate_source_list(packages: Vec<&Package>) -> Result<(), BuildError> {
// build script. For manually run this command, We have specify these
// envs
fn main() -> Result<(), BuildError> {
let _ = with_enabled_crates(|packages| {
for arg in std::env::args().skip(1) {
let key = format!("CARGO_FEATURE_{}", arg.replace("-", "_").to_uppercase());
std::env::set_var(key, "1");
}
let _ = with_enabled_crates_all(|packages| {
match generate_source_list(packages.clone()) {
Err(err) => {
eprintln!("{:?}", err);
Expand Down
63 changes: 56 additions & 7 deletions crates/codegen/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,47 @@ fn resolve_deps_recursively(id: PackageId, deps: &mut Vec<PackageId>, metadata:
deps.append(&mut resolved_deps);
}

fn contains_lisp_macro_crate(crates: Vec<PackageId>, metadata: &Metadata) -> bool {
let lisp_macro_package = lisp_macro_crate(metadata);
fn contains_crate(c: Option<&Package>, crates: &Vec<PackageId>) -> bool {
crates
.into_iter()
.find(|d| lisp_macro_package.map_or(false, |r| r.id == d.clone()))
.find(|d| c.map_or(false, |r| r.id == **d))
.is_some()
}

fn contains_lisp_macro_crate(crates: &Vec<PackageId>, metadata: &Metadata) -> bool {
let c = lisp_macro_crate(metadata);
contains_crate(c, crates)
}

fn contains_emacs_sys_crate(crates: &Vec<PackageId>, metadata: &Metadata) -> bool {
let c = emacs_sys_crate(metadata);
contains_crate(c, crates)
}

// crate are direct dependency of root crate
// for lisp-macro in it's dependency graph should be added to
// c_export.rs
fn needed_for_c_export(id: PackageId, metadata: &Metadata) -> bool {
let mut all_deps: Vec<PackageId> = Vec::new();
resolve_deps_recursively(id, &mut all_deps, &metadata);
contains_lisp_macro_crate(all_deps, metadata)
contains_lisp_macro_crate(&all_deps, metadata)
}

// this predicte is use to check whether the root crate has lisp macro as
// its direct dependency
fn needed_for_lisp_fn_export(id: PackageId, metadata: &Metadata) -> bool {
let deps = resolve_deps(id, &metadata);
contains_lisp_macro_crate(deps, metadata)
contains_lisp_macro_crate(&deps, metadata)
}

// needed for globals.h/DOC
fn needed_for_globals(id: PackageId, metadata: &Metadata) -> bool {
let deps = resolve_deps(id.clone(), &metadata);
if lisp_macro_crate(metadata).map_or(false, |c| c.id == id) {
return false;
}

contains_lisp_macro_crate(&deps, metadata) | contains_emacs_sys_crate(&deps, metadata)
}

pub fn with_metadata<F: FnMut(Metadata) -> Result<(), BuildError>>(
Expand Down Expand Up @@ -108,7 +127,15 @@ pub fn lisp_macro_crate(metadata: &Metadata) -> Option<&Package> {
.find(|p| p.id.repr.contains("lisp-macro"))
}

/// Find list of workspace members with lisp-macro in its dependency graph
pub fn emacs_sys_crate(metadata: &Metadata) -> Option<&Package> {
metadata
.workspace_packages()
.clone()
.into_iter()
.find(|p| p.id.repr.contains("emacs-sys"))
}

/// Find list of workspace members with lisp-macro in its direct dependency graph
pub fn with_enabled_crates<F: FnMut(Vec<&Package>) -> Result<(), BuildError>>(
mut f: F,
) -> Result<(), BuildError> {
Expand All @@ -131,6 +158,29 @@ pub fn with_enabled_crates<F: FnMut(Vec<&Package>) -> Result<(), BuildError>>(
})
}

/// Find list of workspace members with lisp-macro in its dependency graph
pub fn with_enabled_crates_all<F: FnMut(Vec<&Package>) -> Result<(), BuildError>>(
mut f: F,
) -> Result<(), BuildError> {
with_metadata(|metadata| {
let root_package = root_crate(&metadata)?;

let packages = metadata.workspace_packages();
let mut all_deps: Vec<PackageId> = Vec::new();
resolve_deps_recursively(root_package.id.clone(), &mut all_deps, &metadata);

let packages: Vec<&Package> = packages
.into_iter()
.filter(|p| {
all_deps.iter().find(|id| p.id == **id).is_some()
&& needed_for_globals(p.id.clone(), &metadata)
})
.collect();

f(packages)
})
}

pub fn available_features() -> Result<Vec<String>, BuildError> {
let metadata = cargo_metadata::MetadataCommand::new().exec()?;

Expand All @@ -150,7 +200,6 @@ pub fn enabled_features() -> Result<Vec<String>, BuildError> {
// not for default features from Cargo.toml, not sure about `--no-default-features` `--all-features`
for feature in all_features {
let env_key = format!("CARGO_FEATURE_{}", feature.to_uppercase().replace("-", "_"));
println!("cargo:rerun-if-env-changed={}", env_key);
if is_set(env_key.as_str()) {
features.push(feature);
}
Expand Down
17 changes: 9 additions & 8 deletions crates/codegen/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ use std::num::ParseIntError;

use super::LintMsg;
use std::io;
use thiserror::Error;

#[derive(Debug)]
#[derive(Error, Debug)]
pub enum BuildError {
VarError(env::VarError),
#[error("Var error {var:?}: {error:?}")]
VarError { var: String, error: env::VarError },
#[error("{0}")]
IOError(io::Error),
#[error("{0}")]
Metadata(cargo_metadata::Error),
#[error("{0}")]
CargoFiles(cargo_files_core::Error),
#[error("{0}")]
Parse(ParseIntError),
#[error("{0:?}")]
Lint(LintMsg),
}

impl From<env::VarError> for BuildError {
fn from(e: env::VarError) -> Self {
BuildError::VarError(e)
}
}

impl From<io::Error> for BuildError {
fn from(e: io::Error) -> Self {
BuildError::IOError(e)
Expand Down
1 change: 1 addition & 0 deletions crates/codegen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use data::package_targets;
pub use data::packages_source;
use data::target_source;
pub use data::with_enabled_crates;
pub use data::with_enabled_crates_all;
pub use data::with_root_crate;
use data::with_root_crate_checked;
pub use data::Package;
Expand Down
19 changes: 9 additions & 10 deletions src/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,7 @@ LIBEMACSNG_ARCHIVE=libemacsng$(RUST_ARCHIVE_EXTENSION)

crates_dir=$(top_srcdir)/crates
# Crates that contain globals
RUST_CRATES_SOURCES=$(crates_dir)/js/src/*.rs \
$(crates_dir)/lisp-async/src/*.rs \
$(crates_dir)/ng-module/src/*.rs \
$(crates_dir)/font/src/*.rs \
$(crates_dir)/webrender/src/*.rs \
$(crates_dir)/winit-term/src/*.rs \
$(crates_dir)/lsp-json/src/*.rs
RUST_CRATES_SOURCES=.RUST_CRATES_SOURCES
lispsource = $(top_srcdir)/lisp
lib = ../lib
hostlib = $(top_builddir)/lib
Expand Down Expand Up @@ -757,7 +751,7 @@ $(etc)/DOC: $(libsrc)/make-docfile$(EXEEXT) $(doc_obj)
$(AM_V_GEN)$(MKDIR_P) $(etc)
$(AM_V_at)rm -f $(etc)/DOC
$(AM_V_at)$(libsrc)/make-docfile -d $(srcdir) \
$(SOME_MACHINE_OBJECTS) $(doc_obj) $(RUST_CRATES_SOURCES) > $(etc)/DOC
$(SOME_MACHINE_OBJECTS) $(doc_obj) $(file < $(RUST_CRATES_SOURCES)) > $(etc)/DOC

$(libsrc)/make-docfile$(EXEEXT) $(libsrc)/make-fingerprint$(EXEEXT): \
$(hostlib)/libgnu.a
Expand All @@ -770,17 +764,21 @@ buildobj.h: Makefile
done >$@.tmp
$(AM_V_at)mv $@.tmp $@

GLOBAL_SOURCES = $(base_obj:.o=.c) $(NS_OBJC_OBJ:.o=.m) $(RUST_CRATES_SOURCES)
GLOBAL_SOURCES = $(base_obj:.o=.c) $(NS_OBJC_OBJ:.o=.m) $(file < $(RUST_CRATES_SOURCES))

gl-stamp: $(libsrc)/make-docfile$(EXEEXT) $(GLOBAL_SOURCES)
$(AM_V_GLOBALS)$(libsrc)/make-docfile -d $(srcdir) -g $(doc_obj) $(RUST_CRATES_SOURCES) > globals.tmp
$(AM_V_GLOBALS)$(libsrc)/make-docfile -d $(srcdir) -g $(doc_obj) $(file < $(RUST_CRATES_SOURCES)) > globals.tmp
$(AM_V_at)$(top_srcdir)/build-aux/move-if-change globals.tmp globals.h
$(AM_V_at)echo timestamp > $@

globals.h: gl-stamp; @true

HEADERS=$(wildcard $(srcdir)/*.h)

$(RUST_CRATES_SOURCES) : FORCE
cd ../crates/emacsng && \
cargo run -p codegen --bin print-source $(CARGO_FEATURES) > $$OLDPWD/$@

$(LIBEMACSNG_ARCHIVE) : FORCE
RUSTFLAGS="$(RUSTFLAGS)" \
EMACS_CFLAGS="$(EMACS_CFLAGS)" \
Expand Down Expand Up @@ -931,6 +929,7 @@ mostlyclean:
rm -f ./*.res ./*.tmp
$(CARGO_CLEAN) -p emacsng
rm -f ./libemacsng.*
rm -f ./.RUST_CRATES_SOURCES
versionclean:
rm -f emacs$(EXEEXT) emacs-*.*.*[0-9]$(EXEEXT) emacs-*.*.*[0-9].pdmp
rm -f ../etc/DOC*
Expand Down

0 comments on commit 322b9db

Please sign in to comment.