Move everything from /var/adm to /var/log
[unleashed/lotheac.git] / usr / src / lib / README.Makefiles
blob55b07ac6ed8c189b4b706c70003614a06f4af683
2 # CDDL HEADER START
4 # The contents of this file are subject to the terms of the
5 # Common Development and Distribution License (the "License").
6 # You may not use this file except in compliance with the License.
8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 # or http://www.opensolaris.org/os/licensing.
10 # See the License for the specific language governing permissions
11 # and limitations under the License.
13 # When distributing Covered Code, include this CDDL HEADER in each
14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 # If applicable, add the following below this CDDL HEADER, with the
16 # fields enclosed by brackets "[]" replaced with your own identifying
17 # information: Portions Copyright [yyyy] [name of copyright owner]
19 # CDDL HEADER END
22 # Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23 # Use is subject to license terms.
25 # ident "%Z%%M% %I%     %E% SMI"
28 Writing Library Makefiles in ON
29 ===============================
31 Introduction
32 ------------
34 This document guides you through the gnarly process of writing library
35 Makefiles for the ON consolidation.  It assumes that you're comfortable with
36 make(1) and are somewhat familiar with the ON Makefile standards outlined in
37 /shared/ON/general_docs/make_std.txt.
39 Makefile Overview
40 -----------------
42 Your library should consist of a hierarchical collection of Makefiles:
44         lib/<library>/Makefile:
46           This is your library's top-level Makefile.  It should contain rules
47           for building any ISA-independent targets, such as installing header
48           files and building message catalogs, but should defer all other
49           targets to ISA-specific Makefiles.
51         lib/<library>/Makefile.com
53           This is your library's common Makefile.  It should contain rules
54           and macros which are common to all ISAs. This Makefile should never
55           be built explicitly, but instead should be included (using the make
56           include mechanism) by all of your ISA-specific Makefiles.
58         lib/<library>/<isa>/Makefile
60           These are your library's ISA-specific Makefiles, one per ISA
61           (usually sparc and i386, and often sparcv9 and amd64).  These
62           Makefiles should include your common Makefile and then provide any
63           needed ISA-specific rules and definitions, perhaps overriding those
64           provided in your common Makefile.
66 To simplify their maintenance and construction, $(SRC)/lib has a handful of
67 provided Makefiles that yours must include; the examples provided throughout
68 the document will show how to use them.  Please be sure to consult these
69 Makefiles before introducing your own custom build macros or rules.
71         lib/Makefile.lib:
73           This contains the bulk of the macros for building shared objects.
75         lib/Makefile.lib.64
77           This contains macros for building 64-bit objects, and should be
78           included in Makefiles for 64-bit native ISAs.
80         lib/Makefile.rootfs
82           This contains macro overrides for libraries that install into /lib
83           (rather than /usr/lib).
85         lib/Makefile.targ
87           This contains rules for building shared objects.
89 The remainder of this document discusses how to write each of your Makefiles
90 in detail, and provides examples from the libinetutil library.
92 The Library Top-level Makefile
93 ------------------------------
95 As described above, your top-level library Makefile should contain
96 rules for building ISA-independent targets, but should defer the
97 building of all other targets to ISA-specific Makefiles.  The
98 ISA-independent targets usually consist of:
100         install_h
102           Install all library header files into the proto area.
103           Can be omitted if your library has no header files.
105         check
107           Check all library header files for hdrchk compliance.
108           Can be omitted if your library has no header files.
110         _msg
112           Build and install a message catalog.
113           Can be omitted if your library has no message catalog.
115 Of course, other targets (such as `cstyle') are fine as well, as long as
116 they are ISA-independent.
118 The ROOTHDRS and CHECKHDRS targets are provided in lib/Makefile.lib to make
119 it easy for you to install and check your library's header files.  To use
120 these targets, your Makefile must set the HDRS to the list of your library's
121 header files to install and HDRDIR to the their location in the source tree.
122 In addition, if your header files need to be installed in a location other
123 than $(ROOT)/usr/include, your Makefile must also set ROOTHDRDIR to the
124 appropriate location in the proto area.  Once HDRS, HDRDIR and (optionally)
125 ROOTHDRDIR have been set, your Makefile need only contain
127           install_h: $(ROOTHDRS)
129           check: $(CHECKHDRS)
131 to bind the provided targets to the standard `install_h' and `check' rules.
133 Similar rules are provided (in $(SRC)/Makefile.msg.targ) to make it easy for
134 you to build and install message catalogs from your library's source files.
136 To install a catalog into the catalog directory in the proto area, define the
137 POFILE macro to be the name of your catalog, and specify that the _msg target
138 depends on $(MSGDOMAINPOFILE).  The examples below should clarify this.
140 To build a message catalog from arbitrarily many message source files, use
141 the BUILDPO.msgfiles macro.
143           include ../Makefile.lib
145           POFILE =        libfoo.po
146           MSGFILES =      $(OBJECTS:%.o=%.i)
148           # ...
150           $(POFILE): $(MSGFILES)
151                 $(BUILDPO.msgfiles)
153           _msg: $(MSGDOMAINPOFILE)
155           include $(SRC)/Makefile.msg.targ
157 Note that this example doesn't use grep to find message files, since that can
158 mask unreferenced files, and potentially lead to the inclusion of unwanted
159 messages or omission of intended messages in the catalogs.  As such, MSGFILES
160 should be derived from a known list of objects or sources.
162 It is usually preferable to run the source through the C preprocessor prior
163 to extracting messages.  To do this, use the ".i" suffix, as shown in the
164 above example.  If you need to skip the C preprocessor, just use the native
165 (.[ch]) suffix.
167 The only time you shouldn't use BUILDPO.msgfiles as the preferred means of
168 extracting messages is when you're extracting them from shell scripts; in
169 that case, you can use the BUILDPO.pofiles macro as explained below.
171 To build a message catalog from other message catalogs, or from source files
172 that include shell scripts, use the BUILDPO.pofiles macro:
174           include ../Makefile.lib
176           SUBDIRS =       $(MACH)
178           POFILE =        libfoo.po
179           POFILES =       $(SUBDIRS:%=%/_%.po)
181           _msg :=         TARGET = _msg
183           # ...
185           $(POFILE): $(POFILES)
186                 $(BUILDPO.pofiles)
188           _msg: $(MSGDOMAINPOFILE)
190           include $(SRC)/Makefile.msg.targ
192 The Makefile above would work in conjunction with the following in its
193 subdirectories' Makefiles:
195           POFILE =        _thissubdir.po
196           MSGFILES =      $(OBJECTS:%.o=%.i)
198           $(POFILE):      $(MSGFILES)
199                   $(BUILDPO.msgfiles)
201           _msg:           $(POFILE)
203           include $(SRC)/Makefile.msg.targ
205 Since this POFILE will be combined with those in other subdirectories by the
206 parent Makefile and that merged file will be installed into the proto area
207 via MSGDOMAINPOFILE, there is no need to use MSGDOMAINPOFILE in this Makefile
208 (in fact, using it would lead to duplicate messages in the catalog).
210 When using any of these targets, keep in mind that other macros, like
211 XGETFLAGS and TEXT_DOMAIN may also be set in your Makefile to override or
212 augment the defaults provided in higher-level Makefiles.
214 As previously mentioned, you should defer all ISA-specific targets to your
215 ISA-specific Makefiles.  You can do this by:
217         1. Setting SUBDIRS to the list of directories to descend into:
219                 SUBDIRS = $(MACH)
221            Note that if your library is also built 64-bit, then you should
222            also specify
224                 $(BUILD64)SUBDIRS += $(MACH64)
226            so that SUBDIRS contains $(MACH64) if and only if you're compiling
227            on a 64-bit ISA.
229         2. Providing a common "descend into SUBDIRS" rule:
231                 $(SUBDIRS): FRC
232                         @cd $@; pwd; $(MAKE) $(TARGET)
234                 FRC:
236         3. Providing a collection of conditional assignments that set TARGET
237            appropriately:
239                 all     := TARGET= all
240                 clean   := TARGET= clean
241                 clobber := TARGET= clobber
242                 install := TARGET= install
244            The order doesn't matter, but alphabetical is preferable.
246         4. Having the aforementioned targets depend on SUBDIRS:
248                 all clean clobber install: $(SUBDIRS)
250            The `all' target must be listed first so that make uses it as the
251            default target; the others might as well be listed alphabetically.
253 As an example of how all of this goes together, here's libinetutil's
254 top-level library Makefile (license notice and copyright omitted):
256         include ../Makefile.lib
258         HDRS =          libinetutil.h
259         HDRDIR =        common
260         SUBDIRS =       $(MACH)
261         $(BUILD64)SUBDIRS += $(MACH64)
263         all :=          TARGET = all
264         clean :=        TARGET = clean
265         clobber :=      TARGET = clobber
266         install :=      TARGET = install
268         .KEEP_STATE:
270         all clean clobber install: $(SUBDIRS)
272         install_h:      $(ROOTHDRS)
274         check:          $(CHECKHDRS)
276         $(SUBDIRS): FRC
277                 @cd $@; pwd; $(MAKE) $(TARGET)
279         FRC:
281         include ../Makefile.targ
283 The Common Makefile
284 -------------------
286 In concept, your common Makefile should contain all of the rules and
287 definitions that are the same on all ISAs.  However, for reasons of
288 maintainability and cleanliness, you're encouraged to place even
289 ISA-dependent rules and definitions, as long you express them in an
290 ISA-independent way (e.g., by using $(MACH), $(TARGETMACH), and their kin).
291 (TARGETMACH is the same as MACH for 32-bit targets, and the same as MACH64
292 for 64-bit targets).
294 The common Makefile can be conceptually split up into four sections:
296         1. A copyright and comments section.  Please see the prototype
297            files in usr/src/prototypes for examples of how to format the
298            copyright message properly.  For brevity and clarity, this
299            section has been omitted from the examples shown here.
301         2. A list of macros that must be defined prior to the inclusion of
302            Makefile.lib.  This section is conceptually terminated by the
303            inclusion of Makefile.lib, followed, if necessary, by the
304            inclusion of Makefile.rootfs (only if the library is to be
305            installed in /lib rather than the default /usr/lib).
307         3. A list of macros that need not be defined prior to the inclusion
308            of Makefile.lib (or which must be defined following the inclusion
309            of Makefile.lib, to override or augment its definitions).  This
310            section is conceptually terminated by the .KEEP_STATE directive.
312         4. A list of targets.
314 The first section is self-explanatory.  The second typically consists of the
315 following macros:
317         LIBRARY
319           Set to the name of the static version of your library, such
320           as `libinetutil.a'.  You should always specify the `.a' suffix,
321           since pattern-matching rules in higher-level Makefiles rely on it,
322           even though static libraries are not normally built in ON, and
323           are never installed in the proto area.  Note that the LIBS macro
324           (described below) controls the types of libraries that are built
325           when building your library.
327           If you are building a loadable module (i.e., a shared object that
328           is only linked at runtime with dlopen(3dl)), specify the name of
329           the loadable module with a `.a' suffix, such as `devfsadm_mod.a'.
331         VERS
333           Set to the version of your shared library, such as `.1'.  You
334           actually do not need to set this prior to the inclusion of
335           Makefile.lib, but it is good practice to do so since VERS and
336           LIBRARY are so closely related.
338         OBJECTS
340           Set to the list of object files contained in your library, such as
341           `a.o b.o'.  Usually, this will be the same as your library's source
342           files (except with .o extensions), but if your library compiles
343           source files outside of the library directory itself, it will
344           differ.  We'll see an example of this with libinetutil.
346 The third section typically consists of the following macros:
348         LIBS
350           Set to the list of the types of libraries to build when building
351           your library.  For dynamic libraries, you should set this to
352           `$(DYNLIB)' so that a dynamic library is built.
354           If your library needs to be built as a static library (typically
355           to be used in other parts of the build), you should set LIBS to
356           `$(LIBRARY)'.  However, you should do this only when absolutely
357           necessary, and you must *never* ship static libraries to customers.
359         ROOTLIBDIR (if your library installs to a nonstandard directory)
361           Set to the directory your 32-bit shared objects will install into
362           with the standard $(ROOTxxx) macros.  Since this defaults to
363           $(ROOT)/usr/lib ($(ROOT)/lib if you included Makefile.rootfs),
364           you usually do not need to set this.
366         ROOTLIBDIR64 (if your library installs to a nonstandard directory)
368           Set to the directory your 64-bit shared objects will install into
369           with the standard $(ROOTxxx64) macros.  Since this defaults to
370           $(ROOT)/usr/lib/$(MACH64) ($(ROOT)/lib/$(MACH64) if you included
371           Makefile.rootfs), you usually do not need to set this.
373         SRCDIR
375           Set to the directory containing your library's source files, such
376           as `../common'.  Because this Makefile is actually included from
377           your ISA-specific Makefiles, make sure you specify the directory
378           relative to your library's <isa> directory.
380         SRCS (if necessary)
382           Set to the list of source files required to build your library.
383           This defaults to $(OBJECTS:%.o=$(SRCDIR)/%.c) in Makefile.lib, so
384           you only need to set this when source files from directories other
385           than SRCDIR are needed.  Keep in mind that SRCS should be set to a
386           list of source file *pathnames*, not just a list of filenames.
388         LDLIBS
390           Appended with the list of libraries and library directories needed
391           to build your library; minimally "-lc".  Note that this should
392           *never* be set, since that will inadvertently clear the library
393           search path, causing the linker to look in the wrong place for
394           the libraries.
396         MAPFILES (if necessary)
398           Set to the list of mapfiles used to link each ISA-specific version
399           of your library.  This defaults to `$(SRCDIR)/mapfile-vers' in
400           Makefile.lib, so you only need to change this if you have additional
401           mapfiles or your mapfile doesn't follow the standard naming
402           convention.  If you have supplemental ISA-dependent mapfiles that
403           reside in the respective <isa> directories, you can augment
404           MAPFILES like this:
406                 MAPFILES += mapfile-vers
408         CPPFLAGS (if necessary)
410            Appended with any flags that need to be passed to the C
411            preprocessor (typically -D and -I flags).  When compiling large
412            file aware code, CPPFLAGS *must* include.
414         CFLAGS
416            Appended with any flags that need to be passed to the C compiler.
417            Keep in mind that you should add any C preprocessor flags to
418            CPPFLAGS, not CFLAGS.
420         CFLAGS64 (if necessary)
422            Appended with any flags that need to be passed to the C compiler
423            when compiling 64-bit code.
425         COPTFLAG (if necessary)
427            Set to control the optimization level used by the C compiler when
428            compiling 32-bit code.  You should only set this if absolutely
429            necessary, and it should only contain optimization-related
430            settings (or -g).
432         COPTFLAG64 (if necessary)
434            Set to control the optimization level used by the C compiler when
435            compiling 64-bit code.  You should only set this if absolutely
436            necessary, and it should only contain optimization-related
437            settings (or -g).
439 Of course, you may use other macros as necessary.
441 The fourth section typically consists of the following targets:
443         all
445           Build all of the types of the libraries named by LIBS.  Must always
446           be the first real target in common Makefile.  Since the
447           higher-level Makefiles already contain rules to build all of the
448           different types of libraries, you can usually just specify
450                 all: $(LIBS)
452           though it should be listed as an empty target if LIBS is set by your
453           ISA-specific Makefiles (see above).
455 Conspicuously absent from this section are the `clean' and `clobber' targets.
456 These targets are already provided by lib/Makefile.targ and thus should not
457 be provided by your common Makefile.  Instead, your common Makefile should
458 list any additional files to remove during a `clean' and `clobber' by
459 appending to the CLEANFILES and CLOBBERFILES macros.
461 Once again, here's libinetutil's common Makefile, which shows how many of
462 these directives go together.  Note that Makefile.rootfs is included to
463 cause libinetutil.so.1 to be installed in /lib rather than /usr/lib:
465         LIBRARY =       libinetutil.a
466         VERS =          .1
467         OBJECTS =       octet.o inetutil4.o ifspec.o ifaddrlist.o eh.o tq.o
469         include ../../Makefile.lib
470         include ../../Makefile.rootfs
472         LIBS =          $(DYNLIB)
474         SRCDIR =        ../common
475         COMDIR =        $(SRC)/common/net/dhcp
476         SRCS =          $(COMDIR)/octet.c $(SRCDIR)/inetutil4.c \
477                         $(SRCDIR)/ifspec.c $(SRCDIR)/eh.c $(SRCDIR)/tq.c \
478                         $(SRCDIR)/ifaddrlist.c
480         LDLIBS +=        -lc
482         CPPFLAGS +=     -I$(SRCDIR)
484         .KEEP_STATE:
486         all: $(LIBS)
488         pics/%.o: $(COMDIR)/%.c
489                 $(COMPILE.c) -o $@ $<
490                 $(POST_PROCESS_O)
492         include ../../Makefile.targ
494 The mapfile for libinetutil is named `mapfile-vers' and resides in $(SRCDIR),
495 so the MAPFILES definition is omitted, defaulting to $(SRCDIR)/mapfile-vers.
497 Note that for libinetutil, not all of the object files come from SRCDIR.  To
498 support this, an alternate source file directory named COMDIR is defined, and
499 the source files listed in SRCS are specified using both COMDIR and SRCDIR.
500 Additionally, a special build rule is provided to build object files from the
501 sources in COMDIR; the rule uses COMPILE.c and POST_PROCESS_O so that any
502 changes to the compilation and object-post-processing phases will be
503 automatically picked up.
505 The ISA-Specific Makefiles
506 --------------------------
508 As the name implies, your ISA-specific Makefiles should contain macros and
509 rules that cannot be expressed in an ISA-independent way.  Usually, the only
510 rule you will need to put here is `install', which has different dependencies
511 for 32-bit and 64-bit libraries.  For instance, here are the ISA-specific
512 Makefiles for libinetutil:
514         sparc/Makefile:
516                 include ../Makefile.com
518                 install: all $(ROOTLIBS) $(ROOTLINKS)
520         sparcv9/Makefile:
522                 include ../Makefile.com
523                 include ../../Makefile.lib.64
525                 install: all $(ROOTLIBS64) $(ROOTLINKS64)
527         i386/Makefile:
529                 include ../Makefile.com
531                 install: all $(ROOTLIBS) $(ROOTLINKS)
533         amd64/Makefile:
535                 include ../Makefile.com
536                 include ../../Makefile.lib.64
538                 install: all $(ROOTLIBS64) $(ROOTLINKS64)
540 Observe that there is no .KEEP_STATE directive in these Makefiles, since all
541 of these Makefiles include libinetutil/Makefile.com, and it already has a
542 .KEEP_STATE directive.  Also, note that the 64-bit Makefiles also include
543 Makefile.lib.64, which overrides some of the definitions contained in the
544 higher level Makefiles included by the common Makefile so that 64-bit
545 compiles work correctly.
547 CTF Data in Libraries
548 ---------------------
550 By default, all position-independent objects are built with CTF data using
551 ctfconvert, which is then merged together using ctfmerge when the shared
552 object is built.  All C-source objects processed via ctfmerge need to be
553 processed via ctfconvert or the build will fail.  Objects built from non-C
554 sources (such as assembly or C++) are silently ignored for CTF processing.
556 Filter libraries that have no source files will need to explicitly disable
557 CTF by setting CTFMERGE_LIB to ":"; see libw/Makefile.com for an example.
559 More Information
560 ----------------
562 Other issues and questions will undoubtedly arise while you work on your
563 library's Makefiles.  To help in this regard, a number of libraries of
564 varying complexity have been updated to follow the guidelines and practices
565 outlined in this document:
567         lib/libdhcputil
569           Example of a simple 32-bit only library.
571         lib/libdhcpagent
573           Example of a simple 32-bit only library that obtains its sources
574           from multiple directories.
576         lib/ncad_addr
578           Example of a simple loadable module.
580         lib/libipmp
582           Example of a simple library that builds a message catalog.
584         lib/libdhcpsvc
586           Example of a Makefile hierarchy for a library and a collection
587           of related pluggable modules.
589         lib/lvm
591           Example of a Makefile hierarchy for a collection of related
592           libraries and pluggable modules.
594           Also an example of a Makefile hierarchy that supports the
595           _dc target for domain and category specific messages.
597 Of course, if you still have questions, please do not hesitate to send email
598 to the ON gatekeepers.