1 ###########################################################
2 ### SMB Build System ###
3 ### - create output for Makefile ###
5 ### Copyright (C) Stefan (metze) Metzmacher 2004 ###
6 ### Copyright (C) Jelmer Vernooij 2005 ###
7 ### Released under the GNU GPL ###
8 ###########################################################
11 use config qw(%config);
14 sub _prepare_path_vars()
18 $config{srcdir} = '.';
19 $config{builddir} = '.';
21 if ($config{prefix} eq "NONE") {
22 $config{prefix} = $config{ac_default_prefix};
25 if ($config{exec_prefix} eq "NONE") {
26 $config{exec_prefix} = $config{prefix};
29 $output = << "__EOD__";
30 prefix = $config{prefix}
31 exec_prefix = $config{exec_prefix}
32 selftest_prefix = $config{selftest_prefix}
33 VPATH = $config{srcdir}
34 srcdir = $config{srcdir}
35 builddir = $config{builddir}
37 BASEDIR = $config{prefix}
38 BINDIR = $config{bindir}
39 SBINDIR = $config{sbindir}
40 datadir = $config{datadir}
41 LIBDIR = $config{libdir}
42 CONFIGDIR = $config{configdir}
43 localstatedir = $config{localstatedir}
44 SWATDIR = $config{swatdir}
45 VARDIR = $config{localstatedir}
46 LOGFILEBASE = $config{logfilebase}
47 NCALRPCDIR = $config{localstatedir}/ncalrpc
48 LOCKDIR = $config{lockdir}
49 PIDDIR = $config{piddir}
50 MANDIR = $config{mandir}
51 PRIVATEDIR = $config{privatedir}
55 $output.= << '__EOD__';
57 # The permissions to give the executables
60 # set these to where to find various files
61 # These can be overridden by command line switches (see smbd(8))
62 # or in smb.conf (see smb.conf(5))
63 CONFIGFILE = $(CONFIGDIR)/smb.conf
64 LMHOSTSFILE = $(CONFIGDIR)/lmhosts
66 PATH_FLAGS = -DCONFIGFILE=\"$(CONFIGFILE)\" -DSBINDIR=\"$(SBINDIR)\" \
67 -DBINDIR=\"$(BINDIR)\" -DLMHOSTSFILE=\"$(LMHOSTSFILE)\" \
68 -DLOCKDIR=\"$(LOCKDIR)\" -DPIDDIR=\"$(PIDDIR)\" -DLIBDIR=\"$(LIBDIR)\" \
69 -DLOGFILEBASE=\"$(LOGFILEBASE)\" -DSHLIBEXT=\"$(SHLIBEXT)\" \
70 -DCONFIGDIR=\"$(CONFIGDIR)\" -DNCALRPCDIR=\"$(NCALRPCDIR)\" \
71 -DSWATDIR=\"$(SWATDIR)\" -DPRIVATE_DIR=\"$(PRIVATEDIR)\"
77 sub _prepare_compiler_linker()
85 CFLAGS=-I\$(srcdir)/include -I\$(srcdir) -I\$(srcdir)/lib -D_SAMBA_BUILD_ -DHAVE_CONFIG_H $config{CFLAGS} $config{CPPFLAGS}
86 PICFLAG=$config{PICFLAG}
87 HOSTCC=$config{HOSTCC}
90 CPPFLAGS=$config{CPPFLAGS}
93 LD_FLAGS=$config{LDFLAGS}
99 SHLD_FLAGS=$config{LDSHFLAGS}
100 SONAMEFLAG=$config{SONAMEFLAG}
101 SHLIBEXT=$config{SHLIBEXT}
103 XSLTPROC=$config{XSLTPROC}
108 PIDL_ARGS=$config{PIDL_ARGS}
112 DEFAULT_TEST_TARGET=$config{DEFAULT_TEST_TARGET}
117 sub _prepare_default_rule()
125 sub _prepare_SUFFIXES()
128 .SUFFIXES: .x .c .et .y .l .d .o .h .h.gch .a .so .1 .1.xml .3 .3.xml .5 .5.xml .7 .7.xml .ho
133 sub _prepare_man_rule($)
138 .$suffix.xml.$suffix:
139 \$(XSLTPROC) -o \$@ http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl \$<
144 sub _prepare_config_status()
146 my @parsed_files = @smb_build::config_mk::parsed_files;
149 foreach (@parsed_files) {
156 Makefile: config.status $deps
162 sub _prepare_binaries($)
169 foreach (values %$ctx) {
170 next unless defined $_->{OUTPUT_TYPE};
171 next unless ($_->{OUTPUT_TYPE} eq "BINARY");
173 next unless defined($_->{INSTALLDIR});
175 push(@sbn_list, $_->{OUTPUT}) if ($_->{INSTALLDIR} eq "SBINDIR");
176 push(@bbn_list, $_->{OUTPUT}) if ($_->{INSTALLDIR} eq "BINDIR");
179 my $bbn = array2oneperline(\@bbn_list);
180 my $sbn = array2oneperline(\@sbn_list);
187 sub _prepare_manpages($)
193 foreach (values %$ctx) {
194 push (@mp_list, "$_->{BASEDIR}/$_->{MANPAGE}") if (defined($_->{MANPAGE}) and $_->{MANPAGE} ne "");
197 my $mp = array2oneperline(\@mp_list);
204 sub _prepare_dummy_MAKEDIR()
208 my $ret = << '__EOD__';
212 dynconfig.o: dynconfig.c Makefile
214 @$(CC) $(CFLAGS) $(PICFLAG) $(PATH_FLAGS) -c $< -o $@
216 if ($config{BROKEN_CC} eq "yes") {
217 $ret .= ' -mv `echo $@ | sed \'s%^.*/%%g\'` $@
223 sub _prepare_depend_CC_rule()
228 @echo "Generating dependencies for $<"
229 @$(CC) -MM -MG -MT $(<:.c=.o) -MF $@ $(CFLAGS) $<
234 ###########################################################
235 # This function creates a standard make rule which is using $(CC)
237 # $output = _prepare_std_CC_rule($srcext,$destext,$flags,$message,$comment)
239 # $srcext - sourcefile extension
241 # $destext - destinationfile extension
243 # $flags - additional compiler flags
245 # $message - logmessage which is echoed while running this rule
247 # $comment - just a comment what this rule should do
249 # $output - the resulting output buffer
250 sub _prepare_std_CC_rule($$$$$)
252 my ($src,$dst,$flags,$message,$comment) = @_;
254 my $ret = << "__EOD__";
257 \@echo $message \$\*.$src
258 \@\$(CC) `script/cflags.sh \$\@` \$(CFLAGS) $flags -c \$< -o \$\@
260 if ($config{BROKEN_CC} eq "yes") {
261 $ret.= ' -mv `echo $@ | sed \'s%^.*/%%g\'` $@
267 sub _prepare_hostcc_rule()
269 my $ret = << "__EOD__";
271 \@echo Compiling \$\*.c with host compiler
272 \@\$(HOSTCC) `script/cflags.sh \$\@` \$(CFLAGS) -c \$< -o \$\@
274 if ($config{BROKEN_CC} eq "yes") {
275 $ret .= ' -mv `echo $@ | sed \'s%^.*/%%g\' -e \'s%\.ho$$%.o%\'` $@
284 sub array2oneperline($)
290 next unless defined($_);
292 $output .= " \\\n\t\t$_";
298 ###########################################################
299 # This function creates a object file list
301 # $output = _prepare_var_obj_list($var, $var_ctx)
303 # $var_ctx - the subsystem context
305 # $var_ctx->{NAME} - the <var> name
306 # $var_ctx->{OBJ_LIST} - the list of objectfiles which sould be linked to this <var>
308 # $output - the resulting output buffer
309 sub _prepare_obj_list($$)
313 my $tmplist = array2oneperline($ctx->{OBJ_LIST});
314 return "" if ($tmplist eq "");
317 $var\_$ctx->{NAME}_OBJS =$tmplist
321 sub _prepare_cflags($$)
325 my $tmplist = array2oneperline($ctx->{CFLAGS});
326 return "" if ($tmplist eq "");
329 $var\_$ctx->{NAME}_CFLAGS =$tmplist
334 ###########################################################
335 # This function creates a make rule for linking a library
337 # $output = _prepare_shared_library_rule($library_ctx)
339 # $library_ctx - the library context
341 # $library_ctx->{NAME} - the library name
343 # $library_ctx->{DEPEND_LIST} - the list of rules on which this library depends
345 # $library_ctx->{LIBRARY_NAME} - the shared library name
346 # $library_ctx->{LIBRARY_REALNAME} - the shared library real name
347 # $library_ctx->{LIBRARY_SONAME} - the shared library soname
348 # $library_ctx->{LINK_LIST} - the list of objectfiles and external libraries
349 # which sould be linked to this shared library
350 # $library_ctx->{LINK_FLAGS} - linker flags used by this shared library
352 # $output - the resulting output buffer
353 sub _prepare_shared_library_rule($)
358 my $tmpdepend = array2oneperline($ctx->{DEPEND_LIST});
359 my $tmpshlink = array2oneperline($ctx->{LINK_LIST});
360 my $tmpshflag = array2oneperline($ctx->{LINK_FLAGS});
362 $output = << "__EOD__";
363 LIBRARY_$ctx->{NAME}_DEPEND_LIST =$tmpdepend
364 LIBRARY_$ctx->{NAME}_SHARED_LINK_LIST =$tmpshlink
365 LIBRARY_$ctx->{NAME}_SHARED_LINK_FLAGS =$tmpshflag
368 $ctx->{TARGET}: \$(LIBRARY_$ctx->{NAME}_DEPEND_LIST) \$(LIBRARY_$ctx->{NAME}_OBJS) bin/.dummy
370 \@\$(SHLD) \$(SHLD_FLAGS) -o \$\@ \\
371 \$(LIBRARY_$ctx->{NAME}_SHARED_LINK_FLAGS) \\
372 \$(LIBRARY_$ctx->{NAME}_SHARED_LINK_LIST)
376 if (defined($ctx->{LIBRARY_SONAME})) {
377 $output .= << "__EOD__";
378 # Symlink $ctx->{LIBRARY_SONAME}
379 bin/$ctx->{LIBRARY_SONAME}: bin/$ctx->{LIBRARY_REALNAME} bin/.dummy
381 \@ln -sf $ctx->{LIBRARY_REALNAME} \$\@
382 # Symlink $ctx->{LIBRARY_NAME}
383 bin/$ctx->{LIBRARY_NAME}: bin/$ctx->{LIBRARY_SONAME} bin/.dummy
385 \@ln -sf $ctx->{LIBRARY_SONAME} \$\@
390 $output .= << "__EOD__";
391 library_$ctx->{NAME}: basics bin/lib$ctx->{LIBRARY_NAME}
398 sub _prepare_mergedobj_rule($)
402 return "" unless $ctx->{TARGET};
404 my $tmpdepend = array2oneperline($ctx->{DEPEND_LIST});
406 my $output = "$ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST = $tmpdepend\n";
408 $output .= "$ctx->{TARGET}: \$($ctx->{TYPE}_$ctx->{NAME}_OBJS)\n";
410 $output .= "\t\@echo \"Pre-Linking $ctx->{TYPE} $ctx->{NAME}\"\n";
411 $output .= "\t@\$(LD) -r \$($ctx->{TYPE}_$ctx->{NAME}_OBJS) -o $ctx->{TARGET}\n";
417 sub _prepare_objlist_rule($)
420 my $tmpdepend = array2oneperline($ctx->{DEPEND_LIST});
422 return "" unless $ctx->{TARGET};
424 my $output = "$ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST = $tmpdepend\n";
425 $output .= "$ctx->{TARGET}: ";
426 $output .= "\$($ctx->{TYPE}_$ctx->{NAME}_DEPEND_LIST) \$($ctx->{TYPE}_$ctx->{NAME}_OBJS)\n";
427 $output .= "\t\@touch $ctx->{TARGET}\n";
432 ###########################################################
433 # This function creates a make rule for linking a library
435 # $output = _prepare_static_library_rule($library_ctx)
437 # $library_ctx - the library context
439 # $library_ctx->{NAME} - the library name
441 # $library_ctx->{DEPEND_LIST} - the list of rules on which this library depends
443 # $library_ctx->{LIBRARY_NAME} - the static library name
444 # $library_ctx->{LINK_LIST} - the list of objectfiles which sould be linked
445 # to this static library
446 # $library_ctx->{LINK_FLAGS} - linker flags used by this static library
448 # $output - the resulting output buffer
449 sub _prepare_static_library_rule($)
454 my $tmpdepend = array2oneperline($ctx->{DEPEND_LIST});
455 my $tmpstlink = array2oneperline($ctx->{LINK_LIST});
456 my $tmpstflag = array2oneperline($ctx->{LINK_FLAGS});
458 $output = << "__EOD__";
459 LIBRARY_$ctx->{NAME}_DEPEND_LIST =$tmpdepend
461 LIBRARY_$ctx->{NAME}_STATIC_LINK_LIST =$tmpstlink
463 $ctx->{TARGET}: \$(LIBRARY_$ctx->{NAME}_DEPEND_LIST) \$(LIBRARY_$ctx->{NAME}_OBJS) bin/.dummy
465 \@\$(STLD) \$(STLD_FLAGS) \$@ \\
466 \$(LIBRARY_$ctx->{NAME}_STATIC_LINK_LIST)
468 library_$ctx->{NAME}: basics $ctx->{TARGET}
475 ###########################################################
476 # This function creates a make rule for linking a binary
478 # $output = _prepare_binary_rule($binary_ctx)
480 # $binary_ctx - the binary context
482 # $binary_ctx->{NAME} - the binary name
483 # $binary_ctx->{BINARY} - the binary binary name
485 # $binary_ctx->{DEPEND_LIST} - the list of rules on which this binary depends
486 # $binary_ctx->{LINK_LIST} - the list of objectfiles and external libraries
487 # which sould be linked to this binary
488 # $binary_ctx->{LINK_FLAGS} - linker flags used by this binary
490 # $output - the resulting output buffer
491 sub _prepare_binary_rule($)
495 my $tmpdepend = array2oneperline($ctx->{DEPEND_LIST});
496 my $tmplink = array2oneperline($ctx->{LINK_LIST});
497 my $tmpflag = array2oneperline($ctx->{LINK_FLAGS});
499 my $output = << "__EOD__";
501 BINARY_$ctx->{NAME}_DEPEND_LIST =$tmpdepend
502 BINARY_$ctx->{NAME}_LINK_LIST =$tmplink
503 BINARY_$ctx->{NAME}_LINK_FLAGS =$tmpflag
505 bin/$ctx->{BINARY}: bin/.dummy \$(BINARY_$ctx->{NAME}_DEPEND_LIST) \$(BINARY_$ctx->{NAME}_OBJS)
507 \@\$(CC) \$(LD_FLAGS) -o \$\@ \\
508 \$\(BINARY_$ctx->{NAME}_LINK_FLAGS) \\
509 \$\(BINARY_$ctx->{NAME}_LINK_LIST) \\
510 \$\(BINARY_$ctx->{NAME}_LINK_FLAGS)
511 binary_$ctx->{BINARY}: basics bin/$ctx->{BINARY}
518 sub _prepare_custom_rule($)
522 $ctx->{NAME}: bin/.TARGET_$ctx->{NAME}
524 bin/.TARGET_$ctx->{NAME}:
526 touch bin/.TARGET_$ctx->{NAME}
530 sub _prepare_clean_rules()
532 my $output = << '__EOD__';
534 @echo Removing headers
535 @-rm -f include/proto.h
536 @echo Removing objects
537 @-find . -name '*.o' -exec rm -f '{}' \;
538 @echo Removing binaries
539 @-rm -f $(BIN_PROGS) $(SBIN_PROGS)
540 @echo Removing dummy targets
542 @echo Removing generated files
543 @-rm -rf librpc/gen_*
544 @-rm -f lib/registry/regf.h lib/registry/tdr_regf*
548 -rm -f include/config.h include/smb_build.h
551 -rm -f config.log config.cache
552 -rm -f samba4-deps.dot
553 -rm -f config.pm config.mk
554 -rm -f lib/registry/winregistry.pc
557 if ($config{developer} eq "yes") {
558 $output .= "\t\@-rm -f \$(_ALL_OBJS_OBJS:.o=.d)\n";
561 $output .= << '__EOD__';
564 -rm -f *.bak *~ */*.bak */*~ */*/*.bak */*/*~ */*/*/*.bak */*/*/*~
566 realdistclean: distclean removebackup
567 -rm -f include/config.h.in
568 -rm -f include/version.h
576 sub _prepare_make_target($)
581 $tmpdepend = array2oneperline($ctx->{DEPEND_LIST});
584 $ctx->{TARGET}: basics $tmpdepend
589 sub _prepare_target_settings($)
594 foreach my $key (values %$CTX) {
595 if (defined($key->{OBJ_LIST})) {
596 $output .= _prepare_obj_list($key->{TYPE}, $key);
597 $output .= _prepare_cflags($key->{TYPE}, $key);
604 sub _prepare_rule_lists($)
609 foreach my $key (values %{$depend}) {
610 next unless defined $key->{OUTPUT_TYPE};
612 ($output .= _prepare_mergedobj_rule($key)) if $key->{OUTPUT_TYPE} eq "MERGEDOBJ";
613 ($output .= _prepare_objlist_rule($key)) if $key->{OUTPUT_TYPE} eq "OBJLIST";
614 ($output .= _prepare_static_library_rule($key)) if $key->{OUTPUT_TYPE} eq "STATIC_LIBRARY";
615 ($output .= _prepare_shared_library_rule($key)) if $key->{OUTPUT_TYPE} eq "SHARED_LIBRARY";
616 ($output .= _prepare_binary_rule($key)) if $key->{OUTPUT_TYPE} eq "BINARY";
617 ($output .= _prepare_custom_rule($key) ) if $key->{TYPE} eq "TARGET";
620 $output .= _prepare_clean_rules();
625 ###########################################################
626 # This function prepares the output for Makefile
628 # $output = _prepare_makefile($OUTPUT)
630 # $OUTPUT - the global OUTPUT context
632 # $output - the resulting output buffer
633 sub _prepare_makefile($)
638 $output = "############################################\n";
639 $output .= "# Autogenerated by build/smb_build/main.pl #\n";
640 $output .= "############################################\n";
643 $output .= _prepare_path_vars();
644 $output .= _prepare_compiler_linker();
645 $output .= _prepare_default_rule();
646 $output .= _prepare_SUFFIXES();
647 $output .= _prepare_dummy_MAKEDIR();
648 $output .= _prepare_hostcc_rule();
649 $output .= _prepare_std_CC_rule("c","o",'$(PICFLAG)',"Compiling","Rule for std objectfiles");
650 $output .= _prepare_std_CC_rule("h","h.gch",'$(PICFLAG)',"Precompiling","Rule for precompiled headerfiles");
652 $output .= _prepare_depend_CC_rule();
654 $output .= _prepare_man_rule("1");
655 $output .= _prepare_man_rule("3");
656 $output .= _prepare_man_rule("5");
657 $output .= _prepare_man_rule("7");
658 $output .= _prepare_manpages($CTX);
659 $output .= _prepare_binaries($CTX);
660 $output .= _prepare_target_settings($CTX);
661 $output .= _prepare_rule_lists($CTX);
662 $output .= _prepare_config_status();
664 if ($config{developer} eq "yes") {
666 #-include \$(_ALL_OBJS_OBJS:.o=.d)
667 IDL_FILES = \$(wildcard librpc/idl/*.idl)
668 \$(patsubst librpc/idl/%.idl,librpc/gen_ndr/ndr_%.c,\$(IDL_FILES)) \\
669 \$(patsubst librpc/idl/%.idl,librpc/gen_ndr/ndr_\%_c.c,\$(IDL_FILES)) \\
670 \$(patsubst librpc/idl/%.idl,librpc/gen_ndr/ndr_%.h,\$(IDL_FILES)): idl
677 ###########################################################
678 # This function creates Makefile from the OUTPUT
681 # create_makefile($OUTPUT)
683 # $OUTPUT - the global OUTPUT context
685 # $output - the resulting output buffer
686 sub create_makefile($$$)
688 my ($CTX, $mk, $file) = @_;
690 open(MAKEFILE,">$file") || die ("Can't open $file\n");
691 print MAKEFILE _prepare_makefile($CTX) . $mk;
694 print "build/smb_build/main.pl: creating $file\n";