This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Globals and structs via macros - part 1 of N
[perl5.git] / embed.pl
CommitLineData
5f05dabc 1#!/usr/bin/perl -w
e50aee73 2
5f05dabc
PP
3require 5.003;
4
5sub readsyms (\%$) {
6 my ($syms, $file) = @_;
7 %$syms = ();
8 local (*FILE, $_);
9 open(FILE, "< $file")
10 or die "embed.pl: Can't open $file: $!\n";
11 while (<FILE>) {
12 s/[ \t]*#.*//; # Delete comments.
13 if (/^\s*(\S+)\s*$/) {
14 $$syms{$1} = 1;
15 }
16 }
17 close(FILE);
18}
19
20readsyms %global, 'global.sym';
21readsyms %interp, 'interp.sym';
5f05dabc
PP
22
23sub hide ($$) {
24 my ($from, $to) = @_;
25 my $t = int(length($from) / 8);
26 "#define $from" . "\t" x ($t < 3 ? 3 - $t : 1) . "$to\n";
27}
28sub embed ($) {
29 my ($sym) = @_;
30 hide($sym, "Perl_$sym");
31}
32sub multon ($) {
33 my ($sym) = @_;
34 hide($sym, "(curinterp->I$sym)");
35}
36sub multoff ($) {
37 my ($sym) = @_;
38 hide("I$sym", $sym);
39}
40
41unlink 'embed.h';
42open(EM, '> embed.h')
43 or die "Can't create embed.h: $!\n";
e50aee73
AD
44
45print EM <<'END';
76b72cf1 46/* !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
dc86dda3
NIS
47 This file is built by embed.pl from global.sym and interp.sym.
48 Any changes made here will be lost!
76b72cf1 49*/
e50aee73
AD
50
51/* (Doing namespace management portably in C is really gross.) */
52
820c3be9
PP
53/* EMBED has no run-time penalty, but helps keep the Perl namespace
54 from colliding with that used by other libraries pulled in
55 by extensions or by embedding perl. Allow a cc -DNO_EMBED
56 override, however, to keep binary compatability with previous
57 versions of perl.
58*/
59#ifndef NO_EMBED
60# define EMBED 1
61#endif
62
5f05dabc
PP
63/* Hide global symbols? */
64
e50aee73
AD
65#ifdef EMBED
66
e50aee73
AD
67END
68
5f05dabc 69for $sym (sort keys %global) {
dc86dda3 70 print EM embed($sym);
e50aee73
AD
71}
72
e50aee73
AD
73
74print EM <<'END';
75
76#endif /* EMBED */
77
5f05dabc 78/* Put interpreter-specific symbols into a struct? */
e50aee73
AD
79
80#ifdef MULTIPLICITY
81
82END
83
5f05dabc
PP
84for $sym (sort keys %interp) {
85 print EM multon($sym);
760ac839 86}
760ac839 87
55497cff
PP
88print EM <<'END';
89
5f05dabc 90#else /* !MULTIPLICITY */
55497cff
PP
91
92END
760ac839 93
5f05dabc
PP
94for $sym (sort keys %interp) {
95 print EM multoff($sym);
e50aee73 96}
e50aee73 97
56d28764
CS
98print EM <<'END';
99
5f05dabc
PP
100/* Hide interpreter-specific symbols? */
101
56d28764
CS
102#ifdef EMBED
103
104END
e50aee73 105
5f05dabc 106for $sym (sort keys %interp) {
dc86dda3 107 print EM embed($sym);
5f05dabc
PP
108}
109
110print EM <<'END';
111
56d28764 112#endif /* EMBED */
e50aee73
AD
113#endif /* MULTIPLICITY */
114END
115