• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1Developing for CUPS
2===================
3
4Please see the [Contributing to CUPS](CONTRIBUTING.md) file for information on
5contributing to the CUPS project.
6
7
8How To Contact The Developers
9-----------------------------
10
11The Linux Foundation's "printing-architecture" mailing list is the primary means
12of asking questions and informally discussing issues and feature requests with
13the OpenPrinting developers.  To subscribe or see the mailing list archives, go
14to <https://lists.linuxfoundation.org/mailman/listinfo/printing-architecture>.
15
16
17Interfaces
18----------
19
20CUPS interfaces, including the C APIs and command-line arguments, environment
21variables, configuration files, and output format, are stable across patch
22versions and are generally backwards-compatible with interfaces used in prior
23major and minor versions.  However, program interfaces such as those used by
24the scheduler to run filter, port monitor, and backend processes for job
25processing should only be considered stable from the point of view of a
26filter, port monitor, or backend.  Software that simulates the scheduler in
27order to run those programs outside of CUPS must necessarily be updated when
28the corresponding interface is changed in a subsequent CUPS release, otherwise
29undefined behavior can occur.
30
31CUPS C APIs starting with an underscore (`_`) are considered to be private to
32CUPS and are not subject to the normal guarantees of stability between CUPS
33releases and must never be used in non-CUPS source code.  Similarly,
34configuration and state files written by CUPS are considered private if a
35corresponding man page is not provided with the CUPS release.  Never rely on
36undocumented files or formats when developing software for CUPS.  Always use a
37published C API to access data stored in a file to avoid compatibility problems
38in the future.
39
40
41Build System
42------------
43
44The CUPS build system uses GNU autoconf to tailor the software to the local
45operating system.  Project files for the current release of Microsoft Visual
46Studio are also provided for Microsoft Windows®.  To improve portability,
47makefiles must not make use of features unique to GNU make.  See the MAKEFILE
48GUIDELINES section for a description of the allowed make features and makefile
49guidelines.
50
51Additional GNU build programs such as GNU autoheader, GNU automake, and GNU
52libtool must not be used.  GNU autoheader rewrites the "config.h" template, GNU
53automake produces non-portable makefiles which depend on GNU-specific
54extensions, and GNU libtool is not portable or reliable enough for CUPS.
55
56
57Version Numbering
58-----------------
59
60CUPS uses a three-part version number separated by periods to represent the
61major, minor, and patch release numbers.  Major release numbers indicate large
62design changes or backwards-incompatible changes to the CUPS API or CUPS
63Imaging API.  Minor release numbers indicate new features and other smaller
64changes which are backwards-compatible with previous CUPS releases.  Patch
65numbers indicate bug fixes to the previous feature or patch release.  This
66version numbering scheme is consistent with the
67[Semantic Versioning](http://semver.org) specification.
68
69> Note:
70>
71> When we talk about compatibility, we are talking about binary compatibility
72> for public APIs and output format compatibility for program interfaces.
73> Changes to configuration file formats or the default behavior of programs
74> are not generally considered incompatible as the upgrade process can
75> normally address such changes gracefully.
76
77Production releases use the plain version numbers:
78
79    MAJOR.MINOR.PATCH
80    1.0.0
81    ...
82    1.1.0
83    ...
84    1.1.23
85    ...
86    2.0.0
87    ...
88    2.1.0
89    2.1.1
90    2.1.2
91    2.1.3
92
93The first production release in a MAJOR.MINOR series (MAJOR.MINOR.0) is called
94a feature release.  Feature releases are the only releases that may contain new
95features.  Subsequent production releases in a MAJOR.MINOR series may only
96contain bug fixes.
97
98Beta-test releases are identified by appending the letter B to the major and
99minor version numbers followed by the beta release number:
100
101    MAJOR.MINORbNUMBER
102    2.2b1
103
104Release candidates are identified by appending the letters RC to the major and
105minor version numbers followed by the release candidate number:
106
107    MAJOR.MINORrcNUMBER
108    2.2rc1
109
110
111Coding Guidelines
112-----------------
113
114Contributed source code must follow the guidelines below.  While the examples
115are for C and C++ source files, source code for other languages should conform
116to the same guidelines as allowed by the language.
117
118Source code comments provide the reference portion of the CUPS Programming
119Manual, which is generated using the [codedoc](https://www.msweet.org/codedoc)
120software.
121
122
123### Copyright notice
124
125If there are changes for a file, which contains copyright notice already
126and the notice is not updated for the current year, update the year of copyright
127notice to include the current year.
128
129Only the following files must have copyright notice updated every year: README.md,
130NOTICE, index.html.in, and header.html.in.
131
132
133### Source Files
134
135All source files names must consist of lowercase ASCII letters, numbers, dash
136("-"), underscore ("_"), and period (".") to ensure compatibility across
137different filesystems.  Source files containing functions have an extension of
138".c" for C and ".cxx" for C++ source files.  All other "include" files have an
139extension of ".h".  Tabs are set to 8 characters/columns.
140
141> Note: The ".cxx" extension is used because it is the only common C++ extension
142> between Linux®, macOS®, Unix®, and Windows®.
143
144The top of each source file contains a header giving the purpose or nature of
145the source file and the copyright and licensing notice:
146
147    /*
148     * Description of file contents.
149     *
150     * Copyright © 2021-2023 by OpenPrinting
151     *
152     * Licensed under Apache License v2.0.  See the file "LICENSE" for more
153     * information.
154     */
155
156
157### Header Files
158
159All public header files must include the "versioning.h" header file, or a header
160that does so.  Function declarations are then "decorated" with the correct
161`_CUPS_API_major_minor` macro to define its availability based on the build
162environment, for example:
163
164    extern int cupsDoThis(int foo, int bar) _CUPS_API_2_2;
165
166Private API header files must be named with the suffix "-private", for example
167the "cups.h" header file defines all of the public CUPS APIs while the
168"cups-private.h" header file defines all of the private CUPS APIs as well.
169Typically a private API header file will include the corresponding public API
170header file.
171
172
173### Comments
174
175All source code utilizes block comments within functions to describe the
176operations being performed by a group of statements; avoid putting a comment
177per line unless absolutely necessary, and then consider refactoring the code
178so that it is not necessary.  C source files can use either the block comment
179format ("/* comment */") or the C99/C++ "//" comment format, with the block
180comment format being preferred for multi-line comments:
181
182    /*
183     * Clear the state array before we begin.  Make sure that every
184     * element is set to `CUPS_STATE_IDLE`.
185     */
186
187     for (i = 0; i < (sizeof(array) / sizeof(sizeof(array[0])); i ++)
188       array[i] = CUPS_STATE_IDLE;
189
190     // Wait for state changes on another thread...
191     do
192     {
193       for (i = 0; i < (sizeof(array) / sizeof(sizeof(array[0])); i ++)
194         if (array[i] != CUPS_STATE_IDLE)
195           break;
196
197       if (i == (sizeof(array) / sizeof(array[0])))
198         sleep(1);
199     } while (i == (sizeof(array) / sizeof(array[0])));
200
201
202### Indentation
203
204All code blocks enclosed by brackets begin with the opening brace on a new
205line.  The code then follows starting on a new line after the brace and is
206indented 2 spaces.  The closing brace is then placed on a new line following
207the code at the original indentation:
208
209    {
210      int i; // Looping var
211
212      // Process foobar values from 0 to 999...
213      for (i = 0; i < 1000; i ++)
214      {
215        do_this(i);
216        do_that(i);
217      }
218    }
219
220Single-line statements following "do", "else", "for", "if", and "while" are
221indented 2 spaces as well.  Blocks of code in a "switch" block are indented 4
222spaces after each "case" and "default" case:
223
224    switch (array[i])
225    {
226      case CUPS_STATE_IDLE :
227          do_this(i);
228          do_that(i);
229          break;
230
231      default :
232          do_nothing(i);
233          break;
234    }
235
236
237### Spacing
238
239A space follows each reserved word such as `if`, `while`, etc.  Spaces are not
240inserted between a function name and the arguments in parenthesis.
241
242
243### Return Values
244
245Parenthesis surround values returned from a function:
246
247    return (CUPS_STATE_IDLE);
248
249
250### Functions
251
252Functions with a global scope have a lowercase prefix followed by capitalized
253words, e.g., `cupsDoThis`, `cupsDoThat`, `cupsDoSomethingElse`, etc.  Private
254global functions begin with a leading underscore, e.g., `_cupsDoThis`,
255`_cupsDoThat`, etc.
256
257Functions with a local scope are declared static with lowercase names and
258underscores between words, e.g., `do_this`, `do_that`, `do_something_else`, etc.
259
260Each function begins with a comment header describing what the function does,
261the possible input limits (if any), the possible output values (if any), and
262any special information needed:
263
264    /*
265     * 'do_this()' - Compute y = this(x).
266     *
267     * This function computes "this(x)" and returns the result. "x" must be
268     * between 0.0 and 1.1.
269     *
270     * Notes: none.
271     */
272
273    static float       // O - Inverse power value, 0.0 <= y <= 1.1
274    do_this(float x)   // I - Power value (0.0 <= x <= 1.1)
275    {
276      ...
277      return (y);
278    }
279
280Return/output values are indicated using an "O" prefix, input values are
281indicated using the "I" prefix, and values that are both input and output use
282the "IO" prefix for the corresponding in-line comment.
283
284The [codedoc](https://www.msweet.org/codedoc) documentation generator also
285understands some markdown syntax as well as the following special text in the
286function description comment:
287
288    @deprecated@         - Marks the function as deprecated: not recommended
289                           for new development and scheduled for removal.
290    @link name@          - Provides a hyperlink to the corresponding function
291                           or type definition.
292    @since CUPS version@ - Marks the function as new in the specified version
293                           of CUPS.
294    @private@            - Marks the function as private so it will not be
295                           included in the documentation.
296
297
298### Variables
299
300Variables with a global scope are capitalized, e.g., `ThisVariable`,
301`ThatVariable`, `ThisStateVariable`, etc.  Globals in CUPS libraries are either
302part of the per-thread global values managed by the `_cupsGlobals` function
303or are suitably protected for concurrent access.  Global variables should be
304replaced by function arguments whenever possible.
305
306Variables with a local scope are lowercase with underscores between words,
307e.g., `this_variable`, `that_variable`, etc.  Any "local global" variables
308shared by functions within a source file are declared static.  As for global
309variables, local static variables are suitably protected for concurrent access.
310
311Each variable is declared on a separate line and is immediately followed by a
312comment describing the variable:
313
314    int         ThisVariable;    // The current state of this
315    static int  that_variable;   // The current state of that
316
317
318### Types
319
320All type names are lowercase with underscores between words and `_t` appended
321to the end of the name, e.g., `cups_this_type_t`, `cups_that_type_t`, etc.
322Type names start with a prefix, typically `cups` or the name of the program,
323to avoid conflicts with system types.  Private type names start with an
324underscore, e.g., `_cups_this_t`, `_cups_that_t`, etc.
325
326Each type has a comment immediately after the typedef:
327
328    typedef int cups_this_type_t;  // This type is for CUPS foobar options.
329
330
331### Structures
332
333All structure names are lowercase with underscores between words and `_s`
334appended to the end of the name, e.g., `cups_this_s`, `cups_that_s`, etc.
335Structure names start with a prefix, typically `cups` or the name of the
336program, to avoid conflicts with system types.  Private structure names start
337with an underscore, e.g., `_cups_this_s`, `_cups_that_s`, etc.
338
339Each structure has a comment immediately after the struct and each member is
340documented similar to the variable naming policy above:
341
342    struct cups_this_struct_s  // This structure is for CUPS foobar options.
343    {
344      int this_member;         // Current state for this
345      int that_member;         // Current state for that
346    };
347
348
349### Constants
350
351All constant names are uppercase with underscores between words, e.g.,
352`CUPS_THIS_CONSTANT`, `CUPS_THAT_CONSTANT`, etc.  Constants begin with an
353uppercase prefix, typically `CUPS_` or the program or type name.  Private
354constants start with an underscore, e.g., `_CUPS_THIS_CONSTANT`,
355`_CUPS_THAT_CONSTANT`, etc.
356
357Typed enumerations should be used whenever possible to allow for type checking
358by the compiler.
359
360Comments immediately follow each constant:
361
362    typedef enum cups_tray_e  // Tray enumerations
363    {
364      CUPS_TRAY_THIS,         // This tray
365      CUPS_TRAY_THAT          // That tray
366    } cups_tray_t;
367
368
369## Makefile Guidelines
370
371The following is a guide to the (POSIX) makefile-based build system used by
372CUPS.  These guidelines have been developed over the years to allow CUPS to be
373built on as many systems and environments as possible.
374
375
376### General Organization
377
378The CUPS source code is organized functionally into a top-level makefile,
379include file, and subdirectories each with their own makefile and dependencies
380files.  The ".in" files are template files for the autoconf software and are
381used to generate a static version of the corresponding file.
382
383
384### Makefile Documentation
385
386Each makefile starts with the standard CUPS header containing the description
387of the file, and CUPS copyright and license notice:
388
389    #
390    # Makefile for ...
391    #
392    # Copyright © 2021-2023 by OpenPrinting
393    #
394    # Licensed under Apache License v2.0.  See the file "LICENSE" for more
395    # information.
396    #
397
398
399### Portable Makefile Construction
400
401CUPS uses a common subset of POSIX make program syntax to ensure that the
402software can be compiled "out of the box" on as many systems as possible.  The
403following is a list of assumptions we follow when constructing makefiles:
404
405- Targets; we assume that the make program supports the notion of simple
406  targets of the form "name:" that perform tab-indented commands that follow
407  the target, e.g.:
408
409      target:
410      TAB target commands
411
412- Dependencies; we assume that the make program supports recursive dependencies
413  on targets, e.g.:
414
415      target: foo bar
416      TAB target commands
417
418      foo: bla
419      TAB foo commands
420
421      bar:
422      TAB bar commands
423
424      bla:
425      TAB bla commands
426
427- Variable Definition; we assume that the make program supports variable
428  definition on the command-line or in the makefile using the following form:
429
430      name=value
431
432- Variable Substitution; we assume that the make program supports variable
433  substitution using the following forms:
434
435      - `$(name)`; substitutes the value of "name",
436      - `$(name:.old=.new)`; substitutes the value of "name" with the filename
437        extension ".old" changed to ".new",
438      - `$(MAKEFLAGS)`; substitutes the command-line options passed to the
439        program without the leading hyphen (-),
440      - `$$`; substitutes a single $ character,
441      - `$<`; substitutes the current source file or dependency, and
442      - `$@`; substitutes the current target name.
443
444- Suffixes; we assume that the make program supports filename suffixes with
445  assumed dependencies, e.g.:
446
447      .SUFFIXES: .c .o
448      .c.o:
449      TAB $(CC) $(CFLAGS) -o $@ -c $<
450
451- Include Files; we assume that the make program supports the include
452  directive, e.g.:
453
454      include ../Makedefs
455      include Dependencies
456
457- Comments; we assume that comments begin with a # character and proceed to the
458  end of the current line.
459
460- Line Length; we assume that there is no practical limit to the length of
461  lines.
462
463- Continuation of long lines; we assume that the `\` character may be placed at
464  the end of a line to concatenate two or more lines in a makefile to form a
465  single long line.
466
467- Shell; we assume a POSIX-compatible shell is present on the build system.
468
469
470### Standard Variables
471
472The following variables are defined in the "Makedefs" file generated by the
473autoconf software:
474
475- `ALL_CFLAGS`; the combined C compiler options,
476- `ALL_CXXFLAGS`; the combined C++ compiler options,
477- `AMANDIR`; the administrative man page installation directory (section 8/1m
478  depending on the platform),
479- `AR`; the library archiver command,
480- `ARFLAGS`; options for the library archiver command,
481- `AWK`; the local awk command,
482- `BINDIR`; the binary installation directory,
483- `BUILDROOT`; optional installation prefix (defaults to DSTROOT),
484- `CC`; the C compiler command,
485- `CFLAGS`; options for the C compiler command,
486- `CHMOD`; the chmod command,
487- `CXX`; the C++ compiler command,
488- `CXXFLAGS`; options for the C++ compiler command,
489- `DATADIR`; the data file installation directory,
490- `DSO`; the C shared library building command,
491- `DSOXX`; the C++ shared library building command,
492- `DSOFLAGS`; options for the shared library building command,
493- `INCLUDEDIR`; the public header file installation directory,
494- `INSTALL`; the install command,
495- `INSTALL_BIN`; the program installation command,
496- `INSTALL_COMPDATA`; the compressed data file installation command,
497- `INSTALL_CONFIG`; the configuration file installation command,
498- `INSTALL_DATA`; the data file installation command,
499- `INSTALL_DIR`; the directory installation command,
500- `INSTALL_LIB`; the library installation command,
501- `INSTALL_MAN`; the documentation installation command,
502- `INSTALL_SCRIPT`; the shell script installation command,
503- `LD`; the linker command,
504- `LDFLAGS`; options for the linker,
505- `LIBDIR`; the library installation directory,
506- `LIBS`; libraries for all programs,
507- `LN`; the ln command,
508- `MAN1EXT`; extension for man pages in section 1,
509- `MAN3EXT`; extension for man pages in section 3,
510- `MAN5EXT`; extension for man pages in section 5,
511- `MAN7EXT`; extension for man pages in section 7,
512- `MAN8DIR`; subdirectory for man pages in section 8,
513- `MAN8EXT`; extension for man pages in section 8,
514- `MANDIR`; the man page installation directory,
515- `OPTIM`; common compiler optimization options,
516- `PRIVATEINCLUDE`; the private header file installation directory,
517- `RM`; the rm command,
518- `SHELL`; the sh (POSIX shell) command,
519- `STRIP`; the strip command,
520- `srcdir`; the source directory.
521
522
523### Standard Targets
524
525The following standard targets are defined in each makefile:
526
527- `all`; creates all target programs, libraries, and documentation files,
528- `clean`; removes all target programs libraries, documentation files, and object
529  files,
530- `depend`; generates automatic dependencies for any C or C++ source files (also
531  see "DEPENDENCIES"),
532- `distclean`; removes autoconf-generated files in addition to those removed by
533  the "clean" target,
534- `install`; installs all distribution files in their corresponding locations
535  (also see "INSTALL/UNINSTALL SUPPORT"),
536- `install-data`; installs all data files in their corresponding locations (also
537  see "INSTALL/UNINSTALL SUPPORT"),
538- `install-exec`; installs all executable files in their corresponding locations
539  (also see "INSTALL/UNINSTALL SUPPORT"),
540- `install-headers`; installs all include files in their corresponding locations
541  (also see "INSTALL/UNINSTALL SUPPORT"),
542- `install-libs`; installs all library files in their corresponding locations
543  (also see "INSTALL/UNINSTALL SUPPORT"),
544- `test`: builds and runs unit tests, and
545- `uninstall`; removes all distribution files from their corresponding locations
546  (also see "INSTALL/UNINSTALL SUPPORT").
547
548
549### Object Files
550
551Object files (the result of compiling a C or C++ source file) have the
552extension ".o".
553
554
555### Programs
556
557Program files are the result of linking object files and libraries together to
558form an executable file.  A typical program target looks like:
559
560    program: $(OBJS)
561    TAB echo Linking $@...
562    TAB $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
563
564
565### Static Libraries
566
567Static libraries have a prefix of "lib" and the extension ".a". A typical
568static library target looks like:
569
570    libname.a: $(OBJECTS)
571    TAB echo Creating $@...
572    TAB $(RM) $@
573    TAB $(AR) $(ARFLAGS) $@ $(OBJECTS)
574    TAB $(RANLIB) $@
575
576
577### Shared Libraries
578
579Shared libraries have a prefix of "lib" and the extension ".dylib" or ".so"
580depending on the operating system.  A typical shared library is composed of
581several targets that look like:
582
583    libname.so: $(OBJECTS)
584    TAB echo $(DSOCOMMAND) libname.so.$(DSOVERSION) ...
585    TAB $(DSOCOMMAND) libname.so.$(DSOVERSION) $(OBJECTS)
586    TAB $(RM) libname.so libname.so.$(DSOMAJOR)
587    TAB $(LN) libname.so.$(DSOVERSION) libname.so.$(DSOMAJOR)
588    TAB $(LN) libname.so.$(DSOVERSION) libname.so
589
590    libname.dylib: $(OBJECTS)
591    TAB echo $(DSOCOMMAND) libname.$(DSOVERSION).dylib ...
592    TAB $(DSOCOMMAND) libname.$(DSOVERSION).dylib \
593    TAB TAB -install_name $(libdir)/libname.$(DSOMAJOR).dylib \
594    TAB TAB -current_version libname.$(DSOVERSION).dylib \
595    TAB TAB -compatibility_version $(DSOMAJOR).0 \
596    TAB TAB $(OBJECTS) $(LIBS)
597    TAB $(RM) libname.dylib
598    TAB $(RM) libname.$(DSOMAJOR).dylib
599    TAB $(LN) libname.$(DSOVERSION).dylib libname.$(DSOMAJOR).dylib
600    TAB $(LN) libname.$(DSOVERSION).dylib libname.dylib
601
602
603### Dependencies
604
605Static dependencies are expressed in each makefile following the target, for
606example:
607
608    foo: bar
609
610Static dependencies are only used when it is not possible to automatically
611generate them.  Automatic dependencies are stored in a file named
612"Dependencies" and included at the end of the makefile.  The following "depend"
613target rule is used to create the automatic dependencies:
614
615    depend:
616    TAB $(CC) -MM $(ALL_CFLAGS) $(OBJS:.o=.c) >Dependencies
617
618We regenerate the automatic dependencies on an Ubuntu system and express any
619non-Linux dependencies statically in the makefile.
620
621
622### Install/Uninstall Support
623
624All makefiles contains install and uninstall rules which install or remove the
625corresponding software.  These rules must use the $(BUILDROOT) variable as a
626prefix to any installation directory so that CUPS can be installed in a
627temporary location for packaging by programs like rpmbuild.
628
629The `INSTALL_BIN`, `INSTALL_COMPDATA`, `INSTALL_CONFIG`, `INSTALL_DATA`,
630`INSTALL_DIR`, `INSTALL_LIB`, `INSTALL_MAN`, and `INSTALL_SCRIPT` variables
631must be used when installing files so that the proper ownership and permissions
632are set on the installed files.
633
634The `$(RANLIB)` command must be run on any static libraries after installation
635since the symbol table is invalidated when the library is copied on some
636platforms.
637