Skip to content

Commit

Permalink
libjade multirepo support : take 1
Browse files Browse the repository at this point in the history
  • Loading branch information
tfaoliveira-sb committed Aug 5, 2024
1 parent fd181ea commit 4450719
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 28 deletions.
97 changes: 97 additions & 0 deletions scripts/releaser/extract-libjade
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env bash

# pro user tip (because this repository shares the same structure as in libjade); assumes libjade is next to formosa-25519
#
# ./scripts/releaser/extract-libjade --list-implementations | while read implementation; do ./scripts/releaser/extract-libjade --gen-implementation $implementation ../libjade/$(realpath --relative-to="$(pwd)" $implementation); done
#

call=$0
script_dir=$(cd "$(dirname "$0")" ; pwd -P)
top_dir=$(cd "$(dirname "$0")/../../" ; pwd -P)

print_usage()
{
echo "usage:"
echo " $ $call --list-implementations"
echo " $ $call --gen-implementation IMPLEMENTATION DIRECTORY"
}

# if there are no arguments, print usage
if [ $# -eq 0 ]; then
print_usage
exit 1
fi

# check if --list-implementations
if [ "$1" == "--list-implementations" ]; then
make --no-print-directory -C $top_dir/src print-available-implementations
exit 1
fi

# check if --gen-implementation
if [ "$1" == "--gen-implementation" ]; then
# if we are generating an implementation then there should be 3 args
if [ $# -eq 3 ]; then

# start by realpath them (useful to run make)
implementation=$(realpath $2)
directory=$(realpath $3)

# test if IMPLEMENTATION directory exists
if [ ! -d "$implementation" ]; then
echo " IMPLEMENTATION: $implementation does not exist."
exit 1;
fi

# test if libjade DIRECTORY exists
if [ ! -d "$directory" ]; then
echo " DIRECTORY: $directory does not exist."
exit 1;
fi

relative_implementation=$(realpath --relative-to="$top_dir/src" "$implementation")
make --no-print-directory -C $top_dir/src/ $relative_implementation/preprocess-inplace

#############################################################################
# copy the preprocessed files

jazz_files=$(find $implementation -name '*.jazz')
for file in $jazz_files; do
cp $file $directory/
done

# setup the Makefile
echo -n "SRCS := " > $directory/Makefile
for file in $jazz_files; do
echo -n $(basename $file) >> $directory/Makefile
done
echo "" >> $directory/Makefile

# NOTE: the following line will need change (or be deleted) once multi-repo libjade is stable
echo "include ../../../../Makefile.common" >> $directory/Makefile

# NOTE: the following command will change once there is a PR in libjade to move api.h files out of include/ directories
cp $implementation/include/api.h $directory/include/api.h

#
#############################################################################

# restore implementation state
make --no-print-directory -C $top_dir/src/ $relative_implementation/revert-preprocess-inplace

exit 1;

else
echo "error: --gen-implementation : number of required arguments 3 : provided $#"
print_usage
exit 1
fi
fi

# with 'good' options this should be unreachable, hence, print usage
print_usage
exit 1




36 changes: 27 additions & 9 deletions scripts/releaser/jpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ sub jpp_main()
my $dry_run = 0; # if 1, do not write to any file
my $safe_reverse = 1; # if reverse, ask the user if the changes are ok

my $bor_prefix = undef; # path to be removed from the full path in REQUIRE_ENTRY_TAG

# get the command-line arguments
GetOptions(
'in=s' => \$in,
Expand All @@ -51,7 +53,8 @@ sub jpp_main()
'reverse' => \$reverse,
'help|h' => \$help,
'dry-run' => \$dry_run,
'check-diff!' => \$safe_reverse
'check-diff!' => \$safe_reverse,
'bor-prefix|b=s' => \$bor_prefix
);

# check -help or -h
Expand All @@ -66,7 +69,7 @@ sub jpp_main()

if($reverse)
{
my ($reverse_hr) = jpp_reverse($in, $out, $dry_run, $safe_reverse, $verbose);
my ($reverse_hr) = jpp_reverse($in, $out, $dry_run, $safe_reverse, $verbose, $bor_prefix);
}
else
{ # return variables description:
Expand All @@ -75,7 +78,7 @@ sub jpp_main()
# visited_hr: hash (reference) intuitively, 'children' -> '[parents]'
# for each file (canonical path) that was visited, visited_hr contains the list of files that have it required (in the context of preprocessing in)
# preprocessed_ar: array (reference) with the contents of the preprocessed file (if out is defined, then contents for preprocessed_ar were printed to the output file)
my ($preprocessed_ar, $visited_hr, $include_hr) = jpp($in, $out, \@include_a, $with_reverse_info, $dry_run, $verbose);
my ($preprocessed_ar, $visited_hr, $include_hr) = jpp($in, $out, \@include_a, $with_reverse_info, $dry_run, $verbose, $bor_prefix);
}
}

Expand All @@ -89,6 +92,7 @@ sub jpp()
my $with_reverse_info = shift; # with_reverse_info (-with_reverse_info)
my $dry_run = shift; # do not write to files (-dry-run)
my $verbose = shift; # verbose (-v)
my $bor_prefix = shift;

my %visited_h = (); # 'children' -> '[parents]'

Expand All @@ -110,7 +114,7 @@ sub jpp()
# - it receives as arguments:
# -- $in (first file); $include_hr (hash ref. to "-I"'s); \%visited_h (hash ref to visited files, initially empty);
# -- verbose (to print some debug info); level (to keep track of current search depth and do indentation, initially 0);
my ($preprocessed_ar, $visited_hr) = preprocess($in, "entry", $include_hr, \%visited_h, $with_reverse_info, $verbose, 0);
my ($preprocessed_ar, $visited_hr) = preprocess($in, "entry", $include_hr, \%visited_h, $with_reverse_info, $verbose, 0, $bor_prefix);

store($out, $preprocessed_ar, $dry_run);
print_visited($visited_hr) if($verbose);
Expand All @@ -127,6 +131,8 @@ sub jpp_reverse()
my $dry_run = shift; # do not write to files (-dry-run), print to stdout instead
my $safe_reverse = shift; # ask the user about changes in files
my $verbose = shift; # verbose (-v)
my $bor_prefix = shift;

my %reverse_h = (); # 'files' -> '[code]' (final step is to update files with the code)

# get the absolute path and load the contents of the file
Expand All @@ -136,7 +142,7 @@ sub jpp_reverse()
# if out is not defined, update inplace (write the output in $in)
$out = (defined $out) ? realpath($out) : $in;

my ($line_position, $reverse_hr) = preprocess_reverse($in_lines_ar, 0, \%reverse_h, $out, $verbose);
my ($line_position, $reverse_hr) = preprocess_reverse($in_lines_ar, 0, \%reverse_h, $out, $verbose, $bor_prefix);

if( check_changes_reverse($in, $out, $reverse_hr, $safe_reverse) == 1 )
{ store_reverse($reverse_hr, $dry_run); }
Expand Down Expand Up @@ -193,6 +199,7 @@ sub preprocess
my $with_reverse_info = shift; # include information to undo preprocessing
my $verbose = shift; # print some internal information
my $level = shift; # current level of recursion
my $bor_prefix = shift;

# if file was already visited, then return an empty list
if (defined $visited_hr->{$in})
Expand Down Expand Up @@ -225,12 +232,18 @@ sub preprocess
# otherwise concat with base from in
$file = realpath($file);

my $file_path_to_print = $file;

# remove from file some prefix
if(defined $bor_prefix)
{ $file_path_to_print =~ s/^\Q$bor_prefix\E//; }

###
if ($with_reverse_info)
{ my $r_string = $REQUIRE_ENTRY_TAG .
$line .
$REQUIRE_SEPARATOR_TAG .
$file;
$file_path_to_print;
push (@preprocessed, $r_string);
} # NOTE: this includes repetead files (it is not a problem; in the event that one would like to remove these from the code, some quick patch might be able to do it;)
###
Expand All @@ -239,7 +252,7 @@ sub preprocess

# avoid calling preprocess for a file that was already required
if (not defined $visited_hr->{$file})
{ my ($sublines_ar, $visited_hr) = preprocess($file, $in, $include_hr, $visited_hr, $with_reverse_info, $verbose, $level+1); # fetch contents of other files
{ my ($sublines_ar, $visited_hr) = preprocess($file, $in, $include_hr, $visited_hr, $with_reverse_info, $verbose, $level+1, $bor_prefix); # fetch contents of other files
push @preprocessed, @$sublines_ar; # add to the array the lines from the requires
}

Expand Down Expand Up @@ -359,7 +372,7 @@ sub store_reverse

sub preprocess_reverse
{
my ($lines_ar, $line_position, $reverse_hr, $current_file_name, $verbose) = @_;
my ($lines_ar, $line_position, $reverse_hr, $current_file_name, $verbose, $bor_prefix) = @_;

# initialize array for code (if not defined)
if (not defined $reverse_hr->{$current_file_name})
Expand All @@ -375,8 +388,13 @@ sub preprocess_reverse
# if ENTRY_tag, call preprocess_reverse
if($line =~ /^$REQUIRE_ENTRY_TAG/)
{ my ($tag, $require_statement, $file_name) = split($REQUIRE_SEPARATOR_TAG, $line);

# add to file some prefix
if(defined $bor_prefix)
{ $file_name = $bor_prefix . $file_name; }

push @{$reverse_hr->{$current_file_name}}, $require_statement;
($line_position, $reverse_hr) = preprocess_reverse($lines_ar, ($line_position+1), $reverse_hr, $file_name, $verbose);
($line_position, $reverse_hr) = preprocess_reverse($lines_ar, ($line_position+1), $reverse_hr, $file_name, $verbose, $bor_prefix);
next;
}

Expand Down
7 changes: 4 additions & 3 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.jpp
*.s
*.safety
*.safety_*
Expand All @@ -6,11 +7,11 @@
*.o
*.a
_build/
libjade.a
libjade.h
libformosa25519.a
libformosa25519.h
.deps
.ec
.log
.error
.jflags
.JFLAGS
.ci/
Loading

0 comments on commit 4450719

Please sign in to comment.