Debian Codegraph @

Makefile.PL

raw
1 # X11::GUITest ($Id: Makefile.PL 249 2026-08-15 18:43:40Z ctrondlp $)
2
3 use strict;
4 use Config;
5 use File::Copy qw(copy);
6 use File::Spec;
7 use ExtUtils::MakeMaker;
8
9 # File::Temp became core in Perl 5.6.1; this module otherwise tries to work
10 # with much older Perls (see the 5.005/5.008 guards below), so probing is
11 # simply skipped -- falling back to the old directory-existence guesses --
12 # on anything old enough not to have it.
13 my $HAVE_FILE_TEMP = eval { require File::Temp; File::Temp->import('tempdir'); 1 };
14
15 # --- CLI Handler for "make docs" Callback ---
16 if (@ARGV && $ARGV[0] eq '--build-docs') {
17 BuildDocs();
18 exit 0;
19 }
20
21 # --- EUMM Version Compatibility Guards ---
22 my $eumm_version = $ExtUtils::MakeMaker::VERSION;
23 $eumm_version =~ s/_//g;
24
25 my %extra_params;
26
27 if ($eumm_version >= 6.31) {
28 $extra_params{LICENSE} = 'gpl_2';
29 }
30
31 if ($eumm_version >= 6.46) {
32 $extra_params{META_ADD} = {
33 resources => {
34 homepage => 'https://sourceforge.net/projects/x11guitest/',
35 bugtracker => {
36 web => 'https://sourceforge.net/p/x11guitest/tickets/',
37 },
38 },
39 };
40 $extra_params{META_MERGE} = {
41 resources => {
42 repository => {
43 type => 'svn',
44 url => 'https://svn.code.sf.net/p/x11guitest/code/trunk',
45 web => 'https://sourceforge.net/p/x11guitest/code/HEAD/tree/trunk/',
46 },
47 },
48 };
49 }
50
51 # --- Compiler Flag Portability Guard ---
52 # Only append -Wall if using GCC/Clang to avoid breaking native compilers on HP-UX/Solaris/AIX
53 my $ccflags = $Config{ccflags} || '';
54 if ($Config{gccversion}) {
55 $ccflags .= ' -Wall';
56 }
57
58 # --- Locate X11/XTest headers & libraries ---
59 # Rather than trusting a fixed list of "does this directory exist" guesses,
60 # determine what actually works: prefer pkg-config when it knows about x11/
61 # xtst, otherwise probe candidate locations with a real compile+link and
62 # keep the first one that succeeds. Only if neither approach can confirm a
63 # working combination (e.g. no usable compiler available at configure time,
64 # or File::Temp is unavailable on a very old Perl) do we fall back to the
65 # previous directory-existence heuristic.
66 my ($X11_LIBS, $X11_INC) = DetermineX11Flags();
67
68 WriteMakefile(
69 'NAME' => 'X11::GUITest',
70 ($] ge '5.005') ? (
71 'AUTHOR' => 'Dennis K. Paulsen <[email protected]>',
72 ) : (),
73 'VERSION_FROM' => 'GUITest.pm',
74 'ABSTRACT_FROM' => 'GUITest.pm',
75 ($] lt '5.008') ? (
76 'INST_MAN3DIR' => './blib/man3',
77 'MAN3EXT' => '3pm'
78 ) : (),
79 'MAN3PODS' => {'GUITest.pm' => '$(INST_MAN3DIR)/X11::GUITest.$(MAN3EXT)'},
80 'LIBS' => $X11_LIBS,
81 # If the target X server has Xinerama active, XTestFakeMotionEvent() is
82 # known to misbehave for MoveMouseAbs(). An alternate XWarpPointer()-based
83 # implementation exists but isn't auto-detected -- see the DEPENDENCIES
84 # section in GUITest.pm for how to opt in via X11_GUITEST_USING_XINERAMA.
85 'DEFINE' => '-DNDEBUG -DX11_GUITEST_ALT_L_FALLBACK_META_L',
86 'INC' => $X11_INC,
87 'CCFLAGS' => $ccflags,
88 'OBJECT' => 'GUITest$(OBJ_EXT) KeyUtil$(OBJ_EXT)',
89 'OPTIMIZE' => '-O2',
90 %extra_params,
91 );
92
93
94 # --- Candidate (include, lib) install-tree pairs to probe, in preference order ---
95 # Each pair represents one plausible self-contained X11 installation prefix.
96 # These are only ever used if pkg-config can't tell us, and even then only
97 # the first pair that actually compiles & links a real X11/XTest test
98 # program is kept -- see DetermineX11Flags()/CanBuildX11Test() below.
99 my @X11_PROFILES = (
100 # Linux
101 ['/usr/X11R6/include', '/usr/X11R6/lib64'],
102 ['/usr/X11R6/include', '/usr/X11R6/lib'],
103 ['/usr/include', '/usr/lib64'],
104 ['/usr/include', '/usr/lib'],
105 # FreeBSD / OpenBSD (ports/pkg install under /usr/local)
106 ['/usr/local/include/X11', '/usr/local/lib'],
107 ['/usr/local/include', '/usr/local/lib'],
108 # NetBSD pkgsrc
109 ['/usr/pkg/include', '/usr/pkg/lib'],
110 # Solaris: legacy OpenWindows tree (32-bit and 64-bit), modern /usr/X11
111 # alias, and the two third-party package managers admins commonly rely
112 # on when the base OS has no X11 dev files or toolchain at all.
113 ['/usr/openwin/include', '/usr/openwin/lib'],
114 ['/usr/openwin/include', '/usr/openwin/lib/64'],
115 ['/usr/X11/include', '/usr/X11/lib'],
116 ['/opt/csw/include', '/opt/csw/lib'],
117 ['/usr/sfw/include', '/usr/sfw/lib'],
118 # HP-UX: HP shipped both a "base" and a separate "contrib" X11R6 tree
119 ['/usr/include/X11R6', '/usr/lib/X11R6'],
120 ['/usr/contrib/X11R6/include', '/usr/contrib/X11R6/lib'],
121 ['/opt/X11R6/include', '/opt/X11R6/lib'],
122 # AIX: base X11 fileset, and the AIX Toolbox for Linux Applications
123 # (the common source of open-source dependencies on AIX)
124 ['/usr/lpp/X11/include', '/usr/lpp/X11/lib'],
125 ['/usr/lpp/X11/include', '/usr/lpp/X11/lib64'],
126 ['/opt/freeware/include', '/opt/freeware/lib'],
127 # Misc legacy alias seen on some Unixes
128 ['/usr/X/include', undef],
129 );
130
131 # --- Determines the LIBS/INC WriteMakefile args for X11 + XTest ---
132 sub DetermineX11Flags {
133 if (my ($libs, $inc) = TryPkgConfig()) {
134 print "Using pkg-config for X11/XTest build flags.\n";
135 return ($libs, $inc);
136 }
137
138 if ($HAVE_FILE_TEMP) {
139 if (CanBuildX11Test()) {
140 print "X11/XTest found via the compiler's default search paths.\n";
141 return ('-lXtst -lX11', '');
142 }
143 for my $profile (@X11_PROFILES) {
144 my ($inc, $lib) = @$profile;
145 next if $inc && !-d $inc;
146 next if $lib && !-d $lib;
147 if (CanBuildX11Test($inc, $lib)) {
148 print "X11/XTest found under: ", join(', ', grep { defined } $inc, $lib), "\n";
149 my @libflags;
150 push @libflags, "-L$lib" if $lib;
151 push @libflags, '-lXtst', '-lX11';
152 return (join(' ', @libflags), $inc ? "-I$inc" : '');
153 }
154 }
155 print "Warning: Could not verify a working X11/XTest combination by ",
156 "compiling a test program. Falling back to directory-existence ",
157 "guesses; the build may still fail.\n";
158 }
159
160 # Last resort: nothing above could confirm a working combination (no
161 # File::Temp, or no compiler available yet), so fall back to the old
162 # "the directory merely exists" heuristic across all candidate dirs.
163 return (LegacyGetLibs(), LegacyGetIncludes());
164 }
165
166 # --- Attempts to compile & link a small X11/XTest program with the given
167 # (optional) include/lib dirs. Returns true if it succeeds. Does not
168 # require a running/reachable X server -- this only needs to link. ---
169 sub CanBuildX11Test {
170 my ($inc, $lib) = @_;
171
172 my $dir = eval { File::Temp::tempdir(CLEANUP => 1) };
173 return 0 unless defined $dir;
174
175 my $src = File::Spec->catfile($dir, 'x11guitest_probe.c');
176 my $exe = File::Spec->catfile($dir, 'x11guitest_probe' . ($Config{_exe} || ''));
177
178 open(my $fh, '>', $src) or return 0;
179 print $fh <<'C_SRC';
180 #include <X11/Xlib.h>
181 #include <X11/extensions/XTest.h>
182 int main(void) {
183 Display *d = XOpenDisplay((char *)0);
184 int ev = 0, er = 0, maj = 0, min = 0;
185 XTestQueryExtension(d, &ev, &er, &maj, &min);
186 return 0;
187 }
188 C_SRC
189 close($fh);
190
191 my $cc = $Config{cc} || 'cc';
192 my $ccflags = $Config{ccflags} || '';
193 my $ldflags = $Config{ldflags} || '';
194 my $incflag = $inc ? "-I$inc" : '';
195 my $libflag = $lib ? "-L$lib" : '';
196 my $devnull = File::Spec->devnull;
197
198 my $ok = system(
199 "$cc $ccflags $incflag \"$src\" -o \"$exe\" $ldflags $libflag -lX11 -lXtst "
200 . ">$devnull 2>&1"
201 ) == 0;
202
203 return ($ok && -x $exe) ? 1 : 0;
204 }
205
206 # --- Tries pkg-config (or pkgconf) for x11/xtst. Returns (libs, cflags) or
207 # an empty list if unavailable/unusable. ---
208 sub TryPkgConfig {
209 my $pkgconfig = _WhichProg('pkg-config') || _WhichProg('pkgconf');
210 return () unless $pkgconfig;
211
212 my $devnull = File::Spec->devnull;
213 return () unless system("$pkgconfig --exists x11 xtst >$devnull 2>&1") == 0;
214
215 my $libs = `$pkgconfig --libs x11 xtst 2>$devnull`;
216 my $cflags = `$pkgconfig --cflags x11 xtst 2>$devnull`;
217 return () unless defined $libs && defined $cflags;
218 chomp($libs, $cflags);
219 return () unless length $libs;
220
221 return ($libs, $cflags);
222 }
223
224 # --- Legacy fallback: was GetLibs()/GetIncludes(). Only used when neither
225 # pkg-config nor a compile probe can confirm a working combination. ---
226 sub LegacyGetLibs {
227 my @found;
228 for my $profile (@X11_PROFILES) {
229 my (undef, $lib) = @$profile;
230 push @found, "-L$lib" if $lib && -d $lib;
231 }
232 push @found, '-lXtst', '-lX11';
233 return join(' ', @found);
234 }
235
236 sub LegacyGetIncludes {
237 my %seen;
238 my @found;
239 for my $profile (@X11_PROFILES) {
240 my ($inc) = @$profile;
241 next unless $inc && -d $inc && !$seen{$inc}++;
242 push @found, "-I$inc";
243 }
244 return join(' ', @found);
245 }
246
247
248 # --- Appends custom target to generated Makefile ---
249 # FORCE is used as a POSIX-compliant substitute for .PHONY across traditional Unix makes
250 sub MY::postamble {
251 return <<'MAKE_FRAG';
252 docs : FORCE
253 $(PERL) Makefile.PL --build-docs
254
255 FORCE:
256 MAKE_FRAG
257 }
258
259
260 # Helper routine callable via "make docs"
261 sub BuildDocs {
262 my $podfile = 'GUITest.pm';
263 my $tools = _FindPodTools();
264
265 unless ($tools->{podchecker} && $tools->{pod2text}) {
266 print "Required POD tools (podchecker/pod2text) not found. Skipping docs.\n";
267 return 0;
268 }
269
270 if (system("$tools->{podchecker} $podfile") != 0) {
271 print "POD validation failed. Documentation generation aborted.\n";
272 return 0;
273 }
274
275 print "Writing documentation for X11::GUITest...\n";
276
277 unless (-d 'docs') {
278 mkdir('docs', 0755) or die "Cannot create docs directory: $!\n";
279 }
280
281 if (system("$tools->{pod2text} $podfile docs/X11-GUITest.txt") == 0) {
282 copy("docs/X11-GUITest.txt", "README");
283 } else {
284 print "Warning: pod2text failed.\n";
285 }
286
287 if ($tools->{pod2html}) {
288 system("$tools->{pod2html} --infile=$podfile --outfile=docs/X11-GUITest.html") == 0
289 or print "Warning: pod2html failed.\n";
290 }
291
292 # Portable cleanup of pod2html working files
293 for my $f ('pod2htmd.tmp', 'pod2htmi.tmp') {
294 unlink $f if -e $f;
295 }
296
297 return 1;
298 }
299
300
301 sub _FindPodTools {
302 my @tools = qw(podchecker pod2text pod2html);
303 my %found;
304
305 foreach my $tool (@tools) {
306 my $path = _WhichProg($tool);
307 $found{$tool} = $path if $path;
308 }
309
310 return \%found;
311 }
312
313 # --- Locates a program on PATH; prefers File::Which when available, else
314 # does its own POSIX-style PATH search. Shared by _FindPodTools() and
315 # TryPkgConfig(). ---
316 sub _WhichProg {
317 my ($prog) = @_;
318
319 eval {
320 require File::Which;
321 File::Which->import('which');
322 };
323 if (defined &which) {
324 return which($prog);
325 }
326
327 my $sep = $Config{path_sep} || ':';
328 foreach my $dir (split /\Q$sep\E/, $ENV{PATH} || '') {
329 my $candidate = File::Spec->catfile($dir, $prog);
330 return $candidate if -x $candidate && ! -d $candidate;
331 }
332 return undef;
333 }