compiler: Do not declare type switch variable outside case statements.
[official-gcc.git] / gcc / ada / gnat_ugn.texi
blob3099556174fef4ff704878c774807810460cc2ef
1 \input texinfo   @c -*-texinfo-*-
2 @c %**start of header
3 @setfilename gnat_ugn.info
4 @documentencoding UTF-8
5 @ifinfo
6 @*Generated by Sphinx 1.3b2.@*
7 @end ifinfo
8 @settitle GNAT User's Guide for Native Platforms
9 @defindex ge
10 @paragraphindent 0
11 @exampleindent 4
12 @finalout
13 @dircategory GNU Ada Tools 
14 @direntry
15 * gnat_ugn: (gnat_ugn.info). gnat_ugn
16 @end direntry
18 @definfoenclose strong,`,'
19 @definfoenclose emph,`,'
20 @c %**end of header
22 @copying
23 @quotation
24 GNAT User's Guide for Native Platforms , March 01, 2015
26 AdaCore
28 Copyright @copyright{} 2008-2015, Free Software Foundation
29 @end quotation
31 @end copying
33 @titlepage
34 @title GNAT User's Guide for Native Platforms
35 @insertcopying
36 @end titlepage
37 @contents
39 @c %** start of user preamble
41 @c %** end of user preamble
43 @ifnottex
44 @node Top
45 @top GNAT User's Guide for Native Platforms
46 @insertcopying
47 @end ifnottex
49 @c %**start of body
50 @anchor{gnat_ugn doc}@anchor{0}
51 @emph{GNAT, The GNU Ada Development Environment}
54 @include gcc-common.texi
55 GCC version @value{version-GCC}@*
56 AdaCore
58 Permission is granted to copy, distribute and/or modify this document
59 under the terms of the GNU Free Documentation License, Version 1.3 or
60 any later version published by the Free Software Foundation; with no
61 Invariant Sections, with the Front-Cover Texts being
62 "GNAT User's Guide for Native Platforms",
63 and with no Back-Cover Texts.  A copy of the license is
64 included in the section entitled @ref{1,,GNU Free Documentation License}.
66 @menu
67 * About This Guide:: 
68 * Getting Started with GNAT:: 
69 * The GNAT Compilation Model:: 
70 * Building Executable Programs with GNAT:: 
71 * GNAT Project Manager:: 
72 * Tools Supporting Project Files:: 
73 * GNAT Utility Programs:: 
74 * GNAT and Program Execution:: 
75 * Platform-Specific Information:: 
76 * Example of Binder Output File:: 
77 * Elaboration Order Handling in GNAT:: 
78 * Inline Assembler:: 
79 * GNU Free Documentation License:: 
80 * Index:: 
82 @detailmenu
83  --- The Detailed Node Listing ---
85 About This Guide
87 * What This Guide Contains:: 
88 * What You Should Know before Reading This Guide:: 
89 * Related Information:: 
90 * A Note to Readers of Previous Versions of the Manual:: 
91 * Conventions:: 
93 Getting Started with GNAT
95 * Running GNAT:: 
96 * Running a Simple Ada Program:: 
97 * Running a Program with Multiple Units:: 
98 * Using the gnatmake Utility:: 
100 The GNAT Compilation Model
102 * Source Representation:: 
103 * Foreign Language Representation:: 
104 * File Naming Topics and Utilities:: 
105 * Configuration Pragmas:: 
106 * Generating Object Files:: 
107 * Source Dependencies:: 
108 * The Ada Library Information Files:: 
109 * Binding an Ada Program:: 
110 * GNAT and Libraries:: 
111 * Conditional Compilation:: 
112 * Mixed Language Programming:: 
113 * GNAT and Other Compilation Models:: 
114 * Using GNAT Files with External Tools:: 
116 Foreign Language Representation
118 * Latin-1:: 
119 * Other 8-Bit Codes:: 
120 * Wide_Character Encodings:: 
121 * Wide_Wide_Character Encodings:: 
123 File Naming Topics and Utilities
125 * File Naming Rules:: 
126 * Using Other File Names:: 
127 * Alternative File Naming Schemes:: 
128 * Handling Arbitrary File Naming Conventions with gnatname:: 
129 * File Name Krunching with gnatkr:: 
130 * Renaming Files with gnatchop:: 
132 Handling Arbitrary File Naming Conventions with gnatname
134 * Arbitrary File Naming Conventions:: 
135 * Running gnatname:: 
136 * Switches for gnatname:: 
137 * Examples of gnatname Usage:: 
139 File Name Krunching with gnatkr
141 * About gnatkr:: 
142 * Using gnatkr:: 
143 * Krunching Method:: 
144 * Examples of gnatkr Usage:: 
146 Renaming Files with gnatchop
148 * Handling Files with Multiple Units:: 
149 * Operating gnatchop in Compilation Mode:: 
150 * Command Line for gnatchop:: 
151 * Switches for gnatchop:: 
152 * Examples of gnatchop Usage:: 
154 Configuration Pragmas
156 * Handling of Configuration Pragmas:: 
157 * The Configuration Pragmas Files:: 
159 GNAT and Libraries
161 * Introduction to Libraries in GNAT:: 
162 * General Ada Libraries:: 
163 * Stand-alone Ada Libraries:: 
164 * Rebuilding the GNAT Run-Time Library:: 
166 General Ada Libraries
168 * Building a library:: 
169 * Installing a library:: 
170 * Using a library:: 
172 Stand-alone Ada Libraries
174 * Introduction to Stand-alone Libraries:: 
175 * Building a Stand-alone Library:: 
176 * Creating a Stand-alone Library to be used in a non-Ada context:: 
177 * Restrictions in Stand-alone Libraries:: 
179 Conditional Compilation
181 * Modeling Conditional Compilation in Ada:: 
182 * Preprocessing with gnatprep:: 
183 * Integrated Preprocessing:: 
185 Modeling Conditional Compilation in Ada
187 * Use of Boolean Constants:: 
188 * Debugging - A Special Case:: 
189 * Conditionalizing Declarations:: 
190 * Use of Alternative Implementations:: 
191 * Preprocessing:: 
193 Preprocessing with gnatprep
195 * Preprocessing Symbols:: 
196 * Using gnatprep:: 
197 * Switches for gnatprep:: 
198 * Form of Definitions File:: 
199 * Form of Input Text for gnatprep:: 
201 Mixed Language Programming
203 * Interfacing to C:: 
204 * Calling Conventions:: 
205 * Building Mixed Ada and C++ Programs:: 
206 * Generating Ada Bindings for C and C++ headers:: 
208 Building Mixed Ada and C++ Programs
210 * Interfacing to C++:: 
211 * Linking a Mixed C++ & Ada Program:: 
212 * A Simple Example:: 
213 * Interfacing with C++ constructors:: 
214 * Interfacing with C++ at the Class Level:: 
216 Generating Ada Bindings for C and C++ headers
218 * Running the binding generator:: 
219 * Generating bindings for C++ headers:: 
220 * Switches:: 
222 GNAT and Other Compilation Models
224 * Comparison between GNAT and C/C++ Compilation Models:: 
225 * Comparison between GNAT and Conventional Ada Library Models:: 
227 Using GNAT Files with External Tools
229 * Using Other Utility Programs with GNAT:: 
230 * The External Symbol Naming Scheme of GNAT:: 
232 Building Executable Programs with GNAT
234 * Building with gnatmake:: 
235 * Compiling with gcc:: 
236 * Compiler Switches:: 
237 * Binding with gnatbind:: 
238 * Linking with gnatlink:: 
239 * Using the GNU make Utility:: 
241 Building with gnatmake
243 * Running gnatmake:: 
244 * Switches for gnatmake:: 
245 * Mode Switches for gnatmake:: 
246 * Notes on the Command Line:: 
247 * How gnatmake Works:: 
248 * Examples of gnatmake Usage:: 
250 Compiling with gcc
252 * Compiling Programs:: 
253 * Search Paths and the Run-Time Library (RTL): Search Paths and the Run-Time Library RTL. 
254 * Order of Compilation Issues:: 
255 * Examples:: 
257 Compiler Switches
259 * Alphabetical List of All Switches:: 
260 * Output and Error Message Control:: 
261 * Warning Message Control:: 
262 * Debugging and Assertion Control:: 
263 * Validity Checking:: 
264 * Style Checking:: 
265 * Run-Time Checks:: 
266 * Using gcc for Syntax Checking:: 
267 * Using gcc for Semantic Checking:: 
268 * Compiling Different Versions of Ada:: 
269 * Character Set Control:: 
270 * File Naming Control:: 
271 * Subprogram Inlining Control:: 
272 * Auxiliary Output Control:: 
273 * Debugging Control:: 
274 * Exception Handling Control:: 
275 * Units to Sources Mapping Files:: 
276 * Code Generation Control:: 
278 Binding with gnatbind
280 * Running gnatbind:: 
281 * Switches for gnatbind:: 
282 * Command-Line Access:: 
283 * Search Paths for gnatbind:: 
284 * Examples of gnatbind Usage:: 
286 Switches for gnatbind
288 * Consistency-Checking Modes:: 
289 * Binder Error Message Control:: 
290 * Elaboration Control:: 
291 * Output Control:: 
292 * Dynamic Allocation Control:: 
293 * Binding with Non-Ada Main Programs:: 
294 * Binding Programs with No Main Subprogram:: 
296 Linking with gnatlink
298 * Running gnatlink:: 
299 * Switches for gnatlink:: 
301 Using the GNU make Utility
303 * Using gnatmake in a Makefile:: 
304 * Automatically Creating a List of Directories:: 
305 * Generating the Command Line Switches:: 
306 * Overcoming Command Line Length Limits:: 
308 GNAT Project Manager
310 * Introduction:: 
311 * Building With Projects:: 
312 * Organizing Projects into Subsystems:: 
313 * Scenarios in Projects:: 
314 * Library Projects:: 
315 * Project Extension:: 
316 * Aggregate Projects:: 
317 * Aggregate Library Projects:: 
318 * Project File Reference:: 
320 Building With Projects
322 * Source Files and Directories:: 
323 * Duplicate Sources in Projects:: 
324 * Object and Exec Directory:: 
325 * Main Subprograms:: 
326 * Tools Options in Project Files:: 
327 * Compiling with Project Files:: 
328 * Executable File Names:: 
329 * Avoid Duplication With Variables:: 
330 * Naming Schemes:: 
331 * Installation:: 
332 * Distributed support:: 
334 Organizing Projects into Subsystems
336 * Project Dependencies:: 
337 * Cyclic Project Dependencies:: 
338 * Sharing Between Projects:: 
339 * Global Attributes:: 
341 Library Projects
343 * Building Libraries:: 
344 * Using Library Projects:: 
345 * Stand-alone Library Projects:: 
346 * Installing a library with project files:: 
348 Project Extension
350 * Project Hierarchy Extension:: 
352 Aggregate Projects
354 * Building all main programs from a single project tree:: 
355 * Building a set of projects with a single command:: 
356 * Define a build environment:: 
357 * Performance improvements in builder:: 
358 * Syntax of aggregate projects:: 
359 * package Builder in aggregate projects:: 
361 Aggregate Library Projects
363 * Building aggregate library projects:: 
364 * Syntax of aggregate library projects:: 
366 Project File Reference
368 * Project Declaration:: 
369 * Qualified Projects:: 
370 * Declarations:: 
371 * Packages:: 
372 * Expressions:: 
373 * External Values:: 
374 * Typed String Declaration:: 
375 * Variables:: 
376 * Case Constructions:: 
377 * Attributes:: 
379 Attributes
381 * Project Level Attributes:: 
382 * Package Binder Attributes:: 
383 * Package Builder Attributes:: 
384 * Package Clean Attributes:: 
385 * Package Compiler Attributes:: 
386 * Package Cross_Reference Attributes:: 
387 * Package Finder Attributes:: 
388 * Package gnatls Attributes:: 
389 * Package IDE Attributes:: 
390 * Package Install Attributes:: 
391 * Package Linker Attributes:: 
392 * Package Naming Attributes:: 
393 * Package Remote Attributes:: 
394 * Package Stack Attributes:: 
395 * Package Synchronize Attributes:: 
397 Tools Supporting Project Files
399 * gnatmake and Project Files:: 
400 * The GNAT Driver and Project Files:: 
402 gnatmake and Project Files
404 * Switches Related to Project Files:: 
405 * Switches and Project Files:: 
406 * Specifying Configuration Pragmas:: 
407 * Project Files and Main Subprograms:: 
408 * Library Project Files:: 
410 GNAT Utility Programs
412 * The File Cleanup Utility gnatclean:: 
413 * The GNAT Library Browser gnatls:: 
414 * The Cross-Referencing Tools gnatxref and gnatfind:: 
415 * The Ada to HTML Converter gnathtml:: 
417 The File Cleanup Utility gnatclean
419 * Running gnatclean:: 
420 * Switches for gnatclean:: 
422 The GNAT Library Browser gnatls
424 * Running gnatls:: 
425 * Switches for gnatls:: 
426 * Example of gnatls Usage:: 
428 The Cross-Referencing Tools gnatxref and gnatfind
430 * gnatxref Switches:: 
431 * gnatfind Switches:: 
432 * Project Files for gnatxref and gnatfind:: 
433 * Regular Expressions in gnatfind and gnatxref:: 
434 * Examples of gnatxref Usage:: 
435 * Examples of gnatfind Usage:: 
437 Examples of gnatxref Usage
439 * General Usage:: 
440 * Using gnatxref with vi:: 
442 The Ada to HTML Converter gnathtml
444 * Invoking gnathtml:: 
445 * Installing gnathtml:: 
447 GNAT and Program Execution
449 * Running and Debugging Ada Programs:: 
450 * Code Coverage and Profiling:: 
451 * Improving Performance:: 
452 * Overflow Check Handling in GNAT:: 
453 * Performing Dimensionality Analysis in GNAT:: 
454 * Stack Related Facilities:: 
455 * Memory Management Issues:: 
457 Running and Debugging Ada Programs
459 * The GNAT Debugger GDB:: 
460 * Running GDB:: 
461 * Introduction to GDB Commands:: 
462 * Using Ada Expressions:: 
463 * Calling User-Defined Subprograms:: 
464 * Using the next Command in a Function:: 
465 * Stopping When Ada Exceptions Are Raised:: 
466 * Ada Tasks:: 
467 * Debugging Generic Units:: 
468 * Remote Debugging with gdbserver:: 
469 * GNAT Abnormal Termination or Failure to Terminate:: 
470 * Naming Conventions for GNAT Source Files:: 
471 * Getting Internal Debugging Information:: 
472 * Stack Traceback:: 
474 Stack Traceback
476 * Non-Symbolic Traceback:: 
477 * Symbolic Traceback:: 
479 Code Coverage and Profiling
481 * Code Coverage of Ada Programs with gcov:: 
482 * Profiling an Ada Program with gprof:: 
484 Code Coverage of Ada Programs with gcov
486 * Quick startup guide:: 
487 * GNAT specifics:: 
489 Profiling an Ada Program with gprof
491 * Compilation for profiling:: 
492 * Program execution:: 
493 * Running gprof:: 
494 * Interpretation of profiling results:: 
496 Improving Performance
498 * Performance Considerations:: 
499 * Text_IO Suggestions:: 
500 * Reducing Size of Executables with Unused Subprogram/Data Elimination:: 
502 Performance Considerations
504 * Controlling Run-Time Checks:: 
505 * Use of Restrictions:: 
506 * Optimization Levels:: 
507 * Debugging Optimized Code:: 
508 * Inlining of Subprograms:: 
509 * Floating_Point_Operations:: 
510 * Vectorization of loops:: 
511 * Other Optimization Switches:: 
512 * Optimization and Strict Aliasing:: 
513 * Aliased Variables and Optimization:: 
514 * Atomic Variables and Optimization:: 
515 * Passive Task Optimization:: 
517 Reducing Size of Executables with Unused Subprogram/Data Elimination
519 * About unused subprogram/data elimination:: 
520 * Compilation options:: 
521 * Example of unused subprogram/data elimination:: 
523 Overflow Check Handling in GNAT
525 * Background:: 
526 * Overflow Checking Modes in GNAT:: 
527 * Specifying the Desired Mode:: 
528 * Default Settings:: 
529 * Implementation Notes:: 
531 Stack Related Facilities
533 * Stack Overflow Checking:: 
534 * Static Stack Usage Analysis:: 
535 * Dynamic Stack Usage Analysis:: 
537 Memory Management Issues
539 * Some Useful Memory Pools:: 
540 * The GNAT Debug Pool Facility:: 
542 Platform-Specific Information
544 * Run-Time Libraries:: 
545 * Specifying a Run-Time Library:: 
546 * Microsoft Windows Topics:: 
547 * Mac OS Topics:: 
549 Run-Time Libraries
551 * Summary of Run-Time Configurations:: 
553 Specifying a Run-Time Library
555 * Choosing the Scheduling Policy:: 
556 * Solaris-Specific Considerations:: 
557 * Solaris Threads Issues:: 
558 * AIX-Specific Considerations:: 
560 Microsoft Windows Topics
562 * Using GNAT on Windows:: 
563 * Using a network installation of GNAT:: 
564 * CONSOLE and WINDOWS subsystems:: 
565 * Temporary Files:: 
566 * Mixed-Language Programming on Windows:: 
568 Mixed-Language Programming on Windows
570 * Windows Calling Conventions:: 
571 * Introduction to Dynamic Link Libraries (DLLs): Introduction to Dynamic Link Libraries DLLs. 
572 * Using DLLs with GNAT:: 
573 * Building DLLs with GNAT Project files:: 
574 * Building DLLs with GNAT:: 
575 * Building DLLs with gnatdll:: 
576 * Ada DLLs and Finalization:: 
577 * Creating a Spec for Ada DLLs:: 
578 * GNAT and Windows Resources:: 
579 * Debugging a DLL:: 
580 * Setting Stack Size from gnatlink:: 
581 * Setting Heap Size from gnatlink:: 
583 Windows Calling Conventions
585 * C Calling Convention:: 
586 * Stdcall Calling Convention:: 
587 * Win32 Calling Convention:: 
588 * DLL Calling Convention:: 
590 Using DLLs with GNAT
592 * Creating an Ada Spec for the DLL Services:: 
593 * Creating an Import Library:: 
595 Building DLLs with gnatdll
597 * Limitations When Using Ada DLLs from Ada:: 
598 * Exporting Ada Entities:: 
599 * Ada DLLs and Elaboration:: 
601 Creating a Spec for Ada DLLs
603 * Creating the Definition File:: 
604 * Using gnatdll:: 
606 GNAT and Windows Resources
608 * Building Resources:: 
609 * Compiling Resources:: 
610 * Using Resources:: 
612 Debugging a DLL
614 * Program and DLL Both Built with GCC/GNAT:: 
615 * Program Built with Foreign Tools and DLL Built with GCC/GNAT:: 
617 Mac OS Topics
619 * Codesigning the Debugger:: 
621 Elaboration Order Handling in GNAT
623 * Elaboration Code:: 
624 * Checking the Elaboration Order:: 
625 * Controlling the Elaboration Order:: 
626 * Controlling Elaboration in GNAT - Internal Calls:: 
627 * Controlling Elaboration in GNAT - External Calls:: 
628 * Default Behavior in GNAT - Ensuring Safety:: 
629 * Treatment of Pragma Elaborate:: 
630 * Elaboration Issues for Library Tasks:: 
631 * Mixing Elaboration Models:: 
632 * What to Do If the Default Elaboration Behavior Fails:: 
633 * Elaboration for Indirect Calls:: 
634 * Summary of Procedures for Elaboration Control:: 
635 * Other Elaboration Order Considerations:: 
636 * Determining the Chosen Elaboration Order:: 
638 Inline Assembler
640 * Basic Assembler Syntax:: 
641 * A Simple Example of Inline Assembler:: 
642 * Output Variables in Inline Assembler:: 
643 * Input Variables in Inline Assembler:: 
644 * Inlining Inline Assembler Code:: 
645 * Other Asm Functionality:: 
647 Other Asm Functionality
649 * The Clobber Parameter:: 
650 * The Volatile Parameter:: 
652 @end detailmenu
653 @end menu
655 @node About This Guide,Getting Started with GNAT,Top,Top
656 @anchor{gnat_ugn/about_this_guide about-this-guide}@anchor{2}@anchor{gnat_ugn/about_this_guide doc}@anchor{3}@anchor{gnat_ugn/about_this_guide gnat-user-s-guide-for-native-platforms}@anchor{4}@anchor{gnat_ugn/about_this_guide id1}@anchor{5}
657 @chapter About This Guide
661 This guide describes the use of GNAT,
662 a compiler and software development
663 toolset for the full Ada programming language.
664 It documents the features of the compiler and tools, and explains
665 how to use them to build Ada applications.
667 GNAT implements Ada 95, Ada 2005 and Ada 2012, and it may also be
668 invoked in Ada 83 compatibility mode.
669 By default, GNAT assumes Ada 2012, but you can override with a
670 compiler switch (@ref{6,,Compiling Different Versions of Ada})
671 to explicitly specify the language version.
672 Throughout this manual, references to 'Ada' without a year suffix
673 apply to all Ada 95/2005/2012 versions of the language.
675 @menu
676 * What This Guide Contains:: 
677 * What You Should Know before Reading This Guide:: 
678 * Related Information:: 
679 * A Note to Readers of Previous Versions of the Manual:: 
680 * Conventions:: 
682 @end menu
684 @node What This Guide Contains,What You Should Know before Reading This Guide,,About This Guide
685 @anchor{gnat_ugn/about_this_guide what-this-guide-contains}@anchor{7}
686 @section What This Guide Contains
689 This guide contains the following chapters:
692 @itemize *
694 @item 
695 @ref{8,,Getting Started with GNAT} describes how to get started compiling
696 and running Ada programs with the GNAT Ada programming environment.
698 @item 
699 @ref{9,,The GNAT Compilation Model} describes the compilation model used
700 by GNAT.
702 @item 
703 @ref{a,,Building Executable Programs with GNAT} describes how to use the
704 main GNAT tools to build executable programs, and it also gives examples of
705 using the GNU make utility with GNAT.
707 @item 
708 @ref{b,,GNAT Project Manager} describes how to use project files
709 to organize large projects.
711 @item 
712 @ref{c,,Tools Supporting Project Files} described how to use the project
713 facility in conjunction with various GNAT tools.
715 @item 
716 @ref{d,,GNAT Utility Programs} explains the various utility programs that
717 are included in the GNAT environment
719 @item 
720 @ref{e,,GNAT and Program Execution} covers a number of topics related to
721 running, debugging, and tuning the performace of programs developed
722 with GNAT
723 @end itemize
725 Appendices cover several additional topics:
728 @itemize *
730 @item 
731 @ref{f,,Platform-Specific Information} describes the different run-time
732 library implementations and also presents information on how to use
733 GNAT on several specific platforms
735 @item 
736 @ref{10,,Example of Binder Output File} shows the source code for the binder
737 output file for a sample program.
739 @item 
740 @ref{11,,Elaboration Order Handling in GNAT} describes how GNAT helps
741 you deal with elaboration order issues.
743 @item 
744 @ref{12,,Inline Assembler} shows how to use the inline assembly facility
745 in an Ada program.
746 @end itemize
748 @node What You Should Know before Reading This Guide,Related Information,What This Guide Contains,About This Guide
749 @anchor{gnat_ugn/about_this_guide what-you-should-know-before-reading-this-guide}@anchor{13}
750 @section What You Should Know before Reading This Guide
753 @geindex Ada 95 Language Reference Manual
755 @geindex Ada 2005 Language Reference Manual
757 This guide assumes a basic familiarity with the Ada 95 language, as
758 described in the International Standard ANSI/ISO/IEC-8652:1995, January
759 1995.
760 It does not require knowledge of the features introduced by Ada 2005
761 or Ada 2012.
762 Reference manuals for Ada 95, Ada 2005, and Ada 2012 are included in
763 the GNAT documentation package.
765 @node Related Information,A Note to Readers of Previous Versions of the Manual,What You Should Know before Reading This Guide,About This Guide
766 @anchor{gnat_ugn/about_this_guide related-information}@anchor{14}
767 @section Related Information
770 For further information about Ada and related tools, please refer to the
771 following documents:
774 @itemize *
776 @item 
777 @cite{Ada 95 Reference Manual}, @cite{Ada 2005 Reference Manual}, and
778 @cite{Ada 2012 Reference Manual}, which contain reference
779 material for the several revisions of the Ada language standard.
781 @item 
782 @cite{GNAT Reference_Manual}, which contains all reference material for the GNAT
783 implementation of Ada.
785 @item 
786 @cite{Using the GNAT Programming Studio}, which describes the GPS
787 Integrated Development Environment.
789 @item 
790 @cite{GNAT Programming Studio Tutorial}, which introduces the
791 main GPS features through examples.
793 @item 
794 @cite{Debugging with GDB},
795 for all details on the use of the GNU source-level debugger.
797 @item 
798 @cite{GNU Emacs Manual},
799 for full information on the extensible editor and programming
800 environment Emacs.
801 @end itemize
803 @node A Note to Readers of Previous Versions of the Manual,Conventions,Related Information,About This Guide
804 @anchor{gnat_ugn/about_this_guide a-note-to-readers-of-previous-versions-of-the-manual}@anchor{15}
805 @section A Note to Readers of Previous Versions of the Manual
808 In early 2015 the GNAT manuals were transitioned to the
809 reStructuredText (rst) / Sphinx documentation generator technology.
810 During that process the @cite{GNAT User's Guide} was reorganized
811 so that related topics would be described together in the same chapter
812 or appendix.  Here's a summary of the major changes realized in
813 the new document structure.
816 @itemize *
818 @item 
819 @ref{9,,The GNAT Compilation Model} has been extended so that it now covers
820 the following material:
823 @itemize -
825 @item 
826 The @cite{gnatname}, @cite{gnatkr}, and @cite{gnatchop} tools
828 @item 
829 @ref{16,,Configuration Pragmas}
831 @item 
832 @ref{17,,GNAT and Libraries}
834 @item 
835 @ref{18,,Conditional Compilation} including @ref{19,,Preprocessing with gnatprep}
836 and @ref{1a,,Integrated Preprocessing}
838 @item 
839 @ref{1b,,Generating Ada Bindings for C and C++ headers}
841 @item 
842 @ref{1c,,Using GNAT Files with External Tools}
843 @end itemize
845 @item 
846 @ref{a,,Building Executable Programs with GNAT} is a new chapter consolidating
847 the following content:
850 @itemize -
852 @item 
853 @ref{1d,,Building with gnatmake}
855 @item 
856 @ref{1e,,Compiling with gcc}
858 @item 
859 @ref{1f,,Binding with gnatbind}
861 @item 
862 @ref{20,,Linking with gnatlink}
864 @item 
865 @ref{21,,Using the GNU make Utility}
866 @end itemize
868 @item 
869 @ref{d,,GNAT Utility Programs} is a new chapter consolidating the information about several
870 GNAT tools:
874 @itemize -
876 @item 
877 @ref{22,,The File Cleanup Utility gnatclean}
879 @item 
880 @ref{23,,The GNAT Library Browser gnatls}
882 @item 
883 @ref{24,,The Cross-Referencing Tools gnatxref and gnatfind}
885 @item 
886 @ref{25,,The Ada to HTML Converter gnathtml}
887 @end itemize
889 @item 
890 @ref{e,,GNAT and Program Execution} is a new chapter consolidating the following:
893 @itemize -
895 @item 
896 @ref{26,,Running and Debugging Ada Programs}
898 @item 
899 @ref{27,,Code Coverage and Profiling}
901 @item 
902 @ref{28,,Improving Performance}
904 @item 
905 @ref{29,,Overflow Check Handling in GNAT}
907 @item 
908 @ref{2a,,Performing Dimensionality Analysis in GNAT}
910 @item 
911 @ref{2b,,Stack Related Facilities}
913 @item 
914 @ref{2c,,Memory Management Issues}
915 @end itemize
917 @item 
918 @ref{f,,Platform-Specific Information} is a new appendix consolidating the following:
921 @itemize -
923 @item 
924 @ref{2d,,Run-Time Libraries}
926 @item 
927 @ref{2e,,Microsoft Windows Topics}
929 @item 
930 @ref{2f,,Mac OS Topics}
931 @end itemize
933 @item 
934 The @cite{Compatibility and Porting Guide} appendix has been moved to the
935 @cite{GNAT Reference Manual}. It now includes a section
936 @cite{Writing Portable Fixed-Point Declarations} which was previously
937 a separate chapter in the @cite{GNAT User's Guide}.
938 @end itemize
940 @node Conventions,,A Note to Readers of Previous Versions of the Manual,About This Guide
941 @anchor{gnat_ugn/about_this_guide conventions}@anchor{30}
942 @section Conventions
945 @geindex Conventions
946 @geindex typographical
948 @geindex Typographical conventions
950 Following are examples of the typographical and graphic conventions used
951 in this guide:
954 @itemize *
956 @item 
957 @cite{Functions}, @cite{utility program names}, @cite{standard names},
958 and @cite{classes}.
960 @item 
961 @cite{Option flags}
963 @item 
964 @code{File names}
966 @item 
967 @cite{Variables}
969 @item 
970 @emph{Emphasis}
972 @item 
973 [optional information or parameters]
975 @item 
976 Examples are described by text
978 @example
979 and then shown this way.
980 @end example
982 @item 
983 Commands that are entered by the user are shown as preceded by a prompt string
984 comprising the @code{$} character followed by a space.
986 @item 
987 Full file names are shown with the '/' character
988 as the directory separator; e.g., @code{parent-dir/subdir/myfile.adb}.
989 If you are using GNAT on a Windows platform, please note that
990 the '\' character should be used instead.
991 @end itemize
993 @node Getting Started with GNAT,The GNAT Compilation Model,About This Guide,Top
994 @anchor{gnat_ugn/getting_started_with_gnat getting-started-with-gnat}@anchor{8}@anchor{gnat_ugn/getting_started_with_gnat doc}@anchor{31}@anchor{gnat_ugn/getting_started_with_gnat id1}@anchor{32}
995 @chapter Getting Started with GNAT
998 This chapter describes how to use GNAT's command line interface to build
999 executable Ada programs.
1000 On most platforms a visually oriented Integrated Development Environment
1001 is also available, the GNAT Programming Studio (GPS).
1002 GPS offers a graphical "look and feel", support for development in
1003 other programming languages, comprehensive browsing features, and
1004 many other capabilities.
1005 For information on GPS please refer to
1006 @cite{Using the GNAT Programming Studio}.
1008 @menu
1009 * Running GNAT:: 
1010 * Running a Simple Ada Program:: 
1011 * Running a Program with Multiple Units:: 
1012 * Using the gnatmake Utility:: 
1014 @end menu
1016 @node Running GNAT,Running a Simple Ada Program,,Getting Started with GNAT
1017 @anchor{gnat_ugn/getting_started_with_gnat running-gnat}@anchor{33}@anchor{gnat_ugn/getting_started_with_gnat id2}@anchor{34}
1018 @section Running GNAT
1021 Three steps are needed to create an executable file from an Ada source
1022 file:
1025 @itemize *
1027 @item 
1028 The source file(s) must be compiled.
1030 @item 
1031 The file(s) must be bound using the GNAT binder.
1033 @item 
1034 All appropriate object files must be linked to produce an executable.
1035 @end itemize
1037 All three steps are most commonly handled by using the @emph{gnatmake}
1038 utility program that, given the name of the main program, automatically
1039 performs the necessary compilation, binding and linking steps.
1041 @node Running a Simple Ada Program,Running a Program with Multiple Units,Running GNAT,Getting Started with GNAT
1042 @anchor{gnat_ugn/getting_started_with_gnat running-a-simple-ada-program}@anchor{35}@anchor{gnat_ugn/getting_started_with_gnat id3}@anchor{36}
1043 @section Running a Simple Ada Program
1046 Any text editor may be used to prepare an Ada program.
1047 (If Emacs is used, the optional Ada mode may be helpful in laying out the
1048 program.)
1049 The program text is a normal text file. We will assume in our initial
1050 example that you have used your editor to prepare the following
1051 standard format text file:
1053 @example
1054 with Ada.Text_IO; use Ada.Text_IO;
1055 procedure Hello is
1056 begin
1057    Put_Line ("Hello WORLD!");
1058 end Hello;
1059 @end example
1061 This file should be named @code{hello.adb}.
1062 With the normal default file naming conventions, GNAT requires
1063 that each file
1064 contain a single compilation unit whose file name is the
1065 unit name,
1066 with periods replaced by hyphens; the
1067 extension is @code{ads} for a
1068 spec and @code{adb} for a body.
1069 You can override this default file naming convention by use of the
1070 special pragma @cite{Source_File_Name} (for further information please
1071 see @ref{37,,Using Other File Names}).
1072 Alternatively, if you want to rename your files according to this default
1073 convention, which is probably more convenient if you will be using GNAT
1074 for all your compilations, then the @cite{gnatchop} utility
1075 can be used to generate correctly-named source files
1076 (see @ref{38,,Renaming Files with gnatchop}).
1078 You can compile the program using the following command (@cite{$} is used
1079 as the command prompt in the examples in this document):
1081 @example
1082 $ gcc -c hello.adb
1083 @end example
1085 @emph{gcc} is the command used to run the compiler. This compiler is
1086 capable of compiling programs in several languages, including Ada and
1087 C. It assumes that you have given it an Ada program if the file extension is
1088 either @code{.ads} or @code{.adb}, and it will then call
1089 the GNAT compiler to compile the specified file.
1091 The @code{-c} switch is required. It tells @emph{gcc} to only do a
1092 compilation. (For C programs, @emph{gcc} can also do linking, but this
1093 capability is not used directly for Ada programs, so the @code{-c}
1094 switch must always be present.)
1096 This compile command generates a file
1097 @code{hello.o}, which is the object
1098 file corresponding to your Ada program. It also generates
1099 an 'Ada Library Information' file @code{hello.ali},
1100 which contains additional information used to check
1101 that an Ada program is consistent.
1102 To build an executable file,
1103 use @cite{gnatbind} to bind the program
1104 and @emph{gnatlink} to link it. The
1105 argument to both @cite{gnatbind} and @emph{gnatlink} is the name of the
1106 @code{ALI} file, but the default extension of @code{.ali} can
1107 be omitted. This means that in the most common case, the argument
1108 is simply the name of the main program:
1110 @example
1111 $ gnatbind hello
1112 $ gnatlink hello
1113 @end example
1115 A simpler method of carrying out these steps is to use @emph{gnatmake},
1116 a master program that invokes all the required
1117 compilation, binding and linking tools in the correct order. In particular,
1118 @emph{gnatmake} automatically recompiles any sources that have been
1119 modified since they were last compiled, or sources that depend
1120 on such modified sources, so that 'version skew' is avoided.
1122 @geindex Version skew (avoided by *gnatmake*)
1124 @example
1125 $ gnatmake hello.adb
1126 @end example
1128 The result is an executable program called @code{hello}, which can be
1129 run by entering:
1131 @example
1132 $ hello
1133 @end example
1135 assuming that the current directory is on the search path
1136 for executable programs.
1138 and, if all has gone well, you will see:
1140 @example
1141 Hello WORLD!
1142 @end example
1144 appear in response to this command.
1146 @node Running a Program with Multiple Units,Using the gnatmake Utility,Running a Simple Ada Program,Getting Started with GNAT
1147 @anchor{gnat_ugn/getting_started_with_gnat id4}@anchor{39}@anchor{gnat_ugn/getting_started_with_gnat running-a-program-with-multiple-units}@anchor{3a}
1148 @section Running a Program with Multiple Units
1151 Consider a slightly more complicated example that has three files: a
1152 main program, and the spec and body of a package:
1154 @example
1155 package Greetings is
1156    procedure Hello;
1157    procedure Goodbye;
1158 end Greetings;
1160 with Ada.Text_IO; use Ada.Text_IO;
1161 package body Greetings is
1162    procedure Hello is
1163    begin
1164       Put_Line ("Hello WORLD!");
1165    end Hello;
1167    procedure Goodbye is
1168    begin
1169       Put_Line ("Goodbye WORLD!");
1170    end Goodbye;
1171 end Greetings;
1173 with Greetings;
1174 procedure Gmain is
1175 begin
1176    Greetings.Hello;
1177    Greetings.Goodbye;
1178 end Gmain;
1179 @end example
1181 Following the one-unit-per-file rule, place this program in the
1182 following three separate files:
1185 @table @asis
1187 @item @emph{greetings.ads}
1189 spec of package @cite{Greetings}
1191 @item @emph{greetings.adb}
1193 body of package @cite{Greetings}
1195 @item @emph{gmain.adb}
1197 body of main program
1198 @end table
1200 To build an executable version of
1201 this program, we could use four separate steps to compile, bind, and link
1202 the program, as follows:
1204 @example
1205 $ gcc -c gmain.adb
1206 $ gcc -c greetings.adb
1207 $ gnatbind gmain
1208 $ gnatlink gmain
1209 @end example
1211 Note that there is no required order of compilation when using GNAT.
1212 In particular it is perfectly fine to compile the main program first.
1213 Also, it is not necessary to compile package specs in the case where
1214 there is an accompanying body; you only need to compile the body. If you want
1215 to submit these files to the compiler for semantic checking and not code
1216 generation, then use the @code{-gnatc} switch:
1218 @example
1219 $ gcc -c greetings.ads -gnatc
1220 @end example
1222 Although the compilation can be done in separate steps as in the
1223 above example, in practice it is almost always more convenient
1224 to use the @emph{gnatmake} tool. All you need to know in this case
1225 is the name of the main program's source file. The effect of the above four
1226 commands can be achieved with a single one:
1228 @example
1229 $ gnatmake gmain.adb
1230 @end example
1232 In the next section we discuss the advantages of using @emph{gnatmake} in
1233 more detail.
1235 @node Using the gnatmake Utility,,Running a Program with Multiple Units,Getting Started with GNAT
1236 @anchor{gnat_ugn/getting_started_with_gnat using-the-gnatmake-utility}@anchor{3b}@anchor{gnat_ugn/getting_started_with_gnat id5}@anchor{3c}
1237 @section Using the @emph{gnatmake} Utility
1240 If you work on a program by compiling single components at a time using
1241 @emph{gcc}, you typically keep track of the units you modify. In order to
1242 build a consistent system, you compile not only these units, but also any
1243 units that depend on the units you have modified.
1244 For example, in the preceding case,
1245 if you edit @code{gmain.adb}, you only need to recompile that file. But if
1246 you edit @code{greetings.ads}, you must recompile both
1247 @code{greetings.adb} and @code{gmain.adb}, because both files contain
1248 units that depend on @code{greetings.ads}.
1250 @emph{gnatbind} will warn you if you forget one of these compilation
1251 steps, so that it is impossible to generate an inconsistent program as a
1252 result of forgetting to do a compilation. Nevertheless it is tedious and
1253 error-prone to keep track of dependencies among units.
1254 One approach to handle the dependency-bookkeeping is to use a
1255 makefile. However, makefiles present maintenance problems of their own:
1256 if the dependencies change as you change the program, you must make
1257 sure that the makefile is kept up-to-date manually, which is also an
1258 error-prone process.
1260 The @emph{gnatmake} utility takes care of these details automatically.
1261 Invoke it using either one of the following forms:
1263 @example
1264 $ gnatmake gmain.adb
1265 $ gnatmake gmain
1266 @end example
1268 The argument is the name of the file containing the main program;
1269 you may omit the extension. @emph{gnatmake}
1270 examines the environment, automatically recompiles any files that need
1271 recompiling, and binds and links the resulting set of object files,
1272 generating the executable file, @code{gmain}.
1273 In a large program, it
1274 can be extremely helpful to use @emph{gnatmake}, because working out by hand
1275 what needs to be recompiled can be difficult.
1277 Note that @emph{gnatmake} takes into account all the Ada rules that
1278 establish dependencies among units. These include dependencies that result
1279 from inlining subprogram bodies, and from
1280 generic instantiation. Unlike some other
1281 Ada make tools, @emph{gnatmake} does not rely on the dependencies that were
1282 found by the compiler on a previous compilation, which may possibly
1283 be wrong when sources change. @emph{gnatmake} determines the exact set of
1284 dependencies from scratch each time it is run.
1286 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
1288 @node The GNAT Compilation Model,Building Executable Programs with GNAT,Getting Started with GNAT,Top
1289 @anchor{gnat_ugn/the_gnat_compilation_model doc}@anchor{3d}@anchor{gnat_ugn/the_gnat_compilation_model the-gnat-compilation-model}@anchor{9}@anchor{gnat_ugn/the_gnat_compilation_model id1}@anchor{3e}
1290 @chapter The GNAT Compilation Model
1293 @geindex GNAT compilation model
1295 @geindex Compilation model
1297 This chapter describes the compilation model used by GNAT. Although
1298 similar to that used by other languages such as C and C++, this model
1299 is substantially different from the traditional Ada compilation models,
1300 which are based on a centralized program library. The chapter covers
1301 the following material:
1304 @itemize *
1306 @item 
1307 Topics related to source file makeup and naming
1310 @itemize *
1312 @item 
1313 @ref{3f,,Source Representation}
1315 @item 
1316 @ref{40,,Foreign Language Representation}
1318 @item 
1319 @ref{41,,File Naming Topics and Utilities}
1320 @end itemize
1322 @item 
1323 @ref{16,,Configuration Pragmas}
1325 @item 
1326 @ref{42,,Generating Object Files}
1328 @item 
1329 @ref{43,,Source Dependencies}
1331 @item 
1332 @ref{44,,The Ada Library Information Files}
1334 @item 
1335 @ref{45,,Binding an Ada Program}
1337 @item 
1338 @ref{17,,GNAT and Libraries}
1340 @item 
1341 @ref{18,,Conditional Compilation}
1343 @item 
1344 @ref{46,,Mixed Language Programming}
1346 @item 
1347 @ref{47,,GNAT and Other Compilation Models}
1349 @item 
1350 @ref{1c,,Using GNAT Files with External Tools}
1351 @end itemize
1353 @menu
1354 * Source Representation:: 
1355 * Foreign Language Representation:: 
1356 * File Naming Topics and Utilities:: 
1357 * Configuration Pragmas:: 
1358 * Generating Object Files:: 
1359 * Source Dependencies:: 
1360 * The Ada Library Information Files:: 
1361 * Binding an Ada Program:: 
1362 * GNAT and Libraries:: 
1363 * Conditional Compilation:: 
1364 * Mixed Language Programming:: 
1365 * GNAT and Other Compilation Models:: 
1366 * Using GNAT Files with External Tools:: 
1368 @end menu
1370 @node Source Representation,Foreign Language Representation,,The GNAT Compilation Model
1371 @anchor{gnat_ugn/the_gnat_compilation_model source-representation}@anchor{3f}@anchor{gnat_ugn/the_gnat_compilation_model id2}@anchor{48}
1372 @section Source Representation
1375 @geindex Latin-1
1377 @geindex VT
1378 @geindex HT
1379 @geindex CR
1380 @geindex LF
1381 @geindex FF
1383 Ada source programs are represented in standard text files, using
1384 Latin-1 coding. Latin-1 is an 8-bit code that includes the familiar
1385 7-bit ASCII set, plus additional characters used for
1386 representing foreign languages (see @ref{40,,Foreign Language Representation}
1387 for support of non-USA character sets). The format effector characters
1388 are represented using their standard ASCII encodings, as follows:
1390 @quotation
1393 @multitable {xxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxx} 
1394 @item
1396 Character
1398 @tab
1400 Effect
1402 @tab
1404 Code
1406 @item
1408 @code{VT}
1410 @tab
1412 Vertical tab
1414 @tab
1416 @cite{16#0B#}
1418 @item
1420 @code{HT}
1422 @tab
1424 Horizontal tab
1426 @tab
1428 @cite{16#09#}
1430 @item
1432 @code{CR}
1434 @tab
1436 Carriage return
1438 @tab
1440 @cite{16#0D#}
1442 @item
1444 @code{LF}
1446 @tab
1448 Line feed
1450 @tab
1452 @cite{16#0A#}
1454 @item
1456 @code{FF}
1458 @tab
1460 Form feed
1462 @tab
1464 @cite{16#0C#}
1466 @end multitable
1468 @end quotation
1470 Source files are in standard text file format. In addition, GNAT will
1471 recognize a wide variety of stream formats, in which the end of
1472 physical lines is marked by any of the following sequences:
1473 @cite{LF}, @cite{CR}, @cite{CR-LF}, or @cite{LF-CR}. This is useful
1474 in accommodating files that are imported from other operating systems.
1476 @geindex End of source file; Source file@comma{} end
1478 @geindex SUB (control character)
1480 The end of a source file is normally represented by the physical end of
1481 file. However, the control character @cite{16#1A#} (@code{SUB}) is also
1482 recognized as signalling the end of the source file. Again, this is
1483 provided for compatibility with other operating systems where this
1484 code is used to represent the end of file.
1486 @geindex spec (definition)
1487 @geindex compilation (definition)
1489 Each file contains a single Ada compilation unit, including any pragmas
1490 associated with the unit. For example, this means you must place a
1491 package declaration (a package @cite{spec}) and the corresponding body in
1492 separate files. An Ada @cite{compilation} (which is a sequence of
1493 compilation units) is represented using a sequence of files. Similarly,
1494 you will place each subunit or child unit in a separate file.
1496 @node Foreign Language Representation,File Naming Topics and Utilities,Source Representation,The GNAT Compilation Model
1497 @anchor{gnat_ugn/the_gnat_compilation_model foreign-language-representation}@anchor{40}@anchor{gnat_ugn/the_gnat_compilation_model id3}@anchor{49}
1498 @section Foreign Language Representation
1501 GNAT supports the standard character sets defined in Ada as well as
1502 several other non-standard character sets for use in localized versions
1503 of the compiler (@ref{4a,,Character Set Control}).
1505 @menu
1506 * Latin-1:: 
1507 * Other 8-Bit Codes:: 
1508 * Wide_Character Encodings:: 
1509 * Wide_Wide_Character Encodings:: 
1511 @end menu
1513 @node Latin-1,Other 8-Bit Codes,,Foreign Language Representation
1514 @anchor{gnat_ugn/the_gnat_compilation_model id4}@anchor{4b}@anchor{gnat_ugn/the_gnat_compilation_model latin-1}@anchor{4c}
1515 @subsection Latin-1
1518 @geindex Latin-1
1520 The basic character set is Latin-1. This character set is defined by ISO
1521 standard 8859, part 1. The lower half (character codes @cite{16#00#}
1522 ... @cite{16#7F#)} is identical to standard ASCII coding, but the upper
1523 half is used to represent additional characters. These include extended letters
1524 used by European languages, such as French accents, the vowels with umlauts
1525 used in German, and the extra letter A-ring used in Swedish.
1527 @geindex Ada.Characters.Latin_1
1529 For a complete list of Latin-1 codes and their encodings, see the source
1530 file of library unit @cite{Ada.Characters.Latin_1} in file
1531 @code{a-chlat1.ads}.
1532 You may use any of these extended characters freely in character or
1533 string literals. In addition, the extended characters that represent
1534 letters can be used in identifiers.
1536 @node Other 8-Bit Codes,Wide_Character Encodings,Latin-1,Foreign Language Representation
1537 @anchor{gnat_ugn/the_gnat_compilation_model other-8-bit-codes}@anchor{4d}@anchor{gnat_ugn/the_gnat_compilation_model id5}@anchor{4e}
1538 @subsection Other 8-Bit Codes
1541 GNAT also supports several other 8-bit coding schemes:
1543 @geindex Latin-2
1545 @geindex ISO 8859-2
1548 @table @asis
1550 @item @emph{ISO 8859-2 (Latin-2)}
1552 Latin-2 letters allowed in identifiers, with uppercase and lowercase
1553 equivalence.
1554 @end table
1556 @geindex Latin-3
1558 @geindex ISO 8859-3
1561 @table @asis
1563 @item @emph{ISO 8859-3 (Latin-3)}
1565 Latin-3 letters allowed in identifiers, with uppercase and lowercase
1566 equivalence.
1567 @end table
1569 @geindex Latin-4
1571 @geindex ISO 8859-4
1574 @table @asis
1576 @item @emph{ISO 8859-4 (Latin-4)}
1578 Latin-4 letters allowed in identifiers, with uppercase and lowercase
1579 equivalence.
1580 @end table
1582 @geindex ISO 8859-5
1584 @geindex Cyrillic
1587 @table @asis
1589 @item @emph{ISO 8859-5 (Cyrillic)}
1591 ISO 8859-5 letters (Cyrillic) allowed in identifiers, with uppercase and
1592 lowercase equivalence.
1593 @end table
1595 @geindex ISO 8859-15
1597 @geindex Latin-9
1600 @table @asis
1602 @item @emph{ISO 8859-15 (Latin-9)}
1604 ISO 8859-15 (Latin-9) letters allowed in identifiers, with uppercase and
1605 lowercase equivalence
1606 @end table
1608 @geindex code page 437 (IBM PC)
1611 @table @asis
1613 @item @emph{IBM PC (code page 437)}
1615 This code page is the normal default for PCs in the U.S. It corresponds
1616 to the original IBM PC character set. This set has some, but not all, of
1617 the extended Latin-1 letters, but these letters do not have the same
1618 encoding as Latin-1. In this mode, these letters are allowed in
1619 identifiers with uppercase and lowercase equivalence.
1620 @end table
1622 @geindex code page 850 (IBM PC)
1625 @table @asis
1627 @item @emph{IBM PC (code page 850)}
1629 This code page is a modification of 437 extended to include all the
1630 Latin-1 letters, but still not with the usual Latin-1 encoding. In this
1631 mode, all these letters are allowed in identifiers with uppercase and
1632 lowercase equivalence.
1634 @item @emph{Full Upper 8-bit}
1636 Any character in the range 80-FF allowed in identifiers, and all are
1637 considered distinct. In other words, there are no uppercase and lowercase
1638 equivalences in this range. This is useful in conjunction with
1639 certain encoding schemes used for some foreign character sets (e.g.,
1640 the typical method of representing Chinese characters on the PC).
1642 @item @emph{No Upper-Half}
1644 No upper-half characters in the range 80-FF are allowed in identifiers.
1645 This gives Ada 83 compatibility for identifier names.
1646 @end table
1648 For precise data on the encodings permitted, and the uppercase and lowercase
1649 equivalences that are recognized, see the file @code{csets.adb} in
1650 the GNAT compiler sources. You will need to obtain a full source release
1651 of GNAT to obtain this file.
1653 @node Wide_Character Encodings,Wide_Wide_Character Encodings,Other 8-Bit Codes,Foreign Language Representation
1654 @anchor{gnat_ugn/the_gnat_compilation_model id6}@anchor{4f}@anchor{gnat_ugn/the_gnat_compilation_model wide-character-encodings}@anchor{50}
1655 @subsection Wide_Character Encodings
1658 GNAT allows wide character codes to appear in character and string
1659 literals, and also optionally in identifiers, by means of the following
1660 possible encoding schemes:
1663 @table @asis
1665 @item @emph{Hex Coding}
1667 In this encoding, a wide character is represented by the following five
1668 character sequence:
1670 @example
1671 ESC a b c d
1672 @end example
1674 where @cite{a}, @cite{b}, @cite{c}, @cite{d} are the four hexadecimal
1675 characters (using uppercase letters) of the wide character code. For
1676 example, ESC A345 is used to represent the wide character with code
1677 @cite{16#A345#}.
1678 This scheme is compatible with use of the full Wide_Character set.
1680 @item @emph{Upper-Half Coding}
1682 @geindex Upper-Half Coding
1684 The wide character with encoding @cite{16#abcd#} where the upper bit is on
1685 (in other words, 'a' is in the range 8-F) is represented as two bytes,
1686 @cite{16#ab#} and @cite{16#cd#}. The second byte cannot be a format control
1687 character, but is not required to be in the upper half. This method can
1688 be also used for shift-JIS or EUC, where the internal coding matches the
1689 external coding.
1691 @item @emph{Shift JIS Coding}
1693 @geindex Shift JIS Coding
1695 A wide character is represented by a two-character sequence,
1696 @cite{16#ab#} and
1697 @cite{16#cd#}, with the restrictions described for upper-half encoding as
1698 described above. The internal character code is the corresponding JIS
1699 character according to the standard algorithm for Shift-JIS
1700 conversion. Only characters defined in the JIS code set table can be
1701 used with this encoding method.
1703 @item @emph{EUC Coding}
1705 @geindex EUC Coding
1707 A wide character is represented by a two-character sequence
1708 @cite{16#ab#} and
1709 @cite{16#cd#}, with both characters being in the upper half. The internal
1710 character code is the corresponding JIS character according to the EUC
1711 encoding algorithm. Only characters defined in the JIS code set table
1712 can be used with this encoding method.
1714 @item @emph{UTF-8 Coding}
1716 A wide character is represented using
1717 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
1718 10646-1/Am.2. Depending on the character value, the representation
1719 is a one, two, or three byte sequence:
1721 @example
1722 16#0000#-16#007f#: 2#0`xxxxxxx`#
1723 16#0080#-16#07ff#: 2#110`xxxxx`# 2#10`xxxxxx`#
1724 16#0800#-16#ffff#: 2#1110`xxxx`# 2#10`xxxxxx`# 2#10`xxxxxx`#
1725 @end example
1727 where the @cite{xxx} bits correspond to the left-padded bits of the
1728 16-bit character value. Note that all lower half ASCII characters
1729 are represented as ASCII bytes and all upper half characters and
1730 other wide characters are represented as sequences of upper-half
1731 (The full UTF-8 scheme allows for encoding 31-bit characters as
1732 6-byte sequences, and in the following section on wide wide
1733 characters, the use of these sequences is documented).
1735 @item @emph{Brackets Coding}
1737 In this encoding, a wide character is represented by the following eight
1738 character sequence:
1740 @example
1741 [ " a b c d " ]
1742 @end example
1744 where @cite{a}, @cite{b}, @cite{c}, @cite{d} are the four hexadecimal
1745 characters (using uppercase letters) of the wide character code. For
1746 example, ['A345'] is used to represent the wide character with code
1747 @cite{16#A345#}. It is also possible (though not required) to use the
1748 Brackets coding for upper half characters. For example, the code
1749 @cite{16#A3#} can be represented as @cite{['A3']}.
1751 This scheme is compatible with use of the full Wide_Character set,
1752 and is also the method used for wide character encoding in some standard
1753 ACATS (Ada Conformity Assessment Test Suite) test suite distributions.
1754 @end table
1756 @cartouche
1757 @quotation Note 
1758 Some of these coding schemes do not permit the full use of the
1759 Ada character set. For example, neither Shift JIS nor EUC allow the
1760 use of the upper half of the Latin-1 set.
1761 @end quotation
1762 @end cartouche
1764 @node Wide_Wide_Character Encodings,,Wide_Character Encodings,Foreign Language Representation
1765 @anchor{gnat_ugn/the_gnat_compilation_model id7}@anchor{51}@anchor{gnat_ugn/the_gnat_compilation_model wide-wide-character-encodings}@anchor{52}
1766 @subsection Wide_Wide_Character Encodings
1769 GNAT allows wide wide character codes to appear in character and string
1770 literals, and also optionally in identifiers, by means of the following
1771 possible encoding schemes:
1774 @table @asis
1776 @item @emph{UTF-8 Coding}
1778 A wide character is represented using
1779 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
1780 10646-1/Am.2. Depending on the character value, the representation
1781 of character codes with values greater than 16#FFFF# is a
1782 is a four, five, or six byte sequence:
1784 @example
1785 16#01_0000#-16#10_FFFF#:     11110xxx 10xxxxxx 10xxxxxx
1786                              10xxxxxx
1787 16#0020_0000#-16#03FF_FFFF#: 111110xx 10xxxxxx 10xxxxxx
1788                              10xxxxxx 10xxxxxx
1789 16#0400_0000#-16#7FFF_FFFF#: 1111110x 10xxxxxx 10xxxxxx
1790                              10xxxxxx 10xxxxxx 10xxxxxx
1791 @end example
1793 where the @cite{xxx} bits correspond to the left-padded bits of the
1794 32-bit character value.
1796 @item @emph{Brackets Coding}
1798 In this encoding, a wide wide character is represented by the following ten or
1799 twelve byte character sequence:
1801 @example
1802 [ " a b c d e f " ]
1803 [ " a b c d e f g h " ]
1804 @end example
1806 where @cite{a-h} are the six or eight hexadecimal
1807 characters (using uppercase letters) of the wide wide character code. For
1808 example, ["1F4567"] is used to represent the wide wide character with code
1809 @cite{16#001F_4567#}.
1811 This scheme is compatible with use of the full Wide_Wide_Character set,
1812 and is also the method used for wide wide character encoding in some standard
1813 ACATS (Ada Conformity Assessment Test Suite) test suite distributions.
1814 @end table
1816 @node File Naming Topics and Utilities,Configuration Pragmas,Foreign Language Representation,The GNAT Compilation Model
1817 @anchor{gnat_ugn/the_gnat_compilation_model id8}@anchor{53}@anchor{gnat_ugn/the_gnat_compilation_model file-naming-topics-and-utilities}@anchor{41}
1818 @section File Naming Topics and Utilities
1821 GNAT has a default file naming scheme and also provides the user with
1822 a high degree of control over how the names and extensions of the
1823 source files correspond to the Ada compilation units that they contain.
1825 @menu
1826 * File Naming Rules:: 
1827 * Using Other File Names:: 
1828 * Alternative File Naming Schemes:: 
1829 * Handling Arbitrary File Naming Conventions with gnatname:: 
1830 * File Name Krunching with gnatkr:: 
1831 * Renaming Files with gnatchop:: 
1833 @end menu
1835 @node File Naming Rules,Using Other File Names,,File Naming Topics and Utilities
1836 @anchor{gnat_ugn/the_gnat_compilation_model file-naming-rules}@anchor{54}@anchor{gnat_ugn/the_gnat_compilation_model id9}@anchor{55}
1837 @subsection File Naming Rules
1840 The default file name is determined by the name of the unit that the
1841 file contains. The name is formed by taking the full expanded name of
1842 the unit and replacing the separating dots with hyphens and using
1843 lowercase for all letters.
1845 An exception arises if the file name generated by the above rules starts
1846 with one of the characters
1847 @cite{a}, @cite{g}, @cite{i}, or @cite{s}, and the second character is a
1848 minus. In this case, the character tilde is used in place
1849 of the minus. The reason for this special rule is to avoid clashes with
1850 the standard names for child units of the packages System, Ada,
1851 Interfaces, and GNAT, which use the prefixes
1852 @cite{s-}, @cite{a-}, @cite{i-}, and @cite{g-},
1853 respectively.
1855 The file extension is @code{.ads} for a spec and
1856 @code{.adb} for a body. The following table shows some
1857 examples of these rules.
1859 @quotation
1862 @multitable {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} 
1863 @item
1865 Source File
1867 @tab
1869 Ada Compilation Unit
1871 @item
1873 @code{main.ads}
1875 @tab
1877 Main (spec)
1879 @item
1881 @code{main.adb}
1883 @tab
1885 Main (body)
1887 @item
1889 @code{arith_functions.ads}
1891 @tab
1893 Arith_Functions (package spec)
1895 @item
1897 @code{arith_functions.adb}
1899 @tab
1901 Arith_Functions (package body)
1903 @item
1905 @code{func-spec.ads}
1907 @tab
1909 Func.Spec (child package spec)
1911 @item
1913 @code{func-spec.adb}
1915 @tab
1917 Func.Spec (child package body)
1919 @item
1921 @code{main-sub.adb}
1923 @tab
1925 Sub (subunit of Main)
1927 @item
1929 @code{a~bad.adb}
1931 @tab
1933 A.Bad (child package body)
1935 @end multitable
1937 @end quotation
1939 Following these rules can result in excessively long
1940 file names if corresponding
1941 unit names are long (for example, if child units or subunits are
1942 heavily nested). An option is available to shorten such long file names
1943 (called file name 'krunching'). This may be particularly useful when
1944 programs being developed with GNAT are to be used on operating systems
1945 with limited file name lengths. @ref{56,,Using gnatkr}.
1947 Of course, no file shortening algorithm can guarantee uniqueness over
1948 all possible unit names; if file name krunching is used, it is your
1949 responsibility to ensure no name clashes occur. Alternatively you
1950 can specify the exact file names that you want used, as described
1951 in the next section. Finally, if your Ada programs are migrating from a
1952 compiler with a different naming convention, you can use the gnatchop
1953 utility to produce source files that follow the GNAT naming conventions.
1954 (For details see @ref{38,,Renaming Files with gnatchop}.)
1956 Note: in the case of Windows or Mac OS operating systems, case is not
1957 significant. So for example on @cite{Windows} if the canonical name is
1958 @cite{main-sub.adb}, you can use the file name @code{Main-Sub.adb} instead.
1959 However, case is significant for other operating systems, so for example,
1960 if you want to use other than canonically cased file names on a Unix system,
1961 you need to follow the procedures described in the next section.
1963 @node Using Other File Names,Alternative File Naming Schemes,File Naming Rules,File Naming Topics and Utilities
1964 @anchor{gnat_ugn/the_gnat_compilation_model id10}@anchor{57}@anchor{gnat_ugn/the_gnat_compilation_model using-other-file-names}@anchor{37}
1965 @subsection Using Other File Names
1968 @geindex File names
1970 In the previous section, we have described the default rules used by
1971 GNAT to determine the file name in which a given unit resides. It is
1972 often convenient to follow these default rules, and if you follow them,
1973 the compiler knows without being explicitly told where to find all
1974 the files it needs.
1976 @geindex Source_File_Name pragma
1978 However, in some cases, particularly when a program is imported from
1979 another Ada compiler environment, it may be more convenient for the
1980 programmer to specify which file names contain which units. GNAT allows
1981 arbitrary file names to be used by means of the Source_File_Name pragma.
1982 The form of this pragma is as shown in the following examples:
1984 @example
1985 pragma Source_File_Name (My_Utilities.Stacks,
1986   Spec_File_Name => "myutilst_a.ada");
1987 pragma Source_File_name (My_Utilities.Stacks,
1988   Body_File_Name => "myutilst.ada");
1989 @end example
1991 As shown in this example, the first argument for the pragma is the unit
1992 name (in this example a child unit). The second argument has the form
1993 of a named association. The identifier
1994 indicates whether the file name is for a spec or a body;
1995 the file name itself is given by a string literal.
1997 The source file name pragma is a configuration pragma, which means that
1998 normally it will be placed in the @code{gnat.adc}
1999 file used to hold configuration
2000 pragmas that apply to a complete compilation environment.
2001 For more details on how the @code{gnat.adc} file is created and used
2002 see @ref{58,,Handling of Configuration Pragmas}.
2004 @geindex gnat.adc
2006 GNAT allows completely arbitrary file names to be specified using the
2007 source file name pragma. However, if the file name specified has an
2008 extension other than @code{.ads} or @code{.adb} it is necessary to use
2009 a special syntax when compiling the file. The name in this case must be
2010 preceded by the special sequence @emph{-x} followed by a space and the name
2011 of the language, here @cite{ada}, as in:
2013 @example
2014 $ gcc -c -x ada peculiar_file_name.sim
2015 @end example
2017 @cite{gnatmake} handles non-standard file names in the usual manner (the
2018 non-standard file name for the main program is simply used as the
2019 argument to gnatmake). Note that if the extension is also non-standard,
2020 then it must be included in the @cite{gnatmake} command, it may not
2021 be omitted.
2023 @node Alternative File Naming Schemes,Handling Arbitrary File Naming Conventions with gnatname,Using Other File Names,File Naming Topics and Utilities
2024 @anchor{gnat_ugn/the_gnat_compilation_model id11}@anchor{59}@anchor{gnat_ugn/the_gnat_compilation_model alternative-file-naming-schemes}@anchor{5a}
2025 @subsection Alternative File Naming Schemes
2028 @geindex File naming schemes
2029 @geindex alternative
2031 @geindex File names
2033 The previous section described the use of the @cite{Source_File_Name}
2034 pragma to allow arbitrary names to be assigned to individual source files.
2035 However, this approach requires one pragma for each file, and especially in
2036 large systems can result in very long @code{gnat.adc} files, and also create
2037 a maintenance problem.
2039 @geindex Source_File_Name pragma
2041 GNAT also provides a facility for specifying systematic file naming schemes
2042 other than the standard default naming scheme previously described. An
2043 alternative scheme for naming is specified by the use of
2044 @cite{Source_File_Name} pragmas having the following format:
2046 @example
2047 pragma Source_File_Name (
2048    Spec_File_Name  => FILE_NAME_PATTERN
2049  [ , Casing          => CASING_SPEC]
2050  [ , Dot_Replacement => STRING_LITERAL ] );
2052 pragma Source_File_Name (
2053    Body_File_Name  => FILE_NAME_PATTERN
2054  [ , Casing          => CASING_SPEC ]
2055  [ , Dot_Replacement => STRING_LITERAL ] ) ;
2057 pragma Source_File_Name (
2058    Subunit_File_Name  => FILE_NAME_PATTERN
2059  [ , Casing          => CASING_SPEC ]
2060  [ , Dot_Replacement => STRING_LITERAL ] ) ;
2062 FILE_NAME_PATTERN ::= STRING_LITERAL
2063 CASING_SPEC ::= Lowercase | Uppercase | Mixedcase
2064 @end example
2066 The @cite{FILE_NAME_PATTERN} string shows how the file name is constructed.
2067 It contains a single asterisk character, and the unit name is substituted
2068 systematically for this asterisk. The optional parameter
2069 @cite{Casing} indicates
2070 whether the unit name is to be all upper-case letters, all lower-case letters,
2071 or mixed-case. If no
2072 @cite{Casing} parameter is used, then the default is all
2073 lower-case.
2075 The optional @cite{Dot_Replacement} string is used to replace any periods
2076 that occur in subunit or child unit names. If no @cite{Dot_Replacement}
2077 argument is used then separating dots appear unchanged in the resulting
2078 file name.
2079 Although the above syntax indicates that the
2080 @cite{Casing} argument must appear
2081 before the @cite{Dot_Replacement} argument, but it
2082 is also permissible to write these arguments in the opposite order.
2084 As indicated, it is possible to specify different naming schemes for
2085 bodies, specs, and subunits. Quite often the rule for subunits is the
2086 same as the rule for bodies, in which case, there is no need to give
2087 a separate @cite{Subunit_File_Name} rule, and in this case the
2088 @cite{Body_File_name} rule is used for subunits as well.
2090 The separate rule for subunits can also be used to implement the rather
2091 unusual case of a compilation environment (e.g., a single directory) which
2092 contains a subunit and a child unit with the same unit name. Although
2093 both units cannot appear in the same partition, the Ada Reference Manual
2094 allows (but does not require) the possibility of the two units coexisting
2095 in the same environment.
2097 The file name translation works in the following steps:
2100 @itemize *
2102 @item 
2103 If there is a specific @cite{Source_File_Name} pragma for the given unit,
2104 then this is always used, and any general pattern rules are ignored.
2106 @item 
2107 If there is a pattern type @cite{Source_File_Name} pragma that applies to
2108 the unit, then the resulting file name will be used if the file exists. If
2109 more than one pattern matches, the latest one will be tried first, and the
2110 first attempt resulting in a reference to a file that exists will be used.
2112 @item 
2113 If no pattern type @cite{Source_File_Name} pragma that applies to the unit
2114 for which the corresponding file exists, then the standard GNAT default
2115 naming rules are used.
2116 @end itemize
2118 As an example of the use of this mechanism, consider a commonly used scheme
2119 in which file names are all lower case, with separating periods copied
2120 unchanged to the resulting file name, and specs end with @code{.1.ada}, and
2121 bodies end with @code{.2.ada}. GNAT will follow this scheme if the following
2122 two pragmas appear:
2124 @example
2125 pragma Source_File_Name
2126   (Spec_File_Name => ".1.ada");
2127 pragma Source_File_Name
2128   (Body_File_Name => ".2.ada");
2129 @end example
2131 The default GNAT scheme is actually implemented by providing the following
2132 default pragmas internally:
2134 @example
2135 pragma Source_File_Name
2136   (Spec_File_Name => ".ads", Dot_Replacement => "-");
2137 pragma Source_File_Name
2138   (Body_File_Name => ".adb", Dot_Replacement => "-");
2139 @end example
2141 Our final example implements a scheme typically used with one of the
2142 Ada 83 compilers, where the separator character for subunits was '__'
2143 (two underscores), specs were identified by adding @code{_.ADA}, bodies
2144 by adding @code{.ADA}, and subunits by
2145 adding @code{.SEP}. All file names were
2146 upper case. Child units were not present of course since this was an
2147 Ada 83 compiler, but it seems reasonable to extend this scheme to use
2148 the same double underscore separator for child units.
2150 @example
2151 pragma Source_File_Name
2152   (Spec_File_Name => "_.ADA",
2153    Dot_Replacement => "__",
2154    Casing = Uppercase);
2155 pragma Source_File_Name
2156   (Body_File_Name => ".ADA",
2157    Dot_Replacement => "__",
2158    Casing = Uppercase);
2159 pragma Source_File_Name
2160   (Subunit_File_Name => ".SEP",
2161    Dot_Replacement => "__",
2162    Casing = Uppercase);
2163 @end example
2165 @geindex gnatname
2167 @node Handling Arbitrary File Naming Conventions with gnatname,File Name Krunching with gnatkr,Alternative File Naming Schemes,File Naming Topics and Utilities
2168 @anchor{gnat_ugn/the_gnat_compilation_model handling-arbitrary-file-naming-conventions-with-gnatname}@anchor{5b}@anchor{gnat_ugn/the_gnat_compilation_model id12}@anchor{5c}
2169 @subsection Handling Arbitrary File Naming Conventions with @cite{gnatname}
2172 @geindex File Naming Conventions
2174 @menu
2175 * Arbitrary File Naming Conventions:: 
2176 * Running gnatname:: 
2177 * Switches for gnatname:: 
2178 * Examples of gnatname Usage:: 
2180 @end menu
2182 @node Arbitrary File Naming Conventions,Running gnatname,,Handling Arbitrary File Naming Conventions with gnatname
2183 @anchor{gnat_ugn/the_gnat_compilation_model arbitrary-file-naming-conventions}@anchor{5d}@anchor{gnat_ugn/the_gnat_compilation_model id13}@anchor{5e}
2184 @subsubsection Arbitrary File Naming Conventions
2187 The GNAT compiler must be able to know the source file name of a compilation
2188 unit.  When using the standard GNAT default file naming conventions
2189 (@cite{.ads} for specs, @cite{.adb} for bodies), the GNAT compiler
2190 does not need additional information.
2192 When the source file names do not follow the standard GNAT default file naming
2193 conventions, the GNAT compiler must be given additional information through
2194 a configuration pragmas file (@ref{16,,Configuration Pragmas})
2195 or a project file.
2196 When the non-standard file naming conventions are well-defined,
2197 a small number of pragmas @cite{Source_File_Name} specifying a naming pattern
2198 (@ref{5a,,Alternative File Naming Schemes}) may be sufficient. However,
2199 if the file naming conventions are irregular or arbitrary, a number
2200 of pragma @cite{Source_File_Name} for individual compilation units
2201 must be defined.
2202 To help maintain the correspondence between compilation unit names and
2203 source file names within the compiler,
2204 GNAT provides a tool @cite{gnatname} to generate the required pragmas for a
2205 set of files.
2207 @node Running gnatname,Switches for gnatname,Arbitrary File Naming Conventions,Handling Arbitrary File Naming Conventions with gnatname
2208 @anchor{gnat_ugn/the_gnat_compilation_model running-gnatname}@anchor{5f}@anchor{gnat_ugn/the_gnat_compilation_model id14}@anchor{60}
2209 @subsubsection Running @cite{gnatname}
2212 The usual form of the @cite{gnatname} command is:
2214 @example
2215 $ gnatname [`switches`] `naming_pattern` [`naming_patterns`]
2216     [--and [`switches`] `naming_pattern` [`naming_patterns`]]
2217 @end example
2219 All of the arguments are optional. If invoked without any argument,
2220 @cite{gnatname} will display its usage.
2222 When used with at least one naming pattern, @cite{gnatname} will attempt to
2223 find all the compilation units in files that follow at least one of the
2224 naming patterns. To find these compilation units,
2225 @cite{gnatname} will use the GNAT compiler in syntax-check-only mode on all
2226 regular files.
2228 One or several Naming Patterns may be given as arguments to @cite{gnatname}.
2229 Each Naming Pattern is enclosed between double quotes (or single
2230 quotes on Windows).
2231 A Naming Pattern is a regular expression similar to the wildcard patterns
2232 used in file names by the Unix shells or the DOS prompt.
2234 @cite{gnatname} may be called with several sections of directories/patterns.
2235 Sections are separated by switch @cite{--and}. In each section, there must be
2236 at least one pattern. If no directory is specified in a section, the current
2237 directory (or the project directory is @cite{-P} is used) is implied.
2238 The options other that the directory switches and the patterns apply globally
2239 even if they are in different sections.
2241 Examples of Naming Patterns are:
2243 @example
2244 "*.[12].ada"
2245 "*.ad[sb]*"
2246 "body_*"    "spec_*"
2247 @end example
2249 For a more complete description of the syntax of Naming Patterns,
2250 see the second kind of regular expressions described in @code{g-regexp.ads}
2251 (the 'Glob' regular expressions).
2253 When invoked with no switch @cite{-P}, @cite{gnatname} will create a
2254 configuration pragmas file @code{gnat.adc} in the current working directory,
2255 with pragmas @cite{Source_File_Name} for each file that contains a valid Ada
2256 unit.
2258 @node Switches for gnatname,Examples of gnatname Usage,Running gnatname,Handling Arbitrary File Naming Conventions with gnatname
2259 @anchor{gnat_ugn/the_gnat_compilation_model id15}@anchor{61}@anchor{gnat_ugn/the_gnat_compilation_model switches-for-gnatname}@anchor{62}
2260 @subsubsection Switches for @cite{gnatname}
2263 Switches for @cite{gnatname} must precede any specified Naming Pattern.
2265 You may specify any of the following switches to @cite{gnatname}:
2267 @geindex --version (gnatname)
2270 @table @asis
2272 @item @code{--version}
2274 Display Copyright and version, then exit disregarding all other options.
2275 @end table
2277 @geindex --help (gnatname)
2280 @table @asis
2282 @item @code{--help}
2284 If @emph{--version} was not used, display usage, then exit disregarding
2285 all other options.
2287 @item @code{--subdirs=@emph{dir}}
2289 Real object, library or exec directories are subdirectories <dir> of the
2290 specified ones.
2292 @item @code{--no-backup}
2294 Do not create a backup copy of an existing project file.
2296 @item @code{--and}
2298 Start another section of directories/patterns.
2299 @end table
2301 @geindex -c (gnatname)
2304 @table @asis
2306 @item @code{-c@emph{filename}}
2308 Create a configuration pragmas file @code{filename} (instead of the default
2309 @code{gnat.adc}).
2310 There may be zero, one or more space between @emph{-c} and
2311 @code{filename}.
2312 @code{filename} may include directory information. @code{filename} must be
2313 writable. There may be only one switch @emph{-c}.
2314 When a switch @emph{-c} is
2315 specified, no switch @emph{-P} may be specified (see below).
2316 @end table
2318 @geindex -d (gnatname)
2321 @table @asis
2323 @item @code{-d@emph{dir}}
2325 Look for source files in directory @code{dir}. There may be zero, one or more
2326 spaces between @emph{-d} and @code{dir}.
2327 @code{dir} may end with @cite{/**}, that is it may be of the form
2328 @cite{root_dir/**}. In this case, the directory @cite{root_dir} and all of its
2329 subdirectories, recursively, have to be searched for sources.
2330 When a switch @emph{-d}
2331 is specified, the current working directory will not be searched for source
2332 files, unless it is explicitly specified with a @emph{-d}
2333 or @emph{-D} switch.
2334 Several switches @emph{-d} may be specified.
2335 If @code{dir} is a relative path, it is relative to the directory of
2336 the configuration pragmas file specified with switch
2337 @emph{-c},
2338 or to the directory of the project file specified with switch
2339 @emph{-P} or,
2340 if neither switch @emph{-c}
2341 nor switch @emph{-P} are specified, it is relative to the
2342 current working directory. The directory
2343 specified with switch @emph{-d} must exist and be readable.
2344 @end table
2346 @geindex -D (gnatname)
2349 @table @asis
2351 @item @code{-D@emph{filename}}
2353 Look for source files in all directories listed in text file @code{filename}.
2354 There may be zero, one or more spaces between @emph{-D}
2355 and @code{filename}.
2356 @code{filename} must be an existing, readable text file.
2357 Each nonempty line in @code{filename} must be a directory.
2358 Specifying switch @emph{-D} is equivalent to specifying as many
2359 switches @emph{-d} as there are nonempty lines in
2360 @code{file}.
2362 @item @code{-eL}
2364 Follow symbolic links when processing project files.
2366 @geindex -f (gnatname)
2368 @item @code{-f@emph{pattern}}
2370 Foreign patterns. Using this switch, it is possible to add sources of languages
2371 other than Ada to the list of sources of a project file.
2372 It is only useful if a -P switch is used.
2373 For example,
2375 @example
2376 gnatname -Pprj -f"*.c" "*.ada"
2377 @end example
2379 will look for Ada units in all files with the @code{.ada} extension,
2380 and will add to the list of file for project @code{prj.gpr} the C files
2381 with extension @code{.c}.
2383 @geindex -h (gnatname)
2385 @item @code{-h}
2387 Output usage (help) information. The output is written to @code{stdout}.
2389 @geindex -P (gnatname)
2391 @item @code{-P@emph{proj}}
2393 Create or update project file @code{proj}. There may be zero, one or more space
2394 between @emph{-P} and @code{proj}. @code{proj} may include directory
2395 information. @code{proj} must be writable.
2396 There may be only one switch @emph{-P}.
2397 When a switch @emph{-P} is specified,
2398 no switch @emph{-c} may be specified.
2399 On all platforms, except on VMS, when @cite{gnatname} is invoked for an
2400 existing project file <proj>.gpr, a backup copy of the project file is created
2401 in the project directory with file name <proj>.gpr.saved_x. 'x' is the first
2402 non negative number that makes this backup copy a new file.
2404 @geindex -v (gnatname)
2406 @item @code{-v}
2408 Verbose mode. Output detailed explanation of behavior to @code{stdout}.
2409 This includes name of the file written, the name of the directories to search
2410 and, for each file in those directories whose name matches at least one of
2411 the Naming Patterns, an indication of whether the file contains a unit,
2412 and if so the name of the unit.
2413 @end table
2415 @geindex -v -v (gnatname)
2418 @table @asis
2420 @item @code{-v -v}
2422 Very Verbose mode. In addition to the output produced in verbose mode,
2423 for each file in the searched directories whose name matches none of
2424 the Naming Patterns, an indication is given that there is no match.
2426 @geindex -x (gnatname)
2428 @item @code{-x@emph{pattern}}
2430 Excluded patterns. Using this switch, it is possible to exclude some files
2431 that would match the name patterns. For example,
2433 @example
2434 gnatname -x "*_nt.ada" "*.ada"
2435 @end example
2437 will look for Ada units in all files with the @code{.ada} extension,
2438 except those whose names end with @code{_nt.ada}.
2439 @end table
2441 @node Examples of gnatname Usage,,Switches for gnatname,Handling Arbitrary File Naming Conventions with gnatname
2442 @anchor{gnat_ugn/the_gnat_compilation_model examples-of-gnatname-usage}@anchor{63}@anchor{gnat_ugn/the_gnat_compilation_model id16}@anchor{64}
2443 @subsubsection Examples of @cite{gnatname} Usage
2446 @example
2447 $ gnatname -c /home/me/names.adc -d sources "[a-z]*.ada*"
2448 @end example
2450 In this example, the directory @code{/home/me} must already exist
2451 and be writable. In addition, the directory
2452 @code{/home/me/sources} (specified by
2453 @emph{-d sources}) must exist and be readable.
2455 Note the optional spaces after @emph{-c} and @emph{-d}.
2457 @example
2458 $ gnatname -P/home/me/proj -x "*_nt_body.ada"
2459 -dsources -dsources/plus -Dcommon_dirs.txt "body_*" "spec_*"
2460 @end example
2462 Note that several switches @emph{-d} may be used,
2463 even in conjunction with one or several switches
2464 @emph{-D}. Several Naming Patterns and one excluded pattern
2465 are used in this example.
2467 @node File Name Krunching with gnatkr,Renaming Files with gnatchop,Handling Arbitrary File Naming Conventions with gnatname,File Naming Topics and Utilities
2468 @anchor{gnat_ugn/the_gnat_compilation_model file-name-krunching-with-gnatkr}@anchor{65}@anchor{gnat_ugn/the_gnat_compilation_model id17}@anchor{66}
2469 @subsection File Name Krunching with @cite{gnatkr}
2472 @geindex gnatkr
2474 This chapter discusses the method used by the compiler to shorten
2475 the default file names chosen for Ada units so that they do not
2476 exceed the maximum length permitted. It also describes the
2477 @cite{gnatkr} utility that can be used to determine the result of
2478 applying this shortening.
2480 @menu
2481 * About gnatkr:: 
2482 * Using gnatkr:: 
2483 * Krunching Method:: 
2484 * Examples of gnatkr Usage:: 
2486 @end menu
2488 @node About gnatkr,Using gnatkr,,File Name Krunching with gnatkr
2489 @anchor{gnat_ugn/the_gnat_compilation_model id18}@anchor{67}@anchor{gnat_ugn/the_gnat_compilation_model about-gnatkr}@anchor{68}
2490 @subsubsection About @cite{gnatkr}
2493 The default file naming rule in GNAT
2494 is that the file name must be derived from
2495 the unit name. The exact default rule is as follows:
2498 @itemize *
2500 @item 
2501 Take the unit name and replace all dots by hyphens.
2503 @item 
2504 If such a replacement occurs in the
2505 second character position of a name, and the first character is
2506 @code{a}, @code{g}, @code{s}, or @code{i},
2507 then replace the dot by the character
2508 @code{~} (tilde)
2509 instead of a minus.
2511 The reason for this exception is to avoid clashes
2512 with the standard names for children of System, Ada, Interfaces,
2513 and GNAT, which use the prefixes
2514 @code{s-}, @code{a-}, @code{i-}, and @code{g-},
2515 respectively.
2516 @end itemize
2518 The @code{-gnatk@emph{nn}}
2519 switch of the compiler activates a 'krunching'
2520 circuit that limits file names to nn characters (where nn is a decimal
2521 integer).
2523 The @cite{gnatkr} utility can be used to determine the krunched name for
2524 a given file, when krunched to a specified maximum length.
2526 @node Using gnatkr,Krunching Method,About gnatkr,File Name Krunching with gnatkr
2527 @anchor{gnat_ugn/the_gnat_compilation_model id19}@anchor{69}@anchor{gnat_ugn/the_gnat_compilation_model using-gnatkr}@anchor{56}
2528 @subsubsection Using @cite{gnatkr}
2531 The @cite{gnatkr} command has the form:
2533 @example
2534 $ gnatkr `name` [`length`]
2535 @end example
2537 @cite{name} is the uncrunched file name, derived from the name of the unit
2538 in the standard manner described in the previous section (i.e., in particular
2539 all dots are replaced by hyphens). The file name may or may not have an
2540 extension (defined as a suffix of the form period followed by arbitrary
2541 characters other than period). If an extension is present then it will
2542 be preserved in the output. For example, when krunching @code{hellofile.ads}
2543 to eight characters, the result will be hellofil.ads.
2545 Note: for compatibility with previous versions of @cite{gnatkr} dots may
2546 appear in the name instead of hyphens, but the last dot will always be
2547 taken as the start of an extension. So if @cite{gnatkr} is given an argument
2548 such as @code{Hello.World.adb} it will be treated exactly as if the first
2549 period had been a hyphen, and for example krunching to eight characters
2550 gives the result @code{hellworl.adb}.
2552 Note that the result is always all lower case.
2553 Characters of the other case are folded as required.
2555 @cite{length} represents the length of the krunched name. The default
2556 when no argument is given is 8 characters. A length of zero stands for
2557 unlimited, in other words do not chop except for system files where the
2558 implied crunching length is always eight characters.
2560 The output is the krunched name. The output has an extension only if the
2561 original argument was a file name with an extension.
2563 @node Krunching Method,Examples of gnatkr Usage,Using gnatkr,File Name Krunching with gnatkr
2564 @anchor{gnat_ugn/the_gnat_compilation_model id20}@anchor{6a}@anchor{gnat_ugn/the_gnat_compilation_model krunching-method}@anchor{6b}
2565 @subsubsection Krunching Method
2568 The initial file name is determined by the name of the unit that the file
2569 contains. The name is formed by taking the full expanded name of the
2570 unit and replacing the separating dots with hyphens and
2571 using lowercase
2572 for all letters, except that a hyphen in the second character position is
2573 replaced by a tilde if the first character is
2574 @code{a}, @code{i}, @code{g}, or @code{s}.
2575 The extension is @cite{.ads} for a
2576 spec and @cite{.adb} for a body.
2577 Krunching does not affect the extension, but the file name is shortened to
2578 the specified length by following these rules:
2581 @itemize *
2583 @item 
2584 The name is divided into segments separated by hyphens, tildes or
2585 underscores and all hyphens, tildes, and underscores are
2586 eliminated. If this leaves the name short enough, we are done.
2588 @item 
2589 If the name is too long, the longest segment is located (left-most
2590 if there are two of equal length), and shortened by dropping
2591 its last character. This is repeated until the name is short enough.
2593 As an example, consider the krunching of @code{our-strings-wide_fixed.adb}
2594 to fit the name into 8 characters as required by some operating systems:
2596 @example
2597 our-strings-wide_fixed 22
2598 our strings wide fixed 19
2599 our string  wide fixed 18
2600 our strin   wide fixed 17
2601 our stri    wide fixed 16
2602 our stri    wide fixe  15
2603 our str     wide fixe  14
2604 our str     wid  fixe  13
2605 our str     wid  fix   12
2606 ou  str     wid  fix   11
2607 ou  st      wid  fix   10
2608 ou  st      wi   fix   9
2609 ou  st      wi   fi    8
2610 Final file name: oustwifi.adb
2611 @end example
2613 @item 
2614 The file names for all predefined units are always krunched to eight
2615 characters. The krunching of these predefined units uses the following
2616 special prefix replacements:
2619 @multitable {xxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxx} 
2620 @item
2622 Prefix
2624 @tab
2626 Replacement
2628 @item
2630 @code{ada-}
2632 @tab
2634 @code{a-}
2636 @item
2638 @code{gnat-}
2640 @tab
2642 @code{g-}
2644 @item
2646 @code{interfac es-}
2648 @tab
2650 @code{i-}
2652 @item
2654 @code{system-}
2656 @tab
2658 @code{s-}
2660 @end multitable
2663 These system files have a hyphen in the second character position. That
2664 is why normal user files replace such a character with a
2665 tilde, to avoid confusion with system file names.
2667 As an example of this special rule, consider
2668 @code{ada-strings-wide_fixed.adb}, which gets krunched as follows:
2670 @example
2671 ada-strings-wide_fixed 22
2672 a-  strings wide fixed 18
2673 a-  string  wide fixed 17
2674 a-  strin   wide fixed 16
2675 a-  stri    wide fixed 15
2676 a-  stri    wide fixe  14
2677 a-  str     wide fixe  13
2678 a-  str     wid  fixe  12
2679 a-  str     wid  fix   11
2680 a-  st      wid  fix   10
2681 a-  st      wi   fix   9
2682 a-  st      wi   fi    8
2683 Final file name: a-stwifi.adb
2684 @end example
2685 @end itemize
2687 Of course no file shortening algorithm can guarantee uniqueness over all
2688 possible unit names, and if file name krunching is used then it is your
2689 responsibility to ensure that no name clashes occur. The utility
2690 program @cite{gnatkr} is supplied for conveniently determining the
2691 krunched name of a file.
2693 @node Examples of gnatkr Usage,,Krunching Method,File Name Krunching with gnatkr
2694 @anchor{gnat_ugn/the_gnat_compilation_model id21}@anchor{6c}@anchor{gnat_ugn/the_gnat_compilation_model examples-of-gnatkr-usage}@anchor{6d}
2695 @subsubsection Examples of @cite{gnatkr} Usage
2698 @example
2699 $ gnatkr very_long_unit_name.ads      --> velounna.ads
2700 $ gnatkr grandparent-parent-child.ads --> grparchi.ads
2701 $ gnatkr Grandparent.Parent.Child.ads --> grparchi.ads
2702 $ gnatkr grandparent-parent-child     --> grparchi
2703 $ gnatkr very_long_unit_name.ads/count=6 --> vlunna.ads
2704 $ gnatkr very_long_unit_name.ads/count=0 --> very_long_unit_name.ads
2705 @end example
2707 @node Renaming Files with gnatchop,,File Name Krunching with gnatkr,File Naming Topics and Utilities
2708 @anchor{gnat_ugn/the_gnat_compilation_model id22}@anchor{6e}@anchor{gnat_ugn/the_gnat_compilation_model renaming-files-with-gnatchop}@anchor{38}
2709 @subsection Renaming Files with @cite{gnatchop}
2712 @geindex gnatchop
2714 This chapter discusses how to handle files with multiple units by using
2715 the @cite{gnatchop} utility. This utility is also useful in renaming
2716 files to meet the standard GNAT default file naming conventions.
2718 @menu
2719 * Handling Files with Multiple Units:: 
2720 * Operating gnatchop in Compilation Mode:: 
2721 * Command Line for gnatchop:: 
2722 * Switches for gnatchop:: 
2723 * Examples of gnatchop Usage:: 
2725 @end menu
2727 @node Handling Files with Multiple Units,Operating gnatchop in Compilation Mode,,Renaming Files with gnatchop
2728 @anchor{gnat_ugn/the_gnat_compilation_model id23}@anchor{6f}@anchor{gnat_ugn/the_gnat_compilation_model handling-files-with-multiple-units}@anchor{70}
2729 @subsubsection Handling Files with Multiple Units
2732 The basic compilation model of GNAT requires that a file submitted to the
2733 compiler have only one unit and there be a strict correspondence
2734 between the file name and the unit name.
2736 The @cite{gnatchop} utility allows both of these rules to be relaxed,
2737 allowing GNAT to process files which contain multiple compilation units
2738 and files with arbitrary file names. @cite{gnatchop}
2739 reads the specified file and generates one or more output files,
2740 containing one unit per file. The unit and the file name correspond,
2741 as required by GNAT.
2743 If you want to permanently restructure a set of 'foreign' files so that
2744 they match the GNAT rules, and do the remaining development using the
2745 GNAT structure, you can simply use @emph{gnatchop} once, generate the
2746 new set of files and work with them from that point on.
2748 Alternatively, if you want to keep your files in the 'foreign' format,
2749 perhaps to maintain compatibility with some other Ada compilation
2750 system, you can set up a procedure where you use @emph{gnatchop} each
2751 time you compile, regarding the source files that it writes as temporary
2752 files that you throw away.
2754 Note that if your file containing multiple units starts with a byte order
2755 mark (BOM) specifying UTF-8 encoding, then the files generated by gnatchop
2756 will each start with a copy of this BOM, meaning that they can be compiled
2757 automatically in UTF-8 mode without needing to specify an explicit encoding.
2759 @node Operating gnatchop in Compilation Mode,Command Line for gnatchop,Handling Files with Multiple Units,Renaming Files with gnatchop
2760 @anchor{gnat_ugn/the_gnat_compilation_model operating-gnatchop-in-compilation-mode}@anchor{71}@anchor{gnat_ugn/the_gnat_compilation_model id24}@anchor{72}
2761 @subsubsection Operating gnatchop in Compilation Mode
2764 The basic function of @cite{gnatchop} is to take a file with multiple units
2765 and split it into separate files. The boundary between files is reasonably
2766 clear, except for the issue of comments and pragmas. In default mode, the
2767 rule is that any pragmas between units belong to the previous unit, except
2768 that configuration pragmas always belong to the following unit. Any comments
2769 belong to the following unit. These rules
2770 almost always result in the right choice of
2771 the split point without needing to mark it explicitly and most users will
2772 find this default to be what they want. In this default mode it is incorrect to
2773 submit a file containing only configuration pragmas, or one that ends in
2774 configuration pragmas, to @cite{gnatchop}.
2776 However, using a special option to activate 'compilation mode',
2777 @cite{gnatchop}
2778 can perform another function, which is to provide exactly the semantics
2779 required by the RM for handling of configuration pragmas in a compilation.
2780 In the absence of configuration pragmas (at the main file level), this
2781 option has no effect, but it causes such configuration pragmas to be handled
2782 in a quite different manner.
2784 First, in compilation mode, if @cite{gnatchop} is given a file that consists of
2785 only configuration pragmas, then this file is appended to the
2786 @code{gnat.adc} file in the current directory. This behavior provides
2787 the required behavior described in the RM for the actions to be taken
2788 on submitting such a file to the compiler, namely that these pragmas
2789 should apply to all subsequent compilations in the same compilation
2790 environment. Using GNAT, the current directory, possibly containing a
2791 @code{gnat.adc} file is the representation
2792 of a compilation environment. For more information on the
2793 @code{gnat.adc} file, see @ref{58,,Handling of Configuration Pragmas}.
2795 Second, in compilation mode, if @cite{gnatchop}
2796 is given a file that starts with
2797 configuration pragmas, and contains one or more units, then these
2798 configuration pragmas are prepended to each of the chopped files. This
2799 behavior provides the required behavior described in the RM for the
2800 actions to be taken on compiling such a file, namely that the pragmas
2801 apply to all units in the compilation, but not to subsequently compiled
2802 units.
2804 Finally, if configuration pragmas appear between units, they are appended
2805 to the previous unit. This results in the previous unit being illegal,
2806 since the compiler does not accept configuration pragmas that follow
2807 a unit. This provides the required RM behavior that forbids configuration
2808 pragmas other than those preceding the first compilation unit of a
2809 compilation.
2811 For most purposes, @cite{gnatchop} will be used in default mode. The
2812 compilation mode described above is used only if you need exactly
2813 accurate behavior with respect to compilations, and you have files
2814 that contain multiple units and configuration pragmas. In this
2815 circumstance the use of @cite{gnatchop} with the compilation mode
2816 switch provides the required behavior, and is for example the mode
2817 in which GNAT processes the ACVC tests.
2819 @node Command Line for gnatchop,Switches for gnatchop,Operating gnatchop in Compilation Mode,Renaming Files with gnatchop
2820 @anchor{gnat_ugn/the_gnat_compilation_model id25}@anchor{73}@anchor{gnat_ugn/the_gnat_compilation_model command-line-for-gnatchop}@anchor{74}
2821 @subsubsection Command Line for @cite{gnatchop}
2824 The @cite{gnatchop} command has the form:
2826 @example
2827 $ gnatchop switches file_name [file_name ...]
2828       [directory]
2829 @end example
2831 The only required argument is the file name of the file to be chopped.
2832 There are no restrictions on the form of this file name. The file itself
2833 contains one or more Ada units, in normal GNAT format, concatenated
2834 together. As shown, more than one file may be presented to be chopped.
2836 When run in default mode, @cite{gnatchop} generates one output file in
2837 the current directory for each unit in each of the files.
2839 @cite{directory}, if specified, gives the name of the directory to which
2840 the output files will be written. If it is not specified, all files are
2841 written to the current directory.
2843 For example, given a
2844 file called @code{hellofiles} containing
2846 @example
2847 procedure Hello;
2849 with Ada.Text_IO; use Ada.Text_IO;
2850 procedure Hello is
2851 begin
2852    Put_Line ("Hello");
2853 end Hello;
2854 @end example
2856 the command
2858 @example
2859 $ gnatchop hellofiles
2860 @end example
2862 generates two files in the current directory, one called
2863 @code{hello.ads} containing the single line that is the procedure spec,
2864 and the other called @code{hello.adb} containing the remaining text. The
2865 original file is not affected. The generated files can be compiled in
2866 the normal manner.
2868 When gnatchop is invoked on a file that is empty or that contains only empty
2869 lines and/or comments, gnatchop will not fail, but will not produce any
2870 new sources.
2872 For example, given a
2873 file called @code{toto.txt} containing
2875 @example
2876 --  Just a comment
2877 @end example
2879 the command
2881 @example
2882 $ gnatchop toto.txt
2883 @end example
2885 will not produce any new file and will result in the following warnings:
2887 @example
2888 toto.txt:1:01: warning: empty file, contains no compilation units
2889 no compilation units found
2890 no source files written
2891 @end example
2893 @node Switches for gnatchop,Examples of gnatchop Usage,Command Line for gnatchop,Renaming Files with gnatchop
2894 @anchor{gnat_ugn/the_gnat_compilation_model switches-for-gnatchop}@anchor{75}@anchor{gnat_ugn/the_gnat_compilation_model id26}@anchor{76}
2895 @subsubsection Switches for @cite{gnatchop}
2898 @emph{gnatchop} recognizes the following switches:
2900 @geindex --version (gnatchop)
2903 @table @asis
2905 @item @code{--version}
2907 Display Copyright and version, then exit disregarding all other options.
2908 @end table
2910 @geindex --help (gnatchop)
2913 @table @asis
2915 @item @code{--help}
2917 If @emph{--version} was not used, display usage, then exit disregarding
2918 all other options.
2919 @end table
2921 @geindex -c (gnatchop)
2924 @table @asis
2926 @item @code{-c}
2928 Causes @cite{gnatchop} to operate in compilation mode, in which
2929 configuration pragmas are handled according to strict RM rules. See
2930 previous section for a full description of this mode.
2932 @item @code{-gnat@emph{xxx}}
2934 This passes the given @emph{-gnat`xxx*` switch to `gnat` which is
2935 used to parse the given file. Not all `xxx` options make sense,
2936 but for example, the use of *-gnati2} allows @cite{gnatchop} to
2937 process a source file that uses Latin-2 coding for identifiers.
2939 @item @code{-h}
2941 Causes @cite{gnatchop} to generate a brief help summary to the standard
2942 output file showing usage information.
2943 @end table
2945 @geindex -k (gnatchop)
2948 @table @asis
2950 @item @code{-k@emph{mm}}
2952 Limit generated file names to the specified number @cite{mm}
2953 of characters.
2954 This is useful if the
2955 resulting set of files is required to be interoperable with systems
2956 which limit the length of file names.
2957 No space is allowed between the @emph{-k} and the numeric value. The numeric
2958 value may be omitted in which case a default of @emph{-k8},
2959 suitable for use
2960 with DOS-like file systems, is used. If no @emph{-k} switch
2961 is present then
2962 there is no limit on the length of file names.
2963 @end table
2965 @geindex -p (gnatchop)
2968 @table @asis
2970 @item @code{-p}
2972 Causes the file modification time stamp of the input file to be
2973 preserved and used for the time stamp of the output file(s). This may be
2974 useful for preserving coherency of time stamps in an environment where
2975 @cite{gnatchop} is used as part of a standard build process.
2976 @end table
2978 @geindex -q (gnatchop)
2981 @table @asis
2983 @item @code{-q}
2985 Causes output of informational messages indicating the set of generated
2986 files to be suppressed. Warnings and error messages are unaffected.
2987 @end table
2989 @geindex -r (gnatchop)
2991 @geindex Source_Reference pragmas
2994 @table @asis
2996 @item @code{-r}
2998 Generate @cite{Source_Reference} pragmas. Use this switch if the output
2999 files are regarded as temporary and development is to be done in terms
3000 of the original unchopped file. This switch causes
3001 @cite{Source_Reference} pragmas to be inserted into each of the
3002 generated files to refers back to the original file name and line number.
3003 The result is that all error messages refer back to the original
3004 unchopped file.
3005 In addition, the debugging information placed into the object file (when
3006 the @emph{-g} switch of @emph{gcc} or @emph{gnatmake} is
3007 specified)
3008 also refers back to this original file so that tools like profilers and
3009 debuggers will give information in terms of the original unchopped file.
3011 If the original file to be chopped itself contains
3012 a @cite{Source_Reference}
3013 pragma referencing a third file, then gnatchop respects
3014 this pragma, and the generated @cite{Source_Reference} pragmas
3015 in the chopped file refer to the original file, with appropriate
3016 line numbers. This is particularly useful when @cite{gnatchop}
3017 is used in conjunction with @cite{gnatprep} to compile files that
3018 contain preprocessing statements and multiple units.
3019 @end table
3021 @geindex -v (gnatchop)
3024 @table @asis
3026 @item @code{-v}
3028 Causes @cite{gnatchop} to operate in verbose mode. The version
3029 number and copyright notice are output, as well as exact copies of
3030 the gnat1 commands spawned to obtain the chop control information.
3031 @end table
3033 @geindex -w (gnatchop)
3036 @table @asis
3038 @item @code{-w}
3040 Overwrite existing file names. Normally @cite{gnatchop} regards it as a
3041 fatal error if there is already a file with the same name as a
3042 file it would otherwise output, in other words if the files to be
3043 chopped contain duplicated units. This switch bypasses this
3044 check, and causes all but the last instance of such duplicated
3045 units to be skipped.
3046 @end table
3048 @geindex --GCC= (gnatchop)
3051 @table @asis
3053 @item @code{--GCC=@emph{xxxx}}
3055 Specify the path of the GNAT parser to be used. When this switch is used,
3056 no attempt is made to add the prefix to the GNAT parser executable.
3057 @end table
3059 @node Examples of gnatchop Usage,,Switches for gnatchop,Renaming Files with gnatchop
3060 @anchor{gnat_ugn/the_gnat_compilation_model id27}@anchor{77}@anchor{gnat_ugn/the_gnat_compilation_model examples-of-gnatchop-usage}@anchor{78}
3061 @subsubsection Examples of @cite{gnatchop} Usage
3064 @example
3065 $ gnatchop -w hello_s.ada prerelease/files
3066 @end example
3068 Chops the source file @code{hello_s.ada}. The output files will be
3069 placed in the directory @code{prerelease/files},
3070 overwriting any
3071 files with matching names in that directory (no files in the current
3072 directory are modified).
3074 @example
3075 $ gnatchop archive
3076 @end example
3078 Chops the source file @code{archive}
3079 into the current directory. One
3080 useful application of @cite{gnatchop} is in sending sets of sources
3081 around, for example in email messages. The required sources are simply
3082 concatenated (for example, using a Unix @cite{cat}
3083 command), and then
3084 @emph{gnatchop} is used at the other end to reconstitute the original
3085 file names.
3087 @example
3088 $ gnatchop file1 file2 file3 direc
3089 @end example
3091 Chops all units in files @code{file1}, @code{file2}, @code{file3}, placing
3092 the resulting files in the directory @code{direc}. Note that if any units
3093 occur more than once anywhere within this set of files, an error message
3094 is generated, and no files are written. To override this check, use the
3095 @emph{-w} switch,
3096 in which case the last occurrence in the last file will
3097 be the one that is output, and earlier duplicate occurrences for a given
3098 unit will be skipped.
3100 @node Configuration Pragmas,Generating Object Files,File Naming Topics and Utilities,The GNAT Compilation Model
3101 @anchor{gnat_ugn/the_gnat_compilation_model id28}@anchor{79}@anchor{gnat_ugn/the_gnat_compilation_model configuration-pragmas}@anchor{16}
3102 @section Configuration Pragmas
3105 @geindex Configuration pragmas
3107 @geindex Pragmas
3108 @geindex configuration
3110 Configuration pragmas include those pragmas described as
3111 such in the Ada Reference Manual, as well as
3112 implementation-dependent pragmas that are configuration pragmas.
3113 See the @cite{Implementation_Defined_Pragmas} chapter in the
3114 @cite{GNAT_Reference_Manual} for details on these
3115 additional GNAT-specific configuration pragmas.
3116 Most notably, the pragma @cite{Source_File_Name}, which allows
3117 specifying non-default names for source files, is a configuration
3118 pragma. The following is a complete list of configuration pragmas
3119 recognized by GNAT:
3121 @example
3122 Ada_83
3123 Ada_95
3124 Ada_05
3125 Ada_2005
3126 Ada_12
3127 Ada_2012
3128 Allow_Integer_Address
3129 Annotate
3130 Assertion_Policy
3131 Assume_No_Invalid_Values
3132 C_Pass_By_Copy
3133 Check_Name
3134 Check_Policy
3135 Compile_Time_Error
3136 Compile_Time_Warning
3137 Compiler_Unit
3138 Component_Alignment
3139 Convention_Identifier
3140 Debug_Policy
3141 Detect_Blocking
3142 Default_Storage_Pool
3143 Discard_Names
3144 Elaboration_Checks
3145 Eliminate
3146 Extend_System
3147 Extensions_Allowed
3148 External_Name_Casing
3149 Fast_Math
3150 Favor_Top_Level
3151 Float_Representation
3152 Implicit_Packing
3153 Initialize_Scalars
3154 Interrupt_State
3155 License
3156 Locking_Policy
3157 Long_Float
3158 No_Run_Time
3159 No_Strict_Aliasing
3160 Normalize_Scalars
3161 Optimize_Alignment
3162 Persistent_BSS
3163 Polling
3164 Priority_Specific_Dispatching
3165 Profile
3166 Profile_Warnings
3167 Propagate_Exceptions
3168 Queuing_Policy
3169 Ravenscar
3170 Restricted_Run_Time
3171 Restrictions
3172 Restrictions_Warnings
3173 Reviewable
3174 Short_Circuit_And_Or
3175 Source_File_Name
3176 Source_File_Name_Project
3177 SPARK_Mode
3178 Style_Checks
3179 Suppress
3180 Suppress_Exception_Locations
3181 Task_Dispatching_Policy
3182 Universal_Data
3183 Unsuppress
3184 Use_VADS_Size
3185 Validity_Checks
3186 Warnings
3187 Wide_Character_Encoding
3188 @end example
3190 @menu
3191 * Handling of Configuration Pragmas:: 
3192 * The Configuration Pragmas Files:: 
3194 @end menu
3196 @node Handling of Configuration Pragmas,The Configuration Pragmas Files,,Configuration Pragmas
3197 @anchor{gnat_ugn/the_gnat_compilation_model id29}@anchor{7a}@anchor{gnat_ugn/the_gnat_compilation_model handling-of-configuration-pragmas}@anchor{58}
3198 @subsection Handling of Configuration Pragmas
3201 Configuration pragmas may either appear at the start of a compilation
3202 unit, or they can appear in a configuration pragma file to apply to
3203 all compilations performed in a given compilation environment.
3205 GNAT also provides the @cite{gnatchop} utility to provide an automatic
3206 way to handle configuration pragmas following the semantics for
3207 compilations (that is, files with multiple units), described in the RM.
3208 See @ref{71,,Operating gnatchop in Compilation Mode} for details.
3209 However, for most purposes, it will be more convenient to edit the
3210 @code{gnat.adc} file that contains configuration pragmas directly,
3211 as described in the following section.
3213 In the case of @cite{Restrictions} pragmas appearing as configuration
3214 pragmas in individual compilation units, the exact handling depends on
3215 the type of restriction.
3217 Restrictions that require partition-wide consistency (like
3218 @cite{No_Tasking}) are
3219 recognized wherever they appear
3220 and can be freely inherited, e.g. from a @emph{with}ed unit to the @emph{with}ing
3221 unit. This makes sense since the binder will in any case insist on seeing
3222 consistent use, so any unit not conforming to any restrictions that are
3223 anywhere in the partition will be rejected, and you might as well find
3224 that out at compile time rather than at bind time.
3226 For restrictions that do not require partition-wide consistency, e.g.
3227 SPARK or No_Implementation_Attributes, in general the restriction applies
3228 only to the unit in which the pragma appears, and not to any other units.
3230 The exception is No_Elaboration_Code which always applies to the entire
3231 object file from a compilation, i.e. to the body, spec, and all subunits.
3232 This restriction can be specified in a configuration pragma file, or it
3233 can be on the body and/or the spec (in eithe case it applies to all the
3234 relevant units). It can appear on a subunit only if it has previously
3235 appeared in the body of spec.
3237 @node The Configuration Pragmas Files,,Handling of Configuration Pragmas,Configuration Pragmas
3238 @anchor{gnat_ugn/the_gnat_compilation_model the-configuration-pragmas-files}@anchor{7b}@anchor{gnat_ugn/the_gnat_compilation_model id30}@anchor{7c}
3239 @subsection The Configuration Pragmas Files
3242 @geindex gnat.adc
3244 In GNAT a compilation environment is defined by the current
3245 directory at the time that a compile command is given. This current
3246 directory is searched for a file whose name is @code{gnat.adc}. If
3247 this file is present, it is expected to contain one or more
3248 configuration pragmas that will be applied to the current compilation.
3249 However, if the switch @emph{-gnatA} is used, @code{gnat.adc} is not
3250 considered. When taken into account, @code{gnat.adc} is added to the
3251 dependencies, so that if @code{gnat.adc} is modified later, an invocation of
3252 @emph{gnatmake} will recompile the source.
3254 Configuration pragmas may be entered into the @code{gnat.adc} file
3255 either by running @cite{gnatchop} on a source file that consists only of
3256 configuration pragmas, or more conveniently by direct editing of the
3257 @code{gnat.adc} file, which is a standard format source file.
3259 Besides @code{gnat.adc}, additional files containing configuration
3260 pragmas may be applied to the current compilation using the switch
3261 @code{-gnatec=@emph{path}} where @cite{path} must designate an existing file that
3262 contains only configuration pragmas. These configuration pragmas are
3263 in addition to those found in @code{gnat.adc} (provided @code{gnat.adc}
3264 is present and switch @emph{-gnatA} is not used).
3266 It is allowable to specify several switches @emph{-gnatec=}, all of which
3267 will be taken into account.
3269 Files containing configuration pragmas specified with switches
3270 @emph{-gnatec=} are added to the dependencies, unless they are
3271 temporary files. A file is considered temporary if its name ends in
3272 @code{.tmp} or @code{.TMP}. Certain tools follow this naming
3273 convention because they pass information to @emph{gcc} via
3274 temporary files that are immediately deleted; it doesn't make sense to
3275 depend on a file that no longer exists. Such tools include
3276 @emph{gprbuild}, @emph{gnatmake}, and @emph{gnatcheck}.
3278 If you are using project file, a separate mechanism is provided using
3279 project attributes, see @ref{7d,,Specifying Configuration Pragmas} for more
3280 details.
3282 @node Generating Object Files,Source Dependencies,Configuration Pragmas,The GNAT Compilation Model
3283 @anchor{gnat_ugn/the_gnat_compilation_model generating-object-files}@anchor{42}@anchor{gnat_ugn/the_gnat_compilation_model id31}@anchor{7e}
3284 @section Generating Object Files
3287 An Ada program consists of a set of source files, and the first step in
3288 compiling the program is to generate the corresponding object files.
3289 These are generated by compiling a subset of these source files.
3290 The files you need to compile are the following:
3293 @itemize *
3295 @item 
3296 If a package spec has no body, compile the package spec to produce the
3297 object file for the package.
3299 @item 
3300 If a package has both a spec and a body, compile the body to produce the
3301 object file for the package. The source file for the package spec need
3302 not be compiled in this case because there is only one object file, which
3303 contains the code for both the spec and body of the package.
3305 @item 
3306 For a subprogram, compile the subprogram body to produce the object file
3307 for the subprogram. The spec, if one is present, is as usual in a
3308 separate file, and need not be compiled.
3309 @end itemize
3311 @geindex Subunits
3314 @itemize *
3316 @item 
3317 In the case of subunits, only compile the parent unit. A single object
3318 file is generated for the entire subunit tree, which includes all the
3319 subunits.
3321 @item 
3322 Compile child units independently of their parent units
3323 (though, of course, the spec of all the ancestor unit must be present in order
3324 to compile a child unit).
3326 @geindex Generics
3328 @item 
3329 Compile generic units in the same manner as any other units. The object
3330 files in this case are small dummy files that contain at most the
3331 flag used for elaboration checking. This is because GNAT always handles generic
3332 instantiation by means of macro expansion. However, it is still necessary to
3333 compile generic units, for dependency checking and elaboration purposes.
3334 @end itemize
3336 The preceding rules describe the set of files that must be compiled to
3337 generate the object files for a program. Each object file has the same
3338 name as the corresponding source file, except that the extension is
3339 @code{.o} as usual.
3341 You may wish to compile other files for the purpose of checking their
3342 syntactic and semantic correctness. For example, in the case where a
3343 package has a separate spec and body, you would not normally compile the
3344 spec. However, it is convenient in practice to compile the spec to make
3345 sure it is error-free before compiling clients of this spec, because such
3346 compilations will fail if there is an error in the spec.
3348 GNAT provides an option for compiling such files purely for the
3349 purposes of checking correctness; such compilations are not required as
3350 part of the process of building a program. To compile a file in this
3351 checking mode, use the @emph{-gnatc} switch.
3353 @node Source Dependencies,The Ada Library Information Files,Generating Object Files,The GNAT Compilation Model
3354 @anchor{gnat_ugn/the_gnat_compilation_model id32}@anchor{7f}@anchor{gnat_ugn/the_gnat_compilation_model source-dependencies}@anchor{43}
3355 @section Source Dependencies
3358 A given object file clearly depends on the source file which is compiled
3359 to produce it. Here we are using "depends" in the sense of a typical
3360 @cite{make} utility; in other words, an object file depends on a source
3361 file if changes to the source file require the object file to be
3362 recompiled.
3363 In addition to this basic dependency, a given object may depend on
3364 additional source files as follows:
3367 @itemize *
3369 @item 
3370 If a file being compiled @emph{with}s a unit @cite{X}, the object file
3371 depends on the file containing the spec of unit @cite{X}. This includes
3372 files that are @emph{with}ed implicitly either because they are parents
3373 of @emph{with}ed child units or they are run-time units required by the
3374 language constructs used in a particular unit.
3376 @item 
3377 If a file being compiled instantiates a library level generic unit, the
3378 object file depends on both the spec and body files for this generic
3379 unit.
3381 @item 
3382 If a file being compiled instantiates a generic unit defined within a
3383 package, the object file depends on the body file for the package as
3384 well as the spec file.
3385 @end itemize
3387 @geindex Inline
3389 @geindex -gnatn switch
3392 @itemize *
3394 @item 
3395 If a file being compiled contains a call to a subprogram for which
3396 pragma @cite{Inline} applies and inlining is activated with the
3397 @emph{-gnatn} switch, the object file depends on the file containing the
3398 body of this subprogram as well as on the file containing the spec. Note
3399 that for inlining to actually occur as a result of the use of this switch,
3400 it is necessary to compile in optimizing mode.
3402 @geindex -gnatN switch
3404 The use of @emph{-gnatN} activates  inlining optimization
3405 that is performed by the front end of the compiler. This inlining does
3406 not require that the code generation be optimized. Like @emph{-gnatn},
3407 the use of this switch generates additional dependencies.
3409 When using a gcc-based back end (in practice this means using any version
3410 of GNAT other than for the JVM, .NET or GNAAMP platforms), then the use of
3411 @emph{-gnatN} is deprecated, and the use of @emph{-gnatn} is preferred.
3412 Historically front end inlining was more extensive than the gcc back end
3413 inlining, but that is no longer the case.
3415 @item 
3416 If an object file @code{O} depends on the proper body of a subunit through
3417 inlining or instantiation, it depends on the parent unit of the subunit.
3418 This means that any modification of the parent unit or one of its subunits
3419 affects the compilation of @code{O}.
3421 @item 
3422 The object file for a parent unit depends on all its subunit body files.
3424 @item 
3425 The previous two rules meant that for purposes of computing dependencies and
3426 recompilation, a body and all its subunits are treated as an indivisible whole.
3428 These rules are applied transitively: if unit @cite{A} @emph{with}s
3429 unit @cite{B}, whose elaboration calls an inlined procedure in package
3430 @cite{C}, the object file for unit @cite{A} will depend on the body of
3431 @cite{C}, in file @code{c.adb}.
3433 The set of dependent files described by these rules includes all the
3434 files on which the unit is semantically dependent, as dictated by the
3435 Ada language standard. However, it is a superset of what the
3436 standard describes, because it includes generic, inline, and subunit
3437 dependencies.
3439 An object file must be recreated by recompiling the corresponding source
3440 file if any of the source files on which it depends are modified. For
3441 example, if the @cite{make} utility is used to control compilation,
3442 the rule for an Ada object file must mention all the source files on
3443 which the object file depends, according to the above definition.
3444 The determination of the necessary
3445 recompilations is done automatically when one uses @emph{gnatmake}.
3446 @end itemize
3448 @node The Ada Library Information Files,Binding an Ada Program,Source Dependencies,The GNAT Compilation Model
3449 @anchor{gnat_ugn/the_gnat_compilation_model id33}@anchor{80}@anchor{gnat_ugn/the_gnat_compilation_model the-ada-library-information-files}@anchor{44}
3450 @section The Ada Library Information Files
3453 @geindex Ada Library Information files
3455 @geindex ALI files
3457 Each compilation actually generates two output files. The first of these
3458 is the normal object file that has a @code{.o} extension. The second is a
3459 text file containing full dependency information. It has the same
3460 name as the source file, but an @code{.ali} extension.
3461 This file is known as the Ada Library Information (@code{ALI}) file.
3462 The following information is contained in the @code{ALI} file.
3465 @itemize *
3467 @item 
3468 Version information (indicates which version of GNAT was used to compile
3469 the unit(s) in question)
3471 @item 
3472 Main program information (including priority and time slice settings,
3473 as well as the wide character encoding used during compilation).
3475 @item 
3476 List of arguments used in the @emph{gcc} command for the compilation
3478 @item 
3479 Attributes of the unit, including configuration pragmas used, an indication
3480 of whether the compilation was successful, exception model used etc.
3482 @item 
3483 A list of relevant restrictions applying to the unit (used for consistency)
3484 checking.
3486 @item 
3487 Categorization information (e.g., use of pragma @cite{Pure}).
3489 @item 
3490 Information on all @emph{with}ed units, including presence of
3491 Elaborate` or @cite{Elaborate_All} pragmas.
3493 @item 
3494 Information from any @cite{Linker_Options} pragmas used in the unit
3496 @item 
3497 Information on the use of @cite{Body_Version} or @cite{Version}
3498 attributes in the unit.
3500 @item 
3501 Dependency information. This is a list of files, together with
3502 time stamp and checksum information. These are files on which
3503 the unit depends in the sense that recompilation is required
3504 if any of these units are modified.
3506 @item 
3507 Cross-reference data. Contains information on all entities referenced
3508 in the unit. Used by tools like @cite{gnatxref} and @cite{gnatfind} to
3509 provide cross-reference information.
3510 @end itemize
3512 For a full detailed description of the format of the @code{ALI} file,
3513 see the source of the body of unit @cite{Lib.Writ}, contained in file
3514 @code{lib-writ.adb} in the GNAT compiler sources.
3516 @node Binding an Ada Program,GNAT and Libraries,The Ada Library Information Files,The GNAT Compilation Model
3517 @anchor{gnat_ugn/the_gnat_compilation_model id34}@anchor{81}@anchor{gnat_ugn/the_gnat_compilation_model binding-an-ada-program}@anchor{45}
3518 @section Binding an Ada Program
3521 When using languages such as C and C++, once the source files have been
3522 compiled the only remaining step in building an executable program
3523 is linking the object modules together. This means that it is possible to
3524 link an inconsistent version of a program, in which two units have
3525 included different versions of the same header.
3527 The rules of Ada do not permit such an inconsistent program to be built.
3528 For example, if two clients have different versions of the same package,
3529 it is illegal to build a program containing these two clients.
3530 These rules are enforced by the GNAT binder, which also determines an
3531 elaboration order consistent with the Ada rules.
3533 The GNAT binder is run after all the object files for a program have
3534 been created. It is given the name of the main program unit, and from
3535 this it determines the set of units required by the program, by reading the
3536 corresponding ALI files. It generates error messages if the program is
3537 inconsistent or if no valid order of elaboration exists.
3539 If no errors are detected, the binder produces a main program, in Ada by
3540 default, that contains calls to the elaboration procedures of those
3541 compilation unit that require them, followed by
3542 a call to the main program. This Ada program is compiled to generate the
3543 object file for the main program. The name of
3544 the Ada file is @code{b~xxx}.adb` (with the corresponding spec
3545 @code{b~xxx}.ads`) where @cite{xxx} is the name of the
3546 main program unit.
3548 Finally, the linker is used to build the resulting executable program,
3549 using the object from the main program from the bind step as well as the
3550 object files for the Ada units of the program.
3552 @node GNAT and Libraries,Conditional Compilation,Binding an Ada Program,The GNAT Compilation Model
3553 @anchor{gnat_ugn/the_gnat_compilation_model gnat-and-libraries}@anchor{17}@anchor{gnat_ugn/the_gnat_compilation_model id35}@anchor{82}
3554 @section GNAT and Libraries
3557 @geindex Library building and using
3559 This chapter describes how to build and use libraries with GNAT, and also shows
3560 how to recompile the GNAT run-time library. You should be familiar with the
3561 Project Manager facility (@ref{b,,GNAT Project Manager}) before reading this
3562 chapter.
3564 @menu
3565 * Introduction to Libraries in GNAT:: 
3566 * General Ada Libraries:: 
3567 * Stand-alone Ada Libraries:: 
3568 * Rebuilding the GNAT Run-Time Library:: 
3570 @end menu
3572 @node Introduction to Libraries in GNAT,General Ada Libraries,,GNAT and Libraries
3573 @anchor{gnat_ugn/the_gnat_compilation_model introduction-to-libraries-in-gnat}@anchor{83}@anchor{gnat_ugn/the_gnat_compilation_model id36}@anchor{84}
3574 @subsection Introduction to Libraries in GNAT
3577 A library is, conceptually, a collection of objects which does not have its
3578 own main thread of execution, but rather provides certain services to the
3579 applications that use it. A library can be either statically linked with the
3580 application, in which case its code is directly included in the application,
3581 or, on platforms that support it, be dynamically linked, in which case
3582 its code is shared by all applications making use of this library.
3584 GNAT supports both types of libraries.
3585 In the static case, the compiled code can be provided in different ways. The
3586 simplest approach is to provide directly the set of objects resulting from
3587 compilation of the library source files. Alternatively, you can group the
3588 objects into an archive using whatever commands are provided by the operating
3589 system. For the latter case, the objects are grouped into a shared library.
3591 In the GNAT environment, a library has three types of components:
3594 @itemize *
3596 @item 
3597 Source files,
3599 @item 
3600 @code{ALI} files (see @ref{44,,The Ada Library Information Files}), and
3602 @item 
3603 Object files, an archive or a shared library.
3604 @end itemize
3606 A GNAT library may expose all its source files, which is useful for
3607 documentation purposes. Alternatively, it may expose only the units needed by
3608 an external user to make use of the library. That is to say, the specs
3609 reflecting the library services along with all the units needed to compile
3610 those specs, which can include generic bodies or any body implementing an
3611 inlined routine. In the case of @emph{stand-alone libraries} those exposed
3612 units are called @emph{interface units} (@ref{85,,Stand-alone Ada Libraries}).
3614 All compilation units comprising an application, including those in a library,
3615 need to be elaborated in an order partially defined by Ada's semantics. GNAT
3616 computes the elaboration order from the @code{ALI} files and this is why they
3617 constitute a mandatory part of GNAT libraries.
3618 @emph{Stand-alone libraries} are the exception to this rule because a specific
3619 library elaboration routine is produced independently of the application(s)
3620 using the library.
3622 @node General Ada Libraries,Stand-alone Ada Libraries,Introduction to Libraries in GNAT,GNAT and Libraries
3623 @anchor{gnat_ugn/the_gnat_compilation_model general-ada-libraries}@anchor{86}@anchor{gnat_ugn/the_gnat_compilation_model id37}@anchor{87}
3624 @subsection General Ada Libraries
3627 @menu
3628 * Building a library:: 
3629 * Installing a library:: 
3630 * Using a library:: 
3632 @end menu
3634 @node Building a library,Installing a library,,General Ada Libraries
3635 @anchor{gnat_ugn/the_gnat_compilation_model building-a-library}@anchor{88}@anchor{gnat_ugn/the_gnat_compilation_model id38}@anchor{89}
3636 @subsubsection Building a library
3639 The easiest way to build a library is to use the Project Manager,
3640 which supports a special type of project called a @emph{Library Project}
3641 (see @ref{8a,,Library Projects}).
3643 A project is considered a library project, when two project-level attributes
3644 are defined in it: @cite{Library_Name} and @cite{Library_Dir}. In order to
3645 control different aspects of library configuration, additional optional
3646 project-level attributes can be specified:
3649 @itemize *
3651 @item 
3653 @table @asis
3655 @item @emph{Library_Kind}
3657 This attribute controls whether the library is to be static or dynamic
3658 @end table
3660 @item 
3662 @table @asis
3664 @item @emph{Library_Version}
3666 This attribute specifies the library version; this value is used
3667 during dynamic linking of shared libraries to determine if the currently
3668 installed versions of the binaries are compatible.
3669 @end table
3671 @item 
3672 @emph{Library_Options}
3674 @item 
3676 @table @asis
3678 @item @emph{Library_GCC}
3680 These attributes specify additional low-level options to be used during
3681 library generation, and redefine the actual application used to generate
3682 library.
3683 @end table
3684 @end itemize
3686 The GNAT Project Manager takes full care of the library maintenance task,
3687 including recompilation of the source files for which objects do not exist
3688 or are not up to date, assembly of the library archive, and installation of
3689 the library (i.e., copying associated source, object and @code{ALI} files
3690 to the specified location).
3692 Here is a simple library project file:
3694 @example
3695 project My_Lib is
3696   for Source_Dirs use ("src1", "src2");
3697   for Object_Dir use "obj";
3698   for Library_Name use "mylib";
3699   for Library_Dir use "lib";
3700   for Library_Kind use "dynamic";
3701 end My_lib;
3702 @end example
3704 and the compilation command to build and install the library:
3706 @example
3707 $ gnatmake -Pmy_lib
3708 @end example
3710 It is not entirely trivial to perform manually all the steps required to
3711 produce a library. We recommend that you use the GNAT Project Manager
3712 for this task. In special cases where this is not desired, the necessary
3713 steps are discussed below.
3715 There are various possibilities for compiling the units that make up the
3716 library: for example with a Makefile (@ref{21,,Using the GNU make Utility}) or
3717 with a conventional script. For simple libraries, it is also possible to create
3718 a dummy main program which depends upon all the packages that comprise the
3719 interface of the library. This dummy main program can then be given to
3720 @emph{gnatmake}, which will ensure that all necessary objects are built.
3722 After this task is accomplished, you should follow the standard procedure
3723 of the underlying operating system to produce the static or shared library.
3725 Here is an example of such a dummy program:
3727 @example
3728 with My_Lib.Service1;
3729 with My_Lib.Service2;
3730 with My_Lib.Service3;
3731 procedure My_Lib_Dummy is
3732 begin
3733    null;
3734 end;
3735 @end example
3737 Here are the generic commands that will build an archive or a shared library.
3739 @example
3740 # compiling the library
3741 $ gnatmake -c my_lib_dummy.adb
3743 # we don't need the dummy object itself
3744 $ rm my_lib_dummy.o my_lib_dummy.ali
3746 # create an archive with the remaining objects
3747 $ ar rc libmy_lib.a *.o
3748 # some systems may require "ranlib" to be run as well
3750 # or create a shared library
3751 $ gcc -shared -o libmy_lib.so *.o
3752 # some systems may require the code to have been compiled with -fPIC
3754 # remove the object files that are now in the library
3755 $ rm *.o
3757 # Make the ALI files read-only so that gnatmake will not try to
3758 # regenerate the objects that are in the library
3759 $ chmod -w *.ali
3760 @end example
3762 Please note that the library must have a name of the form @code{lib@emph{xxx}.a}
3763 or @code{lib@emph{xxx}.so} (or @code{lib@emph{xxx}.dll} on Windows) in order to
3764 be accessed by the directive @code{-l@emph{xxx}} at link time.
3766 @node Installing a library,Using a library,Building a library,General Ada Libraries
3767 @anchor{gnat_ugn/the_gnat_compilation_model installing-a-library}@anchor{8b}@anchor{gnat_ugn/the_gnat_compilation_model id39}@anchor{8c}
3768 @subsubsection Installing a library
3771 @geindex ADA_PROJECT_PATH
3773 @geindex GPR_PROJECT_PATH
3775 If you use project files, library installation is part of the library build
3776 process (@ref{8d,,Installing a library with project files}).
3778 When project files are not an option, it is also possible, but not recommended,
3779 to install the library so that the sources needed to use the library are on the
3780 Ada source path and the ALI files & libraries be on the Ada Object path (see
3781 @ref{8e,,Search Paths and the Run-Time Library (RTL)}. Alternatively, the system
3782 administrator can place general-purpose libraries in the default compiler
3783 paths, by specifying the libraries' location in the configuration files
3784 @code{ada_source_path} and @code{ada_object_path}. These configuration files
3785 must be located in the GNAT installation tree at the same place as the gcc spec
3786 file. The location of the gcc spec file can be determined as follows:
3788 @example
3789 $ gcc -v
3790 @end example
3792 The configuration files mentioned above have a simple format: each line
3793 must contain one unique directory name.
3794 Those names are added to the corresponding path
3795 in their order of appearance in the file. The names can be either absolute
3796 or relative; in the latter case, they are relative to where theses files
3797 are located.
3799 The files @code{ada_source_path} and @code{ada_object_path} might not be
3800 present in a
3801 GNAT installation, in which case, GNAT will look for its run-time library in
3802 the directories @code{adainclude} (for the sources) and @code{adalib} (for the
3803 objects and @code{ALI} files). When the files exist, the compiler does not
3804 look in @code{adainclude} and @code{adalib}, and thus the
3805 @code{ada_source_path} file
3806 must contain the location for the GNAT run-time sources (which can simply
3807 be @code{adainclude}). In the same way, the @code{ada_object_path} file must
3808 contain the location for the GNAT run-time objects (which can simply
3809 be @code{adalib}).
3811 You can also specify a new default path to the run-time library at compilation
3812 time with the switch @emph{--RTS=rts-path}. You can thus choose / change
3813 the run-time library you want your program to be compiled with. This switch is
3814 recognized by @emph{gcc}, @emph{gnatmake}, @emph{gnatbind},
3815 @emph{gnatls}, @emph{gnatfind} and @emph{gnatxref}.
3817 It is possible to install a library before or after the standard GNAT
3818 library, by reordering the lines in the configuration files. In general, a
3819 library must be installed before the GNAT library if it redefines
3820 any part of it.
3822 @node Using a library,,Installing a library,General Ada Libraries
3823 @anchor{gnat_ugn/the_gnat_compilation_model using-a-library}@anchor{8f}@anchor{gnat_ugn/the_gnat_compilation_model id40}@anchor{90}
3824 @subsubsection Using a library
3827 Once again, the project facility greatly simplifies the use of
3828 libraries. In this context, using a library is just a matter of adding a
3829 @emph{with} clause in the user project. For instance, to make use of the
3830 library @cite{My_Lib} shown in examples in earlier sections, you can
3831 write:
3833 @example
3834 with "my_lib";
3835 project My_Proj is
3836   ...
3837 end My_Proj;
3838 @end example
3840 Even if you have a third-party, non-Ada library, you can still use GNAT's
3841 Project Manager facility to provide a wrapper for it. For example, the
3842 following project, when @emph{with}ed by your main project, will link with the
3843 third-party library @code{liba.a}:
3845 @example
3846 project Liba is
3847    for Externally_Built use "true";
3848    for Source_Files use ();
3849    for Library_Dir use "lib";
3850    for Library_Name use "a";
3851    for Library_Kind use "static";
3852 end Liba;
3853 @end example
3855 This is an alternative to the use of @cite{pragma Linker_Options}. It is
3856 especially interesting in the context of systems with several interdependent
3857 static libraries where finding a proper linker order is not easy and best be
3858 left to the tools having visibility over project dependence information.
3860 In order to use an Ada library manually, you need to make sure that this
3861 library is on both your source and object path
3862 (see @ref{8e,,Search Paths and the Run-Time Library (RTL)}
3863 and @ref{91,,Search Paths for gnatbind}). Furthermore, when the objects are grouped
3864 in an archive or a shared library, you need to specify the desired
3865 library at link time.
3867 For example, you can use the library @code{mylib} installed in
3868 @code{/dir/my_lib_src} and @code{/dir/my_lib_obj} with the following commands:
3870 @example
3871 $ gnatmake -aI/dir/my_lib_src -aO/dir/my_lib_obj my_appl \\
3872   -largs -lmy_lib
3873 @end example
3875 This can be expressed more simply:
3877 @example
3878 $ gnatmake my_appl
3879 @end example
3881 when the following conditions are met:
3884 @itemize *
3886 @item 
3887 @code{/dir/my_lib_src} has been added by the user to the environment
3888 variable 
3889 @geindex ADA_INCLUDE_PATH
3890 @geindex environment variable; ADA_INCLUDE_PATH
3891 @code{ADA_INCLUDE_PATH}, or by the administrator to the file
3892 @code{ada_source_path}
3894 @item 
3895 @code{/dir/my_lib_obj} has been added by the user to the environment
3896 variable 
3897 @geindex ADA_OBJECTS_PATH
3898 @geindex environment variable; ADA_OBJECTS_PATH
3899 @code{ADA_OBJECTS_PATH}, or by the administrator to the file
3900 @code{ada_object_path}
3902 @item 
3903 a pragma @cite{Linker_Options} has been added to one of the sources.
3904 For example:
3906 @example
3907 pragma Linker_Options ("-lmy_lib");
3908 @end example
3909 @end itemize
3911 Note that you may also load a library dynamically at
3912 run time given its filename, as illustrated in the GNAT @code{plugins} example
3913 in the directory @code{share/examples/gnat/plugins} within the GNAT
3914 install area.
3916 @node Stand-alone Ada Libraries,Rebuilding the GNAT Run-Time Library,General Ada Libraries,GNAT and Libraries
3917 @anchor{gnat_ugn/the_gnat_compilation_model stand-alone-ada-libraries}@anchor{85}@anchor{gnat_ugn/the_gnat_compilation_model id41}@anchor{92}
3918 @subsection Stand-alone Ada Libraries
3921 @geindex Stand-alone libraries
3923 @menu
3924 * Introduction to Stand-alone Libraries:: 
3925 * Building a Stand-alone Library:: 
3926 * Creating a Stand-alone Library to be used in a non-Ada context:: 
3927 * Restrictions in Stand-alone Libraries:: 
3929 @end menu
3931 @node Introduction to Stand-alone Libraries,Building a Stand-alone Library,,Stand-alone Ada Libraries
3932 @anchor{gnat_ugn/the_gnat_compilation_model introduction-to-stand-alone-libraries}@anchor{93}@anchor{gnat_ugn/the_gnat_compilation_model id42}@anchor{94}
3933 @subsubsection Introduction to Stand-alone Libraries
3936 A Stand-alone Library (abbreviated 'SAL') is a library that contains the
3937 necessary code to
3938 elaborate the Ada units that are included in the library. In contrast with
3939 an ordinary library, which consists of all sources, objects and @code{ALI}
3940 files of the
3941 library, a SAL may specify a restricted subset of compilation units
3942 to serve as a library interface. In this case, the fully
3943 self-sufficient set of files will normally consist of an objects
3944 archive, the sources of interface units' specs, and the @code{ALI}
3945 files of interface units.
3946 If an interface spec contains a generic unit or an inlined subprogram,
3947 the body's
3948 source must also be provided; if the units that must be provided in the source
3949 form depend on other units, the source and @code{ALI} files of those must
3950 also be provided.
3952 The main purpose of a SAL is to minimize the recompilation overhead of client
3953 applications when a new version of the library is installed. Specifically,
3954 if the interface sources have not changed, client applications do not need to
3955 be recompiled. If, furthermore, a SAL is provided in the shared form and its
3956 version, controlled by @cite{Library_Version} attribute, is not changed,
3957 then the clients do not need to be relinked.
3959 SALs also allow the library providers to minimize the amount of library source
3960 text exposed to the clients.  Such 'information hiding' might be useful or
3961 necessary for various reasons.
3963 Stand-alone libraries are also well suited to be used in an executable whose
3964 main routine is not written in Ada.
3966 @node Building a Stand-alone Library,Creating a Stand-alone Library to be used in a non-Ada context,Introduction to Stand-alone Libraries,Stand-alone Ada Libraries
3967 @anchor{gnat_ugn/the_gnat_compilation_model id43}@anchor{95}@anchor{gnat_ugn/the_gnat_compilation_model building-a-stand-alone-library}@anchor{96}
3968 @subsubsection Building a Stand-alone Library
3971 GNAT's Project facility provides a simple way of building and installing
3972 stand-alone libraries; see @ref{97,,Stand-alone Library Projects}.
3973 To be a Stand-alone Library Project, in addition to the two attributes
3974 that make a project a Library Project (@cite{Library_Name} and
3975 @cite{Library_Dir}; see @ref{8a,,Library Projects}), the attribute
3976 @cite{Library_Interface} must be defined.  For example:
3978 @example
3979 for Library_Dir use "lib_dir";
3980 for Library_Name use "dummy";
3981 for Library_Interface use ("int1", "int1.child");
3982 @end example
3984 Attribute @cite{Library_Interface} has a non-empty string list value,
3985 each string in the list designating a unit contained in an immediate source
3986 of the project file.
3988 When a Stand-alone Library is built, first the binder is invoked to build
3989 a package whose name depends on the library name
3990 (@code{b~dummy.ads/b} in the example above).
3991 This binder-generated package includes initialization and
3992 finalization procedures whose
3993 names depend on the library name (@cite{dummyinit} and @cite{dummyfinal}
3994 in the example
3995 above). The object corresponding to this package is included in the library.
3997 You must ensure timely (e.g., prior to any use of interfaces in the SAL)
3998 calling of these procedures if a static SAL is built, or if a shared SAL
3999 is built
4000 with the project-level attribute @cite{Library_Auto_Init} set to
4001 @cite{"false"}.
4003 For a Stand-Alone Library, only the @code{ALI} files of the Interface Units
4004 (those that are listed in attribute @cite{Library_Interface}) are copied to
4005 the Library Directory. As a consequence, only the Interface Units may be
4006 imported from Ada units outside of the library. If other units are imported,
4007 the binding phase will fail.
4009 It is also possible to build an encapsulated library where not only
4010 the code to elaborate and finalize the library is embedded but also
4011 ensuring that the library is linked only against static
4012 libraries. So an encapsulated library only depends on system
4013 libraries, all other code, including the GNAT runtime, is embedded. To
4014 build an encapsulated library the attribute
4015 @cite{Library_Standalone} must be set to @cite{encapsulated}:
4017 @example
4018 for Library_Dir use "lib_dir";
4019 for Library_Name use "dummy";
4020 for Library_Kind use "dynamic";
4021 for Library_Interface use ("int1", "int1.child");
4022 for Library_Standalone use "encapsulated";
4023 @end example
4025 The default value for this attribute is @cite{standard} in which case
4026 a stand-alone library is built.
4028 The attribute @cite{Library_Src_Dir} may be specified for a
4029 Stand-Alone Library. @cite{Library_Src_Dir} is a simple attribute that has a
4030 single string value. Its value must be the path (absolute or relative to the
4031 project directory) of an existing directory. This directory cannot be the
4032 object directory or one of the source directories, but it can be the same as
4033 the library directory. The sources of the Interface
4034 Units of the library that are needed by an Ada client of the library will be
4035 copied to the designated directory, called the Interface Copy directory.
4036 These sources include the specs of the Interface Units, but they may also
4037 include bodies and subunits, when pragmas @cite{Inline} or @cite{Inline_Always}
4038 are used, or when there is a generic unit in the spec. Before the sources
4039 are copied to the Interface Copy directory, an attempt is made to delete all
4040 files in the Interface Copy directory.
4042 Building stand-alone libraries by hand is somewhat tedious, but for those
4043 occasions when it is necessary here are the steps that you need to perform:
4046 @itemize *
4048 @item 
4049 Compile all library sources.
4051 @item 
4052 Invoke the binder with the switch @emph{-n} (No Ada main program),
4053 with all the @code{ALI} files of the interfaces, and
4054 with the switch @emph{-L} to give specific names to the @cite{init}
4055 and @cite{final} procedures.  For example:
4057 @example
4058 $ gnatbind -n int1.ali int2.ali -Lsal1
4059 @end example
4061 @item 
4062 Compile the binder generated file:
4064 @example
4065 $ gcc -c b~int2.adb
4066 @end example
4068 @item 
4069 Link the dynamic library with all the necessary object files,
4070 indicating to the linker the names of the @cite{init} (and possibly
4071 @cite{final}) procedures for automatic initialization (and finalization).
4072 The built library should be placed in a directory different from
4073 the object directory.
4075 @item 
4076 Copy the @cite{ALI} files of the interface to the library directory,
4077 add in this copy an indication that it is an interface to a SAL
4078 (i.e., add a word @emph{SL} on the line in the @code{ALI} file that starts
4079 with letter 'P') and make the modified copy of the @code{ALI} file
4080 read-only.
4081 @end itemize
4083 Using SALs is not different from using other libraries
4084 (see @ref{8f,,Using a library}).
4086 @node Creating a Stand-alone Library to be used in a non-Ada context,Restrictions in Stand-alone Libraries,Building a Stand-alone Library,Stand-alone Ada Libraries
4087 @anchor{gnat_ugn/the_gnat_compilation_model creating-a-stand-alone-library-to-be-used-in-a-non-ada-context}@anchor{98}@anchor{gnat_ugn/the_gnat_compilation_model id44}@anchor{99}
4088 @subsubsection Creating a Stand-alone Library to be used in a non-Ada context
4091 It is easy to adapt the SAL build procedure discussed above for use of a SAL in
4092 a non-Ada context.
4094 The only extra step required is to ensure that library interface subprograms
4095 are compatible with the main program, by means of @cite{pragma Export}
4096 or @cite{pragma Convention}.
4098 Here is an example of simple library interface for use with C main program:
4100 @example
4101 package My_Package is
4103    procedure Do_Something;
4104    pragma Export (C, Do_Something, "do_something");
4106    procedure Do_Something_Else;
4107    pragma Export (C, Do_Something_Else, "do_something_else");
4109 end My_Package;
4110 @end example
4112 On the foreign language side, you must provide a 'foreign' view of the
4113 library interface; remember that it should contain elaboration routines in
4114 addition to interface subprograms.
4116 The example below shows the content of @cite{mylib_interface.h} (note
4117 that there is no rule for the naming of this file, any name can be used)
4119 @example
4120 /* the library elaboration procedure */
4121 extern void mylibinit (void);
4123 /* the library finalization procedure */
4124 extern void mylibfinal (void);
4126 /* the interface exported by the library */
4127 extern void do_something (void);
4128 extern void do_something_else (void);
4129 @end example
4131 Libraries built as explained above can be used from any program, provided
4132 that the elaboration procedures (named @cite{mylibinit} in the previous
4133 example) are called before the library services are used. Any number of
4134 libraries can be used simultaneously, as long as the elaboration
4135 procedure of each library is called.
4137 Below is an example of a C program that uses the @cite{mylib} library.
4139 @example
4140 #include "mylib_interface.h"
4143 main (void)
4145    /* First, elaborate the library before using it */
4146    mylibinit ();
4148    /* Main program, using the library exported entities */
4149    do_something ();
4150    do_something_else ();
4152    /* Library finalization at the end of the program */
4153    mylibfinal ();
4154    return 0;
4156 @end example
4158 Note that invoking any library finalization procedure generated by
4159 @cite{gnatbind} shuts down the Ada run-time environment.
4160 Consequently, the
4161 finalization of all Ada libraries must be performed at the end of the program.
4162 No call to these libraries or to the Ada run-time library should be made
4163 after the finalization phase.
4165 Note also that special care must be taken with multi-tasks
4166 applications. The initialization and finalization routines are not
4167 protected against concurrent access. If such requirement is needed it
4168 must be ensured at the application level using a specific operating
4169 system services like a mutex or a critical-section.
4171 @node Restrictions in Stand-alone Libraries,,Creating a Stand-alone Library to be used in a non-Ada context,Stand-alone Ada Libraries
4172 @anchor{gnat_ugn/the_gnat_compilation_model id45}@anchor{9a}@anchor{gnat_ugn/the_gnat_compilation_model restrictions-in-stand-alone-libraries}@anchor{9b}
4173 @subsubsection Restrictions in Stand-alone Libraries
4176 The pragmas listed below should be used with caution inside libraries,
4177 as they can create incompatibilities with other Ada libraries:
4180 @itemize *
4182 @item 
4183 pragma @cite{Locking_Policy}
4185 @item 
4186 pragma @cite{Partition_Elaboration_Policy}
4188 @item 
4189 pragma @cite{Queuing_Policy}
4191 @item 
4192 pragma @cite{Task_Dispatching_Policy}
4194 @item 
4195 pragma @cite{Unreserve_All_Interrupts}
4196 @end itemize
4198 When using a library that contains such pragmas, the user must make sure
4199 that all libraries use the same pragmas with the same values. Otherwise,
4200 @cite{Program_Error} will
4201 be raised during the elaboration of the conflicting
4202 libraries. The usage of these pragmas and its consequences for the user
4203 should therefore be well documented.
4205 Similarly, the traceback in the exception occurrence mechanism should be
4206 enabled or disabled in a consistent manner across all libraries.
4207 Otherwise, Program_Error will be raised during the elaboration of the
4208 conflicting libraries.
4210 If the @cite{Version} or @cite{Body_Version}
4211 attributes are used inside a library, then you need to
4212 perform a @cite{gnatbind} step that specifies all @code{ALI} files in all
4213 libraries, so that version identifiers can be properly computed.
4214 In practice these attributes are rarely used, so this is unlikely
4215 to be a consideration.
4217 @node Rebuilding the GNAT Run-Time Library,,Stand-alone Ada Libraries,GNAT and Libraries
4218 @anchor{gnat_ugn/the_gnat_compilation_model id46}@anchor{9c}@anchor{gnat_ugn/the_gnat_compilation_model rebuilding-the-gnat-run-time-library}@anchor{9d}
4219 @subsection Rebuilding the GNAT Run-Time Library
4222 @geindex GNAT Run-Time Library
4223 @geindex rebuilding
4225 @geindex Building the GNAT Run-Time Library
4227 @geindex Rebuilding the GNAT Run-Time Library
4229 @geindex Run-Time Library
4230 @geindex rebuilding
4232 It may be useful to recompile the GNAT library in various contexts, the
4233 most important one being the use of partition-wide configuration pragmas
4234 such as @cite{Normalize_Scalars}. A special Makefile called
4235 @cite{Makefile.adalib} is provided to that effect and can be found in
4236 the directory containing the GNAT library. The location of this
4237 directory depends on the way the GNAT environment has been installed and can
4238 be determined by means of the command:
4240 @example
4241 $ gnatls -v
4242 @end example
4244 The last entry in the object search path usually contains the
4245 gnat library. This Makefile contains its own documentation and in
4246 particular the set of instructions needed to rebuild a new library and
4247 to use it.
4249 @geindex Conditional compilation
4251 @node Conditional Compilation,Mixed Language Programming,GNAT and Libraries,The GNAT Compilation Model
4252 @anchor{gnat_ugn/the_gnat_compilation_model id47}@anchor{9e}@anchor{gnat_ugn/the_gnat_compilation_model conditional-compilation}@anchor{18}
4253 @section Conditional Compilation
4256 This section presents some guidelines for modeling conditional compilation in Ada and describes the
4257 gnatprep preprocessor utility.
4259 @geindex Conditional compilation
4261 @menu
4262 * Modeling Conditional Compilation in Ada:: 
4263 * Preprocessing with gnatprep:: 
4264 * Integrated Preprocessing:: 
4266 @end menu
4268 @node Modeling Conditional Compilation in Ada,Preprocessing with gnatprep,,Conditional Compilation
4269 @anchor{gnat_ugn/the_gnat_compilation_model modeling-conditional-compilation-in-ada}@anchor{9f}@anchor{gnat_ugn/the_gnat_compilation_model id48}@anchor{a0}
4270 @subsection Modeling Conditional Compilation in Ada
4273 It is often necessary to arrange for a single source program
4274 to serve multiple purposes, where it is compiled in different
4275 ways to achieve these different goals. Some examples of the
4276 need for this feature are
4279 @itemize *
4281 @item 
4282 Adapting a program to a different hardware environment
4284 @item 
4285 Adapting a program to a different target architecture
4287 @item 
4288 Turning debugging features on and off
4290 @item 
4291 Arranging for a program to compile with different compilers
4292 @end itemize
4294 In C, or C++, the typical approach would be to use the preprocessor
4295 that is defined as part of the language. The Ada language does not
4296 contain such a feature. This is not an oversight, but rather a very
4297 deliberate design decision, based on the experience that overuse of
4298 the preprocessing features in C and C++ can result in programs that
4299 are extremely difficult to maintain. For example, if we have ten
4300 switches that can be on or off, this means that there are a thousand
4301 separate programs, any one of which might not even be syntactically
4302 correct, and even if syntactically correct, the resulting program
4303 might not work correctly. Testing all combinations can quickly become
4304 impossible.
4306 Nevertheless, the need to tailor programs certainly exists, and in
4307 this section we will discuss how this can
4308 be achieved using Ada in general, and GNAT in particular.
4310 @menu
4311 * Use of Boolean Constants:: 
4312 * Debugging - A Special Case:: 
4313 * Conditionalizing Declarations:: 
4314 * Use of Alternative Implementations:: 
4315 * Preprocessing:: 
4317 @end menu
4319 @node Use of Boolean Constants,Debugging - A Special Case,,Modeling Conditional Compilation in Ada
4320 @anchor{gnat_ugn/the_gnat_compilation_model id49}@anchor{a1}@anchor{gnat_ugn/the_gnat_compilation_model use-of-boolean-constants}@anchor{a2}
4321 @subsubsection Use of Boolean Constants
4324 In the case where the difference is simply which code
4325 sequence is executed, the cleanest solution is to use Boolean
4326 constants to control which code is executed.
4328 @example
4329 FP_Initialize_Required : constant Boolean := True;
4331 if FP_Initialize_Required then
4333 end if;
4334 @end example
4336 Not only will the code inside the @cite{if} statement not be executed if
4337 the constant Boolean is @cite{False}, but it will also be completely
4338 deleted from the program.
4339 However, the code is only deleted after the @cite{if} statement
4340 has been checked for syntactic and semantic correctness.
4341 (In contrast, with preprocessors the code is deleted before the
4342 compiler ever gets to see it, so it is not checked until the switch
4343 is turned on.)
4345 @geindex Preprocessors (contrasted with conditional compilation)
4347 Typically the Boolean constants will be in a separate package,
4348 something like:
4350 @example
4351 package Config is
4352    FP_Initialize_Required : constant Boolean := True;
4353    Reset_Available        : constant Boolean := False;
4354    ...
4355 end Config;
4356 @end example
4358 The @cite{Config} package exists in multiple forms for the various targets,
4359 with an appropriate script selecting the version of @cite{Config} needed.
4360 Then any other unit requiring conditional compilation can do a @emph{with}
4361 of @cite{Config} to make the constants visible.
4363 @node Debugging - A Special Case,Conditionalizing Declarations,Use of Boolean Constants,Modeling Conditional Compilation in Ada
4364 @anchor{gnat_ugn/the_gnat_compilation_model debugging-a-special-case}@anchor{a3}@anchor{gnat_ugn/the_gnat_compilation_model id50}@anchor{a4}
4365 @subsubsection Debugging - A Special Case
4368 A common use of conditional code is to execute statements (for example
4369 dynamic checks, or output of intermediate results) under control of a
4370 debug switch, so that the debugging behavior can be turned on and off.
4371 This can be done using a Boolean constant to control whether the code
4372 is active:
4374 @example
4375 if Debugging then
4376    Put_Line ("got to the first stage!");
4377 end if;
4378 @end example
4382 @example
4383 if Debugging and then Temperature > 999.0 then
4384    raise Temperature_Crazy;
4385 end if;
4386 @end example
4388 @geindex pragma Assert
4390 Since this is a common case, there are special features to deal with
4391 this in a convenient manner. For the case of tests, Ada 2005 has added
4392 a pragma @cite{Assert} that can be used for such tests. This pragma is modeled
4393 on the @cite{Assert} pragma that has always been available in GNAT, so this
4394 feature may be used with GNAT even if you are not using Ada 2005 features.
4395 The use of pragma @cite{Assert} is described in the
4396 @cite{GNAT_Reference_Manual}, but as an
4397 example, the last test could be written:
4399 @example
4400 pragma Assert (Temperature <= 999.0, "Temperature Crazy");
4401 @end example
4403 or simply
4405 @example
4406 pragma Assert (Temperature <= 999.0);
4407 @end example
4409 In both cases, if assertions are active and the temperature is excessive,
4410 the exception @cite{Assert_Failure} will be raised, with the given string in
4411 the first case or a string indicating the location of the pragma in the second
4412 case used as the exception message.
4414 @geindex pragma Assertion_Policy
4416 You can turn assertions on and off by using the @cite{Assertion_Policy}
4417 pragma.
4419 @geindex -gnata switch
4421 This is an Ada 2005 pragma which is implemented in all modes by
4422 GNAT. Alternatively, you can use the @emph{-gnata} switch
4423 to enable assertions from the command line, which applies to
4424 all versions of Ada.
4426 @geindex pragma Debug
4428 For the example above with the @cite{Put_Line}, the GNAT-specific pragma
4429 @cite{Debug} can be used:
4431 @example
4432 pragma Debug (Put_Line ("got to the first stage!"));
4433 @end example
4435 If debug pragmas are enabled, the argument, which must be of the form of
4436 a procedure call, is executed (in this case, @cite{Put_Line} will be called).
4437 Only one call can be present, but of course a special debugging procedure
4438 containing any code you like can be included in the program and then
4439 called in a pragma @cite{Debug} argument as needed.
4441 One advantage of pragma @cite{Debug} over the @cite{if Debugging then}
4442 construct is that pragma @cite{Debug} can appear in declarative contexts,
4443 such as at the very beginning of a procedure, before local declarations have
4444 been elaborated.
4446 @geindex pragma Debug_Policy
4448 Debug pragmas are enabled using either the @emph{-gnata} switch that also
4449 controls assertions, or with a separate Debug_Policy pragma.
4451 The latter pragma is new in the Ada 2005 versions of GNAT (but it can be used
4452 in Ada 95 and Ada 83 programs as well), and is analogous to
4453 pragma @cite{Assertion_Policy} to control assertions.
4455 @cite{Assertion_Policy} and @cite{Debug_Policy} are configuration pragmas,
4456 and thus they can appear in @code{gnat.adc} if you are not using a
4457 project file, or in the file designated to contain configuration pragmas
4458 in a project file.
4459 They then apply to all subsequent compilations. In practice the use of
4460 the @emph{-gnata} switch is often the most convenient method of controlling
4461 the status of these pragmas.
4463 Note that a pragma is not a statement, so in contexts where a statement
4464 sequence is required, you can't just write a pragma on its own. You have
4465 to add a @cite{null} statement.
4467 @example
4468 if ... then
4469    ... -- some statements
4470 else
4471    pragma Assert (Num_Cases < 10);
4472    null;
4473 end if;
4474 @end example
4476 @node Conditionalizing Declarations,Use of Alternative Implementations,Debugging - A Special Case,Modeling Conditional Compilation in Ada
4477 @anchor{gnat_ugn/the_gnat_compilation_model conditionalizing-declarations}@anchor{a5}@anchor{gnat_ugn/the_gnat_compilation_model id51}@anchor{a6}
4478 @subsubsection Conditionalizing Declarations
4481 In some cases it may be necessary to conditionalize declarations to meet
4482 different requirements. For example we might want a bit string whose length
4483 is set to meet some hardware message requirement.
4485 This may be possible using declare blocks controlled
4486 by conditional constants:
4488 @example
4489 if Small_Machine then
4490    declare
4491       X : Bit_String (1 .. 10);
4492    begin
4493       ...
4494    end;
4495 else
4496    declare
4497       X : Large_Bit_String (1 .. 1000);
4498    begin
4499       ...
4500    end;
4501 end if;
4502 @end example
4504 Note that in this approach, both declarations are analyzed by the
4505 compiler so this can only be used where both declarations are legal,
4506 even though one of them will not be used.
4508 Another approach is to define integer constants, e.g., @cite{Bits_Per_Word},
4509 or Boolean constants, e.g., @cite{Little_Endian}, and then write declarations
4510 that are parameterized by these constants. For example
4512 @example
4513 for Rec use
4514   Field1 at 0 range Boolean'Pos (Little_Endian) * 10 .. Bits_Per_Word;
4515 end record;
4516 @end example
4518 If @cite{Bits_Per_Word} is set to 32, this generates either
4520 @example
4521 for Rec use
4522   Field1 at 0 range 0 .. 32;
4523 end record;
4524 @end example
4526 for the big endian case, or
4528 @example
4529 for Rec use record
4530     Field1 at 0 range 10 .. 32;
4531 end record;
4532 @end example
4534 for the little endian case. Since a powerful subset of Ada expression
4535 notation is usable for creating static constants, clever use of this
4536 feature can often solve quite difficult problems in conditionalizing
4537 compilation (note incidentally that in Ada 95, the little endian
4538 constant was introduced as @cite{System.Default_Bit_Order}, so you do not
4539 need to define this one yourself).
4541 @node Use of Alternative Implementations,Preprocessing,Conditionalizing Declarations,Modeling Conditional Compilation in Ada
4542 @anchor{gnat_ugn/the_gnat_compilation_model use-of-alternative-implementations}@anchor{a7}@anchor{gnat_ugn/the_gnat_compilation_model id52}@anchor{a8}
4543 @subsubsection Use of Alternative Implementations
4546 In some cases, none of the approaches described above are adequate. This
4547 can occur for example if the set of declarations required is radically
4548 different for two different configurations.
4550 In this situation, the official Ada way of dealing with conditionalizing
4551 such code is to write separate units for the different cases. As long as
4552 this does not result in excessive duplication of code, this can be done
4553 without creating maintenance problems. The approach is to share common
4554 code as far as possible, and then isolate the code and declarations
4555 that are different. Subunits are often a convenient method for breaking
4556 out a piece of a unit that is to be conditionalized, with separate files
4557 for different versions of the subunit for different targets, where the
4558 build script selects the right one to give to the compiler.
4560 @geindex Subunits (and conditional compilation)
4562 As an example, consider a situation where a new feature in Ada 2005
4563 allows something to be done in a really nice way. But your code must be able
4564 to compile with an Ada 95 compiler. Conceptually you want to say:
4566 @example
4567 if Ada_2005 then
4568    ... neat Ada 2005 code
4569 else
4570    ... not quite as neat Ada 95 code
4571 end if;
4572 @end example
4574 where @cite{Ada_2005} is a Boolean constant.
4576 But this won't work when @cite{Ada_2005} is set to @cite{False},
4577 since the @cite{then} clause will be illegal for an Ada 95 compiler.
4578 (Recall that although such unreachable code would eventually be deleted
4579 by the compiler, it still needs to be legal.  If it uses features
4580 introduced in Ada 2005, it will be illegal in Ada 95.)
4582 So instead we write
4584 @example
4585 procedure Insert is separate;
4586 @end example
4588 Then we have two files for the subunit @cite{Insert}, with the two sets of
4589 code.
4590 If the package containing this is called @cite{File_Queries}, then we might
4591 have two files
4594 @itemize *
4596 @item 
4597 @code{file_queries-insert-2005.adb}
4599 @item 
4600 @code{file_queries-insert-95.adb}
4601 @end itemize
4603 and the build script renames the appropriate file to @code{file_queries-insert.adb} and then carries out the compilation.
4605 This can also be done with project files' naming schemes. For example:
4607 @example
4608 for body ("File_Queries.Insert") use "file_queries-insert-2005.ada";
4609 @end example
4611 Note also that with project files it is desirable to use a different extension
4612 than @code{ads} / @code{adb} for alternative versions. Otherwise a naming
4613 conflict may arise through another commonly used feature: to declare as part
4614 of the project a set of directories containing all the sources obeying the
4615 default naming scheme.
4617 The use of alternative units is certainly feasible in all situations,
4618 and for example the Ada part of the GNAT run-time is conditionalized
4619 based on the target architecture using this approach. As a specific example,
4620 consider the implementation of the AST feature in VMS. There is one
4621 spec: @code{s-asthan.ads} which is the same for all architectures, and three
4622 bodies:
4625 @itemize *
4627 @item 
4629 @table @asis
4631 @item @code{s-asthan.adb}
4633 used for all non-VMS operating systems
4634 @end table
4636 @item 
4638 @table @asis
4640 @item @code{s-asthan-vms-alpha.adb}
4642 used for VMS on the Alpha
4643 @end table
4645 @item 
4647 @table @asis
4649 @item @code{s-asthan-vms-ia64.adb}
4651 used for VMS on the ia64
4652 @end table
4653 @end itemize
4655 The dummy version @code{s-asthan.adb} simply raises exceptions noting that
4656 this operating system feature is not available, and the two remaining
4657 versions interface with the corresponding versions of VMS to provide
4658 VMS-compatible AST handling. The GNAT build script knows the architecture
4659 and operating system, and automatically selects the right version,
4660 renaming it if necessary to @code{s-asthan.adb} before the run-time build.
4662 Another style for arranging alternative implementations is through Ada's
4663 access-to-subprogram facility.
4664 In case some functionality is to be conditionally included,
4665 you can declare an access-to-procedure variable @cite{Ref} that is initialized
4666 to designate a 'do nothing' procedure, and then invoke @cite{Ref.all}
4667 when appropriate.
4668 In some library package, set @cite{Ref} to @cite{Proc'Access} for some
4669 procedure @cite{Proc} that performs the relevant processing.
4670 The initialization only occurs if the library package is included in the
4671 program.
4672 The same idea can also be implemented using tagged types and dispatching
4673 calls.
4675 @node Preprocessing,,Use of Alternative Implementations,Modeling Conditional Compilation in Ada
4676 @anchor{gnat_ugn/the_gnat_compilation_model preprocessing}@anchor{a9}@anchor{gnat_ugn/the_gnat_compilation_model id53}@anchor{aa}
4677 @subsubsection Preprocessing
4680 @geindex Preprocessing
4682 Although it is quite possible to conditionalize code without the use of
4683 C-style preprocessing, as described earlier in this section, it is
4684 nevertheless convenient in some cases to use the C approach. Moreover,
4685 older Ada compilers have often provided some preprocessing capability,
4686 so legacy code may depend on this approach, even though it is not
4687 standard.
4689 To accommodate such use, GNAT provides a preprocessor (modeled to a large
4690 extent on the various preprocessors that have been used
4691 with legacy code on other compilers, to enable easier transition).
4693 @geindex gnatprep
4695 The preprocessor may be used in two separate modes. It can be used quite
4696 separately from the compiler, to generate a separate output source file
4697 that is then fed to the compiler as a separate step. This is the
4698 @cite{gnatprep} utility, whose use is fully described in
4699 @ref{19,,Preprocessing with gnatprep}.
4701 The preprocessing language allows such constructs as
4703 @example
4704 #if DEBUG or else (PRIORITY > 4) then
4705    bunch of declarations
4706 #else
4707    completely different bunch of declarations
4708 #end if;
4709 @end example
4711 The values of the symbols @cite{DEBUG} and @cite{PRIORITY} can be
4712 defined either on the command line or in a separate file.
4714 The other way of running the preprocessor is even closer to the C style and
4715 often more convenient. In this approach the preprocessing is integrated into
4716 the compilation process. The compiler is fed the preprocessor input which
4717 includes @cite{#if} lines etc, and then the compiler carries out the
4718 preprocessing internally and processes the resulting output.
4719 For more details on this approach, see @ref{1a,,Integrated Preprocessing}.
4721 @node Preprocessing with gnatprep,Integrated Preprocessing,Modeling Conditional Compilation in Ada,Conditional Compilation
4722 @anchor{gnat_ugn/the_gnat_compilation_model id54}@anchor{ab}@anchor{gnat_ugn/the_gnat_compilation_model preprocessing-with-gnatprep}@anchor{19}
4723 @subsection Preprocessing with @cite{gnatprep}
4726 @geindex gnatprep
4728 @geindex Preprocessing (gnatprep)
4730 This section discusses how to use GNAT's @cite{gnatprep} utility for simple
4731 preprocessing.
4732 Although designed for use with GNAT, @cite{gnatprep} does not depend on any
4733 special GNAT features.
4734 For further discussion of conditional compilation in general, see
4735 @ref{18,,Conditional Compilation}.
4737 @menu
4738 * Preprocessing Symbols:: 
4739 * Using gnatprep:: 
4740 * Switches for gnatprep:: 
4741 * Form of Definitions File:: 
4742 * Form of Input Text for gnatprep:: 
4744 @end menu
4746 @node Preprocessing Symbols,Using gnatprep,,Preprocessing with gnatprep
4747 @anchor{gnat_ugn/the_gnat_compilation_model id55}@anchor{ac}@anchor{gnat_ugn/the_gnat_compilation_model preprocessing-symbols}@anchor{ad}
4748 @subsubsection Preprocessing Symbols
4751 Preprocessing symbols are defined in definition files and referred to in
4752 sources to be preprocessed. A Preprocessing symbol is an identifier, following
4753 normal Ada (case-insensitive) rules for its syntax, with the restriction that
4754 all characters need to be in the ASCII set (no accented letters).
4756 @node Using gnatprep,Switches for gnatprep,Preprocessing Symbols,Preprocessing with gnatprep
4757 @anchor{gnat_ugn/the_gnat_compilation_model using-gnatprep}@anchor{ae}@anchor{gnat_ugn/the_gnat_compilation_model id56}@anchor{af}
4758 @subsubsection Using @cite{gnatprep}
4761 To call @cite{gnatprep} use:
4763 @example
4764 $ gnatprep [`switches`] `infile` `outfile` [`deffile`]
4765 @end example
4767 where
4770 @itemize *
4772 @item 
4774 @table @asis
4776 @item @emph{switches}
4778 is an optional sequence of switches as described in the next section.
4779 @end table
4781 @item 
4783 @table @asis
4785 @item @emph{infile}
4787 is the full name of the input file, which is an Ada source
4788 file containing preprocessor directives.
4789 @end table
4791 @item 
4793 @table @asis
4795 @item @emph{outfile}
4797 is the full name of the output file, which is an Ada source
4798 in standard Ada form. When used with GNAT, this file name will
4799 normally have an ads or adb suffix.
4800 @end table
4802 @item 
4804 @table @asis
4806 @item @emph{deffile}
4808 is the full name of a text file containing definitions of
4809 preprocessing symbols to be referenced by the preprocessor. This argument is
4810 optional, and can be replaced by the use of the @emph{-D} switch.
4811 @end table
4812 @end itemize
4814 @node Switches for gnatprep,Form of Definitions File,Using gnatprep,Preprocessing with gnatprep
4815 @anchor{gnat_ugn/the_gnat_compilation_model switches-for-gnatprep}@anchor{b0}@anchor{gnat_ugn/the_gnat_compilation_model id57}@anchor{b1}
4816 @subsubsection Switches for @cite{gnatprep}
4819 @geindex -b (gnatprep)
4822 @table @asis
4824 @item @code{-b}
4826 Causes both preprocessor lines and the lines deleted by
4827 preprocessing to be replaced by blank lines in the output source file,
4828 preserving line numbers in the output file.
4829 @end table
4831 @geindex -c (gnatprep)
4834 @table @asis
4836 @item @code{-c}
4838 Causes both preprocessor lines and the lines deleted
4839 by preprocessing to be retained in the output source as comments marked
4840 with the special string @cite{"--! "}. This option will result in line numbers
4841 being preserved in the output file.
4842 @end table
4844 @geindex -C (gnatprep)
4847 @table @asis
4849 @item @code{-C}
4851 Causes comments to be scanned. Normally comments are ignored by gnatprep.
4852 If this option is specified, then comments are scanned and any $symbol
4853 substitutions performed as in program text. This is particularly useful
4854 when structured comments are used (e.g., when writing programs in the
4855 SPARK dialect of Ada). Note that this switch is not available when
4856 doing integrated preprocessing (it would be useless in this context
4857 since comments are ignored by the compiler in any case).
4858 @end table
4860 @geindex -D (gnatprep)
4863 @table @asis
4865 @item @code{-D@emph{symbol}=@emph{value}}
4867 Defines a new preprocessing symbol, associated with value. If no value is given
4868 on the command line, then symbol is considered to be @cite{True}. This switch
4869 can be used in place of a definition file.
4870 @end table
4872 @geindex -r (gnatprep)
4875 @table @asis
4877 @item @code{-r}
4879 Causes a @cite{Source_Reference} pragma to be generated that
4880 references the original input file, so that error messages will use
4881 the file name of this original file. The use of this switch implies
4882 that preprocessor lines are not to be removed from the file, so its
4883 use will force @emph{-b} mode if @emph{-c}
4884 has not been specified explicitly.
4886 Note that if the file to be preprocessed contains multiple units, then
4887 it will be necessary to @cite{gnatchop} the output file from
4888 @cite{gnatprep}. If a @cite{Source_Reference} pragma is present
4889 in the preprocessed file, it will be respected by
4890 @cite{gnatchop -r}
4891 so that the final chopped files will correctly refer to the original
4892 input source file for @cite{gnatprep}.
4893 @end table
4895 @geindex -s (gnatprep)
4898 @table @asis
4900 @item @code{-s}
4902 Causes a sorted list of symbol names and values to be
4903 listed on the standard output file.
4904 @end table
4906 @geindex -u (gnatprep)
4909 @table @asis
4911 @item @code{-u}
4913 Causes undefined symbols to be treated as having the value FALSE in the context
4914 of a preprocessor test. In the absence of this option, an undefined symbol in
4915 a @cite{#if} or @cite{#elsif} test will be treated as an error.
4916 @end table
4918 Note: if neither @emph{-b} nor @emph{-c} is present,
4919 then preprocessor lines and
4920 deleted lines are completely removed from the output, unless -r is
4921 specified, in which case -b is assumed.
4923 @node Form of Definitions File,Form of Input Text for gnatprep,Switches for gnatprep,Preprocessing with gnatprep
4924 @anchor{gnat_ugn/the_gnat_compilation_model form-of-definitions-file}@anchor{b2}@anchor{gnat_ugn/the_gnat_compilation_model id58}@anchor{b3}
4925 @subsubsection Form of Definitions File
4928 The definitions file contains lines of the form:
4930 @example
4931 symbol := value
4932 @end example
4934 where @cite{symbol} is a preprocessing symbol, and @cite{value} is one of the following:
4937 @itemize *
4939 @item 
4940 Empty, corresponding to a null substitution,
4942 @item 
4943 A string literal using normal Ada syntax, or
4945 @item 
4946 Any sequence of characters from the set @{letters, digits, period, underline@}.
4947 @end itemize
4949 Comment lines may also appear in the definitions file, starting with
4950 the usual @code{--},
4951 and comments may be added to the definitions lines.
4953 @node Form of Input Text for gnatprep,,Form of Definitions File,Preprocessing with gnatprep
4954 @anchor{gnat_ugn/the_gnat_compilation_model id59}@anchor{b4}@anchor{gnat_ugn/the_gnat_compilation_model form-of-input-text-for-gnatprep}@anchor{b5}
4955 @subsubsection Form of Input Text for @cite{gnatprep}
4958 The input text may contain preprocessor conditional inclusion lines,
4959 as well as general symbol substitution sequences.
4961 The preprocessor conditional inclusion commands have the form:
4963 @example
4964 #if <expression> [then]
4965    lines
4966 #elsif <expression> [then]
4967    lines
4968 #elsif <expression> [then]
4969    lines
4971 #else
4972    lines
4973 #end if;
4974 @end example
4976 In this example, <expression> is defined by the following grammar:
4978 @example
4979 <expression> ::=  <symbol>
4980 <expression> ::=  <symbol> = "<value>"
4981 <expression> ::=  <symbol> = <symbol>
4982 <expression> ::=  <symbol> = <integer>
4983 <expression> ::=  <symbol> > <integer>
4984 <expression> ::=  <symbol> >= <integer>
4985 <expression> ::=  <symbol> < <integer>
4986 <expression> ::=  <symbol> <= <integer>
4987 <expression> ::=  <symbol> 'Defined
4988 <expression> ::=  not <expression>
4989 <expression> ::=  <expression> and <expression>
4990 <expression> ::=  <expression> or <expression>
4991 <expression> ::=  <expression> and then <expression>
4992 <expression> ::=  <expression> or else <expression>
4993 <expression> ::=  ( <expression> )
4994 @end example
4996 Note the following restriction: it is not allowed to have "and" or "or"
4997 following "not" in the same expression without parentheses. For example, this
4998 is not allowed:
5000 @example
5001 not X or Y
5002 @end example
5004 This can be expressed instead as one of the following forms:
5006 @example
5007 (not X) or Y
5008 not (X or Y)
5009 @end example
5011 For the first test (<expression> ::= <symbol>) the symbol must have
5012 either the value true or false, that is to say the right-hand of the
5013 symbol definition must be one of the (case-insensitive) literals
5014 @cite{True} or @cite{False}. If the value is true, then the
5015 corresponding lines are included, and if the value is false, they are
5016 excluded.
5018 When comparing a symbol to an integer, the integer is any non negative
5019 literal integer as defined in the Ada Reference Manual, such as 3, 16#FF# or
5020 2#11#. The symbol value must also be a non negative integer. Integer values
5021 in the range 0 .. 2**31-1 are supported.
5023 The test (<expression> ::= <symbol>'Defined) is true only if
5024 the symbol has been defined in the definition file or by a @emph{-D}
5025 switch on the command line. Otherwise, the test is false.
5027 The equality tests are case insensitive, as are all the preprocessor lines.
5029 If the symbol referenced is not defined in the symbol definitions file,
5030 then the effect depends on whether or not switch @emph{-u}
5031 is specified. If so, then the symbol is treated as if it had the value
5032 false and the test fails. If this switch is not specified, then
5033 it is an error to reference an undefined symbol. It is also an error to
5034 reference a symbol that is defined with a value other than @cite{True}
5035 or @cite{False}.
5037 The use of the @cite{not} operator inverts the sense of this logical test.
5038 The @cite{not} operator cannot be combined with the @cite{or} or @cite{and}
5039 operators, without parentheses. For example, "if not X or Y then" is not
5040 allowed, but "if (not X) or Y then" and "if not (X or Y) then" are.
5042 The @cite{then} keyword is optional as shown
5044 The @cite{#} must be the first non-blank character on a line, but
5045 otherwise the format is free form. Spaces or tabs may appear between
5046 the @cite{#} and the keyword. The keywords and the symbols are case
5047 insensitive as in normal Ada code. Comments may be used on a
5048 preprocessor line, but other than that, no other tokens may appear on a
5049 preprocessor line. Any number of @cite{elsif} clauses can be present,
5050 including none at all. The @cite{else} is optional, as in Ada.
5052 The @cite{#} marking the start of a preprocessor line must be the first
5053 non-blank character on the line, i.e., it must be preceded only by
5054 spaces or horizontal tabs.
5056 Symbol substitution outside of preprocessor lines is obtained by using
5057 the sequence:
5059 @example
5060 $symbol
5061 @end example
5063 anywhere within a source line, except in a comment or within a
5064 string literal. The identifier
5065 following the @cite{$} must match one of the symbols defined in the symbol
5066 definition file, and the result is to substitute the value of the
5067 symbol in place of @cite{$symbol} in the output file.
5069 Note that although the substitution of strings within a string literal
5070 is not possible, it is possible to have a symbol whose defined value is
5071 a string literal. So instead of setting XYZ to @cite{hello} and writing:
5073 @example
5074 Header : String := "$XYZ";
5075 @end example
5077 you should set XYZ to @cite{"hello"} and write:
5079 @example
5080 Header : String := $XYZ;
5081 @end example
5083 and then the substitution will occur as desired.
5085 @node Integrated Preprocessing,,Preprocessing with gnatprep,Conditional Compilation
5086 @anchor{gnat_ugn/the_gnat_compilation_model id60}@anchor{b6}@anchor{gnat_ugn/the_gnat_compilation_model integrated-preprocessing}@anchor{1a}
5087 @subsection Integrated Preprocessing
5090 GNAT sources may be preprocessed immediately before compilation.
5091 In this case, the actual
5092 text of the source is not the text of the source file, but is derived from it
5093 through a process called preprocessing. Integrated preprocessing is specified
5094 through switches @emph{-gnatep} and/or @emph{-gnateD}. @emph{-gnatep}
5095 indicates, through a text file, the preprocessing data to be used.
5096 @code{-gnateD} specifies or modifies the values of preprocessing symbol.
5097 Note that integrated preprocessing applies only to Ada source files, it is
5098 not available for configuration pragma files.
5100 Note that when integrated preprocessing is used, the output from the
5101 preprocessor is not written to any external file. Instead it is passed
5102 internally to the compiler. If you need to preserve the result of
5103 preprocessing in a file, then you should use @emph{gnatprep}
5104 to perform the desired preprocessing in stand-alone mode.
5106 It is recommended that @emph{gnatmake} switch -s should be
5107 used when Integrated Preprocessing is used. The reason is that preprocessing
5108 with another Preprocessing Data file without changing the sources will
5109 not trigger recompilation without this switch.
5111 Note that @emph{gnatmake} switch -m will almost
5112 always trigger recompilation for sources that are preprocessed,
5113 because @emph{gnatmake} cannot compute the checksum of the source after
5114 preprocessing.
5116 The actual preprocessing function is described in detail in section
5117 @ref{19,,Preprocessing with gnatprep}. This section only describes how integrated
5118 preprocessing is triggered and parameterized.
5120 @geindex -gnatep (gcc)
5123 @table @asis
5125 @item @code{-gnatep=@emph{file}}
5127 This switch indicates to the compiler the file name (without directory
5128 information) of the preprocessor data file to use. The preprocessor data file
5129 should be found in the source directories. Note that when the compiler is
5130 called by a builder such as (@emph{gnatmake} with a project
5131 file, if the object directory is not also a source directory, the builder needs
5132 to be called with @emph{-x}.
5134 A preprocessing data file is a text file with significant lines indicating
5135 how should be preprocessed either a specific source or all sources not
5136 mentioned in other lines. A significant line is a nonempty, non-comment line.
5137 Comments are similar to Ada comments.
5139 Each significant line starts with either a literal string or the character '*'.
5140 A literal string is the file name (without directory information) of the source
5141 to preprocess. A character '*' indicates the preprocessing for all the sources
5142 that are not specified explicitly on other lines (order of the lines is not
5143 significant). It is an error to have two lines with the same file name or two
5144 lines starting with the character '*'.
5146 After the file name or the character '*', another optional literal string
5147 indicating the file name of the definition file to be used for preprocessing
5148 (@ref{b2,,Form of Definitions File}). The definition files are found by the
5149 compiler in one of the source directories. In some cases, when compiling
5150 a source in a directory other than the current directory, if the definition
5151 file is in the current directory, it may be necessary to add the current
5152 directory as a source directory through switch -I., otherwise
5153 the compiler would not find the definition file.
5155 Then, optionally, switches similar to those of @cite{gnatprep} may
5156 be found. Those switches are:
5159 @table @asis
5161 @item @code{-b}
5163 Causes both preprocessor lines and the lines deleted by
5164 preprocessing to be replaced by blank lines, preserving the line number.
5165 This switch is always implied; however, if specified after @emph{-c}
5166 it cancels the effect of @emph{-c}.
5168 @item @code{-c}
5170 Causes both preprocessor lines and the lines deleted
5171 by preprocessing to be retained as comments marked
5172 with the special string '@cite{--!}'.
5174 @item @code{-Dsymbol=@emph{value}}
5176 Define or redefine a symbol, associated with value. A symbol is an Ada
5177 identifier, or an Ada reserved word, with the exception of @cite{if},
5178 @cite{else}, @cite{elsif}, @cite{end}, @cite{and}, @cite{or} and @cite{then}.
5179 @cite{value} is either a literal string, an Ada identifier or any Ada reserved
5180 word. A symbol declared with this switch replaces a symbol with the
5181 same name defined in a definition file.
5183 @item @code{-s}
5185 Causes a sorted list of symbol names and values to be
5186 listed on the standard output file.
5188 @item @code{-u}
5190 Causes undefined symbols to be treated as having the value @cite{FALSE}
5191 in the context
5192 of a preprocessor test. In the absence of this option, an undefined symbol in
5193 a @cite{#if} or @cite{#elsif} test will be treated as an error.
5194 @end table
5196 Examples of valid lines in a preprocessor data file:
5198 @example
5199 "toto.adb"  "prep.def" -u
5200 --  preprocess "toto.adb", using definition file "prep.def",
5201 --  undefined symbol are False.
5203 * -c -DVERSION=V101
5204 --  preprocess all other sources without a definition file;
5205 --  suppressed lined are commented; symbol VERSION has the value V101.
5207 "titi.adb" "prep2.def" -s
5208 --  preprocess "titi.adb", using definition file "prep2.def";
5209 --  list all symbols with their values.
5210 @end example
5211 @end table
5213 @geindex -gnateD (gcc)
5216 @table @asis
5218 @item @code{-gnateDsymbol[=value]}
5220 Define or redefine a preprocessing symbol, associated with value. If no value
5221 is given on the command line, then the value of the symbol is @cite{True}.
5222 A symbol is an identifier, following normal Ada (case-insensitive)
5223 rules for its syntax, and value is either an arbitrary string between double
5224 quotes or any sequence (including an empty sequence) of characters from the
5225 set (letters, digits, period, underline).
5226 Ada reserved words may be used as symbols, with the exceptions of @cite{if},
5227 @cite{else}, @cite{elsif}, @cite{end}, @cite{and}, @cite{or} and @cite{then}.
5229 Examples:
5231 @example
5232 -gnateDToto=Titi
5233 -gnateDFoo
5234 -gnateDFoo=\"Foo-Bar\"
5235 @end example
5237 A symbol declared with this switch on the command line replaces a
5238 symbol with the same name either in a definition file or specified with a
5239 switch -D in the preprocessor data file.
5241 This switch is similar to switch @emph{-D} of @cite{gnatprep}.
5243 @item @code{-gnateG}
5245 When integrated preprocessing is performed and the preprocessor modifies
5246 the source text, write the result of this preprocessing into a file
5247 <source>.prep.
5248 @end table
5250 @node Mixed Language Programming,GNAT and Other Compilation Models,Conditional Compilation,The GNAT Compilation Model
5251 @anchor{gnat_ugn/the_gnat_compilation_model mixed-language-programming}@anchor{46}@anchor{gnat_ugn/the_gnat_compilation_model id61}@anchor{b7}
5252 @section Mixed Language Programming
5255 @geindex Mixed Language Programming
5257 This section describes how to develop a mixed-language program,
5258 with a focus on combining Ada with C or C++.
5260 @menu
5261 * Interfacing to C:: 
5262 * Calling Conventions:: 
5263 * Building Mixed Ada and C++ Programs:: 
5264 * Generating Ada Bindings for C and C++ headers:: 
5266 @end menu
5268 @node Interfacing to C,Calling Conventions,,Mixed Language Programming
5269 @anchor{gnat_ugn/the_gnat_compilation_model interfacing-to-c}@anchor{b8}@anchor{gnat_ugn/the_gnat_compilation_model id62}@anchor{b9}
5270 @subsection Interfacing to C
5273 Interfacing Ada with a foreign language such as C involves using
5274 compiler directives to import and/or export entity definitions in each
5275 language -- using @cite{extern} statements in C, for instance, and the
5276 @cite{Import}, @cite{Export}, and @cite{Convention} pragmas in Ada.
5277 A full treatment of these topics is provided in Appendix B, section 1
5278 of the Ada Reference Manual.
5280 There are two ways to build a program using GNAT that contains some Ada
5281 sources and some foreign language sources, depending on whether or not
5282 the main subprogram is written in Ada.  Here is a source example with
5283 the main subprogram in Ada:
5285 @example
5286 /* file1.c */
5287 #include <stdio.h>
5289 void print_num (int num)
5291   printf ("num is %d.\\n", num);
5292   return;
5294 @end example
5296 @example
5297 /* file2.c */
5299 /* num_from_Ada is declared in my_main.adb */
5300 extern int num_from_Ada;
5302 int get_num (void)
5304   return num_from_Ada;
5306 @end example
5308 @example
5309 --  my_main.adb
5310 procedure My_Main is
5312    --  Declare then export an Integer entity called num_from_Ada
5313    My_Num : Integer := 10;
5314    pragma Export (C, My_Num, "num_from_Ada");
5316    --  Declare an Ada function spec for Get_Num, then use
5317    --  C function get_num for the implementation.
5318    function Get_Num return Integer;
5319    pragma Import (C, Get_Num, "get_num");
5321    --  Declare an Ada procedure spec for Print_Num, then use
5322    --  C function print_num for the implementation.
5323    procedure Print_Num (Num : Integer);
5324    pragma Import (C, Print_Num, "print_num";
5326 begin
5327    Print_Num (Get_Num);
5328 end My_Main;
5329 @end example
5331 To build this example:
5334 @itemize *
5336 @item 
5337 First compile the foreign language files to
5338 generate object files:
5340 @example
5341 $ gcc -c file1.c
5342 $ gcc -c file2.c
5343 @end example
5345 @item 
5346 Then, compile the Ada units to produce a set of object files and ALI
5347 files:
5349 @example
5350 $ gnatmake -c my_main.adb
5351 @end example
5353 @item 
5354 Run the Ada binder on the Ada main program:
5356 @example
5357 $ gnatbind my_main.ali
5358 @end example
5360 @item 
5361 Link the Ada main program, the Ada objects and the other language
5362 objects:
5364 @example
5365 $ gnatlink my_main.ali file1.o file2.o
5366 @end example
5367 @end itemize
5369 The last three steps can be grouped in a single command:
5371 @example
5372 $ gnatmake my_main.adb -largs file1.o file2.o
5373 @end example
5375 @geindex Binder output file
5377 If the main program is in a language other than Ada, then you may have
5378 more than one entry point into the Ada subsystem. You must use a special
5379 binder option to generate callable routines that initialize and
5380 finalize the Ada units (@ref{ba,,Binding with Non-Ada Main Programs}).
5381 Calls to the initialization and finalization routines must be inserted
5382 in the main program, or some other appropriate point in the code. The
5383 call to initialize the Ada units must occur before the first Ada
5384 subprogram is called, and the call to finalize the Ada units must occur
5385 after the last Ada subprogram returns. The binder will place the
5386 initialization and finalization subprograms into the
5387 @code{b~xxx.adb} file where they can be accessed by your C
5388 sources.  To illustrate, we have the following example:
5390 @example
5391 /* main.c */
5392 extern void adainit (void);
5393 extern void adafinal (void);
5394 extern int add (int, int);
5395 extern int sub (int, int);
5397 int main (int argc, char *argv[])
5399    int a = 21, b = 7;
5401    adainit();
5403    /* Should print "21 + 7 = 28" */
5404    printf ("%d + %d = %d\\n", a, b, add (a, b));
5406    /* Should print "21 - 7 = 14" */
5407    printf ("%d - %d = %d\\n", a, b, sub (a, b));
5409    adafinal();
5411 @end example
5413 @example
5414 --  unit1.ads
5415 package Unit1 is
5416    function Add (A, B : Integer) return Integer;
5417    pragma Export (C, Add, "add");
5418 end Unit1;
5419 @end example
5421 @example
5422 --  unit1.adb
5423 package body Unit1 is
5424    function Add (A, B : Integer) return Integer is
5425    begin
5426       return A + B;
5427    end Add;
5428 end Unit1;
5429 @end example
5431 @example
5432 --  unit2.ads
5433 package Unit2 is
5434    function Sub (A, B : Integer) return Integer;
5435    pragma Export (C, Sub, "sub");
5436 end Unit2;
5437 @end example
5439 @example
5440 --  unit2.adb
5441 package body Unit2 is
5442    function Sub (A, B : Integer) return Integer is
5443    begin
5444       return A - B;
5445    end Sub;
5446 end Unit2;
5447 @end example
5449 The build procedure for this application is similar to the last
5450 example's:
5453 @itemize *
5455 @item 
5456 First, compile the foreign language files to generate object files:
5458 @example
5459 $ gcc -c main.c
5460 @end example
5462 @item 
5463 Next, compile the Ada units to produce a set of object files and ALI
5464 files:
5466 @example
5467 $ gnatmake -c unit1.adb
5468 $ gnatmake -c unit2.adb
5469 @end example
5471 @item 
5472 Run the Ada binder on every generated ALI file.  Make sure to use the
5473 @code{-n} option to specify a foreign main program:
5475 @example
5476 $ gnatbind -n unit1.ali unit2.ali
5477 @end example
5479 @item 
5480 Link the Ada main program, the Ada objects and the foreign language
5481 objects. You need only list the last ALI file here:
5483 @example
5484 $ gnatlink unit2.ali main.o -o exec_file
5485 @end example
5487 This procedure yields a binary executable called @code{exec_file}.
5488 @end itemize
5490 Depending on the circumstances (for example when your non-Ada main object
5491 does not provide symbol @cite{main}), you may also need to instruct the
5492 GNAT linker not to include the standard startup objects by passing the
5493 @code{-nostartfiles} switch to @cite{gnatlink}.
5495 @node Calling Conventions,Building Mixed Ada and C++ Programs,Interfacing to C,Mixed Language Programming
5496 @anchor{gnat_ugn/the_gnat_compilation_model calling-conventions}@anchor{bb}@anchor{gnat_ugn/the_gnat_compilation_model id63}@anchor{bc}
5497 @subsection Calling Conventions
5500 @geindex Foreign Languages
5502 @geindex Calling Conventions
5504 GNAT follows standard calling sequence conventions and will thus interface
5505 to any other language that also follows these conventions. The following
5506 Convention identifiers are recognized by GNAT:
5508 @geindex Interfacing to Ada
5510 @geindex Other Ada compilers
5512 @geindex Convention Ada
5515 @table @asis
5517 @item @emph{Ada}
5519 This indicates that the standard Ada calling sequence will be
5520 used and all Ada data items may be passed without any limitations in the
5521 case where GNAT is used to generate both the caller and callee. It is also
5522 possible to mix GNAT generated code and code generated by another Ada
5523 compiler. In this case, the data types should be restricted to simple
5524 cases, including primitive types. Whether complex data types can be passed
5525 depends on the situation. Probably it is safe to pass simple arrays, such
5526 as arrays of integers or floats. Records may or may not work, depending
5527 on whether both compilers lay them out identically. Complex structures
5528 involving variant records, access parameters, tasks, or protected types,
5529 are unlikely to be able to be passed.
5531 Note that in the case of GNAT running
5532 on a platform that supports HP Ada 83, a higher degree of compatibility
5533 can be guaranteed, and in particular records are laid out in an identical
5534 manner in the two compilers. Note also that if output from two different
5535 compilers is mixed, the program is responsible for dealing with elaboration
5536 issues. Probably the safest approach is to write the main program in the
5537 version of Ada other than GNAT, so that it takes care of its own elaboration
5538 requirements, and then call the GNAT-generated adainit procedure to ensure
5539 elaboration of the GNAT components. Consult the documentation of the other
5540 Ada compiler for further details on elaboration.
5542 However, it is not possible to mix the tasking run time of GNAT and
5543 HP Ada 83, All the tasking operations must either be entirely within
5544 GNAT compiled sections of the program, or entirely within HP Ada 83
5545 compiled sections of the program.
5546 @end table
5548 @geindex Interfacing to Assembly
5550 @geindex Convention Assembler
5553 @table @asis
5555 @item @emph{Assembler}
5557 Specifies assembler as the convention. In practice this has the
5558 same effect as convention Ada (but is not equivalent in the sense of being
5559 considered the same convention).
5560 @end table
5562 @geindex Convention Asm
5564 @geindex Asm
5567 @table @asis
5569 @item @emph{Asm}
5571 Equivalent to Assembler.
5573 @geindex Interfacing to COBOL
5575 @geindex Convention COBOL
5576 @end table
5578 @geindex COBOL
5581 @table @asis
5583 @item @emph{COBOL}
5585 Data will be passed according to the conventions described
5586 in section B.4 of the Ada Reference Manual.
5587 @end table
5589 @geindex C
5591 @geindex Interfacing to C
5593 @geindex Convention C
5596 @table @asis
5598 @item @emph{C}
5600 Data will be passed according to the conventions described
5601 in section B.3 of the Ada Reference Manual.
5603 A note on interfacing to a C 'varargs' function:
5605 @quotation
5607 @geindex C varargs function
5609 @geindex Interfacing to C varargs function
5611 @geindex varargs function interfaces
5613 In C, @cite{varargs} allows a function to take a variable number of
5614 arguments. There is no direct equivalent in this to Ada. One
5615 approach that can be used is to create a C wrapper for each
5616 different profile and then interface to this C wrapper. For
5617 example, to print an @cite{int} value using @cite{printf},
5618 create a C function @cite{printfi} that takes two arguments, a
5619 pointer to a string and an int, and calls @cite{printf}.
5620 Then in the Ada program, use pragma @cite{Import} to
5621 interface to @cite{printfi}.
5623 It may work on some platforms to directly interface to
5624 a @cite{varargs} function by providing a specific Ada profile
5625 for a particular call. However, this does not work on
5626 all platforms, since there is no guarantee that the
5627 calling sequence for a two argument normal C function
5628 is the same as for calling a @cite{varargs} C function with
5629 the same two arguments.
5630 @end quotation
5631 @end table
5633 @geindex Convention Default
5635 @geindex Default
5638 @table @asis
5640 @item @emph{Default}
5642 Equivalent to C.
5643 @end table
5645 @geindex Convention External
5647 @geindex External
5650 @table @asis
5652 @item @emph{External}
5654 Equivalent to C.
5655 @end table
5657 @geindex C++
5659 @geindex Interfacing to C++
5661 @geindex Convention C++
5664 @table @asis
5666 @item @emph{C_Plus_Plus (or CPP)}
5668 This stands for C++. For most purposes this is identical to C.
5669 See the separate description of the specialized GNAT pragmas relating to
5670 C++ interfacing for further details.
5671 @end table
5673 @geindex Fortran
5675 @geindex Interfacing to Fortran
5677 @geindex Convention Fortran
5680 @table @asis
5682 @item @emph{Fortran}
5684 Data will be passed according to the conventions described
5685 in section B.5 of the Ada Reference Manual.
5687 @item @emph{Intrinsic}
5689 This applies to an intrinsic operation, as defined in the Ada
5690 Reference Manual. If a pragma Import (Intrinsic) applies to a subprogram,
5691 this means that the body of the subprogram is provided by the compiler itself,
5692 usually by means of an efficient code sequence, and that the user does not
5693 supply an explicit body for it. In an application program, the pragma may
5694 be applied to the following sets of names:
5697 @itemize *
5699 @item 
5700 Rotate_Left, Rotate_Right, Shift_Left, Shift_Right, Shift_Right_Arithmetic.
5701 The corresponding subprogram declaration must have
5702 two formal parameters. The
5703 first one must be a signed integer type or a modular type with a binary
5704 modulus, and the second parameter must be of type Natural.
5705 The return type must be the same as the type of the first argument. The size
5706 of this type can only be 8, 16, 32, or 64.
5708 @item 
5709 Binary arithmetic operators: '+', '-', '*', '/'.
5710 The corresponding operator declaration must have parameters and result type
5711 that have the same root numeric type (for example, all three are long_float
5712 types). This simplifies the definition of operations that use type checking
5713 to perform dimensional checks:
5714 @end itemize
5716 @c code-block: ada
5717 @c 
5718 @c   type Distance is new Long_Float;
5719 @c   type Time     is new Long_Float;
5720 @c   type Velocity is new Long_Float;
5721 @c   function "/" (D : Distance; T : Time)
5722 @c     return Velocity;
5723 @c   pragma Import (Intrinsic, "/");
5724 @c 
5725 @c This common idiom is often programmed with a generic definition and an
5726 @c explicit body. The pragma makes it simpler to introduce such declarations.
5727 @c It incurs no overhead in compilation time or code size, because it is
5728 @c implemented as a single machine instruction.
5731 @itemize *
5733 @item 
5734 General subprogram entities. This is used  to bind an Ada subprogram
5735 declaration to
5736 a compiler builtin by name with back-ends where such interfaces are
5737 available. A typical example is the set of @cite{__builtin} functions
5738 exposed by the GCC back-end, as in the following example:
5740 @example
5741 function builtin_sqrt (F : Float) return Float;
5742 pragma Import (Intrinsic, builtin_sqrt, "__builtin_sqrtf");
5743 @end example
5745 Most of the GCC builtins are accessible this way, and as for other
5746 import conventions (e.g. C), it is the user's responsibility to ensure
5747 that the Ada subprogram profile matches the underlying builtin
5748 expectations.
5749 @end itemize
5750 @end table
5752 @geindex Stdcall
5754 @geindex Convention Stdcall
5757 @table @asis
5759 @item @emph{Stdcall}
5761 This is relevant only to Windows XP/2000/NT implementations of GNAT,
5762 and specifies that the @cite{Stdcall} calling sequence will be used,
5763 as defined by the NT API. Nevertheless, to ease building
5764 cross-platform bindings this convention will be handled as a @cite{C} calling
5765 convention on non-Windows platforms.
5766 @end table
5768 @geindex DLL
5770 @geindex Convention DLL
5773 @table @asis
5775 @item @emph{DLL}
5777 This is equivalent to @cite{Stdcall}.
5778 @end table
5780 @geindex Win32
5782 @geindex Convention Win32
5785 @table @asis
5787 @item @emph{Win32}
5789 This is equivalent to @cite{Stdcall}.
5790 @end table
5792 @geindex Stubbed
5794 @geindex Convention Stubbed
5797 @table @asis
5799 @item @emph{Stubbed}
5801 This is a special convention that indicates that the compiler
5802 should provide a stub body that raises @cite{Program_Error}.
5803 @end table
5805 GNAT additionally provides a useful pragma @cite{Convention_Identifier}
5806 that can be used to parameterize conventions and allow additional synonyms
5807 to be specified. For example if you have legacy code in which the convention
5808 identifier Fortran77 was used for Fortran, you can use the configuration
5809 pragma:
5811 @example
5812 pragma Convention_Identifier (Fortran77, Fortran);
5813 @end example
5815 And from now on the identifier Fortran77 may be used as a convention
5816 identifier (for example in an @cite{Import} pragma) with the same
5817 meaning as Fortran.
5819 @node Building Mixed Ada and C++ Programs,Generating Ada Bindings for C and C++ headers,Calling Conventions,Mixed Language Programming
5820 @anchor{gnat_ugn/the_gnat_compilation_model id64}@anchor{bd}@anchor{gnat_ugn/the_gnat_compilation_model building-mixed-ada-and-c-programs}@anchor{be}
5821 @subsection Building Mixed Ada and C++ Programs
5824 A programmer inexperienced with mixed-language development may find that
5825 building an application containing both Ada and C++ code can be a
5826 challenge.  This section gives a few hints that should make this task easier.
5828 @menu
5829 * Interfacing to C++:: 
5830 * Linking a Mixed C++ & Ada Program:: 
5831 * A Simple Example:: 
5832 * Interfacing with C++ constructors:: 
5833 * Interfacing with C++ at the Class Level:: 
5835 @end menu
5837 @node Interfacing to C++,Linking a Mixed C++ & Ada Program,,Building Mixed Ada and C++ Programs
5838 @anchor{gnat_ugn/the_gnat_compilation_model id65}@anchor{bf}@anchor{gnat_ugn/the_gnat_compilation_model id66}@anchor{c0}
5839 @subsubsection Interfacing to C++
5842 GNAT supports interfacing with the G++ compiler (or any C++ compiler
5843 generating code that is compatible with the G++ Application Binary
5844 Interface ---see @indicateurl{http://www.codesourcery.com/archives/cxx-abi}).
5846 Interfacing can be done at 3 levels: simple data, subprograms, and
5847 classes. In the first two cases, GNAT offers a specific @cite{Convention C_Plus_Plus}
5848 (or @cite{CPP}) that behaves exactly like @cite{Convention C}.
5849 Usually, C++ mangles the names of subprograms. To generate proper mangled
5850 names automatically, see @ref{1b,,Generating Ada Bindings for C and C++ headers}).
5851 This problem can also be addressed manually in two ways:
5854 @itemize *
5856 @item 
5857 by modifying the C++ code in order to force a C convention using
5858 the @cite{extern "C"} syntax.
5860 @item 
5861 by figuring out the mangled name (using e.g. @emph{nm}) and using it as the
5862 Link_Name argument of the pragma import.
5863 @end itemize
5865 Interfacing at the class level can be achieved by using the GNAT specific
5866 pragmas such as @cite{CPP_Constructor}.  See the @cite{GNAT_Reference_Manual} for additional information.
5868 @node Linking a Mixed C++ & Ada Program,A Simple Example,Interfacing to C++,Building Mixed Ada and C++ Programs
5869 @anchor{gnat_ugn/the_gnat_compilation_model linking-a-mixed-c-ada-program}@anchor{c1}@anchor{gnat_ugn/the_gnat_compilation_model linking-a-mixed-c-and-ada-program}@anchor{c2}
5870 @subsubsection Linking a Mixed C++ & Ada Program
5873 Usually the linker of the C++ development system must be used to link
5874 mixed applications because most C++ systems will resolve elaboration
5875 issues (such as calling constructors on global class instances)
5876 transparently during the link phase. GNAT has been adapted to ease the
5877 use of a foreign linker for the last phase. Three cases can be
5878 considered:
5881 @itemize *
5883 @item 
5884 Using GNAT and G++ (GNU C++ compiler) from the same GCC installation:
5885 The C++ linker can simply be called by using the C++ specific driver
5886 called @cite{g++}.
5888 Note that if the C++ code uses inline functions, you will need to
5889 compile your C++ code with the @cite{-fkeep-inline-functions} switch in
5890 order to provide an existing function implementation that the Ada code can
5891 link with.
5893 @example
5894 $ g++ -c -fkeep-inline-functions file1.C
5895 $ g++ -c -fkeep-inline-functions file2.C
5896 $ gnatmake ada_unit -largs file1.o file2.o --LINK=g++
5897 @end example
5899 @item 
5900 Using GNAT and G++ from two different GCC installations: If both
5901 compilers are on the :envvar`PATH`, the previous method may be used. It is
5902 important to note that environment variables such as
5903 @geindex C_INCLUDE_PATH
5904 @geindex environment variable; C_INCLUDE_PATH
5905 @code{C_INCLUDE_PATH}, 
5906 @geindex GCC_EXEC_PREFIX
5907 @geindex environment variable; GCC_EXEC_PREFIX
5908 @code{GCC_EXEC_PREFIX},
5909 @geindex BINUTILS_ROOT
5910 @geindex environment variable; BINUTILS_ROOT
5911 @code{BINUTILS_ROOT}, and
5912 @geindex GCC_ROOT
5913 @geindex environment variable; GCC_ROOT
5914 @code{GCC_ROOT} will affect both compilers
5915 at the same time and may make one of the two compilers operate
5916 improperly if set during invocation of the wrong compiler.  It is also
5917 very important that the linker uses the proper @code{libgcc.a} GCC
5918 library -- that is, the one from the C++ compiler installation. The
5919 implicit link command as suggested in the @cite{gnatmake} command
5920 from the former example can be replaced by an explicit link command with
5921 the full-verbosity option in order to verify which library is used:
5923 @example
5924 $ gnatbind ada_unit
5925 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=c++
5926 @end example
5928 If there is a problem due to interfering environment variables, it can
5929 be worked around by using an intermediate script. The following example
5930 shows the proper script to use when GNAT has not been installed at its
5931 default location and g++ has been installed at its default location:
5933 @example
5934 $ cat ./my_script
5935 #!/bin/sh
5936 unset BINUTILS_ROOT
5937 unset GCC_ROOT
5938 c++ $*
5939 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=./my_script
5940 @end example
5942 @item 
5943 Using a non-GNU C++ compiler: The commands previously described can be
5944 used to insure that the C++ linker is used. Nonetheless, you need to add
5945 a few more parameters to the link command line, depending on the exception
5946 mechanism used.
5948 If the @cite{setjmp/longjmp} exception mechanism is used, only the paths
5949 to the libgcc libraries are required:
5951 @example
5952 $ cat ./my_script
5953 #!/bin/sh
5954 CC $* `gcc -print-file-name=libgcc.a` `gcc -print-file-name=libgcc_eh.a`
5955 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
5956 @end example
5958 where CC is the name of the non-GNU C++ compiler.
5960 If the @cite{zero cost} exception mechanism is used, and the platform
5961 supports automatic registration of exception tables (e.g., Solaris),
5962 paths to more objects are required:
5964 @example
5965 $ cat ./my_script
5966 #!/bin/sh
5967 CC `gcc -print-file-name=crtbegin.o` $* \\
5968 `gcc -print-file-name=libgcc.a` `gcc -print-file-name=libgcc_eh.a` \\
5969 `gcc -print-file-name=crtend.o`
5970 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
5971 @end example
5973 If the "zero cost exception" mechanism is used, and the platform
5974 doesn't support automatic registration of exception tables (e.g., HP-UX
5975 or AIX), the simple approach described above will not work and
5976 a pre-linking phase using GNAT will be necessary.
5977 @end itemize
5979 Another alternative is to use the @code{gprbuild} multi-language builder
5980 which has a large knowledge base and knows how to link Ada and C++ code
5981 together automatically in most cases.
5983 @node A Simple Example,Interfacing with C++ constructors,Linking a Mixed C++ & Ada Program,Building Mixed Ada and C++ Programs
5984 @anchor{gnat_ugn/the_gnat_compilation_model id67}@anchor{c3}@anchor{gnat_ugn/the_gnat_compilation_model a-simple-example}@anchor{c4}
5985 @subsubsection A Simple Example
5988 The following example, provided as part of the GNAT examples, shows how
5989 to achieve procedural interfacing between Ada and C++ in both
5990 directions. The C++ class A has two methods. The first method is exported
5991 to Ada by the means of an extern C wrapper function. The second method
5992 calls an Ada subprogram. On the Ada side, The C++ calls are modelled by
5993 a limited record with a layout comparable to the C++ class. The Ada
5994 subprogram, in turn, calls the C++ method. So, starting from the C++
5995 main program, the process passes back and forth between the two
5996 languages.
5998 Here are the compilation commands:
6000 @example
6001 $ gnatmake -c simple_cpp_interface
6002 $ g++ -c cpp_main.C
6003 $ g++ -c ex7.C
6004 $ gnatbind -n simple_cpp_interface
6005 $ gnatlink simple_cpp_interface -o cpp_main --LINK=g++ -lstdc++ ex7.o cpp_main.o
6006 @end example
6008 Here are the corresponding sources:
6010 @example
6011 //cpp_main.C
6013 #include "ex7.h"
6015 extern "C" @{
6016   void adainit (void);
6017   void adafinal (void);
6018   void method1 (A *t);
6021 void method1 (A *t)
6023   t->method1 ();
6026 int main ()
6028   A obj;
6029   adainit ();
6030   obj.method2 (3030);
6031   adafinal ();
6033 @end example
6035 @example
6036 //ex7.h
6038 class Origin @{
6039  public:
6040   int o_value;
6042 class A : public Origin @{
6043  public:
6044   void method1 (void);
6045   void method2 (int v);
6046   A();
6047   int   a_value;
6049 @end example
6051 @example
6052 //ex7.C
6054 #include "ex7.h"
6055 #include <stdio.h>
6057 extern "C" @{ void ada_method2 (A *t, int v);@}
6059 void A::method1 (void)
6061   a_value = 2020;
6062   printf ("in A::method1, a_value = %d \\n",a_value);
6065 void A::method2 (int v)
6067    ada_method2 (this, v);
6068    printf ("in A::method2, a_value = %d \\n",a_value);
6071 A::A(void)
6073    a_value = 1010;
6074   printf ("in A::A, a_value = %d \\n",a_value);
6076 @end example
6078 @example
6079 -- simple_cpp_interface.ads
6080 with System;
6081 package Simple_Cpp_Interface is
6082    type A is limited
6083       record
6084          Vptr    : System.Address;
6085          O_Value : Integer;
6086          A_Value : Integer;
6087       end record;
6088    pragma Convention (C, A);
6090    procedure Method1 (This : in out A);
6091    pragma Import (C, Method1);
6093    procedure Ada_Method2 (This : in out A; V : Integer);
6094    pragma Export (C, Ada_Method2);
6096 end Simple_Cpp_Interface;
6097 @end example
6099 @example
6100 -- simple_cpp_interface.adb
6101 package body Simple_Cpp_Interface is
6103    procedure Ada_Method2 (This : in out A; V : Integer) is
6104    begin
6105       Method1 (This);
6106       This.A_Value := V;
6107    end Ada_Method2;
6109 end Simple_Cpp_Interface;
6110 @end example
6112 @node Interfacing with C++ constructors,Interfacing with C++ at the Class Level,A Simple Example,Building Mixed Ada and C++ Programs
6113 @anchor{gnat_ugn/the_gnat_compilation_model id68}@anchor{c5}@anchor{gnat_ugn/the_gnat_compilation_model interfacing-with-c-constructors}@anchor{c6}
6114 @subsubsection Interfacing with C++ constructors
6117 In order to interface with C++ constructors GNAT provides the
6118 @cite{pragma CPP_Constructor} (see the @cite{GNAT_Reference_Manual}
6119 for additional information).
6120 In this section we present some common uses of C++ constructors
6121 in mixed-languages programs in GNAT.
6123 Let us assume that we need to interface with the following
6124 C++ class:
6126 @example
6127 class Root @{
6128 public:
6129   int  a_value;
6130   int  b_value;
6131   virtual int Get_Value ();
6132   Root();              // Default constructor
6133   Root(int v);         // 1st non-default constructor
6134   Root(int v, int w);  // 2nd non-default constructor
6136 @end example
6138 For this purpose we can write the following package spec (further
6139 information on how to build this spec is available in
6140 @ref{c7,,Interfacing with C++ at the Class Level} and
6141 @ref{1b,,Generating Ada Bindings for C and C++ headers}).
6143 @example
6144 with Interfaces.C; use Interfaces.C;
6145 package Pkg_Root is
6146   type Root is tagged limited record
6147      A_Value : int;
6148      B_Value : int;
6149   end record;
6150   pragma Import (CPP, Root);
6152   function Get_Value (Obj : Root) return int;
6153   pragma Import (CPP, Get_Value);
6155   function Constructor return Root;
6156   pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ev");
6158   function Constructor (v : Integer) return Root;
6159   pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ei");
6161   function Constructor (v, w : Integer) return Root;
6162   pragma Cpp_Constructor (Constructor, "_ZN4RootC1Eii");
6163 end Pkg_Root;
6164 @end example
6166 On the Ada side the constructor is represented by a function (whose
6167 name is arbitrary) that returns the classwide type corresponding to
6168 the imported C++ class. Although the constructor is described as a
6169 function, it is typically a procedure with an extra implicit argument
6170 (the object being initialized) at the implementation level. GNAT
6171 issues the appropriate call, whatever it is, to get the object
6172 properly initialized.
6174 Constructors can only appear in the following contexts:
6177 @itemize *
6179 @item 
6180 On the right side of an initialization of an object of type @cite{T}.
6182 @item 
6183 On the right side of an initialization of a record component of type @cite{T}.
6185 @item 
6186 In an Ada 2005 limited aggregate.
6188 @item 
6189 In an Ada 2005 nested limited aggregate.
6191 @item 
6192 In an Ada 2005 limited aggregate that initializes an object built in
6193 place by an extended return statement.
6194 @end itemize
6196 In a declaration of an object whose type is a class imported from C++,
6197 either the default C++ constructor is implicitly called by GNAT, or
6198 else the required C++ constructor must be explicitly called in the
6199 expression that initializes the object. For example:
6201 @example
6202 Obj1 : Root;
6203 Obj2 : Root := Constructor;
6204 Obj3 : Root := Constructor (v => 10);
6205 Obj4 : Root := Constructor (30, 40);
6206 @end example
6208 The first two declarations are equivalent: in both cases the default C++
6209 constructor is invoked (in the former case the call to the constructor is
6210 implicit, and in the latter case the call is explicit in the object
6211 declaration). @cite{Obj3} is initialized by the C++ non-default constructor
6212 that takes an integer argument, and @cite{Obj4} is initialized by the
6213 non-default C++ constructor that takes two integers.
6215 Let us derive the imported C++ class in the Ada side. For example:
6217 @example
6218 type DT is new Root with record
6219    C_Value : Natural := 2009;
6220 end record;
6221 @end example
6223 In this case the components DT inherited from the C++ side must be
6224 initialized by a C++ constructor, and the additional Ada components
6225 of type DT are initialized by GNAT. The initialization of such an
6226 object is done either by default, or by means of a function returning
6227 an aggregate of type DT, or by means of an extension aggregate.
6229 @example
6230 Obj5 : DT;
6231 Obj6 : DT := Function_Returning_DT (50);
6232 Obj7 : DT := (Constructor (30,40) with C_Value => 50);
6233 @end example
6235 The declaration of @cite{Obj5} invokes the default constructors: the
6236 C++ default constructor of the parent type takes care of the initialization
6237 of the components inherited from Root, and GNAT takes care of the default
6238 initialization of the additional Ada components of type DT (that is,
6239 @cite{C_Value} is initialized to value 2009). The order of invocation of
6240 the constructors is consistent with the order of elaboration required by
6241 Ada and C++. That is, the constructor of the parent type is always called
6242 before the constructor of the derived type.
6244 Let us now consider a record that has components whose type is imported
6245 from C++. For example:
6247 @example
6248 type Rec1 is limited record
6249    Data1 : Root := Constructor (10);
6250    Value : Natural := 1000;
6251 end record;
6253 type Rec2 (D : Integer := 20) is limited record
6254    Rec   : Rec1;
6255    Data2 : Root := Constructor (D, 30);
6256 end record;
6257 @end example
6259 The initialization of an object of type @cite{Rec2} will call the
6260 non-default C++ constructors specified for the imported components.
6261 For example:
6263 @example
6264 Obj8 : Rec2 (40);
6265 @end example
6267 Using Ada 2005 we can use limited aggregates to initialize an object
6268 invoking C++ constructors that differ from those specified in the type
6269 declarations. For example:
6271 @example
6272 Obj9 : Rec2 := (Rec => (Data1 => Constructor (15, 16),
6273                         others => <>),
6274                 others => <>);
6275 @end example
6277 The above declaration uses an Ada 2005 limited aggregate to
6278 initialize @cite{Obj9}, and the C++ constructor that has two integer
6279 arguments is invoked to initialize the @cite{Data1} component instead
6280 of the constructor specified in the declaration of type @cite{Rec1}. In
6281 Ada 2005 the box in the aggregate indicates that unspecified components
6282 are initialized using the expression (if any) available in the component
6283 declaration. That is, in this case discriminant @cite{D} is initialized
6284 to value @cite{20}, @cite{Value} is initialized to value 1000, and the
6285 non-default C++ constructor that handles two integers takes care of
6286 initializing component @cite{Data2} with values @cite{20@comma{}30}.
6288 In Ada 2005 we can use the extended return statement to build the Ada
6289 equivalent to C++ non-default constructors. For example:
6291 @example
6292 function Constructor (V : Integer) return Rec2 is
6293 begin
6294    return Obj : Rec2 := (Rec => (Data1  => Constructor (V, 20),
6295                                  others => <>),
6296                          others => <>) do
6297       --  Further actions required for construction of
6298       --  objects of type Rec2
6299       ...
6300    end record;
6301 end Constructor;
6302 @end example
6304 In this example the extended return statement construct is used to
6305 build in place the returned object whose components are initialized
6306 by means of a limited aggregate. Any further action associated with
6307 the constructor can be placed inside the construct.
6309 @node Interfacing with C++ at the Class Level,,Interfacing with C++ constructors,Building Mixed Ada and C++ Programs
6310 @anchor{gnat_ugn/the_gnat_compilation_model interfacing-with-c-at-the-class-level}@anchor{c7}@anchor{gnat_ugn/the_gnat_compilation_model id69}@anchor{c8}
6311 @subsubsection Interfacing with C++ at the Class Level
6314 In this section we demonstrate the GNAT features for interfacing with
6315 C++ by means of an example making use of Ada 2005 abstract interface
6316 types. This example consists of a classification of animals; classes
6317 have been used to model our main classification of animals, and
6318 interfaces provide support for the management of secondary
6319 classifications. We first demonstrate a case in which the types and
6320 constructors are defined on the C++ side and imported from the Ada
6321 side, and latter the reverse case.
6323 The root of our derivation will be the @cite{Animal} class, with a
6324 single private attribute (the @cite{Age} of the animal), a constructor,
6325 and two public primitives to set and get the value of this attribute.
6327 @example
6328 class Animal @{
6329  public:
6330    virtual void Set_Age (int New_Age);
6331    virtual int Age ();
6332    Animal() @{Age_Count = 0;@};
6333  private:
6334    int Age_Count;
6336 @end example
6338 Abstract interface types are defined in C++ by means of classes with pure
6339 virtual functions and no data members. In our example we will use two
6340 interfaces that provide support for the common management of @cite{Carnivore}
6341 and @cite{Domestic} animals:
6343 @example
6344 class Carnivore @{
6345 public:
6346    virtual int Number_Of_Teeth () = 0;
6349 class Domestic @{
6350 public:
6351    virtual void Set_Owner (char* Name) = 0;
6353 @end example
6355 Using these declarations, we can now say that a @cite{Dog} is an animal that is
6356 both Carnivore and Domestic, that is:
6358 @example
6359 class Dog : Animal, Carnivore, Domestic @{
6360  public:
6361    virtual int  Number_Of_Teeth ();
6362    virtual void Set_Owner (char* Name);
6364    Dog(); // Constructor
6365  private:
6366    int  Tooth_Count;
6367    char *Owner;
6369 @end example
6371 In the following examples we will assume that the previous declarations are
6372 located in a file named @cite{animals.h}. The following package demonstrates
6373 how to import these C++ declarations from the Ada side:
6375 @example
6376 with Interfaces.C.Strings; use Interfaces.C.Strings;
6377 package Animals is
6378   type Carnivore is limited interface;
6379   pragma Convention (C_Plus_Plus, Carnivore);
6380   function Number_Of_Teeth (X : Carnivore)
6381      return Natural is abstract;
6383   type Domestic is limited interface;
6384   pragma Convention (C_Plus_Plus, Domestic);
6385   procedure Set_Owner
6386     (X    : in out Domestic;
6387      Name : Chars_Ptr) is abstract;
6389   type Animal is tagged limited record
6390     Age : Natural;
6391   end record;
6392   pragma Import (C_Plus_Plus, Animal);
6394   procedure Set_Age (X : in out Animal; Age : Integer);
6395   pragma Import (C_Plus_Plus, Set_Age);
6397   function Age (X : Animal) return Integer;
6398   pragma Import (C_Plus_Plus, Age);
6400   function New_Animal return Animal;
6401   pragma CPP_Constructor (New_Animal);
6402   pragma Import (CPP, New_Animal, "_ZN6AnimalC1Ev");
6404   type Dog is new Animal and Carnivore and Domestic with record
6405     Tooth_Count : Natural;
6406     Owner       : String (1 .. 30);
6407   end record;
6408   pragma Import (C_Plus_Plus, Dog);
6410   function Number_Of_Teeth (A : Dog) return Natural;
6411   pragma Import (C_Plus_Plus, Number_Of_Teeth);
6413   procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
6414   pragma Import (C_Plus_Plus, Set_Owner);
6416   function New_Dog return Dog;
6417   pragma CPP_Constructor (New_Dog);
6418   pragma Import (CPP, New_Dog, "_ZN3DogC2Ev");
6419 end Animals;
6420 @end example
6422 Thanks to the compatibility between GNAT run-time structures and the C++ ABI,
6423 interfacing with these C++ classes is easy. The only requirement is that all
6424 the primitives and components must be declared exactly in the same order in
6425 the two languages.
6427 Regarding the abstract interfaces, we must indicate to the GNAT compiler by
6428 means of a @cite{pragma Convention (C_Plus_Plus)}, the convention used to pass
6429 the arguments to the called primitives will be the same as for C++. For the
6430 imported classes we use @cite{pragma Import} with convention @cite{C_Plus_Plus}
6431 to indicate that they have been defined on the C++ side; this is required
6432 because the dispatch table associated with these tagged types will be built
6433 in the C++ side and therefore will not contain the predefined Ada primitives
6434 which Ada would otherwise expect.
6436 As the reader can see there is no need to indicate the C++ mangled names
6437 associated with each subprogram because it is assumed that all the calls to
6438 these primitives will be dispatching calls. The only exception is the
6439 constructor, which must be registered with the compiler by means of
6440 @cite{pragma CPP_Constructor} and needs to provide its associated C++
6441 mangled name because the Ada compiler generates direct calls to it.
6443 With the above packages we can now declare objects of type Dog on the Ada side
6444 and dispatch calls to the corresponding subprograms on the C++ side. We can
6445 also extend the tagged type Dog with further fields and primitives, and
6446 override some of its C++ primitives on the Ada side. For example, here we have
6447 a type derivation defined on the Ada side that inherits all the dispatching
6448 primitives of the ancestor from the C++ side.
6450 @example
6451 with Animals; use Animals;
6452 package Vaccinated_Animals is
6453   type Vaccinated_Dog is new Dog with null record;
6454   function Vaccination_Expired (A : Vaccinated_Dog) return Boolean;
6455 end Vaccinated_Animals;
6456 @end example
6458 It is important to note that, because of the ABI compatibility, the programmer
6459 does not need to add any further information to indicate either the object
6460 layout or the dispatch table entry associated with each dispatching operation.
6462 Now let us define all the types and constructors on the Ada side and export
6463 them to C++, using the same hierarchy of our previous example:
6465 @example
6466 with Interfaces.C.Strings;
6467 use Interfaces.C.Strings;
6468 package Animals is
6469   type Carnivore is limited interface;
6470   pragma Convention (C_Plus_Plus, Carnivore);
6471   function Number_Of_Teeth (X : Carnivore)
6472      return Natural is abstract;
6474   type Domestic is limited interface;
6475   pragma Convention (C_Plus_Plus, Domestic);
6476   procedure Set_Owner
6477     (X    : in out Domestic;
6478      Name : Chars_Ptr) is abstract;
6480   type Animal is tagged record
6481     Age : Natural;
6482   end record;
6483   pragma Convention (C_Plus_Plus, Animal);
6485   procedure Set_Age (X : in out Animal; Age : Integer);
6486   pragma Export (C_Plus_Plus, Set_Age);
6488   function Age (X : Animal) return Integer;
6489   pragma Export (C_Plus_Plus, Age);
6491   function New_Animal return Animal'Class;
6492   pragma Export (C_Plus_Plus, New_Animal);
6494   type Dog is new Animal and Carnivore and Domestic with record
6495     Tooth_Count : Natural;
6496     Owner       : String (1 .. 30);
6497   end record;
6498   pragma Convention (C_Plus_Plus, Dog);
6500   function Number_Of_Teeth (A : Dog) return Natural;
6501   pragma Export (C_Plus_Plus, Number_Of_Teeth);
6503   procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
6504   pragma Export (C_Plus_Plus, Set_Owner);
6506   function New_Dog return Dog'Class;
6507   pragma Export (C_Plus_Plus, New_Dog);
6508 end Animals;
6509 @end example
6511 Compared with our previous example the only differences are the use of
6512 @cite{pragma Convention} (instead of @cite{pragma Import}), and the use of
6513 @cite{pragma Export} to indicate to the GNAT compiler that the primitives will
6514 be available to C++. Thanks to the ABI compatibility, on the C++ side there is
6515 nothing else to be done; as explained above, the only requirement is that all
6516 the primitives and components are declared in exactly the same order.
6518 For completeness, let us see a brief C++ main program that uses the
6519 declarations available in @cite{animals.h} (presented in our first example) to
6520 import and use the declarations from the Ada side, properly initializing and
6521 finalizing the Ada run-time system along the way:
6523 @example
6524 #include "animals.h"
6525 #include <iostream>
6526 using namespace std;
6528 void Check_Carnivore (Carnivore *obj) @{...@}
6529 void Check_Domestic (Domestic *obj)   @{...@}
6530 void Check_Animal (Animal *obj)       @{...@}
6531 void Check_Dog (Dog *obj)             @{...@}
6533 extern "C" @{
6534   void adainit (void);
6535   void adafinal (void);
6536   Dog* new_dog ();
6539 void test ()
6541   Dog *obj = new_dog();  // Ada constructor
6542   Check_Carnivore (obj); // Check secondary DT
6543   Check_Domestic (obj);  // Check secondary DT
6544   Check_Animal (obj);    // Check primary DT
6545   Check_Dog (obj);       // Check primary DT
6548 int main ()
6550   adainit ();  test();  adafinal ();
6551   return 0;
6553 @end example
6555 @node Generating Ada Bindings for C and C++ headers,,Building Mixed Ada and C++ Programs,Mixed Language Programming
6556 @anchor{gnat_ugn/the_gnat_compilation_model id70}@anchor{c9}@anchor{gnat_ugn/the_gnat_compilation_model generating-ada-bindings-for-c-and-c-headers}@anchor{1b}
6557 @subsection Generating Ada Bindings for C and C++ headers
6560 @geindex Binding generation (for C and C++ headers)
6562 @geindex C headers (binding generation)
6564 @geindex C++ headers (binding generation)
6566 GNAT includes a binding generator for C and C++ headers which is
6567 intended to do 95% of the tedious work of generating Ada specs from C
6568 or C++ header files.
6570 Note that this capability is not intended to generate 100% correct Ada specs,
6571 and will is some cases require manual adjustments, although it can often
6572 be used out of the box in practice.
6574 Some of the known limitations include:
6577 @itemize *
6579 @item 
6580 only very simple character constant macros are translated into Ada
6581 constants. Function macros (macros with arguments) are partially translated
6582 as comments, to be completed manually if needed.
6584 @item 
6585 some extensions (e.g. vector types) are not supported
6587 @item 
6588 pointers to pointers or complex structures are mapped to System.Address
6590 @item 
6591 identifiers with identical name (except casing) will generate compilation
6592 errors (e.g. @cite{shm_get} vs @cite{SHM_GET}).
6593 @end itemize
6595 The code generated is using the Ada 2005 syntax, which makes it
6596 easier to interface with other languages than previous versions of Ada.
6598 @menu
6599 * Running the binding generator:: 
6600 * Generating bindings for C++ headers:: 
6601 * Switches:: 
6603 @end menu
6605 @node Running the binding generator,Generating bindings for C++ headers,,Generating Ada Bindings for C and C++ headers
6606 @anchor{gnat_ugn/the_gnat_compilation_model id71}@anchor{ca}@anchor{gnat_ugn/the_gnat_compilation_model running-the-binding-generator}@anchor{cb}
6607 @subsubsection Running the binding generator
6610 The binding generator is part of the @emph{gcc} compiler and can be
6611 invoked via the @emph{-fdump-ada-spec} switch, which will generate Ada
6612 spec files for the header files specified on the command line, and all
6613 header files needed by these files transitively. For example:
6615 @example
6616 $ g++ -c -fdump-ada-spec -C /usr/include/time.h
6617 $ gcc -c -gnat05 *.ads
6618 @end example
6620 will generate, under GNU/Linux, the following files: @code{time_h.ads},
6621 @code{bits_time_h.ads}, @code{stddef_h.ads}, @code{bits_types_h.ads} which
6622 correspond to the files @code{/usr/include/time.h},
6623 @code{/usr/include/bits/time.h}, etc..., and will then compile in Ada 2005
6624 mode these Ada specs.
6626 The @cite{-C} switch tells @emph{gcc} to extract comments from headers,
6627 and will attempt to generate corresponding Ada comments.
6629 If you want to generate a single Ada file and not the transitive closure, you
6630 can use instead the @emph{-fdump-ada-spec-slim} switch.
6632 You can optionally specify a parent unit, of which all generated units will
6633 be children, using @cite{-fada-spec-parent=`@w{`}unit}.
6635 Note that we recommend when possible to use the @emph{g++} driver to
6636 generate bindings, even for most C headers, since this will in general
6637 generate better Ada specs. For generating bindings for C++ headers, it is
6638 mandatory to use the @emph{g++} command, or @emph{gcc -x c++} which
6639 is equivalent in this case. If @emph{g++} cannot work on your C headers
6640 because of incompatibilities between C and C++, then you can fallback to
6641 @emph{gcc} instead.
6643 For an example of better bindings generated from the C++ front-end,
6644 the name of the parameters (when available) are actually ignored by the C
6645 front-end. Consider the following C header:
6647 @example
6648 extern void foo (int variable);
6649 @end example
6651 with the C front-end, @cite{variable} is ignored, and the above is handled as:
6653 @example
6654 extern void foo (int);
6655 @end example
6657 generating a generic:
6659 @example
6660 procedure foo (param1 : int);
6661 @end example
6663 with the C++ front-end, the name is available, and we generate:
6665 @example
6666 procedure foo (variable : int);
6667 @end example
6669 In some cases, the generated bindings will be more complete or more meaningful
6670 when defining some macros, which you can do via the @emph{-D} switch. This
6671 is for example the case with @code{Xlib.h} under GNU/Linux:
6673 @example
6674 $ g++ -c -fdump-ada-spec -DXLIB_ILLEGAL_ACCESS -C /usr/include/X11/Xlib.h
6675 @end example
6677 The above will generate more complete bindings than a straight call without
6678 the @emph{-DXLIB_ILLEGAL_ACCESS} switch.
6680 In other cases, it is not possible to parse a header file in a stand-alone
6681 manner, because other include files need to be included first. In this
6682 case, the solution is to create a small header file including the needed
6683 @cite{#include} and possible @cite{#define} directives. For example, to
6684 generate Ada bindings for @code{readline/readline.h}, you need to first
6685 include @code{stdio.h}, so you can create a file with the following two
6686 lines in e.g. @code{readline1.h}:
6688 @example
6689 #include <stdio.h>
6690 #include <readline/readline.h>
6691 @end example
6693 and then generate Ada bindings from this file:
6695 @example
6696 $ g++ -c -fdump-ada-spec readline1.h
6697 @end example
6699 @node Generating bindings for C++ headers,Switches,Running the binding generator,Generating Ada Bindings for C and C++ headers
6700 @anchor{gnat_ugn/the_gnat_compilation_model id72}@anchor{cc}@anchor{gnat_ugn/the_gnat_compilation_model generating-bindings-for-c-headers}@anchor{cd}
6701 @subsubsection Generating bindings for C++ headers
6704 Generating bindings for C++ headers is done using the same options, always
6705 with the @emph{g++} compiler. Note that generating Ada spec from C++ headers is a
6706 much more complex job and support for C++ headers is much more limited that
6707 support for C headers. As a result, you will need to modify the resulting
6708 bindings by hand more extensively when using C++ headers.
6710 In this mode, C++ classes will be mapped to Ada tagged types, constructors
6711 will be mapped using the @cite{CPP_Constructor} pragma, and when possible,
6712 multiple inheritance of abstract classes will be mapped to Ada interfaces
6713 (see the @emph{Interfacing to C++} section in the @cite{GNAT Reference Manual}
6714 for additional information on interfacing to C++).
6716 For example, given the following C++ header file:
6718 @example
6719 class Carnivore @{
6720 public:
6721    virtual int Number_Of_Teeth () = 0;
6724 class Domestic @{
6725 public:
6726    virtual void Set_Owner (char* Name) = 0;
6729 class Animal @{
6730 public:
6731   int Age_Count;
6732   virtual void Set_Age (int New_Age);
6735 class Dog : Animal, Carnivore, Domestic @{
6736  public:
6737   int  Tooth_Count;
6738   char *Owner;
6740   virtual int  Number_Of_Teeth ();
6741   virtual void Set_Owner (char* Name);
6743   Dog();
6745 @end example
6747 The corresponding Ada code is generated:
6749 @example
6750 package Class_Carnivore is
6751   type Carnivore is limited interface;
6752   pragma Import (CPP, Carnivore);
6754   function Number_Of_Teeth (this : access Carnivore) return int is abstract;
6755 end;
6756 use Class_Carnivore;
6758 package Class_Domestic is
6759   type Domestic is limited interface;
6760   pragma Import (CPP, Domestic);
6762   procedure Set_Owner
6763     (this : access Domestic;
6764      Name : Interfaces.C.Strings.chars_ptr) is abstract;
6765 end;
6766 use Class_Domestic;
6768 package Class_Animal is
6769   type Animal is tagged limited record
6770     Age_Count : aliased int;
6771   end record;
6772   pragma Import (CPP, Animal);
6774   procedure Set_Age (this : access Animal; New_Age : int);
6775   pragma Import (CPP, Set_Age, "_ZN6Animal7Set_AgeEi");
6776 end;
6777 use Class_Animal;
6779 package Class_Dog is
6780   type Dog is new Animal and Carnivore and Domestic with record
6781     Tooth_Count : aliased int;
6782     Owner : Interfaces.C.Strings.chars_ptr;
6783   end record;
6784   pragma Import (CPP, Dog);
6786   function Number_Of_Teeth (this : access Dog) return int;
6787   pragma Import (CPP, Number_Of_Teeth, "_ZN3Dog15Number_Of_TeethEv");
6789   procedure Set_Owner
6790     (this : access Dog; Name : Interfaces.C.Strings.chars_ptr);
6791   pragma Import (CPP, Set_Owner, "_ZN3Dog9Set_OwnerEPc");
6793   function New_Dog return Dog;
6794   pragma CPP_Constructor (New_Dog);
6795   pragma Import (CPP, New_Dog, "_ZN3DogC1Ev");
6796 end;
6797 use Class_Dog;
6798 @end example
6800 @node Switches,,Generating bindings for C++ headers,Generating Ada Bindings for C and C++ headers
6801 @anchor{gnat_ugn/the_gnat_compilation_model switches}@anchor{ce}@anchor{gnat_ugn/the_gnat_compilation_model switches-for-ada-binding-generation}@anchor{cf}
6802 @subsubsection Switches
6805 @geindex -fdump-ada-spec (gcc)
6808 @table @asis
6810 @item @code{-fdump-ada-spec}
6812 Generate Ada spec files for the given header files transitively (including
6813 all header files that these headers depend upon).
6814 @end table
6816 @geindex -fdump-ada-spec-slim (gcc)
6819 @table @asis
6821 @item @code{-fdump-ada-spec-slim}
6823 Generate Ada spec files for the header files specified on the command line
6824 only.
6825 @end table
6827 @geindex -fada-spec-parent (gcc)
6830 @table @asis
6832 @item @code{-fada-spec-parent=@emph{unit}}
6834 Specifies that all files generated by @emph{-fdump-ada-spec*} are
6835 to be child units of the specified parent unit.
6836 @end table
6838 @geindex -C (gcc)
6841 @table @asis
6843 @item @code{-C}
6845 Extract comments from headers and generate Ada comments in the Ada spec files.
6846 @end table
6848 @node GNAT and Other Compilation Models,Using GNAT Files with External Tools,Mixed Language Programming,The GNAT Compilation Model
6849 @anchor{gnat_ugn/the_gnat_compilation_model id73}@anchor{d0}@anchor{gnat_ugn/the_gnat_compilation_model gnat-and-other-compilation-models}@anchor{47}
6850 @section GNAT and Other Compilation Models
6853 This section compares the GNAT model with the approaches taken in
6854 other environents, first the C/C++ model and then the mechanism that
6855 has been used in other Ada systems, in particular those traditionally
6856 used for Ada 83.
6858 @menu
6859 * Comparison between GNAT and C/C++ Compilation Models:: 
6860 * Comparison between GNAT and Conventional Ada Library Models:: 
6862 @end menu
6864 @node Comparison between GNAT and C/C++ Compilation Models,Comparison between GNAT and Conventional Ada Library Models,,GNAT and Other Compilation Models
6865 @anchor{gnat_ugn/the_gnat_compilation_model comparison-between-gnat-and-c-c-compilation-models}@anchor{d1}@anchor{gnat_ugn/the_gnat_compilation_model id74}@anchor{d2}
6866 @subsection Comparison between GNAT and C/C++ Compilation Models
6869 The GNAT model of compilation is close to the C and C++ models. You can
6870 think of Ada specs as corresponding to header files in C. As in C, you
6871 don't need to compile specs; they are compiled when they are used. The
6872 Ada @emph{with} is similar in effect to the @cite{#include} of a C
6873 header.
6875 One notable difference is that, in Ada, you may compile specs separately
6876 to check them for semantic and syntactic accuracy. This is not always
6877 possible with C headers because they are fragments of programs that have
6878 less specific syntactic or semantic rules.
6880 The other major difference is the requirement for running the binder,
6881 which performs two important functions. First, it checks for
6882 consistency. In C or C++, the only defense against assembling
6883 inconsistent programs lies outside the compiler, in a makefile, for
6884 example. The binder satisfies the Ada requirement that it be impossible
6885 to construct an inconsistent program when the compiler is used in normal
6886 mode.
6888 @geindex Elaboration order control
6890 The other important function of the binder is to deal with elaboration
6891 issues. There are also elaboration issues in C++ that are handled
6892 automatically. This automatic handling has the advantage of being
6893 simpler to use, but the C++ programmer has no control over elaboration.
6894 Where @cite{gnatbind} might complain there was no valid order of
6895 elaboration, a C++ compiler would simply construct a program that
6896 malfunctioned at run time.
6898 @node Comparison between GNAT and Conventional Ada Library Models,,Comparison between GNAT and C/C++ Compilation Models,GNAT and Other Compilation Models
6899 @anchor{gnat_ugn/the_gnat_compilation_model comparison-between-gnat-and-conventional-ada-library-models}@anchor{d3}@anchor{gnat_ugn/the_gnat_compilation_model id75}@anchor{d4}
6900 @subsection Comparison between GNAT and Conventional Ada Library Models
6903 This section is intended for Ada programmers who have
6904 used an Ada compiler implementing the traditional Ada library
6905 model, as described in the Ada Reference Manual.
6907 @geindex GNAT library
6909 In GNAT, there is no 'library' in the normal sense. Instead, the set of
6910 source files themselves acts as the library. Compiling Ada programs does
6911 not generate any centralized information, but rather an object file and
6912 a ALI file, which are of interest only to the binder and linker.
6913 In a traditional system, the compiler reads information not only from
6914 the source file being compiled, but also from the centralized library.
6915 This means that the effect of a compilation depends on what has been
6916 previously compiled. In particular:
6919 @itemize *
6921 @item 
6922 When a unit is @emph{with}ed, the unit seen by the compiler corresponds
6923 to the version of the unit most recently compiled into the library.
6925 @item 
6926 Inlining is effective only if the necessary body has already been
6927 compiled into the library.
6929 @item 
6930 Compiling a unit may obsolete other units in the library.
6931 @end itemize
6933 In GNAT, compiling one unit never affects the compilation of any other
6934 units because the compiler reads only source files. Only changes to source
6935 files can affect the results of a compilation. In particular:
6938 @itemize *
6940 @item 
6941 When a unit is @emph{with}ed, the unit seen by the compiler corresponds
6942 to the source version of the unit that is currently accessible to the
6943 compiler.
6945 @geindex Inlining
6947 @item 
6948 Inlining requires the appropriate source files for the package or
6949 subprogram bodies to be available to the compiler. Inlining is always
6950 effective, independent of the order in which units are compiled.
6952 @item 
6953 Compiling a unit never affects any other compilations. The editing of
6954 sources may cause previous compilations to be out of date if they
6955 depended on the source file being modified.
6956 @end itemize
6958 The most important result of these differences is that order of compilation
6959 is never significant in GNAT. There is no situation in which one is
6960 required to do one compilation before another. What shows up as order of
6961 compilation requirements in the traditional Ada library becomes, in
6962 GNAT, simple source dependencies; in other words, there is only a set
6963 of rules saying what source files must be present when a file is
6964 compiled.
6966 @node Using GNAT Files with External Tools,,GNAT and Other Compilation Models,The GNAT Compilation Model
6967 @anchor{gnat_ugn/the_gnat_compilation_model using-gnat-files-with-external-tools}@anchor{1c}@anchor{gnat_ugn/the_gnat_compilation_model id76}@anchor{d5}
6968 @section Using GNAT Files with External Tools
6971 This section explains how files that are produced by GNAT may be
6972 used with tools designed for other languages.
6974 @menu
6975 * Using Other Utility Programs with GNAT:: 
6976 * The External Symbol Naming Scheme of GNAT:: 
6978 @end menu
6980 @node Using Other Utility Programs with GNAT,The External Symbol Naming Scheme of GNAT,,Using GNAT Files with External Tools
6981 @anchor{gnat_ugn/the_gnat_compilation_model using-other-utility-programs-with-gnat}@anchor{d6}@anchor{gnat_ugn/the_gnat_compilation_model id77}@anchor{d7}
6982 @subsection Using Other Utility Programs with GNAT
6985 The object files generated by GNAT are in standard system format and in
6986 particular the debugging information uses this format. This means
6987 programs generated by GNAT can be used with existing utilities that
6988 depend on these formats.
6990 In general, any utility program that works with C will also often work with
6991 Ada programs generated by GNAT. This includes software utilities such as
6992 gprof (a profiling program), gdb (the FSF debugger), and utilities such
6993 as Purify.
6995 @node The External Symbol Naming Scheme of GNAT,,Using Other Utility Programs with GNAT,Using GNAT Files with External Tools
6996 @anchor{gnat_ugn/the_gnat_compilation_model the-external-symbol-naming-scheme-of-gnat}@anchor{d8}@anchor{gnat_ugn/the_gnat_compilation_model id78}@anchor{d9}
6997 @subsection The External Symbol Naming Scheme of GNAT
7000 In order to interpret the output from GNAT, when using tools that are
7001 originally intended for use with other languages, it is useful to
7002 understand the conventions used to generate link names from the Ada
7003 entity names.
7005 All link names are in all lowercase letters. With the exception of library
7006 procedure names, the mechanism used is simply to use the full expanded
7007 Ada name with dots replaced by double underscores. For example, suppose
7008 we have the following package spec:
7010 @example
7011 package QRS is
7012    MN : Integer;
7013 end QRS;
7014 @end example
7016 @geindex pragma Export
7018 The variable @cite{MN} has a full expanded Ada name of @cite{QRS.MN}, so
7019 the corresponding link name is @cite{qrs__mn}.
7020 Of course if a @cite{pragma Export} is used this may be overridden:
7022 @example
7023 package Exports is
7024    Var1 : Integer;
7025    pragma Export (Var1, C, External_Name => "var1_name");
7026    Var2 : Integer;
7027    pragma Export (Var2, C, Link_Name => "var2_link_name");
7028 end Exports;
7029 @end example
7031 In this case, the link name for @cite{Var1} is whatever link name the
7032 C compiler would assign for the C function @cite{var1_name}. This typically
7033 would be either @cite{var1_name} or @cite{_var1_name}, depending on operating
7034 system conventions, but other possibilities exist. The link name for
7035 @cite{Var2} is @cite{var2_link_name}, and this is not operating system
7036 dependent.
7038 One exception occurs for library level procedures. A potential ambiguity
7039 arises between the required name @cite{_main} for the C main program,
7040 and the name we would otherwise assign to an Ada library level procedure
7041 called @cite{Main} (which might well not be the main program).
7043 To avoid this ambiguity, we attach the prefix @cite{_ada_} to such
7044 names. So if we have a library level procedure such as:
7046 @example
7047 procedure Hello (S : String);
7048 @end example
7050 the external name of this procedure will be @cite{_ada_hello}.
7052 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
7054 @node Building Executable Programs with GNAT,GNAT Project Manager,The GNAT Compilation Model,Top
7055 @anchor{gnat_ugn/building_executable_programs_with_gnat building-executable-programs-with-gnat}@anchor{a}@anchor{gnat_ugn/building_executable_programs_with_gnat doc}@anchor{da}@anchor{gnat_ugn/building_executable_programs_with_gnat id1}@anchor{db}
7056 @chapter Building Executable Programs with GNAT
7059 This chapter describes first the gnatmake tool
7060 (@ref{1d,,Building with gnatmake}),
7061 which automatically determines the set of sources
7062 needed by an Ada compilation unit and executes the necessary
7063 (re)compilations, binding and linking.
7064 It also explains how to use each tool individually: the
7065 compiler (gcc, see @ref{1e,,Compiling with gcc}),
7066 binder (gnatbind, see @ref{1f,,Binding with gnatbind}),
7067 and linker (gnatlink, see @ref{20,,Linking with gnatlink})
7068 to build executable programs.
7069 Finally, this chapter provides examples of
7070 how to make use of the general GNU make mechanism
7071 in a GNAT context (see @ref{21,,Using the GNU make Utility}).
7073 @menu
7074 * Building with gnatmake:: 
7075 * Compiling with gcc:: 
7076 * Compiler Switches:: 
7077 * Binding with gnatbind:: 
7078 * Linking with gnatlink:: 
7079 * Using the GNU make Utility:: 
7081 @end menu
7083 @node Building with gnatmake,Compiling with gcc,,Building Executable Programs with GNAT
7084 @anchor{gnat_ugn/building_executable_programs_with_gnat the-gnat-make-program-gnatmake}@anchor{1d}@anchor{gnat_ugn/building_executable_programs_with_gnat building-with-gnatmake}@anchor{dc}
7085 @section Building with @emph{gnatmake}
7088 @geindex gnatmake
7090 A typical development cycle when working on an Ada program consists of
7091 the following steps:
7094 @enumerate 
7096 @item 
7097 Edit some sources to fix bugs;
7099 @item 
7100 Add enhancements;
7102 @item 
7103 Compile all sources affected;
7105 @item 
7106 Rebind and relink; and
7108 @item 
7109 Test.
7110 @end enumerate
7112 @geindex Dependency rules (compilation)
7114 The third step in particular can be tricky, because not only do the modified
7115 files have to be compiled, but any files depending on these files must also be
7116 recompiled. The dependency rules in Ada can be quite complex, especially
7117 in the presence of overloading, @cite{use} clauses, generics and inlined
7118 subprograms.
7120 @emph{gnatmake} automatically takes care of the third and fourth steps
7121 of this process. It determines which sources need to be compiled,
7122 compiles them, and binds and links the resulting object files.
7124 Unlike some other Ada make programs, the dependencies are always
7125 accurately recomputed from the new sources. The source based approach of
7126 the GNAT compilation model makes this possible. This means that if
7127 changes to the source program cause corresponding changes in
7128 dependencies, they will always be tracked exactly correctly by
7129 @emph{gnatmake}.
7131 Note that for advanced description of project structure, we recommend creating
7132 a project file as explained in @ref{b,,GNAT Project Manager} and use the
7133 @emph{gprbuild} tool which supports building with project files and works similarly
7134 to @emph{gnatmake}.
7136 @menu
7137 * Running gnatmake:: 
7138 * Switches for gnatmake:: 
7139 * Mode Switches for gnatmake:: 
7140 * Notes on the Command Line:: 
7141 * How gnatmake Works:: 
7142 * Examples of gnatmake Usage:: 
7144 @end menu
7146 @node Running gnatmake,Switches for gnatmake,,Building with gnatmake
7147 @anchor{gnat_ugn/building_executable_programs_with_gnat running-gnatmake}@anchor{dd}@anchor{gnat_ugn/building_executable_programs_with_gnat id2}@anchor{de}
7148 @subsection Running @emph{gnatmake}
7151 The usual form of the @emph{gnatmake} command is
7153 @example
7154 $ gnatmake [<switches>] <file_name> [<file_names>] [<mode_switches>]
7155 @end example
7157 The only required argument is one @cite{file_name}, which specifies
7158 a compilation unit that is a main program. Several @cite{file_names} can be
7159 specified: this will result in several executables being built.
7160 If @cite{switches} are present, they can be placed before the first
7161 @cite{file_name}, between @cite{file_names} or after the last @cite{file_name}.
7162 If @cite{mode_switches} are present, they must always be placed after
7163 the last @cite{file_name} and all @cite{switches}.
7165 If you are using standard file extensions (@code{.adb} and
7166 @code{.ads}), then the
7167 extension may be omitted from the @cite{file_name} arguments. However, if
7168 you are using non-standard extensions, then it is required that the
7169 extension be given. A relative or absolute directory path can be
7170 specified in a @cite{file_name}, in which case, the input source file will
7171 be searched for in the specified directory only. Otherwise, the input
7172 source file will first be searched in the directory where
7173 @emph{gnatmake} was invoked and if it is not found, it will be search on
7174 the source path of the compiler as described in
7175 @ref{8e,,Search Paths and the Run-Time Library (RTL)}.
7177 All @emph{gnatmake} output (except when you specify @emph{-M}) is sent to
7178 @code{stderr}. The output produced by the
7179 @emph{-M} switch is sent to @code{stdout}.
7181 @node Switches for gnatmake,Mode Switches for gnatmake,Running gnatmake,Building with gnatmake
7182 @anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gnatmake}@anchor{df}@anchor{gnat_ugn/building_executable_programs_with_gnat id3}@anchor{e0}
7183 @subsection Switches for @emph{gnatmake}
7186 You may specify any of the following switches to @emph{gnatmake}:
7188 @geindex --version (gnatmake)
7191 @table @asis
7193 @item @code{--version}
7195 Display Copyright and version, then exit disregarding all other options.
7196 @end table
7198 @geindex --help (gnatmake)
7201 @table @asis
7203 @item @code{--help}
7205 If @code{--version} was not used, display usage, then exit disregarding
7206 all other options.
7207 @end table
7209 @geindex --GCC=compiler_name (gnatmake)
7212 @table @asis
7214 @item @code{--GCC=@emph{compiler_name}}
7216 Program used for compiling. The default is @code{gcc}. You need to use
7217 quotes around @cite{compiler_name} if @cite{compiler_name} contains
7218 spaces or other separator characters.
7219 As an example @code{--GCC="foo -x  -y"}
7220 will instruct @emph{gnatmake} to use @code{foo -x -y} as your
7221 compiler. A limitation of this syntax is that the name and path name of
7222 the executable itself must not include any embedded spaces. Note that
7223 switch @code{-c} is always inserted after your command name. Thus in the
7224 above example the compiler command that will be used by @emph{gnatmake}
7225 will be @code{foo -c -x -y}. If several @code{--GCC=compiler_name} are
7226 used, only the last @cite{compiler_name} is taken into account. However,
7227 all the additional switches are also taken into account. Thus,
7228 @code{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
7229 @code{--GCC="bar -x -y -z -t"}.
7230 @end table
7232 @geindex --GNATBIND=binder_name (gnatmake)
7235 @table @asis
7237 @item @code{--GNATBIND=@emph{binder_name}}
7239 Program used for binding. The default is @code{gnatbind}. You need to
7240 use quotes around @cite{binder_name} if @cite{binder_name} contains spaces
7241 or other separator characters.
7242 As an example @code{--GNATBIND="bar -x  -y"}
7243 will instruct @emph{gnatmake} to use @cite{bar -x -y} as your
7244 binder. Binder switches that are normally appended by @emph{gnatmake}
7245 to @code{gnatbind} are now appended to the end of @cite{bar -x -y}.
7246 A limitation of this syntax is that the name and path name of the executable
7247 itself must not include any embedded spaces.
7248 @end table
7250 @geindex --GNATLINK=linker_name (gnatmake)
7253 @table @asis
7255 @item @code{--GNATLINK=@emph{linker_name}}
7257 Program used for linking. The default is @code{gnatlink}. You need to
7258 use quotes around @cite{linker_name} if @cite{linker_name} contains spaces
7259 or other separator characters.
7260 As an example @code{--GNATLINK="lan -x  -y"}
7261 will instruct @emph{gnatmake} to use @code{lan -x -y} as your
7262 linker. Linker switches that are normally appended by @code{gnatmake} to
7263 @code{gnatlink} are now appended to the end of @code{lan -x -y}.
7264 A limitation of this syntax is that the name and path name of the executable
7265 itself must not include any embedded spaces.
7267 @item @code{--create-map-file}
7269 When linking an executable, create a map file. The name of the map file
7270 has the same name as the executable with extension ".map".
7272 @item @code{--create-map-file=@emph{mapfile}}
7274 When linking an executable, create a map file with the specified name.
7275 @end table
7277 @geindex --create-missing-dirs (gnatmake)
7280 @table @asis
7282 @item @code{--create-missing-dirs}
7284 When using project files (@code{-P@emph{project}}), automatically create
7285 missing object directories, library directories and exec
7286 directories.
7288 @item @code{--single-compile-per-obj-dir}
7290 Disallow simultaneous compilations in the same object directory when
7291 project files are used.
7293 @item @code{--subdirs=@emph{subdir}}
7295 Actual object directory of each project file is the subdirectory subdir of the
7296 object directory specified or defaulted in the project file.
7298 @item @code{--unchecked-shared-lib-imports}
7300 By default, shared library projects are not allowed to import static library
7301 projects. When this switch is used on the command line, this restriction is
7302 relaxed.
7304 @item @code{--source-info=@emph{source info file}}
7306 Specify a source info file. This switch is active only when project files
7307 are used. If the source info file is specified as a relative path, then it is
7308 relative to the object directory of the main project. If the source info file
7309 does not exist, then after the Project Manager has successfully parsed and
7310 processed the project files and found the sources, it creates the source info
7311 file. If the source info file already exists and can be read successfully,
7312 then the Project Manager will get all the needed information about the sources
7313 from the source info file and will not look for them. This reduces the time
7314 to process the project files, especially when looking for sources that take a
7315 long time. If the source info file exists but cannot be parsed successfully,
7316 the Project Manager will attempt to recreate it. If the Project Manager fails
7317 to create the source info file, a message is issued, but gnatmake does not
7318 fail. @emph{gnatmake} "trusts" the source info file. This means that
7319 if the source files have changed (addition, deletion, moving to a different
7320 source directory), then the source info file need to be deleted and recreated.
7321 @end table
7323 @geindex -a (gnatmake)
7326 @table @asis
7328 @item @code{-a}
7330 Consider all files in the make process, even the GNAT internal system
7331 files (for example, the predefined Ada library files), as well as any
7332 locked files. Locked files are files whose ALI file is write-protected.
7333 By default,
7334 @emph{gnatmake} does not check these files,
7335 because the assumption is that the GNAT internal files are properly up
7336 to date, and also that any write protected ALI files have been properly
7337 installed. Note that if there is an installation problem, such that one
7338 of these files is not up to date, it will be properly caught by the
7339 binder.
7340 You may have to specify this switch if you are working on GNAT
7341 itself. The switch @code{-a} is also useful
7342 in conjunction with @code{-f}
7343 if you need to recompile an entire application,
7344 including run-time files, using special configuration pragmas,
7345 such as a @cite{Normalize_Scalars} pragma.
7347 By default
7348 @code{gnatmake -a} compiles all GNAT
7349 internal files with
7350 @code{gcc -c -gnatpg} rather than @code{gcc -c}.
7351 @end table
7353 @geindex -b (gnatmake)
7356 @table @asis
7358 @item @code{-b}
7360 Bind only. Can be combined with @emph{-c} to do
7361 compilation and binding, but no link.
7362 Can be combined with @emph{-l}
7363 to do binding and linking. When not combined with
7364 @emph{-c}
7365 all the units in the closure of the main program must have been previously
7366 compiled and must be up to date. The root unit specified by @cite{file_name}
7367 may be given without extension, with the source extension or, if no GNAT
7368 Project File is specified, with the ALI file extension.
7369 @end table
7371 @geindex -c (gnatmake)
7374 @table @asis
7376 @item @code{-c}
7378 Compile only. Do not perform binding, except when @emph{-b}
7379 is also specified. Do not perform linking, except if both
7380 @emph{-b} and
7381 @emph{-l} are also specified.
7382 If the root unit specified by @cite{file_name} is not a main unit, this is the
7383 default. Otherwise @emph{gnatmake} will attempt binding and linking
7384 unless all objects are up to date and the executable is more recent than
7385 the objects.
7386 @end table
7388 @geindex -C (gnatmake)
7391 @table @asis
7393 @item @code{-C}
7395 Use a temporary mapping file. A mapping file is a way to communicate
7396 to the compiler two mappings: from unit names to file names (without
7397 any directory information) and from file names to path names (with
7398 full directory information). A mapping file can make the compiler's
7399 file searches faster, especially if there are many source directories,
7400 or the sources are read over a slow network connection. If
7401 @emph{-P} is used, a mapping file is always used, so
7402 @emph{-C} is unnecessary; in this case the mapping file
7403 is initially populated based on the project file. If
7404 @emph{-C} is used without
7405 @emph{-P},
7406 the mapping file is initially empty. Each invocation of the compiler
7407 will add any newly accessed sources to the mapping file.
7408 @end table
7410 @geindex -C= (gnatmake)
7413 @table @asis
7415 @item @code{-C=@emph{file}}
7417 Use a specific mapping file. The file, specified as a path name (absolute or
7418 relative) by this switch, should already exist, otherwise the switch is
7419 ineffective. The specified mapping file will be communicated to the compiler.
7420 This switch is not compatible with a project file
7421 (-P`file`) or with multiple compiling processes
7422 (-jnnn, when nnn is greater than 1).
7423 @end table
7425 @geindex -d (gnatmake)
7428 @table @asis
7430 @item @code{-d}
7432 Display progress for each source, up to date or not, as a single line:
7434 @example
7435 completed x out of y (zz%)
7436 @end example
7438 If the file needs to be compiled this is displayed after the invocation of
7439 the compiler. These lines are displayed even in quiet output mode.
7440 @end table
7442 @geindex -D (gnatmake)
7445 @table @asis
7447 @item @code{-D @emph{dir}}
7449 Put all object files and ALI file in directory @cite{dir}.
7450 If the @emph{-D} switch is not used, all object files
7451 and ALI files go in the current working directory.
7453 This switch cannot be used when using a project file.
7454 @end table
7456 @geindex -eI (gnatmake)
7459 @table @asis
7461 @item @code{-eI@emph{nnn}}
7463 Indicates that the main source is a multi-unit source and the rank of the unit
7464 in the source file is nnn. nnn needs to be a positive number and a valid
7465 index in the source. This switch cannot be used when @emph{gnatmake} is
7466 invoked for several mains.
7467 @end table
7469 @geindex -eL (gnatmake)
7471 @geindex symbolic links
7474 @table @asis
7476 @item @code{-eL}
7478 Follow all symbolic links when processing project files.
7479 This should be used if your project uses symbolic links for files or
7480 directories, but is not needed in other cases.
7482 @geindex naming scheme
7484 This also assumes that no directory matches the naming scheme for files (for
7485 instance that you do not have a directory called "sources.ads" when using the
7486 default GNAT naming scheme).
7488 When you do not have to use this switch (i.e., by default), gnatmake is able to
7489 save a lot of system calls (several per source file and object file), which
7490 can result in a significant speed up to load and manipulate a project file,
7491 especially when using source files from a remote system.
7492 @end table
7494 @geindex -eS (gnatmake)
7497 @table @asis
7499 @item @code{-eS}
7501 Output the commands for the compiler, the binder and the linker
7502 on standard output,
7503 instead of standard error.
7504 @end table
7506 @geindex -f (gnatmake)
7509 @table @asis
7511 @item @code{-f}
7513 Force recompilations. Recompile all sources, even though some object
7514 files may be up to date, but don't recompile predefined or GNAT internal
7515 files or locked files (files with a write-protected ALI file),
7516 unless the @emph{-a} switch is also specified.
7517 @end table
7519 @geindex -F (gnatmake)
7522 @table @asis
7524 @item @code{-F}
7526 When using project files, if some errors or warnings are detected during
7527 parsing and verbose mode is not in effect (no use of switch
7528 -v), then error lines start with the full path name of the project
7529 file, rather than its simple file name.
7530 @end table
7532 @geindex -g (gnatmake)
7535 @table @asis
7537 @item @code{-g}
7539 Enable debugging. This switch is simply passed to the compiler and to the
7540 linker.
7541 @end table
7543 @geindex -i (gnatmake)
7546 @table @asis
7548 @item @code{-i}
7550 In normal mode, @emph{gnatmake} compiles all object files and ALI files
7551 into the current directory. If the @emph{-i} switch is used,
7552 then instead object files and ALI files that already exist are overwritten
7553 in place. This means that once a large project is organized into separate
7554 directories in the desired manner, then @emph{gnatmake} will automatically
7555 maintain and update this organization. If no ALI files are found on the
7556 Ada object path (see @ref{8e,,Search Paths and the Run-Time Library (RTL)}),
7557 the new object and ALI files are created in the
7558 directory containing the source being compiled. If another organization
7559 is desired, where objects and sources are kept in different directories,
7560 a useful technique is to create dummy ALI files in the desired directories.
7561 When detecting such a dummy file, @emph{gnatmake} will be forced to
7562 recompile the corresponding source file, and it will be put the resulting
7563 object and ALI files in the directory where it found the dummy file.
7564 @end table
7566 @geindex -j (gnatmake)
7568 @geindex Parallel make
7571 @table @asis
7573 @item @code{-j@emph{n}}
7575 Use @cite{n} processes to carry out the (re)compilations. On a multiprocessor
7576 machine compilations will occur in parallel. If @cite{n} is 0, then the
7577 maximum number of parallel compilations is the number of core processors
7578 on the platform. In the event of compilation errors, messages from various
7579 compilations might get interspersed (but @emph{gnatmake} will give you the
7580 full ordered list of failing compiles at the end). If this is problematic,
7581 rerun the make process with n set to 1 to get a clean list of messages.
7582 @end table
7584 @geindex -k (gnatmake)
7587 @table @asis
7589 @item @code{-k}
7591 Keep going. Continue as much as possible after a compilation error. To
7592 ease the programmer's task in case of compilation errors, the list of
7593 sources for which the compile fails is given when @emph{gnatmake}
7594 terminates.
7596 If @emph{gnatmake} is invoked with several @code{file_names} and with this
7597 switch, if there are compilation errors when building an executable,
7598 @emph{gnatmake} will not attempt to build the following executables.
7599 @end table
7601 @geindex -l (gnatmake)
7604 @table @asis
7606 @item @code{-l}
7608 Link only. Can be combined with @emph{-b} to binding
7609 and linking. Linking will not be performed if combined with
7610 @emph{-c}
7611 but not with @emph{-b}.
7612 When not combined with @emph{-b}
7613 all the units in the closure of the main program must have been previously
7614 compiled and must be up to date, and the main program needs to have been bound.
7615 The root unit specified by @cite{file_name}
7616 may be given without extension, with the source extension or, if no GNAT
7617 Project File is specified, with the ALI file extension.
7618 @end table
7620 @geindex -m (gnatmake)
7623 @table @asis
7625 @item @code{-m}
7627 Specify that the minimum necessary amount of recompilations
7628 be performed. In this mode @emph{gnatmake} ignores time
7629 stamp differences when the only
7630 modifications to a source file consist in adding/removing comments,
7631 empty lines, spaces or tabs. This means that if you have changed the
7632 comments in a source file or have simply reformatted it, using this
7633 switch will tell @emph{gnatmake} not to recompile files that depend on it
7634 (provided other sources on which these files depend have undergone no
7635 semantic modifications). Note that the debugging information may be
7636 out of date with respect to the sources if the @emph{-m} switch causes
7637 a compilation to be switched, so the use of this switch represents a
7638 trade-off between compilation time and accurate debugging information.
7639 @end table
7641 @geindex Dependencies
7642 @geindex producing list
7644 @geindex -M (gnatmake)
7647 @table @asis
7649 @item @code{-M}
7651 Check if all objects are up to date. If they are, output the object
7652 dependences to @code{stdout} in a form that can be directly exploited in
7653 a @code{Makefile}. By default, each source file is prefixed with its
7654 (relative or absolute) directory name. This name is whatever you
7655 specified in the various @emph{-aI}
7656 and @emph{-I} switches. If you use
7657 @cite{gnatmake -M}  @emph{-q}
7658 (see below), only the source file names,
7659 without relative paths, are output. If you just specify the  @emph{-M}
7660 switch, dependencies of the GNAT internal system files are omitted. This
7661 is typically what you want. If you also specify
7662 the @emph{-a} switch,
7663 dependencies of the GNAT internal files are also listed. Note that
7664 dependencies of the objects in external Ada libraries (see
7665 switch  @code{-aL@emph{dir}} in the following list)
7666 are never reported.
7667 @end table
7669 @geindex -n (gnatmake)
7672 @table @asis
7674 @item @code{-n}
7676 Don't compile, bind, or link. Checks if all objects are up to date.
7677 If they are not, the full name of the first file that needs to be
7678 recompiled is printed.
7679 Repeated use of this option, followed by compiling the indicated source
7680 file, will eventually result in recompiling all required units.
7681 @end table
7683 @geindex -o (gnatmake)
7686 @table @asis
7688 @item @code{-o @emph{exec_name}}
7690 Output executable name. The name of the final executable program will be
7691 @cite{exec_name}. If the @emph{-o} switch is omitted the default
7692 name for the executable will be the name of the input file in appropriate form
7693 for an executable file on the host system.
7695 This switch cannot be used when invoking @emph{gnatmake} with several
7696 @code{file_names}.
7697 @end table
7699 @geindex -p (gnatmake)
7702 @table @asis
7704 @item @code{-p}
7706 Same as @code{--create-missing-dirs}
7707 @end table
7709 @geindex -P (gnatmake)
7712 @table @asis
7714 @item @code{-P@emph{project}}
7716 Use project file @cite{project}. Only one such switch can be used.
7717 @ref{e1,,gnatmake and Project Files}.
7718 @end table
7720 @geindex -q (gnatmake)
7723 @table @asis
7725 @item @code{-q}
7727 Quiet. When this flag is not set, the commands carried out by
7728 @emph{gnatmake} are displayed.
7729 @end table
7731 @geindex -s (gnatmake)
7734 @table @asis
7736 @item @code{-s}
7738 Recompile if compiler switches have changed since last compilation.
7739 All compiler switches but -I and -o are taken into account in the
7740 following way:
7741 orders between different 'first letter' switches are ignored, but
7742 orders between same switches are taken into account. For example,
7743 @emph{-O -O2} is different than @emph{-O2 -O}, but @emph{-g -O}
7744 is equivalent to @emph{-O -g}.
7746 This switch is recommended when Integrated Preprocessing is used.
7747 @end table
7749 @geindex -u (gnatmake)
7752 @table @asis
7754 @item @code{-u}
7756 Unique. Recompile at most the main files. It implies -c. Combined with
7757 -f, it is equivalent to calling the compiler directly. Note that using
7758 -u with a project file and no main has a special meaning
7759 (@ref{e2,,Project Files and Main Subprograms}).
7760 @end table
7762 @geindex -U (gnatmake)
7765 @table @asis
7767 @item @code{-U}
7769 When used without a project file or with one or several mains on the command
7770 line, is equivalent to -u. When used with a project file and no main
7771 on the command line, all sources of all project files are checked and compiled
7772 if not up to date, and libraries are rebuilt, if necessary.
7773 @end table
7775 @geindex -v (gnatmake)
7778 @table @asis
7780 @item @code{-v}
7782 Verbose. Display the reason for all recompilations @emph{gnatmake}
7783 decides are necessary, with the highest verbosity level.
7784 @end table
7786 @geindex -vl (gnatmake)
7789 @table @asis
7791 @item @code{-vl}
7793 Verbosity level Low. Display fewer lines than in verbosity Medium.
7794 @end table
7796 @geindex -vm (gnatmake)
7799 @table @asis
7801 @item @code{-vm}
7803 Verbosity level Medium. Potentially display fewer lines than in verbosity High.
7804 @end table
7806 @geindex -vm (gnatmake)
7809 @table @asis
7811 @item @code{-vh}
7813 Verbosity level High. Equivalent to -v.
7815 @item @code{-vP@emph{x}}
7817 Indicate the verbosity of the parsing of GNAT project files.
7818 See @ref{e3,,Switches Related to Project Files}.
7819 @end table
7821 @geindex -x (gnatmake)
7824 @table @asis
7826 @item @code{-x}
7828 Indicate that sources that are not part of any Project File may be compiled.
7829 Normally, when using Project Files, only sources that are part of a Project
7830 File may be compile. When this switch is used, a source outside of all Project
7831 Files may be compiled. The ALI file and the object file will be put in the
7832 object directory of the main Project. The compilation switches used will only
7833 be those specified on the command line. Even when
7834 @emph{-x} is used, mains specified on the
7835 command line need to be sources of a project file.
7837 @item @code{-X@emph{name}=@emph{value}}
7839 Indicate that external variable @cite{name} has the value @cite{value}.
7840 The Project Manager will use this value for occurrences of
7841 @cite{external(name)} when parsing the project file.
7842 @ref{e3,,Switches Related to Project Files}.
7843 @end table
7845 @geindex -z (gnatmake)
7848 @table @asis
7850 @item @code{-z}
7852 No main subprogram. Bind and link the program even if the unit name
7853 given on the command line is a package name. The resulting executable
7854 will execute the elaboration routines of the package and its closure,
7855 then the finalization routines.
7856 @end table
7858 @subsubheading GCC switches
7861 Any uppercase or multi-character switch that is not a @emph{gnatmake} switch
7862 is passed to @emph{gcc} (e.g., @emph{-O}, @emph{-gnato,} etc.)
7864 @subsubheading Source and library search path switches
7867 @geindex -aI (gnatmake)
7870 @table @asis
7872 @item @code{-aI@emph{dir}}
7874 When looking for source files also look in directory @cite{dir}.
7875 The order in which source files search is undertaken is
7876 described in @ref{8e,,Search Paths and the Run-Time Library (RTL)}.
7877 @end table
7879 @geindex -aL (gnatmake)
7882 @table @asis
7884 @item @code{-aL@emph{dir}}
7886 Consider @cite{dir} as being an externally provided Ada library.
7887 Instructs @emph{gnatmake} to skip compilation units whose @code{.ALI}
7888 files have been located in directory @cite{dir}. This allows you to have
7889 missing bodies for the units in @cite{dir} and to ignore out of date bodies
7890 for the same units. You still need to specify
7891 the location of the specs for these units by using the switches
7892 @code{-aI@emph{dir}}  or @code{-I@emph{dir}}.
7893 Note: this switch is provided for compatibility with previous versions
7894 of @emph{gnatmake}. The easier method of causing standard libraries
7895 to be excluded from consideration is to write-protect the corresponding
7896 ALI files.
7897 @end table
7899 @geindex -aO (gnatmake)
7902 @table @asis
7904 @item @code{-aO@emph{dir}}
7906 When searching for library and object files, look in directory
7907 @cite{dir}. The order in which library files are searched is described in
7908 @ref{91,,Search Paths for gnatbind}.
7909 @end table
7911 @geindex Search paths
7912 @geindex for gnatmake
7914 @geindex -A (gnatmake)
7917 @table @asis
7919 @item @code{-A@emph{dir}}
7921 Equivalent to @code{-aL@emph{dir}} @code{-aI@emph{dir}}.
7923 @geindex -I (gnatmake)
7925 @item @code{-I@emph{dir}}
7927 Equivalent to @code{-aO@emph{dir} -aI@emph{dir}}.
7928 @end table
7930 @geindex -I- (gnatmake)
7932 @geindex Source files
7933 @geindex suppressing search
7936 @table @asis
7938 @item @code{-I-}
7940 Do not look for source files in the directory containing the source
7941 file named in the command line.
7942 Do not look for ALI or object files in the directory
7943 where @emph{gnatmake} was invoked.
7944 @end table
7946 @geindex -L (gnatmake)
7948 @geindex Linker libraries
7951 @table @asis
7953 @item @code{-L@emph{dir}}
7955 Add directory @cite{dir} to the list of directories in which the linker
7956 will search for libraries. This is equivalent to
7957 @code{-largs} @code{-L@emph{dir}}.
7958 Furthermore, under Windows, the sources pointed to by the libraries path
7959 set in the registry are not searched for.
7960 @end table
7962 @geindex -nostdinc (gnatmake)
7965 @table @asis
7967 @item @code{-nostdinc}
7969 Do not look for source files in the system default directory.
7970 @end table
7972 @geindex -nostdlib (gnatmake)
7975 @table @asis
7977 @item @code{-nostdlib}
7979 Do not look for library files in the system default directory.
7980 @end table
7982 @geindex --RTS (gnatmake)
7985 @table @asis
7987 @item @code{--RTS=@emph{rts-path}}
7989 Specifies the default location of the runtime library. GNAT looks for the
7990 runtime
7991 in the following directories, and stops as soon as a valid runtime is found
7992 (@code{adainclude} or @code{ada_source_path}, and @code{adalib} or
7993 @code{ada_object_path} present):
7996 @itemize *
7998 @item 
7999 @emph{<current directory>/$rts_path}
8001 @item 
8002 @emph{<default-search-dir>/$rts_path}
8004 @item 
8005 @emph{<default-search-dir>/rts-$rts_path}
8007 @item 
8008 The selected path is handled like a normal RTS path.
8009 @end itemize
8010 @end table
8012 @node Mode Switches for gnatmake,Notes on the Command Line,Switches for gnatmake,Building with gnatmake
8013 @anchor{gnat_ugn/building_executable_programs_with_gnat id4}@anchor{e4}@anchor{gnat_ugn/building_executable_programs_with_gnat mode-switches-for-gnatmake}@anchor{e5}
8014 @subsection Mode Switches for @emph{gnatmake}
8017 The mode switches (referred to as @cite{mode_switches}) allow the
8018 inclusion of switches that are to be passed to the compiler itself, the
8019 binder or the linker. The effect of a mode switch is to cause all
8020 subsequent switches up to the end of the switch list, or up to the next
8021 mode switch, to be interpreted as switches to be passed on to the
8022 designated component of GNAT.
8024 @geindex -cargs (gnatmake)
8027 @table @asis
8029 @item @code{-cargs @emph{switches}}
8031 Compiler switches. Here @cite{switches} is a list of switches
8032 that are valid switches for @emph{gcc}. They will be passed on to
8033 all compile steps performed by @emph{gnatmake}.
8034 @end table
8036 @geindex -bargs (gnatmake)
8039 @table @asis
8041 @item @code{-bargs @emph{switches}}
8043 Binder switches. Here @cite{switches} is a list of switches
8044 that are valid switches for @cite{gnatbind}. They will be passed on to
8045 all bind steps performed by @emph{gnatmake}.
8046 @end table
8048 @geindex -largs (gnatmake)
8051 @table @asis
8053 @item @code{-largs @emph{switches}}
8055 Linker switches. Here @cite{switches} is a list of switches
8056 that are valid switches for @emph{gnatlink}. They will be passed on to
8057 all link steps performed by @emph{gnatmake}.
8058 @end table
8060 @geindex -margs (gnatmake)
8063 @table @asis
8065 @item @code{-margs @emph{switches}}
8067 Make switches. The switches are directly interpreted by @emph{gnatmake},
8068 regardless of any previous occurrence of @emph{-cargs}, @emph{-bargs}
8069 or @emph{-largs}.
8070 @end table
8072 @node Notes on the Command Line,How gnatmake Works,Mode Switches for gnatmake,Building with gnatmake
8073 @anchor{gnat_ugn/building_executable_programs_with_gnat id5}@anchor{e6}@anchor{gnat_ugn/building_executable_programs_with_gnat notes-on-the-command-line}@anchor{e7}
8074 @subsection Notes on the Command Line
8077 This section contains some additional useful notes on the operation
8078 of the @emph{gnatmake} command.
8080 @geindex Recompilation (by gnatmake)
8083 @itemize *
8085 @item 
8086 If @emph{gnatmake} finds no ALI files, it recompiles the main program
8087 and all other units required by the main program.
8088 This means that @emph{gnatmake}
8089 can be used for the initial compile, as well as during subsequent steps of
8090 the development cycle.
8092 @item 
8093 If you enter @code{gnatmake foo.adb}, where @code{foo}
8094 is a subunit or body of a generic unit, @emph{gnatmake} recompiles
8095 @code{foo.adb} (because it finds no ALI) and stops, issuing a
8096 warning.
8098 @item 
8099 In @emph{gnatmake} the switch @emph{-I}
8100 is used to specify both source and
8101 library file paths. Use @emph{-aI}
8102 instead if you just want to specify
8103 source paths only and @emph{-aO}
8104 if you want to specify library paths
8105 only.
8107 @item 
8108 @emph{gnatmake} will ignore any files whose ALI file is write-protected.
8109 This may conveniently be used to exclude standard libraries from
8110 consideration and in particular it means that the use of the
8111 @emph{-f} switch will not recompile these files
8112 unless @emph{-a} is also specified.
8114 @item 
8115 @emph{gnatmake} has been designed to make the use of Ada libraries
8116 particularly convenient. Assume you have an Ada library organized
8117 as follows: @emph{obj-dir} contains the objects and ALI files for
8118 of your Ada compilation units,
8119 whereas @emph{include-dir} contains the
8120 specs of these units, but no bodies. Then to compile a unit
8121 stored in @cite{main.adb}, which uses this Ada library you would just type:
8123 @example
8124 $ gnatmake -aI`include-dir`  -aL`obj-dir`  main
8125 @end example
8127 @item 
8128 Using @emph{gnatmake} along with the @emph{-m (minimal recompilation)}
8129 switch provides a mechanism for avoiding unnecessary recompilations. Using
8130 this switch,
8131 you can update the comments/format of your
8132 source files without having to recompile everything. Note, however, that
8133 adding or deleting lines in a source files may render its debugging
8134 info obsolete. If the file in question is a spec, the impact is rather
8135 limited, as that debugging info will only be useful during the
8136 elaboration phase of your program. For bodies the impact can be more
8137 significant. In all events, your debugger will warn you if a source file
8138 is more recent than the corresponding object, and alert you to the fact
8139 that the debugging information may be out of date.
8140 @end itemize
8142 @node How gnatmake Works,Examples of gnatmake Usage,Notes on the Command Line,Building with gnatmake
8143 @anchor{gnat_ugn/building_executable_programs_with_gnat id6}@anchor{e8}@anchor{gnat_ugn/building_executable_programs_with_gnat how-gnatmake-works}@anchor{e9}
8144 @subsection How @emph{gnatmake} Works
8147 Generally @emph{gnatmake} automatically performs all necessary
8148 recompilations and you don't need to worry about how it works. However,
8149 it may be useful to have some basic understanding of the @emph{gnatmake}
8150 approach and in particular to understand how it uses the results of
8151 previous compilations without incorrectly depending on them.
8153 First a definition: an object file is considered @emph{up to date} if the
8154 corresponding ALI file exists and if all the source files listed in the
8155 dependency section of this ALI file have time stamps matching those in
8156 the ALI file. This means that neither the source file itself nor any
8157 files that it depends on have been modified, and hence there is no need
8158 to recompile this file.
8160 @emph{gnatmake} works by first checking if the specified main unit is up
8161 to date. If so, no compilations are required for the main unit. If not,
8162 @emph{gnatmake} compiles the main program to build a new ALI file that
8163 reflects the latest sources. Then the ALI file of the main unit is
8164 examined to find all the source files on which the main program depends,
8165 and @emph{gnatmake} recursively applies the above procedure on all these
8166 files.
8168 This process ensures that @emph{gnatmake} only trusts the dependencies
8169 in an existing ALI file if they are known to be correct. Otherwise it
8170 always recompiles to determine a new, guaranteed accurate set of
8171 dependencies. As a result the program is compiled 'upside down' from what may
8172 be more familiar as the required order of compilation in some other Ada
8173 systems. In particular, clients are compiled before the units on which
8174 they depend. The ability of GNAT to compile in any order is critical in
8175 allowing an order of compilation to be chosen that guarantees that
8176 @emph{gnatmake} will recompute a correct set of new dependencies if
8177 necessary.
8179 When invoking @emph{gnatmake} with several @cite{file_names}, if a unit is
8180 imported by several of the executables, it will be recompiled at most once.
8182 Note: when using non-standard naming conventions
8183 (@ref{37,,Using Other File Names}), changing through a configuration pragmas
8184 file the version of a source and invoking @emph{gnatmake} to recompile may
8185 have no effect, if the previous version of the source is still accessible
8186 by @emph{gnatmake}. It may be necessary to use the switch
8189 @node Examples of gnatmake Usage,,How gnatmake Works,Building with gnatmake
8190 @anchor{gnat_ugn/building_executable_programs_with_gnat examples-of-gnatmake-usage}@anchor{ea}@anchor{gnat_ugn/building_executable_programs_with_gnat id7}@anchor{eb}
8191 @subsection Examples of @emph{gnatmake} Usage
8195 @table @asis
8197 @item @emph{gnatmake hello.adb}
8199 Compile all files necessary to bind and link the main program
8200 @code{hello.adb} (containing unit @cite{Hello}) and bind and link the
8201 resulting object files to generate an executable file @code{hello}.
8203 @item @emph{gnatmake main1 main2 main3}
8205 Compile all files necessary to bind and link the main programs
8206 @code{main1.adb} (containing unit @cite{Main1}), @code{main2.adb}
8207 (containing unit @cite{Main2}) and @code{main3.adb}
8208 (containing unit @cite{Main3}) and bind and link the resulting object files
8209 to generate three executable files @code{main1},
8210 @code{main2}  and @code{main3}.
8212 @item @emph{gnatmake -q Main_Unit -cargs -O2 -bargs -l}
8214 Compile all files necessary to bind and link the main program unit
8215 @cite{Main_Unit} (from file @code{main_unit.adb}). All compilations will
8216 be done with optimization level 2 and the order of elaboration will be
8217 listed by the binder. @emph{gnatmake} will operate in quiet mode, not
8218 displaying commands it is executing.
8219 @end table
8221 @node Compiling with gcc,Compiler Switches,Building with gnatmake,Building Executable Programs with GNAT
8222 @anchor{gnat_ugn/building_executable_programs_with_gnat compiling-with-gcc}@anchor{1e}@anchor{gnat_ugn/building_executable_programs_with_gnat id8}@anchor{ec}
8223 @section Compiling with @emph{gcc}
8226 This section discusses how to compile Ada programs using the @emph{gcc}
8227 command. It also describes the set of switches
8228 that can be used to control the behavior of the compiler.
8230 @menu
8231 * Compiling Programs:: 
8232 * Search Paths and the Run-Time Library (RTL): Search Paths and the Run-Time Library RTL. 
8233 * Order of Compilation Issues:: 
8234 * Examples:: 
8236 @end menu
8238 @node Compiling Programs,Search Paths and the Run-Time Library RTL,,Compiling with gcc
8239 @anchor{gnat_ugn/building_executable_programs_with_gnat compiling-programs}@anchor{ed}@anchor{gnat_ugn/building_executable_programs_with_gnat id9}@anchor{ee}
8240 @subsection Compiling Programs
8243 The first step in creating an executable program is to compile the units
8244 of the program using the @emph{gcc} command. You must compile the
8245 following files:
8248 @itemize *
8250 @item 
8251 the body file (@code{.adb}) for a library level subprogram or generic
8252 subprogram
8254 @item 
8255 the spec file (@code{.ads}) for a library level package or generic
8256 package that has no body
8258 @item 
8259 the body file (@code{.adb}) for a library level package
8260 or generic package that has a body
8261 @end itemize
8263 You need @emph{not} compile the following files
8266 @itemize *
8268 @item 
8269 the spec of a library unit which has a body
8271 @item 
8272 subunits
8273 @end itemize
8275 because they are compiled as part of compiling related units. GNAT
8276 package specs
8277 when the corresponding body is compiled, and subunits when the parent is
8278 compiled.
8280 @geindex cannot generate code
8282 If you attempt to compile any of these files, you will get one of the
8283 following error messages (where @cite{fff} is the name of the file you
8284 compiled):
8286 @quotation
8288 @example
8289 cannot generate code for file `fff` (package spec)
8290 to check package spec, use -gnatc
8292 cannot generate code for file `fff` (missing subunits)
8293 to check parent unit, use -gnatc
8295 cannot generate code for file `fff` (subprogram spec)
8296 to check subprogram spec, use -gnatc
8298 cannot generate code for file `fff` (subunit)
8299 to check subunit, use -gnatc
8300 @end example
8301 @end quotation
8303 As indicated by the above error messages, if you want to submit
8304 one of these files to the compiler to check for correct semantics
8305 without generating code, then use the @emph{-gnatc} switch.
8307 The basic command for compiling a file containing an Ada unit is:
8309 @example
8310 $ gcc -c [switches] <file name>
8311 @end example
8313 where @cite{file name} is the name of the Ada file (usually
8314 having an extension @code{.ads} for a spec or @code{.adb} for a body).
8315 You specify the
8316 @code{-c} switch to tell @emph{gcc} to compile, but not link, the file.
8317 The result of a successful compilation is an object file, which has the
8318 same name as the source file but an extension of @code{.o} and an Ada
8319 Library Information (ALI) file, which also has the same name as the
8320 source file, but with @code{.ali} as the extension. GNAT creates these
8321 two output files in the current directory, but you may specify a source
8322 file in any directory using an absolute or relative path specification
8323 containing the directory information.
8325 @geindex gnat1
8327 @emph{gcc} is actually a driver program that looks at the extensions of
8328 the file arguments and loads the appropriate compiler. For example, the
8329 GNU C compiler is @code{cc1}, and the Ada compiler is @code{gnat1}.
8330 These programs are in directories known to the driver program (in some
8331 configurations via environment variables you set), but need not be in
8332 your path. The @emph{gcc} driver also calls the assembler and any other
8333 utilities needed to complete the generation of the required object
8334 files.
8336 It is possible to supply several file names on the same @emph{gcc}
8337 command. This causes @emph{gcc} to call the appropriate compiler for
8338 each file. For example, the following command lists two separate
8339 files to be compiled:
8341 @example
8342 $ gcc -c x.adb y.adb
8343 @end example
8345 calls @cite{gnat1} (the Ada compiler) twice to compile @code{x.adb} and
8346 @code{y.adb}.
8347 The compiler generates two object files @code{x.o} and @code{y.o}
8348 and the two ALI files @code{x.ali} and @code{y.ali}.
8350 Any switches apply to all the files listed, see @ref{ef,,Compiler Switches} for a
8351 list of available @emph{gcc} switches.
8353 @node Search Paths and the Run-Time Library RTL,Order of Compilation Issues,Compiling Programs,Compiling with gcc
8354 @anchor{gnat_ugn/building_executable_programs_with_gnat id10}@anchor{f0}@anchor{gnat_ugn/building_executable_programs_with_gnat search-paths-and-the-run-time-library-rtl}@anchor{8e}
8355 @subsection Search Paths and the Run-Time Library (RTL)
8358 With the GNAT source-based library system, the compiler must be able to
8359 find source files for units that are needed by the unit being compiled.
8360 Search paths are used to guide this process.
8362 The compiler compiles one source file whose name must be given
8363 explicitly on the command line. In other words, no searching is done
8364 for this file. To find all other source files that are needed (the most
8365 common being the specs of units), the compiler examines the following
8366 directories, in the following order:
8369 @itemize *
8371 @item 
8372 The directory containing the source file of the main unit being compiled
8373 (the file name on the command line).
8375 @item 
8376 Each directory named by an @emph{-I} switch given on the @emph{gcc}
8377 command line, in the order given.
8379 @geindex ADA_PRJ_INCLUDE_FILE
8381 @item 
8382 Each of the directories listed in the text file whose name is given
8383 by the 
8384 @geindex ADA_PRJ_INCLUDE_FILE
8385 @geindex environment variable; ADA_PRJ_INCLUDE_FILE
8386 @code{ADA_PRJ_INCLUDE_FILE} environment variable.
8387 @geindex ADA_PRJ_INCLUDE_FILE
8388 @geindex environment variable; ADA_PRJ_INCLUDE_FILE
8389 @code{ADA_PRJ_INCLUDE_FILE} is normally set by gnatmake or by the gnat
8390 driver when project files are used. It should not normally be set
8391 by other means.
8393 @geindex ADA_INCLUDE_PATH
8395 @item 
8396 Each of the directories listed in the value of the
8397 @geindex ADA_INCLUDE_PATH
8398 @geindex environment variable; ADA_INCLUDE_PATH
8399 @code{ADA_INCLUDE_PATH} environment variable.
8400 Construct this value
8401 exactly as the 
8402 @geindex PATH
8403 @geindex environment variable; PATH
8404 @code{PATH} environment variable: a list of directory
8405 names separated by colons (semicolons when working with the NT version).
8407 @item 
8408 The content of the @code{ada_source_path} file which is part of the GNAT
8409 installation tree and is used to store standard libraries such as the
8410 GNAT Run Time Library (RTL) source files.
8411 @ref{8b,,Installing a library}
8412 @end itemize
8414 Specifying the switch @emph{-I-}
8415 inhibits the use of the directory
8416 containing the source file named in the command line. You can still
8417 have this directory on your search path, but in this case it must be
8418 explicitly requested with a @emph{-I} switch.
8420 Specifying the switch @emph{-nostdinc}
8421 inhibits the search of the default location for the GNAT Run Time
8422 Library (RTL) source files.
8424 The compiler outputs its object files and ALI files in the current
8425 working directory.
8426 Caution: The object file can be redirected with the @emph{-o} switch;
8427 however, @emph{gcc} and @cite{gnat1} have not been coordinated on this
8428 so the @code{ALI} file will not go to the right place. Therefore, you should
8429 avoid using the @emph{-o} switch.
8431 @geindex System.IO
8433 The packages @cite{Ada}, @cite{System}, and @cite{Interfaces} and their
8434 children make up the GNAT RTL, together with the simple @cite{System.IO}
8435 package used in the @cite{"Hello World"} example. The sources for these units
8436 are needed by the compiler and are kept together in one directory. Not
8437 all of the bodies are needed, but all of the sources are kept together
8438 anyway. In a normal installation, you need not specify these directory
8439 names when compiling or binding. Either the environment variables or
8440 the built-in defaults cause these files to be found.
8442 In addition to the language-defined hierarchies (@cite{System}, @cite{Ada} and
8443 @cite{Interfaces}), the GNAT distribution provides a fourth hierarchy,
8444 consisting of child units of @cite{GNAT}. This is a collection of generally
8445 useful types, subprograms, etc. See the @cite{GNAT_Reference_Manual}
8446 for further details.
8448 Besides simplifying access to the RTL, a major use of search paths is
8449 in compiling sources from multiple directories. This can make
8450 development environments much more flexible.
8452 @node Order of Compilation Issues,Examples,Search Paths and the Run-Time Library RTL,Compiling with gcc
8453 @anchor{gnat_ugn/building_executable_programs_with_gnat id11}@anchor{f1}@anchor{gnat_ugn/building_executable_programs_with_gnat order-of-compilation-issues}@anchor{f2}
8454 @subsection Order of Compilation Issues
8457 If, in our earlier example, there was a spec for the @cite{hello}
8458 procedure, it would be contained in the file @code{hello.ads}; yet this
8459 file would not have to be explicitly compiled. This is the result of the
8460 model we chose to implement library management. Some of the consequences
8461 of this model are as follows:
8464 @itemize *
8466 @item 
8467 There is no point in compiling specs (except for package
8468 specs with no bodies) because these are compiled as needed by clients. If
8469 you attempt a useless compilation, you will receive an error message.
8470 It is also useless to compile subunits because they are compiled as needed
8471 by the parent.
8473 @item 
8474 There are no order of compilation requirements: performing a
8475 compilation never obsoletes anything. The only way you can obsolete
8476 something and require recompilations is to modify one of the
8477 source files on which it depends.
8479 @item 
8480 There is no library as such, apart from the ALI files
8481 (@ref{44,,The Ada Library Information Files}, for information on the format
8482 of these files). For now we find it convenient to create separate ALI files,
8483 but eventually the information therein may be incorporated into the object
8484 file directly.
8486 @item 
8487 When you compile a unit, the source files for the specs of all units
8488 that it @emph{with}s, all its subunits, and the bodies of any generics it
8489 instantiates must be available (reachable by the search-paths mechanism
8490 described above), or you will receive a fatal error message.
8491 @end itemize
8493 @node Examples,,Order of Compilation Issues,Compiling with gcc
8494 @anchor{gnat_ugn/building_executable_programs_with_gnat id12}@anchor{f3}@anchor{gnat_ugn/building_executable_programs_with_gnat examples}@anchor{f4}
8495 @subsection Examples
8498 The following are some typical Ada compilation command line examples:
8500 @example
8501 $ gcc -c xyz.adb
8502 @end example
8504 Compile body in file @code{xyz.adb} with all default options.
8506 @example
8507 $ gcc -c -O2 -gnata xyz-def.adb
8508 @end example
8510 Compile the child unit package in file @code{xyz-def.adb} with extensive
8511 optimizations, and pragma @cite{Assert}/@cite{Debug} statements
8512 enabled.
8514 @example
8515 $ gcc -c -gnatc abc-def.adb
8516 @end example
8518 Compile the subunit in file @code{abc-def.adb} in semantic-checking-only
8519 mode.
8521 @node Compiler Switches,Binding with gnatbind,Compiling with gcc,Building Executable Programs with GNAT
8522 @anchor{gnat_ugn/building_executable_programs_with_gnat compiler-switches}@anchor{f5}@anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gcc}@anchor{ef}
8523 @section Compiler Switches
8526 The @emph{gcc} command accepts switches that control the
8527 compilation process. These switches are fully described in this section:
8528 first an alphabetical listing of all switches with a brief description,
8529 and then functionally grouped sets of switches with more detailed
8530 information.
8532 More switches exist for GCC than those documented here, especially
8533 for specific targets. However, their use is not recommended as
8534 they may change code generation in ways that are incompatible with
8535 the Ada run-time library, or can cause inconsistencies between
8536 compilation units.
8538 @menu
8539 * Alphabetical List of All Switches:: 
8540 * Output and Error Message Control:: 
8541 * Warning Message Control:: 
8542 * Debugging and Assertion Control:: 
8543 * Validity Checking:: 
8544 * Style Checking:: 
8545 * Run-Time Checks:: 
8546 * Using gcc for Syntax Checking:: 
8547 * Using gcc for Semantic Checking:: 
8548 * Compiling Different Versions of Ada:: 
8549 * Character Set Control:: 
8550 * File Naming Control:: 
8551 * Subprogram Inlining Control:: 
8552 * Auxiliary Output Control:: 
8553 * Debugging Control:: 
8554 * Exception Handling Control:: 
8555 * Units to Sources Mapping Files:: 
8556 * Code Generation Control:: 
8558 @end menu
8560 @node Alphabetical List of All Switches,Output and Error Message Control,,Compiler Switches
8561 @anchor{gnat_ugn/building_executable_programs_with_gnat id13}@anchor{f6}@anchor{gnat_ugn/building_executable_programs_with_gnat alphabetical-list-of-all-switches}@anchor{f7}
8562 @subsection Alphabetical List of All Switches
8565 @geindex -b (gcc)
8568 @table @asis
8570 @item @code{-b @emph{target}}
8572 Compile your program to run on @cite{target}, which is the name of a
8573 system configuration. You must have a GNAT cross-compiler built if
8574 @cite{target} is not the same as your host system.
8575 @end table
8577 @geindex -B (gcc)
8580 @table @asis
8582 @item @code{-B@emph{dir}}
8584 Load compiler executables (for example, @cite{gnat1}, the Ada compiler)
8585 from @cite{dir} instead of the default location. Only use this switch
8586 when multiple versions of the GNAT compiler are available.
8587 See the "Options for Directory Search" section in the
8588 @cite{Using the GNU Compiler Collection (GCC)} manual for further details.
8589 You would normally use the @emph{-b} or @emph{-V} switch instead.
8590 @end table
8592 @geindex -c (gcc)
8595 @table @asis
8597 @item @code{-c}
8599 Compile. Always use this switch when compiling Ada programs.
8601 Note: for some other languages when using @emph{gcc}, notably in
8602 the case of C and C++, it is possible to use
8603 use @emph{gcc} without a @emph{-c} switch to
8604 compile and link in one step. In the case of GNAT, you
8605 cannot use this approach, because the binder must be run
8606 and @emph{gcc} cannot be used to run the GNAT binder.
8607 @end table
8609 @geindex -fcallgraph-info (gcc)
8612 @table @asis
8614 @item @code{-fcallgraph-info[=su,da]}
8616 Makes the compiler output callgraph information for the program, on a
8617 per-file basis. The information is generated in the VCG format.  It can
8618 be decorated with additional, per-node and/or per-edge information, if a
8619 list of comma-separated markers is additionally specified. When the
8620 @cite{su} marker is specified, the callgraph is decorated with stack usage
8621 information; it is equivalent to @emph{-fstack-usage}. When the @cite{da}
8622 marker is specified, the callgraph is decorated with information about
8623 dynamically allocated objects.
8624 @end table
8626 @geindex -fdump-scos (gcc)
8629 @table @asis
8631 @item @code{-fdump-scos}
8633 Generates SCO (Source Coverage Obligation) information in the ALI file.
8634 This information is used by advanced coverage tools. See unit @code{SCOs}
8635 in the compiler sources for details in files @code{scos.ads} and
8636 @code{scos.adb}.
8637 @end table
8639 @geindex -fdump-xref (gcc)
8642 @table @asis
8644 @item @code{-fdump-xref}
8646 Generates cross reference information in GLI files for C and C++ sources.
8647 The GLI files have the same syntax as the ALI files for Ada, and can be used
8648 for source navigation in IDEs and on the command line using e.g. gnatxref
8649 and the @emph{--ext=gli} switch.
8650 @end table
8652 @geindex -flto (gcc)
8655 @table @asis
8657 @item @code{-flto[=@emph{n}]}
8659 Enables Link Time Optimization. This switch must be used in conjunction
8660 with the traditional @emph{-Ox} switches and instructs the compiler to
8661 defer most optimizations until the link stage. The advantage of this
8662 approach is that the compiler can do a whole-program analysis and choose
8663 the best interprocedural optimization strategy based on a complete view
8664 of the program, instead of a fragmentary view with the usual approach.
8665 This can also speed up the compilation of big programs and reduce the
8666 size of the executable, compared with a traditional per-unit compilation
8667 with inlining across modules enabled by the @emph{-gnatn} switch.
8668 The drawback of this approach is that it may require more memory and that
8669 the debugging information generated by -g with it might be hardly usable.
8670 The switch, as well as the accompanying @emph{-Ox} switches, must be
8671 specified both for the compilation and the link phases.
8672 If the @cite{n} parameter is specified, the optimization and final code
8673 generation at link time are executed using @cite{n} parallel jobs by
8674 means of an installed @emph{make} program.
8675 @end table
8677 @geindex -fno-inline (gcc)
8680 @table @asis
8682 @item @code{-fno-inline}
8684 Suppresses all inlining, unless requested with pragma @cite{Inline_Always}. The
8685 effect is enforced regardless of other optimization or inlining switches.
8686 Note that inlining can also be suppressed on a finer-grained basis with
8687 pragma @cite{No_Inline}.
8688 @end table
8690 @geindex -fno-inline-functions (gcc)
8693 @table @asis
8695 @item @code{-fno-inline-functions}
8697 Suppresses automatic inlining of subprograms, which is enabled
8698 if @emph{-O3} is used.
8699 @end table
8701 @geindex -fno-inline-small-functions (gcc)
8704 @table @asis
8706 @item @code{-fno-inline-small-functions}
8708 Suppresses automatic inlining of small subprograms, which is enabled
8709 if @emph{-O2} is used.
8710 @end table
8712 @geindex -fno-inline-functions-called-once (gcc)
8715 @table @asis
8717 @item @code{-fno-inline-functions-called-once}
8719 Suppresses inlining of subprograms local to the unit and called once
8720 from within it, which is enabled if @emph{-O1} is used.
8721 @end table
8723 @geindex -fno-ivopts (gcc)
8726 @table @asis
8728 @item @code{-fno-ivopts}
8730 Suppresses high-level loop induction variable optimizations, which are
8731 enabled if @emph{-O1} is used. These optimizations are generally
8732 profitable but, for some specific cases of loops with numerous uses
8733 of the iteration variable that follow a common pattern, they may end
8734 up destroying the regularity that could be exploited at a lower level
8735 and thus producing inferior code.
8736 @end table
8738 @geindex -fno-strict-aliasing (gcc)
8741 @table @asis
8743 @item @code{-fno-strict-aliasing}
8745 Causes the compiler to avoid assumptions regarding non-aliasing
8746 of objects of different types. See
8747 @ref{f8,,Optimization and Strict Aliasing} for details.
8748 @end table
8750 @geindex -fstack-check (gcc)
8753 @table @asis
8755 @item @code{-fstack-check}
8757 Activates stack checking.
8758 See @ref{f9,,Stack Overflow Checking} for details.
8759 @end table
8761 @geindex -fstack-usage (gcc)
8764 @table @asis
8766 @item @code{-fstack-usage}
8768 Makes the compiler output stack usage information for the program, on a
8769 per-subprogram basis. See @ref{fa,,Static Stack Usage Analysis} for details.
8770 @end table
8772 @geindex -g (gcc)
8775 @table @asis
8777 @item @code{-g}
8779 Generate debugging information. This information is stored in the object
8780 file and copied from there to the final executable file by the linker,
8781 where it can be read by the debugger. You must use the
8782 @emph{-g} switch if you plan on using the debugger.
8783 @end table
8785 @geindex -gnat05 (gcc)
8788 @table @asis
8790 @item @code{-gnat05}
8792 Allow full Ada 2005 features.
8793 @end table
8795 @geindex -gnat12 (gcc)
8798 @table @asis
8800 @item @code{-gnat12}
8802 Allow full Ada 2012 features.
8803 @end table
8805 @geindex -gnat83 (gcc)
8807 @geindex -gnat2005 (gcc)
8810 @table @asis
8812 @item @code{-gnat2005}
8814 Allow full Ada 2005 features (same as @emph{-gnat05})
8815 @end table
8817 @geindex -gnat2012 (gcc)
8820 @table @asis
8822 @item @code{-gnat2012}
8824 Allow full Ada 2012 features (same as @emph{-gnat12})
8826 @item @code{-gnat83}
8828 Enforce Ada 83 restrictions.
8829 @end table
8831 @geindex -gnat95 (gcc)
8834 @table @asis
8836 @item @code{-gnat95}
8838 Enforce Ada 95 restrictions.
8840 Note: for compatibility with some Ada 95 compilers which support only
8841 the @cite{overriding} keyword of Ada 2005, the @emph{-gnatd.D} switch can
8842 be used along with @emph{-gnat95} to achieve a similar effect with GNAT.
8844 @emph{-gnatd.D} instructs GNAT to consider @cite{overriding} as a keyword
8845 and handle its associated semantic checks, even in Ada 95 mode.
8846 @end table
8848 @geindex -gnata (gcc)
8851 @table @asis
8853 @item @code{-gnata}
8855 Assertions enabled. @cite{Pragma Assert} and @cite{pragma Debug} to be
8856 activated. Note that these pragmas can also be controlled using the
8857 configuration pragmas @cite{Assertion_Policy} and @cite{Debug_Policy}.
8858 It also activates pragmas @cite{Check}, @cite{Precondition}, and
8859 @cite{Postcondition}. Note that these pragmas can also be controlled
8860 using the configuration pragma @cite{Check_Policy}. In Ada 2012, it
8861 also activates all assertions defined in the RM as aspects: preconditions,
8862 postconditions, type invariants and (sub)type predicates. In all Ada modes,
8863 corresponding pragmas for type invariants and (sub)type predicates are
8864 also activated. The default is that all these assertions are disabled,
8865 and have no effect, other than being checked for syntactic validity, and
8866 in the case of subtype predicates, constructions such as membership tests
8867 still test predicates even if assertions are turned off.
8868 @end table
8870 @geindex -gnatA (gcc)
8873 @table @asis
8875 @item @code{-gnatA}
8877 Avoid processing @code{gnat.adc}. If a @code{gnat.adc} file is present,
8878 it will be ignored.
8879 @end table
8881 @geindex -gnatb (gcc)
8884 @table @asis
8886 @item @code{-gnatb}
8888 Generate brief messages to @code{stderr} even if verbose mode set.
8889 @end table
8891 @geindex -gnatB (gcc)
8894 @table @asis
8896 @item @code{-gnatB}
8898 Assume no invalid (bad) values except for 'Valid attribute use
8899 (@ref{fb,,Validity Checking}).
8900 @end table
8902 @geindex -gnatc (gcc)
8905 @table @asis
8907 @item @code{-gnatc}
8909 Check syntax and semantics only (no code generation attempted). When the
8910 compiler is invoked by @emph{gnatmake}, if the switch @emph{-gnatc} is
8911 only given to the compiler (after @emph{-cargs} or in package Compiler of
8912 the project file, @emph{gnatmake} will fail because it will not find the
8913 object file after compilation. If @emph{gnatmake} is called with
8914 @emph{-gnatc} as a builder switch (before @emph{-cargs} or in package
8915 Builder of the project file) then @emph{gnatmake} will not fail because
8916 it will not look for the object files after compilation, and it will not try
8917 to build and link. This switch may not be given if a previous @cite{-gnatR}
8918 switch has been given, since @cite{-gnatR} requires that the code generator
8919 be called to complete determination of representation information.
8920 @end table
8922 @geindex -gnatC (gcc)
8925 @table @asis
8927 @item @code{-gnatC}
8929 Generate CodePeer intermediate format (no code generation attempted).
8930 This switch will generate an intermediate representation suitable for
8931 use by CodePeer (@code{.scil} files). This switch is not compatible with
8932 code generation (it will, among other things, disable some switches such
8933 as -gnatn, and enable others such as -gnata).
8934 @end table
8936 @geindex -gnatd (gcc)
8939 @table @asis
8941 @item @code{-gnatd}
8943 Specify debug options for the compiler. The string of characters after
8944 the @emph{-gnatd} specify the specific debug options. The possible
8945 characters are 0-9, a-z, A-Z, optionally preceded by a dot. See
8946 compiler source file @code{debug.adb} for details of the implemented
8947 debug options. Certain debug options are relevant to applications
8948 programmers, and these are documented at appropriate points in this
8949 users guide.
8950 @end table
8952 @geindex -gnatD[nn] (gcc)
8955 @table @asis
8957 @item @code{-gnatD}
8959 Create expanded source files for source level debugging. This switch
8960 also suppress generation of cross-reference information
8961 (see @emph{-gnatx}). Note that this switch is not allowed if a previous
8962 -gnatR switch has been given, since these two switches are not compatible.
8963 @end table
8965 @geindex -gnateA (gcc)
8968 @table @asis
8970 @item @code{-gnateA}
8972 Check that the actual parameters of a subprogram call are not aliases of one
8973 another. To qualify as aliasing, the actuals must denote objects of a composite
8974 type, their memory locations must be identical or overlapping, and at least one
8975 of the corresponding formal parameters must be of mode OUT or IN OUT.
8977 @example
8978 type Rec_Typ is record
8979    Data : Integer := 0;
8980 end record;
8982 function Self (Val : Rec_Typ) return Rec_Typ is
8983 begin
8984    return Val;
8985 end Self;
8987 procedure Detect_Aliasing (Val_1 : in out Rec_Typ; Val_2 : Rec_Typ) is
8988 begin
8989    null;
8990 end Detect_Aliasing;
8992 Obj : Rec_Typ;
8994 Detect_Aliasing (Obj, Obj);
8995 Detect_Aliasing (Obj, Self (Obj));
8996 @end example
8998 In the example above, the first call to @cite{Detect_Aliasing} fails with a
8999 @cite{Program_Error} at runtime because the actuals for @cite{Val_1} and
9000 @cite{Val_2} denote the same object. The second call executes without raising
9001 an exception because @cite{Self(Obj)} produces an anonymous object which does
9002 not share the memory location of @cite{Obj}.
9003 @end table
9005 @geindex -gnatec (gcc)
9008 @table @asis
9010 @item @code{-gnatec=@emph{path}}
9012 Specify a configuration pragma file
9013 (the equal sign is optional)
9014 (@ref{7b,,The Configuration Pragmas Files}).
9015 @end table
9017 @geindex -gnateC (gcc)
9020 @table @asis
9022 @item @code{-gnateC}
9024 Generate CodePeer messages in a compiler-like format. This switch is only
9025 effective if @emph{-gnatcC} is also specified and requires an installation
9026 of CodePeer.
9027 @end table
9029 @geindex -gnated (gcc)
9032 @table @asis
9034 @item @code{-gnated}
9036 Disable atomic synchronization
9037 @end table
9039 @geindex -gnateD (gcc)
9042 @table @asis
9044 @item @code{-gnateDsymbol[=@emph{value}]}
9046 Defines a symbol, associated with @cite{value}, for preprocessing.
9047 (@ref{1a,,Integrated Preprocessing}).
9048 @end table
9050 @geindex -gnateE (gcc)
9053 @table @asis
9055 @item @code{-gnateE}
9057 Generate extra information in exception messages. In particular, display
9058 extra column information and the value and range associated with index and
9059 range check failures, and extra column information for access checks.
9060 In cases where the compiler is able to determine at compile time that
9061 a check will fail, it gives a warning, and the extra information is not
9062 produced at run time.
9063 @end table
9065 @geindex -gnatef (gcc)
9068 @table @asis
9070 @item @code{-gnatef}
9072 Display full source path name in brief error messages.
9073 @end table
9075 @geindex -gnateF (gcc)
9078 @table @asis
9080 @item @code{-gnateF}
9082 Check for overflow on all floating-point operations, including those
9083 for unconstrained predefined types. See description of pragma
9084 @cite{Check_Float_Overflow} in GNAT RM.
9085 @end table
9087 @geindex -gnateG (gcc)
9090 @table @asis
9092 @item @code{-gnateG}
9094 Save result of preprocessing in a text file.
9095 @end table
9097 @geindex -gnatei (gcc)
9100 @table @asis
9102 @item @code{-gnatei@emph{nnn}}
9104 Set maximum number of instantiations during compilation of a single unit to
9105 @cite{nnn}. This may be useful in increasing the default maximum of 8000 for
9106 the rare case when a single unit legitimately exceeds this limit.
9107 @end table
9109 @geindex -gnateI (gcc)
9112 @table @asis
9114 @item @code{-gnateI@emph{nnn}}
9116 Indicates that the source is a multi-unit source and that the index of the
9117 unit to compile is @cite{nnn}. @cite{nnn} needs to be a positive number and need
9118 to be a valid index in the multi-unit source.
9119 @end table
9121 @geindex -gnatel (gcc)
9124 @table @asis
9126 @item @code{-gnatel}
9128 This switch can be used with the static elaboration model to issue info
9129 messages showing
9130 where implicit @cite{pragma Elaborate} and @cite{pragma Elaborate_All}
9131 are generated. This is useful in diagnosing elaboration circularities
9132 caused by these implicit pragmas when using the static elaboration
9133 model. See See the section in this guide on elaboration checking for
9134 further details. These messages are not generated by default, and are
9135 intended only for temporary use when debugging circularity problems.
9136 @end table
9138 @geindex -gnatel (gcc)
9141 @table @asis
9143 @item @code{-gnateL}
9145 This switch turns off the info messages about implicit elaboration pragmas.
9146 @end table
9148 @geindex -gnatem (gcc)
9151 @table @asis
9153 @item @code{-gnatem=@emph{path}}
9155 Specify a mapping file
9156 (the equal sign is optional)
9157 (@ref{fc,,Units to Sources Mapping Files}).
9158 @end table
9160 @geindex -gnatep (gcc)
9163 @table @asis
9165 @item @code{-gnatep=@emph{file}}
9167 Specify a preprocessing data file
9168 (the equal sign is optional)
9169 (@ref{1a,,Integrated Preprocessing}).
9170 @end table
9172 @geindex -gnateP (gcc)
9175 @table @asis
9177 @item @code{-gnateP}
9179 Turn categorization dependency errors into warnings.
9180 Ada requires that units that WITH one another have compatible categories, for
9181 example a Pure unit cannot WITH a Preelaborate unit. If this switch is used,
9182 these errors become warnings (which can be ignored, or suppressed in the usual
9183 manner). This can be useful in some specialized circumstances such as the
9184 temporary use of special test software.
9185 @end table
9187 @geindex -gnateS (gcc)
9190 @table @asis
9192 @item @code{-gnateS}
9194 Synonym of @emph{-fdump-scos}, kept for backwards compatibility.
9195 @end table
9197 @geindex -gnatet=file (gcc)
9200 @table @asis
9202 @item @code{-gnatet=@emph{path}}
9204 Generate target dependent information. The format of the output file is
9205 described in the section about switch @emph{-gnateT}.
9206 @end table
9208 @geindex -gnateT (gcc)
9211 @table @asis
9213 @item @code{-gnateT=@emph{path}}
9215 Read target dependent information, such as endianness or sizes and alignments
9216 of base type. If this switch is passed, the default target dependent
9217 information of the compiler is replaced by the one read from the input file.
9218 This is used by tools other than the compiler, e.g. to do
9219 semantic analysis of programs that will run on some other target than
9220 the machine on which the tool is run.
9222 The following target dependent values should be defined,
9223 where @cite{Nat} denotes a natural integer value, @cite{Pos} denotes a
9224 positive integer value, and fields marked with a question mark are
9225 boolean fields, where a value of 0 is False, and a value of 1 is True:
9227 @example
9228 Bits_BE                    : Nat; -- Bits stored big-endian?
9229 Bits_Per_Unit              : Pos; -- Bits in a storage unit
9230 Bits_Per_Word              : Pos; -- Bits in a word
9231 Bytes_BE                   : Nat; -- Bytes stored big-endian?
9232 Char_Size                  : Pos; -- Standard.Character'Size
9233 Double_Float_Alignment     : Nat; -- Alignment of double float
9234 Double_Scalar_Alignment    : Nat; -- Alignment of double length scalar
9235 Double_Size                : Pos; -- Standard.Long_Float'Size
9236 Float_Size                 : Pos; -- Standard.Float'Size
9237 Float_Words_BE             : Nat; -- Float words stored big-endian?
9238 Int_Size                   : Pos; -- Standard.Integer'Size
9239 Long_Double_Size           : Pos; -- Standard.Long_Long_Float'Size
9240 Long_Long_Size             : Pos; -- Standard.Long_Long_Integer'Size
9241 Long_Size                  : Pos; -- Standard.Long_Integer'Size
9242 Maximum_Alignment          : Pos; -- Maximum permitted alignment
9243 Max_Unaligned_Field        : Pos; -- Maximum size for unaligned bit field
9244 Pointer_Size               : Pos; -- System.Address'Size
9245 Short_Enums                : Nat; -- Short foreign convention enums?
9246 Short_Size                 : Pos; -- Standard.Short_Integer'Size
9247 Strict_Alignment           : Nat; -- Strict alignment?
9248 System_Allocator_Alignment : Nat; -- Alignment for malloc calls
9249 Wchar_T_Size               : Pos; -- Interfaces.C.wchar_t'Size
9250 Words_BE                   : Nat; -- Words stored big-endian?
9251 @end example
9253 The format of the input file is as follows. First come the values of
9254 the variables defined above, with one line per value:
9256 @example
9257 name  value
9258 @end example
9260 where @cite{name} is the name of the parameter, spelled out in full,
9261 and cased as in the above list, and @cite{value} is an unsigned decimal
9262 integer. Two or more blanks separates the name from the value.
9264 All the variables must be present, in alphabetical order (i.e. the
9265 same order as the list above).
9267 Then there is a blank line to separate the two parts of the file. Then
9268 come the lines showing the floating-point types to be registered, with
9269 one line per registered mode:
9271 @example
9272 name  digs float_rep size alignment
9273 @end example
9275 where @cite{name} is the string name of the type (which can have
9276 single spaces embedded in the name (e.g. long double), @cite{digs} is
9277 the number of digits for the floating-point type, @cite{float_rep} is
9278 the float representation (I/V/A for IEEE-754-Binary, Vax_Native,
9279 AAMP), @cite{size} is the size in bits, @cite{alignment} is the
9280 alignment in bits. The name is followed by at least two blanks, fields
9281 are separated by at least one blank, and a LF character immediately
9282 follows the alignment field.
9284 Here is an example of a target parameterization file:
9286 @example
9287 Bits_BE                       0
9288 Bits_Per_Unit                 8
9289 Bits_Per_Word                64
9290 Bytes_BE                      0
9291 Char_Size                     8
9292 Double_Float_Alignment        0
9293 Double_Scalar_Alignment       0
9294 Double_Size                  64
9295 Float_Size                   32
9296 Float_Words_BE                0
9297 Int_Size                     64
9298 Long_Double_Size            128
9299 Long_Long_Size               64
9300 Long_Size                    64
9301 Maximum_Alignment            16
9302 Max_Unaligned_Field          64
9303 Pointer_Size                 64
9304 Short_Size                   16
9305 Strict_Alignment              0
9306 System_Allocator_Alignment   16
9307 Wchar_T_Size                 32
9308 Words_BE                      0
9310 float         15  I  64  64
9311 double        15  I  64  64
9312 long double   18  I  80 128
9313 TF            33  I 128 128
9314 @end example
9315 @end table
9317 @geindex -gnateu (gcc)
9320 @table @asis
9322 @item @code{-gnateu}
9324 Ignore unrecognized validity, warning, and style switches that
9325 appear after this switch is given. This may be useful when
9326 compiling sources developed on a later version of the compiler
9327 with an earlier version. Of course the earlier version must
9328 support this switch.
9329 @end table
9331 @geindex -gnateV (gcc)
9334 @table @asis
9336 @item @code{-gnateV}
9338 Check that all actual parameters of a subprogram call are valid according to
9339 the rules of validity checking (@ref{fb,,Validity Checking}).
9340 @end table
9342 @geindex -gnateY (gcc)
9345 @table @asis
9347 @item @code{-gnateY}
9349 Ignore all STYLE_CHECKS pragmas. Full legality checks
9350 are still carried out, but the pragmas have no effect
9351 on what style checks are active. This allows all style
9352 checking options to be controlled from the command line.
9353 @end table
9355 @geindex -gnatE (gcc)
9358 @table @asis
9360 @item @code{-gnatE}
9362 Full dynamic elaboration checks.
9363 @end table
9365 @geindex -gnatf (gcc)
9368 @table @asis
9370 @item @code{-gnatf}
9372 Full errors. Multiple errors per line, all undefined references, do not
9373 attempt to suppress cascaded errors.
9374 @end table
9376 @geindex -gnatF (gcc)
9379 @table @asis
9381 @item @code{-gnatF}
9383 Externals names are folded to all uppercase.
9384 @end table
9386 @geindex -gnatg (gcc)
9389 @table @asis
9391 @item @code{-gnatg}
9393 Internal GNAT implementation mode. This should not be used for
9394 applications programs, it is intended only for use by the compiler
9395 and its run-time library. For documentation, see the GNAT sources.
9396 Note that @emph{-gnatg} implies
9397 @emph{-gnatw.ge} and
9398 @emph{-gnatyg}
9399 so that all standard warnings and all standard style options are turned on.
9400 All warnings and style messages are treated as errors.
9401 @end table
9403 @geindex -gnatG[nn] (gcc)
9406 @table @asis
9408 @item @code{-gnatG=nn}
9410 List generated expanded code in source form.
9411 @end table
9413 @geindex -gnath (gcc)
9416 @table @asis
9418 @item @code{-gnath}
9420 Output usage information. The output is written to @code{stdout}.
9421 @end table
9423 @geindex -gnati (gcc)
9426 @table @asis
9428 @item @code{-gnati@emph{c}}
9430 Identifier character set (@cite{c} = 1/2/3/4/8/9/p/f/n/w).
9431 For details of the possible selections for @cite{c},
9432 see @ref{4a,,Character Set Control}.
9433 @end table
9435 @geindex -gnatI (gcc)
9438 @table @asis
9440 @item @code{-gnatI}
9442 Ignore representation clauses. When this switch is used,
9443 representation clauses are treated as comments. This is useful
9444 when initially porting code where you want to ignore rep clause
9445 problems, and also for compiling foreign code (particularly
9446 for use with ASIS). The representation clauses that are ignored
9447 are: enumeration_representation_clause, record_representation_clause,
9448 and attribute_definition_clause for the following attributes:
9449 Address, Alignment, Bit_Order, Component_Size, Machine_Radix,
9450 Object_Size, Size, Small, Stream_Size, and Value_Size.
9451 Note that this option should be used only for compiling -- the
9452 code is likely to malfunction at run time.
9454 Note that when @cite{-gnatct} is used to generate trees for input
9455 into @cite{ASIS} tools, these representation clauses are removed
9456 from the tree and ignored. This means that the tool will not see them.
9457 @end table
9459 @geindex -gnatjnn (gcc)
9462 @table @asis
9464 @item @code{-gnatj@emph{nn}}
9466 Reformat error messages to fit on @cite{nn} character lines
9467 @end table
9469 @geindex -gnatk (gcc)
9472 @table @asis
9474 @item @code{-gnatk=@emph{n}}
9476 Limit file names to @cite{n} (1-999) characters (@cite{k} = krunch).
9477 @end table
9479 @geindex -gnatl (gcc)
9482 @table @asis
9484 @item @code{-gnatl}
9486 Output full source listing with embedded error messages.
9487 @end table
9489 @geindex -gnatL (gcc)
9492 @table @asis
9494 @item @code{-gnatL}
9496 Used in conjunction with -gnatG or -gnatD to intersperse original
9497 source lines (as comment lines with line numbers) in the expanded
9498 source output.
9499 @end table
9501 @geindex -gnatm (gcc)
9504 @table @asis
9506 @item @code{-gnatm=@emph{n}}
9508 Limit number of detected error or warning messages to @cite{n}
9509 where @cite{n} is in the range 1..999999. The default setting if
9510 no switch is given is 9999. If the number of warnings reaches this
9511 limit, then a message is output and further warnings are suppressed,
9512 but the compilation is continued. If the number of error messages
9513 reaches this limit, then a message is output and the compilation
9514 is abandoned. The equal sign here is optional. A value of zero
9515 means that no limit applies.
9516 @end table
9518 @geindex -gnatn (gcc)
9521 @table @asis
9523 @item @code{-gnatn[12]}
9525 Activate inlining for subprograms for which pragma @cite{Inline} is
9526 specified. This inlining is performed by the GCC back-end. An optional
9527 digit sets the inlining level: 1 for moderate inlining across modules
9528 or 2 for full inlining across modules. If no inlining level is specified,
9529 the compiler will pick it based on the optimization level.
9530 @end table
9532 @geindex -gnatN (gcc)
9535 @table @asis
9537 @item @code{-gnatN}
9539 Activate front end inlining for subprograms for which
9540 pragma @cite{Inline} is specified. This inlining is performed
9541 by the front end and will be visible in the
9542 @emph{-gnatG} output.
9544 When using a gcc-based back end (in practice this means using any version
9545 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
9546 @emph{-gnatN} is deprecated, and the use of @emph{-gnatn} is preferred.
9547 Historically front end inlining was more extensive than the gcc back end
9548 inlining, but that is no longer the case.
9549 @end table
9551 @geindex -gnato0 (gcc)
9554 @table @asis
9556 @item @code{-gnato0}
9558 Suppresses overflow checking. This causes the behavior of the compiler to
9559 match the default for older versions where overflow checking was suppressed
9560 by default. This is equivalent to having
9561 @cite{pragma Suppress (Overflow_Mode)} in a configuration pragma file.
9562 @end table
9564 @geindex -gnato?? (gcc)
9567 @table @asis
9569 @item @code{-gnato??}
9571 Set default mode for handling generation of code to avoid intermediate
9572 arithmetic overflow. Here @cite{??} is two digits, a
9573 single digit, or nothing. Each digit is one of the digits @cite{1}
9574 through @cite{3}:
9577 @multitable {xxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} 
9578 @item
9580 Digit
9582 @tab
9584 Interpretation
9586 @item
9588 @emph{1}
9590 @tab
9592 All intermediate overflows checked against base type (@cite{STRICT})
9594 @item
9596 @emph{2}
9598 @tab
9600 Minimize intermediate overflows (@cite{MINIMIZED})
9602 @item
9604 @emph{3}
9606 @tab
9608 Eliminate intermediate overflows (@cite{ELIMINATED})
9610 @end multitable
9613 If only one digit appears then it applies to all
9614 cases; if two digits are given, then the first applies outside
9615 assertions, and the second within assertions.
9617 If no digits follow the @emph{-gnato}, then it is equivalent to
9618 @emph{-gnato11},
9619 causing all intermediate overflows to be handled in strict mode.
9621 This switch also causes arithmetic overflow checking to be performed
9622 (as though @cite{pragma Unsuppress (Overflow_Mode)} had been specified.
9624 The default if no option @emph{-gnato} is given is that overflow handling
9625 is in @cite{STRICT} mode (computations done using the base type), and that
9626 overflow checking is enabled.
9628 Note that division by zero is a separate check that is not
9629 controlled by this switch (division by zero checking is on by default).
9631 See also @ref{fd,,Specifying the Desired Mode}.
9632 @end table
9634 @geindex -gnatp (gcc)
9637 @table @asis
9639 @item @code{-gnatp}
9641 Suppress all checks. See @ref{fe,,Run-Time Checks} for details. This switch
9642 has no effect if cancelled by a subsequent @emph{-gnat-p} switch.
9643 @end table
9645 @geindex -gnat-p (gcc)
9648 @table @asis
9650 @item @code{-gnat-p}
9652 Cancel effect of previous @emph{-gnatp} switch.
9653 @end table
9655 @geindex -gnatP (gcc)
9658 @table @asis
9660 @item @code{-gnatP}
9662 Enable polling. This is required on some systems (notably Windows NT) to
9663 obtain asynchronous abort and asynchronous transfer of control capability.
9664 See @cite{Pragma_Polling} in the @cite{GNAT_Reference_Manual} for full
9665 details.
9666 @end table
9668 @geindex -gnatq (gcc)
9671 @table @asis
9673 @item @code{-gnatq}
9675 Don't quit. Try semantics, even if parse errors.
9676 @end table
9678 @geindex -gnatQ (gcc)
9681 @table @asis
9683 @item @code{-gnatQ}
9685 Don't quit. Generate @code{ALI} and tree files even if illegalities.
9686 Note that code generation is still suppressed in the presence of any
9687 errors, so even with @emph{-gnatQ} no object file is generated.
9688 @end table
9690 @geindex -gnatr (gcc)
9693 @table @asis
9695 @item @code{-gnatr}
9697 Treat pragma Restrictions as Restriction_Warnings.
9698 @end table
9700 @geindex -gnatR (gcc)
9703 @table @asis
9705 @item @code{-gnatR[0/1/2/3[s]]}
9707 Output representation information for declared types and objects.
9708 Note that this switch is not allowed if a previous @cite{-gnatD} switch has
9709 been given, since these two switches are not compatible.
9711 @item @code{-gnatRm[s]}
9713 Output convention and parameter passing mechanisms for all subprograms.
9714 @end table
9716 @geindex -gnats (gcc)
9719 @table @asis
9721 @item @code{-gnats}
9723 Syntax check only.
9724 @end table
9726 @geindex -gnatS (gcc)
9729 @table @asis
9731 @item @code{-gnatS}
9733 Print package Standard.
9734 @end table
9736 @geindex -gnatt (gcc)
9739 @table @asis
9741 @item @code{-gnatt}
9743 Generate tree output file.
9744 @end table
9746 @geindex -gnatT (gcc)
9749 @table @asis
9751 @item @code{-gnatT@emph{nnn}}
9753 All compiler tables start at @cite{nnn} times usual starting size.
9754 @end table
9756 @geindex -gnatu (gcc)
9759 @table @asis
9761 @item @code{-gnatu}
9763 List units for this compilation.
9764 @end table
9766 @geindex -gnatU (gcc)
9769 @table @asis
9771 @item @code{-gnatU}
9773 Tag all error messages with the unique string 'error:'
9774 @end table
9776 @geindex -gnatv (gcc)
9779 @table @asis
9781 @item @code{-gnatv}
9783 Verbose mode. Full error output with source lines to @code{stdout}.
9784 @end table
9786 @geindex -gnatV (gcc)
9789 @table @asis
9791 @item @code{-gnatV}
9793 Control level of validity checking (@ref{fb,,Validity Checking}).
9794 @end table
9796 @geindex -gnatw (gcc)
9799 @table @asis
9801 @item @code{-gnatw@emph{xxx}}
9803 Warning mode where
9804 @cite{xxx} is a string of option letters that denotes
9805 the exact warnings that
9806 are enabled or disabled (@ref{ff,,Warning Message Control}).
9807 @end table
9809 @geindex -gnatW (gcc)
9812 @table @asis
9814 @item @code{-gnatW@emph{e}}
9816 Wide character encoding method
9817 (@cite{e}=n/h/u/s/e/8).
9818 @end table
9820 @geindex -gnatx (gcc)
9823 @table @asis
9825 @item @code{-gnatx}
9827 Suppress generation of cross-reference information.
9828 @end table
9830 @geindex -gnatX (gcc)
9833 @table @asis
9835 @item @code{-gnatX}
9837 Enable GNAT implementation extensions and latest Ada version.
9838 @end table
9840 @geindex -gnaty (gcc)
9843 @table @asis
9845 @item @code{-gnaty}
9847 Enable built-in style checks (@ref{100,,Style Checking}).
9848 @end table
9850 @geindex -gnatz (gcc)
9853 @table @asis
9855 @item @code{-gnatz@emph{m}}
9857 Distribution stub generation and compilation
9858 (@cite{m}=r/c for receiver/caller stubs).
9859 @end table
9861 @geindex -I (gcc)
9864 @table @asis
9866 @item @code{-I@emph{dir}}
9868 @geindex RTL
9870 Direct GNAT to search the @cite{dir} directory for source files needed by
9871 the current compilation
9872 (see @ref{8e,,Search Paths and the Run-Time Library (RTL)}).
9873 @end table
9875 @geindex -I- (gcc)
9878 @table @asis
9880 @item @code{-I-}
9882 @geindex RTL
9884 Except for the source file named in the command line, do not look for source
9885 files in the directory containing the source file named in the command line
9886 (see @ref{8e,,Search Paths and the Run-Time Library (RTL)}).
9887 @end table
9889 @geindex -o (gcc)
9892 @table @asis
9894 @item @code{-o @emph{file}}
9896 This switch is used in @emph{gcc} to redirect the generated object file
9897 and its associated ALI file. Beware of this switch with GNAT, because it may
9898 cause the object file and ALI file to have different names which in turn
9899 may confuse the binder and the linker.
9900 @end table
9902 @geindex -nostdinc (gcc)
9905 @table @asis
9907 @item @code{-nostdinc}
9909 Inhibit the search of the default location for the GNAT Run Time
9910 Library (RTL) source files.
9911 @end table
9913 @geindex -nostdlib (gcc)
9916 @table @asis
9918 @item @code{-nostdlib}
9920 Inhibit the search of the default location for the GNAT Run Time
9921 Library (RTL) ALI files.
9922 @end table
9924 @geindex -O (gcc)
9927 @table @asis
9929 @item @code{-O[@emph{n}]}
9931 @cite{n} controls the optimization level:
9934 @multitable {xxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} 
9935 @item
9937 @emph{n}
9939 @tab
9941 Effect
9943 @item
9945 @emph{0}
9947 @tab
9949 No optimization, the default setting if no @emph{-O} appears
9951 @item
9953 @emph{1}
9955 @tab
9957 Normal optimization, the default if you specify @emph{-O} without an
9958 operand. A good compromise between code quality and compilation
9959 time.
9961 @item
9963 @emph{2}
9965 @tab
9967 Extensive optimization, may improve execution time, possibly at
9968 the cost of substantially increased compilation time.
9970 @item
9972 @emph{3}
9974 @tab
9976 Same as @emph{-O2}, and also includes inline expansion for small
9977 subprograms in the same unit.
9979 @item
9981 @emph{s}
9983 @tab
9985 Optimize space usage
9987 @end multitable
9990 See also @ref{101,,Optimization Levels}.
9991 @end table
9993 @geindex -pass-exit-codes (gcc)
9996 @table @asis
9998 @item @code{-pass-exit-codes}
10000 Catch exit codes from the compiler and use the most meaningful as
10001 exit status.
10002 @end table
10004 @geindex --RTS (gcc)
10007 @table @asis
10009 @item @code{--RTS=@emph{rts-path}}
10011 Specifies the default location of the runtime library. Same meaning as the
10012 equivalent @emph{gnatmake} flag (@ref{df,,Switches for gnatmake}).
10013 @end table
10015 @geindex -S (gcc)
10018 @table @asis
10020 @item @code{-S}
10022 Used in place of @emph{-c} to
10023 cause the assembler source file to be
10024 generated, using @code{.s} as the extension,
10025 instead of the object file.
10026 This may be useful if you need to examine the generated assembly code.
10027 @end table
10029 @geindex -fverbose-asm (gcc)
10032 @table @asis
10034 @item @code{-fverbose-asm}
10036 Used in conjunction with @emph{-S}
10037 to cause the generated assembly code file to be annotated with variable
10038 names, making it significantly easier to follow.
10039 @end table
10041 @geindex -v (gcc)
10044 @table @asis
10046 @item @code{-v}
10048 Show commands generated by the @emph{gcc} driver. Normally used only for
10049 debugging purposes or if you need to be sure what version of the
10050 compiler you are executing.
10051 @end table
10053 @geindex -V (gcc)
10056 @table @asis
10058 @item @code{-V @emph{ver}}
10060 Execute @cite{ver} version of the compiler. This is the @emph{gcc}
10061 version, not the GNAT version.
10062 @end table
10064 @geindex -w (gcc)
10067 @table @asis
10069 @item @code{-w}
10071 Turn off warnings generated by the back end of the compiler. Use of
10072 this switch also causes the default for front end warnings to be set
10073 to suppress (as though @emph{-gnatws} had appeared at the start of
10074 the options).
10075 @end table
10077 @geindex Combining GNAT switches
10079 You may combine a sequence of GNAT switches into a single switch. For
10080 example, the combined switch
10082 @quotation
10084 @example
10085 -gnatofi3
10086 @end example
10087 @end quotation
10089 is equivalent to specifying the following sequence of switches:
10091 @quotation
10093 @example
10094 -gnato -gnatf -gnati3
10095 @end example
10096 @end quotation
10098 The following restrictions apply to the combination of switches
10099 in this manner:
10102 @itemize *
10104 @item 
10105 The switch @emph{-gnatc} if combined with other switches must come
10106 first in the string.
10108 @item 
10109 The switch @emph{-gnats} if combined with other switches must come
10110 first in the string.
10112 @item 
10113 The switches
10114 @emph{-gnatzc} and @emph{-gnatzr} may not be combined with any other
10115 switches, and only one of them may appear in the command line.
10117 @item 
10118 The switch @emph{-gnat-p} may not be combined with any other switch.
10120 @item 
10121 Once a 'y' appears in the string (that is a use of the @emph{-gnaty}
10122 switch), then all further characters in the switch are interpreted
10123 as style modifiers (see description of @emph{-gnaty}).
10125 @item 
10126 Once a 'd' appears in the string (that is a use of the @emph{-gnatd}
10127 switch), then all further characters in the switch are interpreted
10128 as debug flags (see description of @emph{-gnatd}).
10130 @item 
10131 Once a 'w' appears in the string (that is a use of the @emph{-gnatw}
10132 switch), then all further characters in the switch are interpreted
10133 as warning mode modifiers (see description of @emph{-gnatw}).
10135 @item 
10136 Once a 'V' appears in the string (that is a use of the @emph{-gnatV}
10137 switch), then all further characters in the switch are interpreted
10138 as validity checking options (@ref{fb,,Validity Checking}).
10140 @item 
10141 Option 'em', 'ec', 'ep', 'l=' and 'R' must be the last options in
10142 a combined list of options.
10143 @end itemize
10145 @node Output and Error Message Control,Warning Message Control,Alphabetical List of All Switches,Compiler Switches
10146 @anchor{gnat_ugn/building_executable_programs_with_gnat id14}@anchor{102}@anchor{gnat_ugn/building_executable_programs_with_gnat output-and-error-message-control}@anchor{103}
10147 @subsection Output and Error Message Control
10150 @geindex stderr
10152 The standard default format for error messages is called 'brief format'.
10153 Brief format messages are written to @code{stderr} (the standard error
10154 file) and have the following form:
10156 @example
10157 e.adb:3:04: Incorrect spelling of keyword "function"
10158 e.adb:4:20: ";" should be "is"
10159 @end example
10161 The first integer after the file name is the line number in the file,
10162 and the second integer is the column number within the line.
10163 @cite{GPS} can parse the error messages
10164 and point to the referenced character.
10165 The following switches provide control over the error message
10166 format:
10168 @geindex -gnatv (gcc)
10171 @table @asis
10173 @item @code{-gnatv}
10175 The @cite{v} stands for verbose.
10176 The effect of this setting is to write long-format error
10177 messages to @code{stdout} (the standard output file.
10178 The same program compiled with the
10179 @emph{-gnatv} switch would generate:
10181 @example
10182 3. funcion X (Q : Integer)
10183    |
10184 >>> Incorrect spelling of keyword "function"
10185 4. return Integer;
10186                  |
10187 >>> ";" should be "is"
10188 @end example
10190 The vertical bar indicates the location of the error, and the @code{>>>}
10191 prefix can be used to search for error messages. When this switch is
10192 used the only source lines output are those with errors.
10193 @end table
10195 @geindex -gnatl (gcc)
10198 @table @asis
10200 @item @code{-gnatl}
10202 The @cite{l} stands for list.
10203 This switch causes a full listing of
10204 the file to be generated. In the case where a body is
10205 compiled, the corresponding spec is also listed, along
10206 with any subunits. Typical output from compiling a package
10207 body @code{p.adb} might look like:
10209 @example
10210 Compiling: p.adb
10212      1. package body p is
10213      2.    procedure a;
10214      3.    procedure a is separate;
10215      4. begin
10216      5.    null
10217                |
10218         >>> missing ";"
10220      6. end;
10222 Compiling: p.ads
10224      1. package p is
10225      2.    pragma Elaborate_Body
10226                                 |
10227         >>> missing ";"
10229      3. end p;
10231 Compiling: p-a.adb
10233      1. separate p
10234                 |
10235         >>> missing "("
10237      2. procedure a is
10238      3. begin
10239      4.    null
10240                |
10241         >>> missing ";"
10243      5. end;
10244 @end example
10246 When you specify the @emph{-gnatv} or @emph{-gnatl} switches and
10247 standard output is redirected, a brief summary is written to
10248 @code{stderr} (standard error) giving the number of error messages and
10249 warning messages generated.
10250 @end table
10252 @geindex -gnatl=fname (gcc)
10255 @table @asis
10257 @item @code{-gnatl=@emph{fname}}
10259 This has the same effect as @emph{-gnatl} except that the output is
10260 written to a file instead of to standard output. If the given name
10261 @code{fname} does not start with a period, then it is the full name
10262 of the file to be written. If @code{fname} is an extension, it is
10263 appended to the name of the file being compiled. For example, if
10264 file @code{xyz.adb} is compiled with @emph{-gnatl=.lst},
10265 then the output is written to file xyz.adb.lst.
10266 @end table
10268 @geindex -gnatU (gcc)
10271 @table @asis
10273 @item @code{-gnatU}
10275 This switch forces all error messages to be preceded by the unique
10276 string 'error:'. This means that error messages take a few more
10277 characters in space, but allows easy searching for and identification
10278 of error messages.
10279 @end table
10281 @geindex -gnatb (gcc)
10284 @table @asis
10286 @item @code{-gnatb}
10288 The @cite{b} stands for brief.
10289 This switch causes GNAT to generate the
10290 brief format error messages to @code{stderr} (the standard error
10291 file) as well as the verbose
10292 format message or full listing (which as usual is written to
10293 @code{stdout} (the standard output file).
10294 @end table
10296 @geindex -gnatm (gcc)
10299 @table @asis
10301 @item @code{-gnatm=@emph{n}}
10303 The @cite{m} stands for maximum.
10304 @cite{n} is a decimal integer in the
10305 range of 1 to 999999 and limits the number of error or warning
10306 messages to be generated. For example, using
10307 @emph{-gnatm2} might yield
10309 @example
10310 e.adb:3:04: Incorrect spelling of keyword "function"
10311 e.adb:5:35: missing ".."
10312 fatal error: maximum number of errors detected
10313 compilation abandoned
10314 @end example
10316 The default setting if
10317 no switch is given is 9999. If the number of warnings reaches this
10318 limit, then a message is output and further warnings are suppressed,
10319 but the compilation is continued. If the number of error messages
10320 reaches this limit, then a message is output and the compilation
10321 is abandoned. A value of zero means that no limit applies.
10323 Note that the equal sign is optional, so the switches
10324 @emph{-gnatm2} and @emph{-gnatm=2} are equivalent.
10325 @end table
10327 @geindex -gnatf (gcc)
10330 @table @asis
10332 @item @code{-gnatf}
10334 @geindex Error messages
10335 @geindex suppressing
10337 The @cite{f} stands for full.
10338 Normally, the compiler suppresses error messages that are likely to be
10339 redundant. This switch causes all error
10340 messages to be generated. In particular, in the case of
10341 references to undefined variables. If a given variable is referenced
10342 several times, the normal format of messages is
10344 @example
10345 e.adb:7:07: "V" is undefined (more references follow)
10346 @end example
10348 where the parenthetical comment warns that there are additional
10349 references to the variable @cite{V}. Compiling the same program with the
10350 @emph{-gnatf} switch yields
10352 @example
10353 e.adb:7:07: "V" is undefined
10354 e.adb:8:07: "V" is undefined
10355 e.adb:8:12: "V" is undefined
10356 e.adb:8:16: "V" is undefined
10357 e.adb:9:07: "V" is undefined
10358 e.adb:9:12: "V" is undefined
10359 @end example
10361 The @emph{-gnatf} switch also generates additional information for
10362 some error messages.  Some examples are:
10365 @itemize *
10367 @item 
10368 Details on possibly non-portable unchecked conversion
10370 @item 
10371 List possible interpretations for ambiguous calls
10373 @item 
10374 Additional details on incorrect parameters
10375 @end itemize
10376 @end table
10378 @geindex -gnatjnn (gcc)
10381 @table @asis
10383 @item @code{-gnatjnn}
10385 In normal operation mode (or if @emph{-gnatj0} is used), then error messages
10386 with continuation lines are treated as though the continuation lines were
10387 separate messages (and so a warning with two continuation lines counts as
10388 three warnings, and is listed as three separate messages).
10390 If the @emph{-gnatjnn} switch is used with a positive value for nn, then
10391 messages are output in a different manner. A message and all its continuation
10392 lines are treated as a unit, and count as only one warning or message in the
10393 statistics totals. Furthermore, the message is reformatted so that no line
10394 is longer than nn characters.
10395 @end table
10397 @geindex -gnatq (gcc)
10400 @table @asis
10402 @item @code{-gnatq}
10404 The @cite{q} stands for quit (really 'don't quit').
10405 In normal operation mode, the compiler first parses the program and
10406 determines if there are any syntax errors. If there are, appropriate
10407 error messages are generated and compilation is immediately terminated.
10408 This switch tells
10409 GNAT to continue with semantic analysis even if syntax errors have been
10410 found. This may enable the detection of more errors in a single run. On
10411 the other hand, the semantic analyzer is more likely to encounter some
10412 internal fatal error when given a syntactically invalid tree.
10413 @end table
10415 @geindex -gnatQ (gcc)
10418 @table @asis
10420 @item @code{-gnatQ}
10422 In normal operation mode, the @code{ALI} file is not generated if any
10423 illegalities are detected in the program. The use of @emph{-gnatQ} forces
10424 generation of the @code{ALI} file. This file is marked as being in
10425 error, so it cannot be used for binding purposes, but it does contain
10426 reasonably complete cross-reference information, and thus may be useful
10427 for use by tools (e.g., semantic browsing tools or integrated development
10428 environments) that are driven from the @code{ALI} file. This switch
10429 implies @emph{-gnatq}, since the semantic phase must be run to get a
10430 meaningful ALI file.
10432 In addition, if @emph{-gnatt} is also specified, then the tree file is
10433 generated even if there are illegalities. It may be useful in this case
10434 to also specify @emph{-gnatq} to ensure that full semantic processing
10435 occurs. The resulting tree file can be processed by ASIS, for the purpose
10436 of providing partial information about illegal units, but if the error
10437 causes the tree to be badly malformed, then ASIS may crash during the
10438 analysis.
10440 When @emph{-gnatQ} is used and the generated @code{ALI} file is marked as
10441 being in error, @emph{gnatmake} will attempt to recompile the source when it
10442 finds such an @code{ALI} file, including with switch @emph{-gnatc}.
10444 Note that @emph{-gnatQ} has no effect if @emph{-gnats} is specified,
10445 since ALI files are never generated if @emph{-gnats} is set.
10446 @end table
10448 @node Warning Message Control,Debugging and Assertion Control,Output and Error Message Control,Compiler Switches
10449 @anchor{gnat_ugn/building_executable_programs_with_gnat warning-message-control}@anchor{ff}@anchor{gnat_ugn/building_executable_programs_with_gnat id15}@anchor{104}
10450 @subsection Warning Message Control
10453 @geindex Warning messages
10455 In addition to error messages, which correspond to illegalities as defined
10456 in the Ada Reference Manual, the compiler detects two kinds of warning
10457 situations.
10459 First, the compiler considers some constructs suspicious and generates a
10460 warning message to alert you to a possible error. Second, if the
10461 compiler detects a situation that is sure to raise an exception at
10462 run time, it generates a warning message. The following shows an example
10463 of warning messages:
10465 @example
10466 e.adb:4:24: warning: creation of object may raise Storage_Error
10467 e.adb:10:17: warning: static value out of range
10468 e.adb:10:17: warning: "Constraint_Error" will be raised at run time
10469 @end example
10471 GNAT considers a large number of situations as appropriate
10472 for the generation of warning messages. As always, warnings are not
10473 definite indications of errors. For example, if you do an out-of-range
10474 assignment with the deliberate intention of raising a
10475 @cite{Constraint_Error} exception, then the warning that may be
10476 issued does not indicate an error. Some of the situations for which GNAT
10477 issues warnings (at least some of the time) are given in the following
10478 list. This list is not complete, and new warnings are often added to
10479 subsequent versions of GNAT. The list is intended to give a general idea
10480 of the kinds of warnings that are generated.
10483 @itemize *
10485 @item 
10486 Possible infinitely recursive calls
10488 @item 
10489 Out-of-range values being assigned
10491 @item 
10492 Possible order of elaboration problems
10494 @item 
10495 Size not a multiple of alignment for a record type
10497 @item 
10498 Assertions (pragma Assert) that are sure to fail
10500 @item 
10501 Unreachable code
10503 @item 
10504 Address clauses with possibly unaligned values, or where an attempt is
10505 made to overlay a smaller variable with a larger one.
10507 @item 
10508 Fixed-point type declarations with a null range
10510 @item 
10511 Direct_IO or Sequential_IO instantiated with a type that has access values
10513 @item 
10514 Variables that are never assigned a value
10516 @item 
10517 Variables that are referenced before being initialized
10519 @item 
10520 Task entries with no corresponding @cite{accept} statement
10522 @item 
10523 Duplicate accepts for the same task entry in a @cite{select}
10525 @item 
10526 Objects that take too much storage
10528 @item 
10529 Unchecked conversion between types of differing sizes
10531 @item 
10532 Missing @cite{return} statement along some execution path in a function
10534 @item 
10535 Incorrect (unrecognized) pragmas
10537 @item 
10538 Incorrect external names
10540 @item 
10541 Allocation from empty storage pool
10543 @item 
10544 Potentially blocking operation in protected type
10546 @item 
10547 Suspicious parenthesization of expressions
10549 @item 
10550 Mismatching bounds in an aggregate
10552 @item 
10553 Attempt to return local value by reference
10555 @item 
10556 Premature instantiation of a generic body
10558 @item 
10559 Attempt to pack aliased components
10561 @item 
10562 Out of bounds array subscripts
10564 @item 
10565 Wrong length on string assignment
10567 @item 
10568 Violations of style rules if style checking is enabled
10570 @item 
10571 Unused @emph{with} clauses
10573 @item 
10574 @cite{Bit_Order} usage that does not have any effect
10576 @item 
10577 @cite{Standard.Duration} used to resolve universal fixed expression
10579 @item 
10580 Dereference of possibly null value
10582 @item 
10583 Declaration that is likely to cause storage error
10585 @item 
10586 Internal GNAT unit @emph{with}ed by application unit
10588 @item 
10589 Values known to be out of range at compile time
10591 @item 
10592 Unreferenced or unmodified variables. Note that a special
10593 exemption applies to variables which contain any of the substrings
10594 @cite{DISCARD@comma{} DUMMY@comma{} IGNORE@comma{} JUNK@comma{} UNUSED}, in any casing. Such variables
10595 are considered likely to be intentionally used in a situation where
10596 otherwise a warning would be given, so warnings of this kind are
10597 always suppressed for such variables.
10599 @item 
10600 Address overlays that could clobber memory
10602 @item 
10603 Unexpected initialization when address clause present
10605 @item 
10606 Bad alignment for address clause
10608 @item 
10609 Useless type conversions
10611 @item 
10612 Redundant assignment statements and other redundant constructs
10614 @item 
10615 Useless exception handlers
10617 @item 
10618 Accidental hiding of name by child unit
10620 @item 
10621 Access before elaboration detected at compile time
10623 @item 
10624 A range in a @cite{for} loop that is known to be null or might be null
10625 @end itemize
10627 The following section lists compiler switches that are available
10628 to control the handling of warning messages. It is also possible
10629 to exercise much finer control over what warnings are issued and
10630 suppressed using the GNAT pragma Warnings (see the description
10631 of the pragma in the @cite{GNAT_Reference_manual}).
10633 @geindex -gnatwa (gcc)
10636 @table @asis
10638 @item @code{-gnatwa}
10640 @emph{Activate most optional warnings.}
10642 This switch activates most optional warning messages.  See the remaining list
10643 in this section for details on optional warning messages that can be
10644 individually controlled.  The warnings that are not turned on by this
10645 switch are:
10648 @itemize *
10650 @item 
10651 @code{-gnatwd} (implicit dereferencing)
10653 @item 
10654 @code{-gnatw.d} (tag warnings with -gnatw switch)
10656 @item 
10657 @code{-gnatwh} (hiding)
10659 @item 
10660 @code{-gnatw.h} (holes in record layouts)
10662 @item 
10663 @code{-gnatw.k} (redefinition of names in standard)
10665 @item 
10666 @code{-gnatwl} (elaboration warnings)
10668 @item 
10669 @code{-gnatw.l} (inherited aspects)
10671 @item 
10672 @code{-gnatw.n} (atomic synchronization)
10674 @item 
10675 @code{-gnatwo} (address clause overlay)
10677 @item 
10678 @code{-gnatw.o} (values set by out parameters ignored)
10680 @item 
10681 @code{-gnatw.s} (overridden size clause)
10683 @item 
10684 @code{-gnatwt} (tracking of deleted conditional code)
10686 @item 
10687 @code{-gnatw.u} (unordered enumeration)
10689 @item 
10690 @code{-gnatw.w} (use of Warnings Off)
10692 @item 
10693 @code{-gnatw.y} (reasons for package needing body)
10694 @end itemize
10696 All other optional warnings are turned on.
10697 @end table
10699 @geindex -gnatwA (gcc)
10702 @table @asis
10704 @item @code{-gnatwA}
10706 @emph{Suppress all optional errors.}
10708 This switch suppresses all optional warning messages, see remaining list
10709 in this section for details on optional warning messages that can be
10710 individually controlled. Note that unlike switch @emph{-gnatws}, the
10711 use of switch @emph{-gnatwA} does not suppress warnings that are
10712 normally given unconditionally and cannot be individually controlled
10713 (for example, the warning about a missing exit path in a function).
10714 Also, again unlike switch @emph{-gnatws}, warnings suppressed by
10715 the use of switch @emph{-gnatwA} can be individually turned back
10716 on. For example the use of switch @emph{-gnatwA} followed by
10717 switch @emph{-gnatwd} will suppress all optional warnings except
10718 the warnings for implicit dereferencing.
10719 @end table
10721 @geindex -gnatw.a (gcc)
10724 @table @asis
10726 @item @code{-gnatw.a}
10728 @emph{Activate warnings on failing assertions.}
10730 @geindex Assert failures
10732 This switch activates warnings for assertions where the compiler can tell at
10733 compile time that the assertion will fail. Note that this warning is given
10734 even if assertions are disabled. The default is that such warnings are
10735 generated.
10736 @end table
10738 @geindex -gnatw.A (gcc)
10741 @table @asis
10743 @item @code{-gnatw.A}
10745 @emph{Suppress warnings on failing assertions.}
10747 @geindex Assert failures
10749 This switch suppresses warnings for assertions where the compiler can tell at
10750 compile time that the assertion will fail.
10751 @end table
10753 @geindex -gnatwb (gcc)
10756 @table @asis
10758 @item @code{-gnatwb}
10760 @emph{Activate warnings on bad fixed values.}
10762 @geindex Bad fixed values
10764 @geindex Fixed-point Small value
10766 @geindex Small value
10768 This switch activates warnings for static fixed-point expressions whose
10769 value is not an exact multiple of Small. Such values are implementation
10770 dependent, since an implementation is free to choose either of the multiples
10771 that surround the value. GNAT always chooses the closer one, but this is not
10772 required behavior, and it is better to specify a value that is an exact
10773 multiple, ensuring predictable execution. The default is that such warnings
10774 are not generated.
10775 @end table
10777 @geindex -gnatwB (gcc)
10780 @table @asis
10782 @item @code{-gnatwB}
10784 @emph{Suppress warnings on bad fixed values.}
10786 This switch suppresses warnings for static fixed-point expressions whose
10787 value is not an exact multiple of Small.
10788 @end table
10790 @geindex -gnatw.b (gcc)
10793 @table @asis
10795 @item @code{-gnatw.b}
10797 @emph{Activate warnings on biased representation.}
10799 @geindex Biased representation
10801 This switch activates warnings when a size clause, value size clause, component
10802 clause, or component size clause forces the use of biased representation for an
10803 integer type (e.g. representing a range of 10..11 in a single bit by using 0/1
10804 to represent 10/11). The default is that such warnings are generated.
10805 @end table
10807 @geindex -gnatwB (gcc)
10810 @table @asis
10812 @item @code{-gnatw.B}
10814 @emph{Suppress warnings on biased representation.}
10816 This switch suppresses warnings for representation clauses that force the use
10817 of biased representation.
10818 @end table
10820 @geindex -gnatwc (gcc)
10823 @table @asis
10825 @item @code{-gnatwc}
10827 @emph{Activate warnings on conditionals.}
10829 @geindex Conditionals
10830 @geindex constant
10832 This switch activates warnings for conditional expressions used in
10833 tests that are known to be True or False at compile time. The default
10834 is that such warnings are not generated.
10835 Note that this warning does
10836 not get issued for the use of boolean variables or constants whose
10837 values are known at compile time, since this is a standard technique
10838 for conditional compilation in Ada, and this would generate too many
10839 false positive warnings.
10841 This warning option also activates a special test for comparisons using
10842 the operators '>=' and' <='.
10843 If the compiler can tell that only the equality condition is possible,
10844 then it will warn that the '>' or '<' part of the test
10845 is useless and that the operator could be replaced by '='.
10846 An example would be comparing a @cite{Natural} variable <= 0.
10848 This warning option also generates warnings if
10849 one or both tests is optimized away in a membership test for integer
10850 values if the result can be determined at compile time. Range tests on
10851 enumeration types are not included, since it is common for such tests
10852 to include an end point.
10854 This warning can also be turned on using @emph{-gnatwa}.
10855 @end table
10857 @geindex -gnatwC (gcc)
10860 @table @asis
10862 @item @code{-gnatwC}
10864 @emph{Suppress warnings on conditionals.}
10866 This switch suppresses warnings for conditional expressions used in
10867 tests that are known to be True or False at compile time.
10868 @end table
10870 @geindex -gnatw.c (gcc)
10873 @table @asis
10875 @item @code{-gnatw.c}
10877 @emph{Activate warnings on missing component clauses.}
10879 @geindex Component clause
10880 @geindex missing
10882 This switch activates warnings for record components where a record
10883 representation clause is present and has component clauses for the
10884 majority, but not all, of the components. A warning is given for each
10885 component for which no component clause is present.
10886 @end table
10888 @geindex -gnatwC (gcc)
10891 @table @asis
10893 @item @code{-gnatw.C}
10895 @emph{Suppress warnings on missing component clauses.}
10897 This switch suppresses warnings for record components that are
10898 missing a component clause in the situation described above.
10899 @end table
10901 @geindex -gnatwd (gcc)
10904 @table @asis
10906 @item @code{-gnatwd}
10908 @emph{Activate warnings on implicit dereferencing.}
10910 If this switch is set, then the use of a prefix of an access type
10911 in an indexed component, slice, or selected component without an
10912 explicit @cite{.all} will generate a warning. With this warning
10913 enabled, access checks occur only at points where an explicit
10914 @cite{.all} appears in the source code (assuming no warnings are
10915 generated as a result of this switch). The default is that such
10916 warnings are not generated.
10917 @end table
10919 @geindex -gnatwD (gcc)
10922 @table @asis
10924 @item @code{-gnatwD}
10926 @emph{Suppress warnings on implicit dereferencing.}
10928 @geindex Implicit dereferencing
10930 @geindex Dereferencing
10931 @geindex implicit
10933 This switch suppresses warnings for implicit dereferences in
10934 indexed components, slices, and selected components.
10935 @end table
10937 @geindex -gnatw.d (gcc)
10940 @table @asis
10942 @item @code{-gnatw.d}
10944 @emph{Activate tagging of warning and info messages.}
10946 If this switch is set, then warning messages are tagged, with one of the
10947 following strings:
10949 @quotation
10952 @itemize -
10954 @item 
10955 @emph{[-gnatw?]}
10956 Used to tag warnings controlled by the switch @emph{-gnatwx} where x
10957 is a letter a-z.
10959 @item 
10960 @emph{[-gnatw.?]}
10961 Used to tag warnings controlled by the switch @emph{-gnatw.x} where x
10962 is a letter a-z.
10964 @item 
10965 @emph{[-gnatel]}
10966 Used to tag elaboration information (info) messages generated when the
10967 static model of elaboration is used and the @emph{-gnatel} switch is set.
10969 @item 
10970 @emph{[restriction warning]}
10971 Used to tag warning messages for restriction violations, activated by use
10972 of the pragma @emph{Restriction_Warnings}.
10974 @item 
10975 @emph{[warning-as-error]}
10976 Used to tag warning messages that have been converted to error messages by
10977 use of the pragma Warning_As_Error. Note that such warnings are prefixed by
10978 the string "error: " rather than "warning: ".
10980 @item 
10981 @emph{[enabled by default]}
10982 Used to tag all other warnings that are always given by default, unless
10983 warnings are completely suppressed using pragma @emph{Warnings(Off)} or
10984 the switch @emph{-gnatws}.
10985 @end itemize
10986 @end quotation
10987 @end table
10989 @geindex -gnatw.d (gcc)
10992 @table @asis
10994 @item @code{-gnatw.D}
10996 @emph{Deactivate tagging of warning and info messages messages.}
10998 If this switch is set, then warning messages return to the default
10999 mode in which warnings and info messages are not tagged as described above for
11000 @cite{-gnatw.d}.
11001 @end table
11003 @geindex -gnatwe (gcc)
11005 @geindex Warnings
11006 @geindex treat as error
11009 @table @asis
11011 @item @code{-gnatwe}
11013 @emph{Treat warnings and style checks as errors.}
11015 This switch causes warning messages and style check messages to be
11016 treated as errors.
11017 The warning string still appears, but the warning messages are counted
11018 as errors, and prevent the generation of an object file. Note that this
11019 is the only -gnatw switch that affects the handling of style check messages.
11020 Note also that this switch has no effect on info (information) messages, which
11021 are not treated as errors if this switch is present.
11022 @end table
11024 @geindex -gnatw.e (gcc)
11027 @table @asis
11029 @item @code{-gnatw.e}
11031 @emph{Activate every optional warning}
11033 @geindex Warnings
11034 @geindex activate every optional warning
11036 This switch activates all optional warnings, including those which
11037 are not activated by @cite{-gnatwa}. The use of this switch is not
11038 recommended for normal use. If you turn this switch on, it is almost
11039 certain that you will get large numbers of useless warnings. The
11040 warnings that are excluded from @cite{-gnatwa} are typically highly
11041 specialized warnings that are suitable for use only in code that has
11042 been specifically designed according to specialized coding rules.
11043 @end table
11045 @geindex -gnatwf (gcc)
11048 @table @asis
11050 @item @code{-gnatwf}
11052 @emph{Activate warnings on unreferenced formals.}
11054 @geindex Formals
11055 @geindex unreferenced
11057 This switch causes a warning to be generated if a formal parameter
11058 is not referenced in the body of the subprogram. This warning can
11059 also be turned on using @emph{-gnatwu}. The
11060 default is that these warnings are not generated.
11061 @end table
11063 @geindex -gnatwF (gcc)
11066 @table @asis
11068 @item @code{-gnatwF}
11070 @emph{Suppress warnings on unreferenced formals.}
11072 This switch suppresses warnings for unreferenced formal
11073 parameters. Note that the
11074 combination @emph{-gnatwu} followed by @emph{-gnatwF} has the
11075 effect of warning on unreferenced entities other than subprogram
11076 formals.
11077 @end table
11079 @geindex -gnatwg (gcc)
11082 @table @asis
11084 @item @code{-gnatwg}
11086 @emph{Activate warnings on unrecognized pragmas.}
11088 @geindex Pragmas
11089 @geindex unrecognized
11091 This switch causes a warning to be generated if an unrecognized
11092 pragma is encountered. Apart from issuing this warning, the
11093 pragma is ignored and has no effect. The default
11094 is that such warnings are issued (satisfying the Ada Reference
11095 Manual requirement that such warnings appear).
11096 @end table
11098 @geindex -gnatwG (gcc)
11101 @table @asis
11103 @item @code{-gnatwG}
11105 @emph{Suppress warnings on unrecognized pragmas.}
11107 This switch suppresses warnings for unrecognized pragmas.
11108 @end table
11110 @geindex -gnatw.g (gcc)
11113 @table @asis
11115 @item @code{-gnatw.g}
11117 @emph{Warnings used for GNAT sources}
11119 This switch sets the warning categories that are used by the standard
11120 GNAT style. Currently this is equivalent to
11121 @emph{-gnatwAao.sI.C.V.X}
11122 but more warnings may be added in the future without advanced notice.
11123 @end table
11125 @geindex -gnatwh (gcc)
11128 @table @asis
11130 @item @code{-gnatwh}
11132 @emph{Activate warnings on hiding.}
11134 @geindex Hiding of Declarations
11136 This switch activates warnings on hiding declarations.
11137 A declaration is considered hiding
11138 if it is for a non-overloadable entity, and it declares an entity with the
11139 same name as some other entity that is directly or use-visible. The default
11140 is that such warnings are not generated.
11141 @end table
11143 @geindex -gnatwH (gcc)
11146 @table @asis
11148 @item @code{-gnatwH}
11150 @emph{Suppress warnings on hiding.}
11152 This switch suppresses warnings on hiding declarations.
11153 @end table
11155 @geindex -gnatw.h (gcc)
11158 @table @asis
11160 @item @code{-gnatw.h}
11162 @emph{Activate warnings on holes/gaps in records.}
11164 @geindex Record Representation (gaps)
11166 This switch activates warnings on component clauses in record
11167 representation clauses that leave holes (gaps) in the record layout.
11168 If this warning option is active, then record representation clauses
11169 should specify a contiguous layout, adding unused fill fields if needed.
11170 @end table
11172 @geindex -gnatw.H (gcc)
11175 @table @asis
11177 @item @code{-gnatw.H}
11179 @emph{Suppress warnings on holes/gaps in records.}
11181 This switch suppresses warnings on component clauses in record
11182 representation clauses that leave holes (haps) in the record layout.
11183 @end table
11185 @geindex -gnatwi (gcc)
11188 @table @asis
11190 @item @code{-gnatwi}
11192 @emph{Activate warnings on implementation units.}
11194 This switch activates warnings for a @emph{with} of an internal GNAT
11195 implementation unit, defined as any unit from the @cite{Ada},
11196 @cite{Interfaces}, @cite{GNAT},
11197 or @cite{System}
11198 hierarchies that is not
11199 documented in either the Ada Reference Manual or the GNAT
11200 Programmer's Reference Manual. Such units are intended only
11201 for internal implementation purposes and should not be @emph{with}ed
11202 by user programs. The default is that such warnings are generated
11203 @end table
11205 @geindex -gnatwI (gcc)
11208 @table @asis
11210 @item @code{-gnatwI}
11212 @emph{Disable warnings on implementation units.}
11214 This switch disables warnings for a @emph{with} of an internal GNAT
11215 implementation unit.
11216 @end table
11218 @geindex -gnatw.i (gcc)
11221 @table @asis
11223 @item @code{-gnatw.i}
11225 @emph{Activate warnings on overlapping actuals.}
11227 This switch enables a warning on statically detectable overlapping actuals in
11228 a subprogram call, when one of the actuals is an in-out parameter, and the
11229 types of the actuals are not by-copy types. This warning is off by default.
11230 @end table
11232 @geindex -gnatw.I (gcc)
11235 @table @asis
11237 @item @code{-gnatw.I}
11239 @emph{Disable warnings on overlapping actuals.}
11241 This switch disables warnings on overlapping actuals in a call..
11242 @end table
11244 @geindex -gnatwj (gcc)
11247 @table @asis
11249 @item @code{-gnatwj}
11251 @emph{Activate warnings on obsolescent features (Annex J).}
11253 @geindex Features
11254 @geindex obsolescent
11256 @geindex Obsolescent features
11258 If this warning option is activated, then warnings are generated for
11259 calls to subprograms marked with @cite{pragma Obsolescent} and
11260 for use of features in Annex J of the Ada Reference Manual. In the
11261 case of Annex J, not all features are flagged. In particular use
11262 of the renamed packages (like @cite{Text_IO}) and use of package
11263 @cite{ASCII} are not flagged, since these are very common and
11264 would generate many annoying positive warnings. The default is that
11265 such warnings are not generated.
11267 In addition to the above cases, warnings are also generated for
11268 GNAT features that have been provided in past versions but which
11269 have been superseded (typically by features in the new Ada standard).
11270 For example, @cite{pragma Ravenscar} will be flagged since its
11271 function is replaced by @cite{pragma Profile(Ravenscar)}, and
11272 @cite{pragma Interface_Name} will be flagged since its function
11273 is replaced by @cite{pragma Import}.
11275 Note that this warning option functions differently from the
11276 restriction @cite{No_Obsolescent_Features} in two respects.
11277 First, the restriction applies only to annex J features.
11278 Second, the restriction does flag uses of package @cite{ASCII}.
11280 @item @code{-gnatwJ}
11282 @emph{Suppress warnings on obsolescent features (Annex J).}
11283 .. index:: -gnatwJ  (gcc)
11285 This switch disables warnings on use of obsolescent features.
11287 @item @code{-gnatwk}
11289 @emph{Activate warnings on variables that could be constants.}
11290 .. index:: -gnatwk  (gcc)
11292 This switch activates warnings for variables that are initialized but
11293 never modified, and then could be declared constants. The default is that
11294 such warnings are not given.
11295 @end table
11297 @geindex -gnatwK (gcc)
11300 @table @asis
11302 @item @code{-gnatwK}
11304 @emph{Suppress warnings on variables that could be constants.}
11306 This switch disables warnings on variables that could be declared constants.
11307 @end table
11309 @geindex -gnatw.k (gcc)
11312 @table @asis
11314 @item @code{-gnatw.k}
11316 @emph{Activate warnings on redefinition of names in standard.}
11318 This switch activates warnings for declarations that declare a name that
11319 is defined in package Standard. Such declarations can be confusing,
11320 especially since the names in package Standard continue to be directly
11321 visible, meaning that use visibiliy on such redeclared names does not
11322 work as expected. Names of discriminants and components in records are
11323 not included in this check.
11324 @end table
11326 @geindex -gnatwK (gcc)
11329 @table @asis
11331 @item @code{-gnatw.K}
11333 @emph{Suppress warnings on redefinition of names in standard.}
11335 This switch activates warnings for declarations that declare a name that
11336 is defined in package Standard.
11337 @end table
11339 @geindex -gnatwl (gcc)
11342 @table @asis
11344 @item @code{-gnatwl}
11346 @emph{Activate warnings for elaboration pragmas.}
11348 @geindex Elaboration
11349 @geindex warnings
11351 This switch activates warnings for possible elaboration problems,
11352 including suspicious use
11353 of @cite{Elaborate} pragmas, when using the static elaboration model, and
11354 possible situations that may raise @cite{Program_Error} when using the
11355 dynamic elaboration model.
11356 See the section in this guide on elaboration checking for further details.
11357 The default is that such warnings
11358 are not generated.
11359 @end table
11361 @geindex -gnatwL (gcc)
11364 @table @asis
11366 @item @code{-gnatwL}
11368 @emph{Suppress warnings for elaboration pragmas.}
11370 This switch suppresses warnings for possible elaboration problems.
11371 @end table
11373 @geindex -gnatw.l (gcc)
11376 @table @asis
11378 @item @code{-gnatw.l}
11380 @emph{List inherited aspects.}
11382 This switch causes the compiler to list inherited invariants,
11383 preconditions, and postconditions from Type_Invariant'Class, Invariant'Class,
11384 Pre'Class, and Post'Class aspects. Also list inherited subtype predicates.
11385 @end table
11387 @geindex -gnatw.L (gcc)
11390 @table @asis
11392 @item @code{-gnatw.L}
11394 @emph{Suppress listing of inherited aspects.}
11396 This switch suppresses listing of inherited aspects.
11397 @end table
11399 @geindex -gnatwm (gcc)
11402 @table @asis
11404 @item @code{-gnatwm}
11406 @emph{Activate warnings on modified but unreferenced variables.}
11408 This switch activates warnings for variables that are assigned (using
11409 an initialization value or with one or more assignment statements) but
11410 whose value is never read. The warning is suppressed for volatile
11411 variables and also for variables that are renamings of other variables
11412 or for which an address clause is given.
11413 The default is that these warnings are not given.
11414 @end table
11416 @geindex -gnatwM (gcc)
11419 @table @asis
11421 @item @code{-gnatwM}
11423 @emph{Disable warnings on modified but unreferenced variables.}
11425 This switch disables warnings for variables that are assigned or
11426 initialized, but never read.
11427 @end table
11429 @geindex -gnatw.m (gcc)
11432 @table @asis
11434 @item @code{-gnatw.m}
11436 @emph{Activate warnings on suspicious modulus values.}
11438 This switch activates warnings for modulus values that seem suspicious.
11439 The cases caught are where the size is the same as the modulus (e.g.
11440 a modulus of 7 with a size of 7 bits), and modulus values of 32 or 64
11441 with no size clause. The guess in both cases is that 2**x was intended
11442 rather than x. In addition expressions of the form 2*x for small x
11443 generate a warning (the almost certainly accurate guess being that
11444 2**x was intended). The default is that these warnings are given.
11445 @end table
11447 @geindex -gnatw.M (gcc)
11450 @table @asis
11452 @item @code{-gnatw.M}
11454 @emph{Disable warnings on suspicious modulus values.}
11456 This switch disables warnings for suspicious modulus values.
11457 @end table
11459 @geindex -gnatwn (gcc)
11462 @table @asis
11464 @item @code{-gnatwn}
11466 @emph{Set normal warnings mode.}
11468 This switch sets normal warning mode, in which enabled warnings are
11469 issued and treated as warnings rather than errors. This is the default
11470 mode. the switch @emph{-gnatwn} can be used to cancel the effect of
11471 an explicit @emph{-gnatws} or
11472 @emph{-gnatwe}. It also cancels the effect of the
11473 implicit @emph{-gnatwe} that is activated by the
11474 use of @emph{-gnatg}.
11475 @end table
11477 @geindex -gnatw.n (gcc)
11479 @geindex Atomic Synchronization
11480 @geindex warnings
11483 @table @asis
11485 @item @code{-gnatw.n}
11487 @emph{Activate warnings on atomic synchronization.}
11489 This switch actives warnings when an access to an atomic variable
11490 requires the generation of atomic synchronization code. These
11491 warnings are off by default.
11492 @end table
11494 @geindex -gnatw.N (gcc)
11497 @table @asis
11499 @item @code{-gnatw.N}
11501 @emph{Suppress warnings on atomic synchronization.}
11503 @geindex Atomic Synchronization
11504 @geindex warnings
11506 This switch suppresses warnings when an access to an atomic variable
11507 requires the generation of atomic synchronization code.
11508 @end table
11510 @geindex -gnatwo (gcc)
11512 @geindex Address Clauses
11513 @geindex warnings
11516 @table @asis
11518 @item @code{-gnatwo}
11520 @emph{Activate warnings on address clause overlays.}
11522 This switch activates warnings for possibly unintended initialization
11523 effects of defining address clauses that cause one variable to overlap
11524 another. The default is that such warnings are generated.
11525 @end table
11527 @geindex -gnatwO (gcc)
11530 @table @asis
11532 @item @code{-gnatwO}
11534 @emph{Suppress warnings on address clause overlays.}
11536 This switch suppresses warnings on possibly unintended initialization
11537 effects of defining address clauses that cause one variable to overlap
11538 another.
11539 @end table
11541 @geindex -gnatw.o (gcc)
11544 @table @asis
11546 @item @code{-gnatw.o}
11548 @emph{Activate warnings on modified but unreferenced out parameters.}
11550 This switch activates warnings for variables that are modified by using
11551 them as actuals for a call to a procedure with an out mode formal, where
11552 the resulting assigned value is never read. It is applicable in the case
11553 where there is more than one out mode formal. If there is only one out
11554 mode formal, the warning is issued by default (controlled by -gnatwu).
11555 The warning is suppressed for volatile
11556 variables and also for variables that are renamings of other variables
11557 or for which an address clause is given.
11558 The default is that these warnings are not given.
11559 @end table
11561 @geindex -gnatw.O (gcc)
11564 @table @asis
11566 @item @code{-gnatw.O}
11568 @emph{Disable warnings on modified but unreferenced out parameters.}
11570 This switch suppresses warnings for variables that are modified by using
11571 them as actuals for a call to a procedure with an out mode formal, where
11572 the resulting assigned value is never read.
11573 @end table
11575 @geindex -gnatwp (gcc)
11577 @geindex Inlining
11578 @geindex warnings
11581 @table @asis
11583 @item @code{-gnatwp}
11585 @emph{Activate warnings on ineffective pragma Inlines.}
11587 This switch activates warnings for failure of front end inlining
11588 (activated by @emph{-gnatN}) to inline a particular call. There are
11589 many reasons for not being able to inline a call, including most
11590 commonly that the call is too complex to inline. The default is
11591 that such warnings are not given.
11592 Warnings on ineffective inlining by the gcc back-end can be activated
11593 separately, using the gcc switch -Winline.
11594 @end table
11596 @geindex -gnatwP (gcc)
11599 @table @asis
11601 @item @code{-gnatwP}
11603 @emph{Suppress warnings on ineffective pragma Inlines.}
11605 This switch suppresses warnings on ineffective pragma Inlines. If the
11606 inlining mechanism cannot inline a call, it will simply ignore the
11607 request silently.
11608 @end table
11610 @geindex -gnatw.p (gcc)
11612 @geindex Parameter order
11613 @geindex warnings
11616 @table @asis
11618 @item @code{-gnatw.p}
11620 @emph{Activate warnings on parameter ordering.}
11622 This switch activates warnings for cases of suspicious parameter
11623 ordering when the list of arguments are all simple identifiers that
11624 match the names of the formals, but are in a different order. The
11625 warning is suppressed if any use of named parameter notation is used,
11626 so this is the appropriate way to suppress a false positive (and
11627 serves to emphasize that the "misordering" is deliberate). The
11628 default is that such warnings are not given.
11629 @end table
11631 @geindex -gnatw.P (gcc)
11634 @table @asis
11636 @item @code{-gnatw.P}
11638 @emph{Suppress warnings on parameter ordering.}
11640 This switch suppresses warnings on cases of suspicious parameter
11641 ordering.
11642 @end table
11644 @geindex -gnatwq (gcc)
11646 @geindex Parentheses
11647 @geindex warnings
11650 @table @asis
11652 @item @code{-gnatwq}
11654 @emph{Activate warnings on questionable missing parentheses.}
11656 This switch activates warnings for cases where parentheses are not used and
11657 the result is potential ambiguity from a readers point of view. For example
11658 (not a > b) when a and b are modular means ((not a) > b) and very likely the
11659 programmer intended (not (a > b)). Similarly (-x mod 5) means (-(x mod 5)) and
11660 quite likely ((-x) mod 5) was intended. In such situations it seems best to
11661 follow the rule of always parenthesizing to make the association clear, and
11662 this warning switch warns if such parentheses are not present. The default
11663 is that these warnings are given.
11664 @end table
11666 @geindex -gnatwQ (gcc)
11669 @table @asis
11671 @item @code{-gnatwQ}
11673 @emph{Suppress warnings on questionable missing parentheses.}
11675 This switch suppresses warnings for cases where the association is not
11676 clear and the use of parentheses is preferred.
11677 @end table
11679 @geindex -gnatwr (gcc)
11682 @table @asis
11684 @item @code{-gnatwr}
11686 @emph{Activate warnings on redundant constructs.}
11688 This switch activates warnings for redundant constructs. The following
11689 is the current list of constructs regarded as redundant:
11692 @itemize *
11694 @item 
11695 Assignment of an item to itself.
11697 @item 
11698 Type conversion that converts an expression to its own type.
11700 @item 
11701 Use of the attribute @cite{Base} where @cite{typ'Base} is the same
11702 as @cite{typ}.
11704 @item 
11705 Use of pragma @cite{Pack} when all components are placed by a record
11706 representation clause.
11708 @item 
11709 Exception handler containing only a reraise statement (raise with no
11710 operand) which has no effect.
11712 @item 
11713 Use of the operator abs on an operand that is known at compile time
11714 to be non-negative
11716 @item 
11717 Comparison of boolean expressions to an explicit True value.
11718 @end itemize
11720 The default is that warnings for redundant constructs are not given.
11721 @end table
11723 @geindex -gnatwR (gcc)
11726 @table @asis
11728 @item @code{-gnatwR}
11730 @emph{Suppress warnings on redundant constructs.}
11732 This switch suppresses warnings for redundant constructs.
11733 @end table
11735 @geindex -gnatw.r (gcc)
11738 @table @asis
11740 @item @code{-gnatw.r}
11742 @emph{Activate warnings for object renaming function.}
11744 This switch activates warnings for an object renaming that renames a
11745 function call, which is equivalent to a constant declaration (as
11746 opposed to renaming the function itself).  The default is that these
11747 warnings are given.
11748 @end table
11750 @geindex -gnatwT (gcc)
11753 @table @asis
11755 @item @code{-gnatw.R}
11757 @emph{Suppress warnings for object renaming function.}
11759 This switch suppresses warnings for object renaming function.
11760 @end table
11762 @geindex -gnatws (gcc)
11765 @table @asis
11767 @item @code{-gnatws}
11769 @emph{Suppress all warnings.}
11771 This switch completely suppresses the
11772 output of all warning messages from the GNAT front end, including
11773 both warnings that can be controlled by switches described in this
11774 section, and those that are normally given unconditionally. The
11775 effect of this suppress action can only be cancelled by a subsequent
11776 use of the switch @emph{-gnatwn}.
11778 Note that switch @emph{-gnatws} does not suppress
11779 warnings from the @emph{gcc} back end.
11780 To suppress these back end warnings as well, use the switch @emph{-w}
11781 in addition to @emph{-gnatws}. Also this switch has no effect on the
11782 handling of style check messages.
11783 @end table
11785 @geindex -gnatw.s (gcc)
11787 @geindex Record Representation (component sizes)
11790 @table @asis
11792 @item @code{-gnatw.s}
11794 @emph{Activate warnings on overridden size clauses.}
11796 This switch activates warnings on component clauses in record
11797 representation clauses where the length given overrides that
11798 specified by an explicit size clause for the component type. A
11799 warning is similarly given in the array case if a specified
11800 component size overrides an explicit size clause for the array
11801 component type.
11802 @end table
11804 @geindex -gnatw.S (gcc)
11807 @table @asis
11809 @item @code{-gnatw.S}
11811 @emph{Suppress warnings on overridden size clauses.}
11813 This switch suppresses warnings on component clauses in record
11814 representation clauses that override size clauses, and similar
11815 warnings when an array component size overrides a size clause.
11816 @end table
11818 @geindex -gnatwt (gcc)
11820 @geindex Deactivated code
11821 @geindex warnings
11823 @geindex Deleted code
11824 @geindex warnings
11827 @table @asis
11829 @item @code{-gnatwt}
11831 @emph{Activate warnings for tracking of deleted conditional code.}
11833 This switch activates warnings for tracking of code in conditionals (IF and
11834 CASE statements) that is detected to be dead code which cannot be executed, and
11835 which is removed by the front end. This warning is off by default. This may be
11836 useful for detecting deactivated code in certified applications.
11837 @end table
11839 @geindex -gnatwT (gcc)
11842 @table @asis
11844 @item @code{-gnatwT}
11846 @emph{Suppress warnings for tracking of deleted conditional code.}
11848 This switch suppresses warnings for tracking of deleted conditional code.
11849 @end table
11851 @geindex -gnatw.t (gcc)
11854 @table @asis
11856 @item @code{-gnatw.t}
11858 @emph{Activate warnings on suspicious contracts.}
11860 This switch activates warnings on suspicious contracts. This includes
11861 warnings on suspicious postconditions (whether a pragma @cite{Postcondition} or a
11862 @cite{Post} aspect in Ada 2012) and suspicious contract cases (pragma or aspect
11863 @cite{Contract_Cases}). A function postcondition or contract case is suspicious
11864 when no postcondition or contract case for this function mentions the result
11865 of the function.  A procedure postcondition or contract case is suspicious
11866 when it only refers to the pre-state of the procedure, because in that case
11867 it should rather be expressed as a precondition. This switch also controls
11868 warnings on suspicious cases of expressions typically found in contracts like
11869 quantified expressions and uses of Update attribute. The default is that such
11870 warnings are generated.
11871 @end table
11873 @geindex -gnatw.T (gcc)
11876 @table @asis
11878 @item @code{-gnatw.T}
11880 @emph{Suppress warnings on suspicious contracts.}
11882 This switch suppresses warnings on suspicious contracts.
11883 @end table
11885 @geindex -gnatwu (gcc)
11888 @table @asis
11890 @item @code{-gnatwu}
11892 @emph{Activate warnings on unused entities.}
11894 This switch activates warnings to be generated for entities that
11895 are declared but not referenced, and for units that are @emph{with}ed
11896 and not
11897 referenced. In the case of packages, a warning is also generated if
11898 no entities in the package are referenced. This means that if a with'ed
11899 package is referenced but the only references are in @cite{use}
11900 clauses or @cite{renames}
11901 declarations, a warning is still generated. A warning is also generated
11902 for a generic package that is @emph{with}ed but never instantiated.
11903 In the case where a package or subprogram body is compiled, and there
11904 is a @emph{with} on the corresponding spec
11905 that is only referenced in the body,
11906 a warning is also generated, noting that the
11907 @emph{with} can be moved to the body. The default is that
11908 such warnings are not generated.
11909 This switch also activates warnings on unreferenced formals
11910 (it includes the effect of @emph{-gnatwf}).
11911 @end table
11913 @geindex -gnatwU (gcc)
11916 @table @asis
11918 @item @code{-gnatwU}
11920 @emph{Suppress warnings on unused entities.}
11922 This switch suppresses warnings for unused entities and packages.
11923 It also turns off warnings on unreferenced formals (and thus includes
11924 the effect of @emph{-gnatwF}).
11925 @end table
11927 @geindex -gnatw.u (gcc)
11930 @table @asis
11932 @item @code{-gnatw.u}
11934 @emph{Activate warnings on unordered enumeration types.}
11936 This switch causes enumeration types to be considered as conceptually
11937 unordered, unless an explicit pragma @cite{Ordered} is given for the type.
11938 The effect is to generate warnings in clients that use explicit comparisons
11939 or subranges, since these constructs both treat objects of the type as
11940 ordered. (A @emph{client} is defined as a unit that is other than the unit in
11941 which the type is declared, or its body or subunits.) Please refer to
11942 the description of pragma @cite{Ordered} in the
11943 @cite{GNAT Reference Manual} for further details.
11944 The default is that such warnings are not generated.
11945 @end table
11947 @geindex -gnatw.U (gcc)
11950 @table @asis
11952 @item @code{-gnatw.U}
11954 @emph{Deactivate warnings on unordered enumeration types.}
11956 This switch causes all enumeration types to be considered as ordered, so
11957 that no warnings are given for comparisons or subranges for any type.
11958 @end table
11960 @geindex -gnatwv (gcc)
11962 @geindex Unassigned variable warnings
11965 @table @asis
11967 @item @code{-gnatwv}
11969 @emph{Activate warnings on unassigned variables.}
11971 This switch activates warnings for access to variables which
11972 may not be properly initialized. The default is that
11973 such warnings are generated.
11974 @end table
11976 @geindex -gnatwV (gcc)
11979 @table @asis
11981 @item @code{-gnatwV}
11983 @emph{Suppress warnings on unassigned variables.}
11985 This switch suppresses warnings for access to variables which
11986 may not be properly initialized.
11987 For variables of a composite type, the warning can also be suppressed in
11988 Ada 2005 by using a default initialization with a box. For example, if
11989 Table is an array of records whose components are only partially uninitialized,
11990 then the following code:
11992 @example
11993 Tab : Table := (others => <>);
11994 @end example
11996 will suppress warnings on subsequent statements that access components
11997 of variable Tab.
11998 @end table
12000 @geindex -gnatw.v (gcc)
12002 @geindex bit order warnings
12005 @table @asis
12007 @item @code{-gnatw.v}
12009 @emph{Activate info messages for non-default bit order.}
12011 This switch activates messages (labeled "info", they are not warnings,
12012 just informational messages) about the effects of non-default bit-order
12013 on records to which a component clause is applied. The effect of specifying
12014 non-default bit ordering is a bit subtle (and changed with Ada 2005), so
12015 these messages, which are given by default, are useful in understanding the
12016 exact consequences of using this feature.
12017 @end table
12019 @geindex -gnatw.V (gcc)
12022 @table @asis
12024 @item @code{-gnatw.V}
12026 @emph{Suppress info messages for non-default bit order.}
12028 This switch suppresses information messages for the effects of specifying
12029 non-default bit order on record components with component clauses.
12030 @end table
12032 @geindex -gnatww (gcc)
12034 @geindex String indexing warnings
12037 @table @asis
12039 @item @code{-gnatww}
12041 @emph{Activate warnings on wrong low bound assumption.}
12043 This switch activates warnings for indexing an unconstrained string parameter
12044 with a literal or S'Length. This is a case where the code is assuming that the
12045 low bound is one, which is in general not true (for example when a slice is
12046 passed). The default is that such warnings are generated.
12047 @end table
12049 @geindex -gnatwW (gcc)
12052 @table @asis
12054 @item @code{-gnatwW}
12056 @emph{Suppress warnings on wrong low bound assumption.}
12058 This switch suppresses warnings for indexing an unconstrained string parameter
12059 with a literal or S'Length. Note that this warning can also be suppressed
12060 in a particular case by adding an assertion that the lower bound is 1,
12061 as shown in the following example:
12063 @example
12064 procedure K (S : String) is
12065    pragma Assert (S'First = 1);
12066    ...
12067 @end example
12068 @end table
12070 @geindex -gnatw.w (gcc)
12072 @geindex Warnings Off control
12075 @table @asis
12077 @item @code{-gnatw.w}
12079 @emph{Activate warnings on Warnings Off pragmas}
12081 This switch activates warnings for use of @cite{pragma Warnings (Off@comma{} entity)}
12082 where either the pragma is entirely useless (because it suppresses no
12083 warnings), or it could be replaced by @cite{pragma Unreferenced} or
12084 @cite{pragma Unmodified}.
12085 Also activates warnings for the case of
12086 Warnings (Off, String), where either there is no matching
12087 Warnings (On, String), or the Warnings (Off) did not suppress any warning.
12088 The default is that these warnings are not given.
12089 @end table
12091 @geindex -gnatw.W (gcc)
12094 @table @asis
12096 @item @code{-gnatw.W}
12098 @emph{Suppress warnings on unnecessary Warnings Off pragmas}
12100 This switch suppresses warnings for use of @cite{pragma Warnings (Off@comma{} ...)}.
12101 @end table
12103 @geindex -gnatwx (gcc)
12105 @geindex Export/Import pragma warnings
12108 @table @asis
12110 @item @code{-gnatwx}
12112 @emph{Activate warnings on Export/Import pragmas.}
12114 This switch activates warnings on Export/Import pragmas when
12115 the compiler detects a possible conflict between the Ada and
12116 foreign language calling sequences. For example, the use of
12117 default parameters in a convention C procedure is dubious
12118 because the C compiler cannot supply the proper default, so
12119 a warning is issued. The default is that such warnings are
12120 generated.
12121 @end table
12123 @geindex -gnatwX (gcc)
12126 @table @asis
12128 @item @code{-gnatwX}
12130 @emph{Suppress warnings on Export/Import pragmas.}
12132 This switch suppresses warnings on Export/Import pragmas.
12133 The sense of this is that you are telling the compiler that
12134 you know what you are doing in writing the pragma, and it
12135 should not complain at you.
12136 @end table
12138 @geindex -gnatwm (gcc)
12141 @table @asis
12143 @item @code{-gnatw.x}
12145 @emph{Activate warnings for No_Exception_Propagation mode.}
12147 This switch activates warnings for exception usage when pragma Restrictions
12148 (No_Exception_Propagation) is in effect. Warnings are given for implicit or
12149 explicit exception raises which are not covered by a local handler, and for
12150 exception handlers which do not cover a local raise. The default is that these
12151 warnings are not given.
12153 @item @code{-gnatw.X}
12155 @emph{Disable warnings for No_Exception_Propagation mode.}
12157 This switch disables warnings for exception usage when pragma Restrictions
12158 (No_Exception_Propagation) is in effect.
12159 @end table
12161 @geindex -gnatwy (gcc)
12163 @geindex Ada compatibility issues warnings
12166 @table @asis
12168 @item @code{-gnatwy}
12170 @emph{Activate warnings for Ada compatibility issues.}
12172 For the most part, newer versions of Ada are upwards compatible
12173 with older versions. For example, Ada 2005 programs will almost
12174 always work when compiled as Ada 2012.
12175 However there are some exceptions (for example the fact that
12176 @cite{some} is now a reserved word in Ada 2012). This
12177 switch activates several warnings to help in identifying
12178 and correcting such incompatibilities. The default is that
12179 these warnings are generated. Note that at one point Ada 2005
12180 was called Ada 0Y, hence the choice of character.
12181 @end table
12183 @geindex -gnatwY (gcc)
12185 @geindex Ada compatibility issues warnings
12188 @table @asis
12190 @item @code{-gnatwY}
12192 @emph{Disable warnings for Ada compatibility issues.}
12194 This switch suppresses the warnings intended to help in identifying
12195 incompatibilities between Ada language versions.
12196 @end table
12198 @geindex -gnatw.y (gcc)
12200 @geindex Package spec needing body
12203 @table @asis
12205 @item @code{-gnatw.y}
12207 @emph{Activate information messages for why package spec needs body}
12209 There are a number of cases in which a package spec needs a body.
12210 For example, the use of pragma Elaborate_Body, or the declaration
12211 of a procedure specification requiring a completion. This switch
12212 causes information messages to be output showing why a package
12213 specification requires a body. This can be useful in the case of
12214 a large package specification which is unexpectedly requiring a
12215 body. The default is that such information messages are not output.
12216 @end table
12218 @geindex -gnatw.Y (gcc)
12220 @geindex No information messages for why package spec needs body
12223 @table @asis
12225 @item @code{-gnatw.Y}
12227 @emph{Disable information messages for why package spec needs body}
12229 This switch suppresses the output of information messages showing why
12230 a package specification needs a body.
12231 @end table
12233 @geindex -gnatwz (gcc)
12235 @geindex Unchecked_Conversion warnings
12238 @table @asis
12240 @item @code{-gnatwz}
12242 @emph{Activate warnings on unchecked conversions.}
12244 This switch activates warnings for unchecked conversions
12245 where the types are known at compile time to have different
12246 sizes. The default is that such warnings are generated. Warnings are also
12247 generated for subprogram pointers with different conventions.
12248 @end table
12250 @geindex -gnatwZ (gcc)
12253 @table @asis
12255 @item @code{-gnatwZ}
12257 @emph{Suppress warnings on unchecked conversions.}
12259 This switch suppresses warnings for unchecked conversions
12260 where the types are known at compile time to have different
12261 sizes or conventions.
12262 @end table
12264 @geindex -gnatw.z (gcc)
12266 @geindex Size/Alignment warnings
12269 @table @asis
12271 @item @code{-gnatw.z}
12273 @emph{Activate warnings for size not a multiple of alignment.}
12275 This switch activates warnings for cases of record types with
12276 specified @cite{Size} and @cite{Alignment} attributes where the
12277 size is not a multiple of the alignment, resulting in an object
12278 size that is greater than the specified size. The default
12279 is that such warnings are generated.
12280 @end table
12282 @geindex -gnatw.Z (gcc)
12284 @geindex Size/Alignment warnings
12287 @table @asis
12289 @item @code{-gnatw.Z}
12291 @emph{Suppress warnings for size not a multiple of alignment.}
12293 This switch suppresses warnings for cases of record types with
12294 specified @cite{Size} and @cite{Alignment} attributes where the
12295 size is not a multiple of the alignment, resulting in an object
12296 size that is greater than the specified size.
12297 The warning can also be
12298 suppressed by giving an explicit @cite{Object_Size} value.
12299 @end table
12301 @geindex -Wunused (gcc)
12304 @table @asis
12306 @item @code{-Wunused}
12308 The warnings controlled by the @emph{-gnatw} switch are generated by
12309 the front end of the compiler. The @emph{GCC} back end can provide
12310 additional warnings and they are controlled by the @emph{-W} switch.
12311 For example, @emph{-Wunused} activates back end
12312 warnings for entities that are declared but not referenced.
12313 @end table
12315 @geindex -Wuninitialized (gcc)
12318 @table @asis
12320 @item @code{-Wuninitialized}
12322 Similarly, @emph{-Wuninitialized} activates
12323 the back end warning for uninitialized variables. This switch must be
12324 used in conjunction with an optimization level greater than zero.
12325 @end table
12327 @geindex -Wstack-usage (gcc)
12330 @table @asis
12332 @item @code{-Wstack-usage=@emph{len}}
12334 Warn if the stack usage of a subprogram might be larger than @cite{len} bytes.
12335 See @ref{fa,,Static Stack Usage Analysis} for details.
12336 @end table
12338 @geindex -Wall (gcc)
12341 @table @asis
12343 @item @code{-Wall}
12345 This switch enables most warnings from the @emph{GCC} back end.
12346 The code generator detects a number of warning situations that are missed
12347 by the @emph{GNAT} front end, and this switch can be used to activate them.
12348 The use of this switch also sets the default front end warning mode to
12349 @emph{-gnatwa}, that is, most front end warnings activated as well.
12350 @end table
12352 @geindex -w (gcc)
12355 @table @asis
12357 @item @code{-w}
12359 Conversely, this switch suppresses warnings from the @emph{GCC} back end.
12360 The use of this switch also sets the default front end warning mode to
12361 @emph{-gnatws}, that is, front end warnings suppressed as well.
12362 @end table
12364 @geindex -Werror (gcc)
12367 @table @asis
12369 @item @code{-Werror}
12371 This switch causes warnings from the @emph{GCC} back end to be treated as
12372 errors.  The warning string still appears, but the warning messages are
12373 counted as errors, and prevent the generation of an object file.
12374 @end table
12376 A string of warning parameters can be used in the same parameter. For example:
12378 @example
12379 -gnatwaGe
12380 @end example
12382 will turn on all optional warnings except for unrecognized pragma warnings,
12383 and also specify that warnings should be treated as errors.
12385 When no switch @emph{-gnatw} is used, this is equivalent to:
12387 @quotation
12390 @itemize *
12392 @item 
12393 @code{-gnatw.a}
12395 @item 
12396 @code{-gnatwB}
12398 @item 
12399 @code{-gnatw.b}
12401 @item 
12402 @code{-gnatwC}
12404 @item 
12405 @code{-gnatw.C}
12407 @item 
12408 @code{-gnatwD}
12410 @item 
12411 @code{-gnatwF}
12413 @item 
12414 @code{-gnatwg}
12416 @item 
12417 @code{-gnatwH}
12419 @item 
12420 @code{-gnatwi}
12422 @item 
12423 @code{-gnatw.I}
12425 @item 
12426 @code{-gnatwJ}
12428 @item 
12429 @code{-gnatwK}
12431 @item 
12432 @code{-gnatwL}
12434 @item 
12435 @code{-gnatw.L}
12437 @item 
12438 @code{-gnatwM}
12440 @item 
12441 @code{-gnatw.m}
12443 @item 
12444 @code{-gnatwn}
12446 @item 
12447 @code{-gnatwo}
12449 @item 
12450 @code{-gnatw.O}
12452 @item 
12453 @code{-gnatwP}
12455 @item 
12456 @code{-gnatw.P}
12458 @item 
12459 @code{-gnatwq}
12461 @item 
12462 @code{-gnatwR}
12464 @item 
12465 @code{-gnatw.R}
12467 @item 
12468 @code{-gnatw.S}
12470 @item 
12471 @code{-gnatwT}
12473 @item 
12474 @code{-gnatw.T}
12476 @item 
12477 @code{-gnatwU}
12479 @item 
12480 @code{-gnatwv}
12482 @item 
12483 @code{-gnatww}
12485 @item 
12486 @code{-gnatw.W}
12488 @item 
12489 @code{-gnatwx}
12491 @item 
12492 @code{-gnatw.X}
12494 @item 
12495 @code{-gnatwy}
12497 @item 
12498 @code{-gnatwz}
12499 @end itemize
12500 @end quotation
12502 @node Debugging and Assertion Control,Validity Checking,Warning Message Control,Compiler Switches
12503 @anchor{gnat_ugn/building_executable_programs_with_gnat debugging-and-assertion-control}@anchor{105}@anchor{gnat_ugn/building_executable_programs_with_gnat id16}@anchor{106}
12504 @subsection Debugging and Assertion Control
12507 @geindex -gnata (gcc)
12510 @table @asis
12512 @item @code{-gnata}
12514 @geindex Assert
12516 @geindex Debug
12518 @geindex Assertions
12520 The pragmas @cite{Assert} and @cite{Debug} normally have no effect and
12521 are ignored. This switch, where @code{a} stands for assert, causes
12522 @cite{Assert} and @cite{Debug} pragmas to be activated.
12524 The pragmas have the form:
12526 @example
12527 pragma Assert (<Boolean-expression> [, <static-string-expression>])
12528 pragma Debug (<procedure call>)
12529 @end example
12531 The @cite{Assert} pragma causes @cite{Boolean-expression} to be tested.
12532 If the result is @cite{True}, the pragma has no effect (other than
12533 possible side effects from evaluating the expression). If the result is
12534 @cite{False}, the exception @cite{Assert_Failure} declared in the package
12535 @cite{System.Assertions} is
12536 raised (passing @cite{static-string-expression}, if present, as the
12537 message associated with the exception). If no string expression is
12538 given the default is a string giving the file name and line number
12539 of the pragma.
12541 The @cite{Debug} pragma causes @cite{procedure} to be called. Note that
12542 @cite{pragma Debug} may appear within a declaration sequence, allowing
12543 debugging procedures to be called between declarations.
12544 @end table
12546 @node Validity Checking,Style Checking,Debugging and Assertion Control,Compiler Switches
12547 @anchor{gnat_ugn/building_executable_programs_with_gnat validity-checking}@anchor{fb}@anchor{gnat_ugn/building_executable_programs_with_gnat id17}@anchor{107}
12548 @subsection Validity Checking
12551 @geindex Validity Checking
12553 The Ada Reference Manual defines the concept of invalid values (see
12554 RM 13.9.1). The primary source of invalid values is uninitialized
12555 variables. A scalar variable that is left uninitialized may contain
12556 an invalid value; the concept of invalid does not apply to access or
12557 composite types.
12559 It is an error to read an invalid value, but the RM does not require
12560 run-time checks to detect such errors, except for some minimal
12561 checking to prevent erroneous execution (i.e. unpredictable
12562 behavior). This corresponds to the @emph{-gnatVd} switch below,
12563 which is the default. For example, by default, if the expression of a
12564 case statement is invalid, it will raise Constraint_Error rather than
12565 causing a wild jump, and if an array index on the left-hand side of an
12566 assignment is invalid, it will raise Constraint_Error rather than
12567 overwriting an arbitrary memory location.
12569 The @emph{-gnatVa} may be used to enable additional validity checks,
12570 which are not required by the RM. These checks are often very
12571 expensive (which is why the RM does not require them). These checks
12572 are useful in tracking down uninitialized variables, but they are
12573 not usually recommended for production builds, and in particular
12574 we do not recommend using these extra validity checking options in
12575 combination with optimization, since this can confuse the optimizer.
12576 If performance is a consideration, leading to the need to optimize,
12577 then the validity checking options should not be used.
12579 The other @emph{-gnatV}@code{x} switches below allow finer-grained
12580 control; you can enable whichever validity checks you desire. However,
12581 for most debugging purposes, @emph{-gnatVa} is sufficient, and the
12582 default @emph{-gnatVd} (i.e. standard Ada behavior) is usually
12583 sufficient for non-debugging use.
12585 The @emph{-gnatB} switch tells the compiler to assume that all
12586 values are valid (that is, within their declared subtype range)
12587 except in the context of a use of the Valid attribute. This means
12588 the compiler can generate more efficient code, since the range
12589 of values is better known at compile time. However, an uninitialized
12590 variable can cause wild jumps and memory corruption in this mode.
12592 The @emph{-gnatV}@code{x} switch allows control over the validity
12593 checking mode as described below.
12594 The @code{x} argument is a string of letters that
12595 indicate validity checks that are performed or not performed in addition
12596 to the default checks required by Ada as described above.
12598 @geindex -gnatVa (gcc)
12601 @table @asis
12603 @item @code{-gnatVa}
12605 @emph{All validity checks.}
12607 All validity checks are turned on.
12608 That is, @emph{-gnatVa} is
12609 equivalent to @emph{gnatVcdfimorst}.
12610 @end table
12612 @geindex -gnatVc (gcc)
12615 @table @asis
12617 @item @code{-gnatVc}
12619 @emph{Validity checks for copies.}
12621 The right hand side of assignments, and the initializing values of
12622 object declarations are validity checked.
12623 @end table
12625 @geindex -gnatVd (gcc)
12628 @table @asis
12630 @item @code{-gnatVd}
12632 @emph{Default (RM) validity checks.}
12634 Some validity checks are done by default following normal Ada semantics
12635 (RM 13.9.1 (9-11)).
12636 A check is done in case statements that the expression is within the range
12637 of the subtype. If it is not, Constraint_Error is raised.
12638 For assignments to array components, a check is done that the expression used
12639 as index is within the range. If it is not, Constraint_Error is raised.
12640 Both these validity checks may be turned off using switch @emph{-gnatVD}.
12641 They are turned on by default. If @emph{-gnatVD} is specified, a subsequent
12642 switch @emph{-gnatVd} will leave the checks turned on.
12643 Switch @emph{-gnatVD} should be used only if you are sure that all such
12644 expressions have valid values. If you use this switch and invalid values
12645 are present, then the program is erroneous, and wild jumps or memory
12646 overwriting may occur.
12647 @end table
12649 @geindex -gnatVe (gcc)
12652 @table @asis
12654 @item @code{-gnatVe}
12656 @emph{Validity checks for elementary components.}
12658 In the absence of this switch, assignments to record or array components are
12659 not validity checked, even if validity checks for assignments generally
12660 (@emph{-gnatVc}) are turned on. In Ada, assignment of composite values do not
12661 require valid data, but assignment of individual components does. So for
12662 example, there is a difference between copying the elements of an array with a
12663 slice assignment, compared to assigning element by element in a loop. This
12664 switch allows you to turn off validity checking for components, even when they
12665 are assigned component by component.
12666 @end table
12668 @geindex -gnatVf (gcc)
12671 @table @asis
12673 @item @code{-gnatVf}
12675 @emph{Validity checks for floating-point values.}
12677 In the absence of this switch, validity checking occurs only for discrete
12678 values. If @emph{-gnatVf} is specified, then validity checking also applies
12679 for floating-point values, and NaNs and infinities are considered invalid,
12680 as well as out of range values for constrained types. Note that this means
12681 that standard IEEE infinity mode is not allowed. The exact contexts
12682 in which floating-point values are checked depends on the setting of other
12683 options. For example, @emph{-gnatVif} or @emph{-gnatVfi}
12684 (the order does not matter) specifies that floating-point parameters of mode
12685 @cite{in} should be validity checked.
12686 @end table
12688 @geindex -gnatVi (gcc)
12691 @table @asis
12693 @item @code{-gnatVi}
12695 @emph{Validity checks for `in} mode parameters`
12697 Arguments for parameters of mode @cite{in} are validity checked in function
12698 and procedure calls at the point of call.
12699 @end table
12701 @geindex -gnatVm (gcc)
12704 @table @asis
12706 @item @code{-gnatVm}
12708 @emph{Validity checks for `in out} mode parameters.`
12710 Arguments for parameters of mode @cite{in out} are validity checked in
12711 procedure calls at the point of call. The @cite{'m'} here stands for
12712 modify, since this concerns parameters that can be modified by the call.
12713 Note that there is no specific option to test @cite{out} parameters,
12714 but any reference within the subprogram will be tested in the usual
12715 manner, and if an invalid value is copied back, any reference to it
12716 will be subject to validity checking.
12717 @end table
12719 @geindex -gnatVn (gcc)
12722 @table @asis
12724 @item @code{-gnatVn}
12726 @emph{No validity checks.}
12728 This switch turns off all validity checking, including the default checking
12729 for case statements and left hand side subscripts. Note that the use of
12730 the switch @emph{-gnatp} suppresses all run-time checks, including
12731 validity checks, and thus implies @emph{-gnatVn}. When this switch
12732 is used, it cancels any other @emph{-gnatV} previously issued.
12734 @item @code{-gnatVo}
12736 @emph{Validity checks for operator and attribute operands.}
12737 .. index:: -gnatVo  (gcc)
12739 Arguments for predefined operators and attributes are validity checked.
12740 This includes all operators in package @cite{Standard},
12741 the shift operators defined as intrinsic in package @cite{Interfaces}
12742 and operands for attributes such as @cite{Pos}. Checks are also made
12743 on individual component values for composite comparisons, and on the
12744 expressions in type conversions and qualified expressions. Checks are
12745 also made on explicit ranges using @code{..} (e.g., slices, loops etc).
12746 @end table
12748 @geindex -gnatVp (gcc)
12751 @table @asis
12753 @item @code{-gnatVp}
12755 @emph{Validity checks for parameters.}
12757 This controls the treatment of parameters within a subprogram (as opposed
12758 to @emph{-gnatVi} and @emph{-gnatVm} which control validity testing
12759 of parameters on a call. If either of these call options is used, then
12760 normally an assumption is made within a subprogram that the input arguments
12761 have been validity checking at the point of call, and do not need checking
12762 again within a subprogram). If @emph{-gnatVp} is set, then this assumption
12763 is not made, and parameters are not assumed to be valid, so their validity
12764 will be checked (or rechecked) within the subprogram.
12765 @end table
12767 @geindex -gnatVr (gcc)
12770 @table @asis
12772 @item @code{-gnatVr}
12774 @emph{Validity checks for function returns.}
12776 The expression in @cite{return} statements in functions is validity
12777 checked.
12778 @end table
12780 @geindex -gnatVs (gcc)
12783 @table @asis
12785 @item @code{-gnatVs}
12787 @emph{Validity checks for subscripts.}
12789 All subscripts expressions are checked for validity, whether they appear
12790 on the right side or left side (in default mode only left side subscripts
12791 are validity checked).
12792 @end table
12794 @geindex -gnatVt (gcc)
12797 @table @asis
12799 @item @code{-gnatVt}
12801 @emph{Validity checks for tests.}
12803 Expressions used as conditions in @cite{if}, @cite{while} or @cite{exit}
12804 statements are checked, as well as guard expressions in entry calls.
12805 @end table
12807 The @emph{-gnatV} switch may be followed by a string of letters
12808 to turn on a series of validity checking options.
12809 For example, @code{-gnatVcr}
12810 specifies that in addition to the default validity checking, copies and
12811 function return expressions are to be validity checked.
12812 In order to make it easier to specify the desired combination of effects,
12813 the upper case letters @cite{CDFIMORST} may
12814 be used to turn off the corresponding lower case option.
12815 Thus @code{-gnatVaM} turns on all validity checking options except for
12816 checking of @cite{**in out**} procedure arguments.
12818 The specification of additional validity checking generates extra code (and
12819 in the case of @emph{-gnatVa} the code expansion can be substantial).
12820 However, these additional checks can be very useful in detecting
12821 uninitialized variables, incorrect use of unchecked conversion, and other
12822 errors leading to invalid values. The use of pragma @cite{Initialize_Scalars}
12823 is useful in conjunction with the extra validity checking, since this
12824 ensures that wherever possible uninitialized variables have invalid values.
12826 See also the pragma @cite{Validity_Checks} which allows modification of
12827 the validity checking mode at the program source level, and also allows for
12828 temporary disabling of validity checks.
12830 @node Style Checking,Run-Time Checks,Validity Checking,Compiler Switches
12831 @anchor{gnat_ugn/building_executable_programs_with_gnat id18}@anchor{108}@anchor{gnat_ugn/building_executable_programs_with_gnat style-checking}@anchor{100}
12832 @subsection Style Checking
12835 @geindex Style checking
12837 @geindex -gnaty (gcc)
12839 The @emph{-gnatyx} switch causes the compiler to
12840 enforce specified style rules. A limited set of style rules has been used
12841 in writing the GNAT sources themselves. This switch allows user programs
12842 to activate all or some of these checks. If the source program fails a
12843 specified style check, an appropriate message is given, preceded by
12844 the character sequence '(style)'. This message does not prevent
12845 successful compilation (unless the @emph{-gnatwe} switch is used).
12847 Note that this is by no means intended to be a general facility for
12848 checking arbitrary coding standards. It is simply an embedding of the
12849 style rules we have chosen for the GNAT sources. If you are starting
12850 a project which does not have established style standards, you may
12851 find it useful to adopt the entire set of GNAT coding standards, or
12852 some subset of them.
12855 The string @cite{x} is a sequence of letters or digits
12856 indicating the particular style
12857 checks to be performed. The following checks are defined:
12859 @geindex -gnaty[0-9] (gcc)
12862 @table @asis
12864 @item @code{-gnaty0}
12866 @emph{Specify indentation level.}
12868 If a digit from 1-9 appears
12869 in the string after @emph{-gnaty}
12870 then proper indentation is checked, with the digit indicating the
12871 indentation level required. A value of zero turns off this style check.
12872 The general style of required indentation is as specified by
12873 the examples in the Ada Reference Manual. Full line comments must be
12874 aligned with the @cite{--} starting on a column that is a multiple of
12875 the alignment level, or they may be aligned the same way as the following
12876 non-blank line (this is useful when full line comments appear in the middle
12877 of a statement, or they may be aligned with the source line on the previous
12878 non-blank line.
12879 @end table
12881 @geindex -gnatya (gcc)
12884 @table @asis
12886 @item @code{-gnatya}
12888 @emph{Check attribute casing.}
12890 Attribute names, including the case of keywords such as @cite{digits}
12891 used as attributes names, must be written in mixed case, that is, the
12892 initial letter and any letter following an underscore must be uppercase.
12893 All other letters must be lowercase.
12894 @end table
12896 @geindex -gnatyA (gcc)
12899 @table @asis
12901 @item @code{-gnatyA}
12903 @emph{Use of array index numbers in array attributes.}
12905 When using the array attributes First, Last, Range,
12906 or Length, the index number must be omitted for one-dimensional arrays
12907 and is required for multi-dimensional arrays.
12908 @end table
12910 @geindex -gnatyb (gcc)
12913 @table @asis
12915 @item @code{-gnatyb}
12917 @emph{Blanks not allowed at statement end.}
12919 Trailing blanks are not allowed at the end of statements. The purpose of this
12920 rule, together with h (no horizontal tabs), is to enforce a canonical format
12921 for the use of blanks to separate source tokens.
12922 @end table
12924 @geindex -gnatyB (gcc)
12927 @table @asis
12929 @item @code{-gnatyB}
12931 @emph{Check Boolean operators.}
12933 The use of AND/OR operators is not permitted except in the cases of modular
12934 operands, array operands, and simple stand-alone boolean variables or
12935 boolean constants. In all other cases @cite{and then}/@cite{or else} are
12936 required.
12937 @end table
12939 @geindex -gnatyc (gcc)
12942 @table @asis
12944 @item @code{-gnatyc}
12946 @emph{Check comments, double space.}
12948 Comments must meet the following set of rules:
12951 @itemize *
12953 @item 
12954 The '@cite{--}' that starts the column must either start in column one,
12955 or else at least one blank must precede this sequence.
12957 @item 
12958 Comments that follow other tokens on a line must have at least one blank
12959 following the '@cite{--}' at the start of the comment.
12961 @item 
12962 Full line comments must have at least two blanks following the
12963 '@cite{--}' that starts the comment, with the following exceptions.
12965 @item 
12966 A line consisting only of the '@cite{--}' characters, possibly preceded
12967 by blanks is permitted.
12969 @item 
12970 A comment starting with '@cite{--x}' where @cite{x} is a special character
12971 is permitted.
12972 This allows proper processing of the output generated by specialized tools
12973 including @emph{gnatprep} (where '@cite{--!}' is used) and the SPARK
12974 annotation
12975 language (where '@cite{--#}' is used). For the purposes of this rule, a
12976 special character is defined as being in one of the ASCII ranges
12977 @cite{16#21#...16#2F#} or @cite{16#3A#...16#3F#}.
12978 Note that this usage is not permitted
12979 in GNAT implementation units (i.e., when @emph{-gnatg} is used).
12981 @item 
12982 A line consisting entirely of minus signs, possibly preceded by blanks, is
12983 permitted. This allows the construction of box comments where lines of minus
12984 signs are used to form the top and bottom of the box.
12986 @item 
12987 A comment that starts and ends with '@cite{--}' is permitted as long as at
12988 least one blank follows the initial '@cite{--}'. Together with the preceding
12989 rule, this allows the construction of box comments, as shown in the following
12990 example:
12992 @example
12993 ---------------------------
12994 -- This is a box comment --
12995 -- with two text lines.  --
12996 ---------------------------
12997 @end example
12998 @end itemize
12999 @end table
13001 @geindex -gnatyC (gcc)
13004 @table @asis
13006 @item @code{-gnatyC}
13008 @emph{Check comments, single space.}
13010 This is identical to @cite{c} except that only one space
13011 is required following the @cite{--} of a comment instead of two.
13012 @end table
13014 @geindex -gnatyd (gcc)
13017 @table @asis
13019 @item @code{-gnatyd}
13021 @emph{Check no DOS line terminators present.}
13023 All lines must be terminated by a single ASCII.LF
13024 character (in particular the DOS line terminator sequence CR/LF is not
13025 allowed).
13026 @end table
13028 @geindex -gnatye (gcc)
13031 @table @asis
13033 @item @code{-gnatye}
13035 @emph{Check end/exit labels.}
13037 Optional labels on @cite{end} statements ending subprograms and on
13038 @cite{exit} statements exiting named loops, are required to be present.
13039 @end table
13041 @geindex -gnatyf (gcc)
13044 @table @asis
13046 @item @code{-gnatyf}
13048 @emph{No form feeds or vertical tabs.}
13050 Neither form feeds nor vertical tab characters are permitted
13051 in the source text.
13052 @end table
13054 @geindex -gnatyg (gcc)
13057 @table @asis
13059 @item @code{-gnatyg}
13061 @emph{GNAT style mode.}
13063 The set of style check switches is set to match that used by the GNAT sources.
13064 This may be useful when developing code that is eventually intended to be
13065 incorporated into GNAT. Currently this is equivalent to @emph{-gnatwydISux})
13066 but additional style switches may be added to this set in the future without
13067 advance notice.
13068 @end table
13070 @geindex -gnatyh (gcc)
13073 @table @asis
13075 @item @code{-gnatyh}
13077 @emph{No horizontal tabs.}
13079 Horizontal tab characters are not permitted in the source text.
13080 Together with the b (no blanks at end of line) check, this
13081 enforces a canonical form for the use of blanks to separate
13082 source tokens.
13083 @end table
13085 @geindex -gnatyi (gcc)
13088 @table @asis
13090 @item @code{-gnatyi}
13092 @emph{Check if-then layout.}
13094 The keyword @cite{then} must appear either on the same
13095 line as corresponding @cite{if}, or on a line on its own, lined
13096 up under the @cite{if}.
13097 @end table
13099 @geindex -gnatyI (gcc)
13102 @table @asis
13104 @item @code{-gnatyI}
13106 @emph{check mode IN keywords.}
13108 Mode @cite{in} (the default mode) is not
13109 allowed to be given explicitly. @cite{in out} is fine,
13110 but not @cite{in} on its own.
13111 @end table
13113 @geindex -gnatyk (gcc)
13116 @table @asis
13118 @item @code{-gnatyk}
13120 @emph{Check keyword casing.}
13122 All keywords must be in lower case (with the exception of keywords
13123 such as @cite{digits} used as attribute names to which this check
13124 does not apply).
13125 @end table
13127 @geindex -gnatyl (gcc)
13130 @table @asis
13132 @item @code{-gnatyl}
13134 @emph{Check layout.}
13136 Layout of statement and declaration constructs must follow the
13137 recommendations in the Ada Reference Manual, as indicated by the
13138 form of the syntax rules. For example an @cite{else} keyword must
13139 be lined up with the corresponding @cite{if} keyword.
13141 There are two respects in which the style rule enforced by this check
13142 option are more liberal than those in the Ada Reference Manual. First
13143 in the case of record declarations, it is permissible to put the
13144 @cite{record} keyword on the same line as the @cite{type} keyword, and
13145 then the @cite{end} in @cite{end record} must line up under @cite{type}.
13146 This is also permitted when the type declaration is split on two lines.
13147 For example, any of the following three layouts is acceptable:
13149 @example
13150 type q is record
13151    a : integer;
13152    b : integer;
13153 end record;
13155 type q is
13156    record
13157       a : integer;
13158       b : integer;
13159    end record;
13161 type q is
13162    record
13163       a : integer;
13164       b : integer;
13165 end record;
13166 @end example
13168 Second, in the case of a block statement, a permitted alternative
13169 is to put the block label on the same line as the @cite{declare} or
13170 @cite{begin} keyword, and then line the @cite{end} keyword up under
13171 the block label. For example both the following are permitted:
13173 @example
13174 Block : declare
13175    A : Integer := 3;
13176 begin
13177    Proc (A, A);
13178 end Block;
13180 Block :
13181    declare
13182       A : Integer := 3;
13183    begin
13184       Proc (A, A);
13185    end Block;
13186 @end example
13188 The same alternative format is allowed for loops. For example, both of
13189 the following are permitted:
13191 @example
13192 Clear : while J < 10 loop
13193    A (J) := 0;
13194 end loop Clear;
13196 Clear :
13197    while J < 10 loop
13198       A (J) := 0;
13199    end loop Clear;
13200 @end example
13201 @end table
13203 @geindex -gnatyLnnn (gcc)
13206 @table @asis
13208 @item @code{-gnatyL}
13210 @emph{Set maximum nesting level.}
13212 The maximum level of nesting of constructs (including subprograms, loops,
13213 blocks, packages, and conditionals) may not exceed the given value
13214 @emph{nnn}. A value of zero disconnects this style check.
13215 @end table
13217 @geindex -gnatym (gcc)
13220 @table @asis
13222 @item @code{-gnatym}
13224 @emph{Check maximum line length.}
13226 The length of source lines must not exceed 79 characters, including
13227 any trailing blanks. The value of 79 allows convenient display on an
13228 80 character wide device or window, allowing for possible special
13229 treatment of 80 character lines. Note that this count is of
13230 characters in the source text. This means that a tab character counts
13231 as one character in this count and a wide character sequence counts as
13232 a single character (however many bytes are needed in the encoding).
13233 @end table
13235 @geindex -gnatyMnnn (gcc)
13238 @table @asis
13240 @item @code{-gnatyM}
13242 @emph{Set maximum line length.}
13244 The length of lines must not exceed the
13245 given value @emph{nnn}. The maximum value that can be specified is 32767.
13246 If neither style option for setting the line length is used, then the
13247 default is 255. This also controls the maximum length of lexical elements,
13248 where the only restriction is that they must fit on a single line.
13249 @end table
13251 @geindex -gnatyn (gcc)
13254 @table @asis
13256 @item @code{-gnatyn}
13258 @emph{Check casing of entities in Standard.}
13260 Any identifier from Standard must be cased
13261 to match the presentation in the Ada Reference Manual (for example,
13262 @cite{Integer} and @cite{ASCII.NUL}).
13263 @end table
13265 @geindex -gnatyN (gcc)
13268 @table @asis
13270 @item @code{-gnatyN}
13272 @emph{Turn off all style checks.}
13274 All style check options are turned off.
13275 @end table
13277 @geindex -gnatyo (gcc)
13280 @table @asis
13282 @item @code{-gnatyo}
13284 @emph{Check order of subprogram bodies.}
13286 All subprogram bodies in a given scope
13287 (e.g., a package body) must be in alphabetical order. The ordering
13288 rule uses normal Ada rules for comparing strings, ignoring casing
13289 of letters, except that if there is a trailing numeric suffix, then
13290 the value of this suffix is used in the ordering (e.g., Junk2 comes
13291 before Junk10).
13292 @end table
13294 @geindex -gnatyO (gcc)
13297 @table @asis
13299 @item @code{-gnatyO}
13301 @emph{Check that overriding subprograms are explicitly marked as such.}
13303 The declaration of a primitive operation of a type extension that overrides
13304 an inherited operation must carry an overriding indicator.
13305 @end table
13307 @geindex -gnatyp (gcc)
13310 @table @asis
13312 @item @code{-gnatyp}
13314 @emph{Check pragma casing.}
13316 Pragma names must be written in mixed case, that is, the
13317 initial letter and any letter following an underscore must be uppercase.
13318 All other letters must be lowercase. An exception is that SPARK_Mode is
13319 allowed as an alternative for Spark_Mode.
13320 @end table
13322 @geindex -gnatyr (gcc)
13325 @table @asis
13327 @item @code{-gnatyr}
13329 @emph{Check references.}
13331 All identifier references must be cased in the same way as the
13332 corresponding declaration. No specific casing style is imposed on
13333 identifiers. The only requirement is for consistency of references
13334 with declarations.
13335 @end table
13337 @geindex -gnatys (gcc)
13340 @table @asis
13342 @item @code{-gnatys}
13344 @emph{Check separate specs.}
13346 Separate declarations ('specs') are required for subprograms (a
13347 body is not allowed to serve as its own declaration). The only
13348 exception is that parameterless library level procedures are
13349 not required to have a separate declaration. This exception covers
13350 the most frequent form of main program procedures.
13351 @end table
13353 @geindex -gnatyS (gcc)
13356 @table @asis
13358 @item @code{-gnatyS}
13360 @emph{Check no statements after then/else.}
13362 No statements are allowed
13363 on the same line as a @cite{then} or @cite{else} keyword following the
13364 keyword in an @cite{if} statement. @cite{or else} and @cite{and then} are not
13365 affected, and a special exception allows a pragma to appear after @cite{else}.
13366 @end table
13368 @geindex -gnatyt (gcc)
13371 @table @asis
13373 @item @code{-gnatyt}
13375 @emph{Check token spacing.}
13377 The following token spacing rules are enforced:
13380 @itemize *
13382 @item 
13383 The keywords @cite{abs} and @cite{not} must be followed by a space.
13385 @item 
13386 The token @cite{=>} must be surrounded by spaces.
13388 @item 
13389 The token @cite{<>} must be preceded by a space or a left parenthesis.
13391 @item 
13392 Binary operators other than @cite{**} must be surrounded by spaces.
13393 There is no restriction on the layout of the @cite{**} binary operator.
13395 @item 
13396 Colon must be surrounded by spaces.
13398 @item 
13399 Colon-equal (assignment, initialization) must be surrounded by spaces.
13401 @item 
13402 Comma must be the first non-blank character on the line, or be
13403 immediately preceded by a non-blank character, and must be followed
13404 by a space.
13406 @item 
13407 If the token preceding a left parenthesis ends with a letter or digit, then
13408 a space must separate the two tokens.
13410 @item 
13411 If the token following a right parenthesis starts with a letter or digit, then
13412 a space must separate the two tokens.
13414 @item 
13415 A right parenthesis must either be the first non-blank character on
13416 a line, or it must be preceded by a non-blank character.
13418 @item 
13419 A semicolon must not be preceded by a space, and must not be followed by
13420 a non-blank character.
13422 @item 
13423 A unary plus or minus may not be followed by a space.
13425 @item 
13426 A vertical bar must be surrounded by spaces.
13427 @end itemize
13429 Exactly one blank (and no other white space) must appear between
13430 a @cite{not} token and a following @cite{in} token.
13431 @end table
13433 @geindex -gnatyu (gcc)
13436 @table @asis
13438 @item @code{-gnatyu}
13440 @emph{Check unnecessary blank lines.}
13442 Unnecessary blank lines are not allowed. A blank line is considered
13443 unnecessary if it appears at the end of the file, or if more than
13444 one blank line occurs in sequence.
13445 @end table
13447 @geindex -gnatyx (gcc)
13450 @table @asis
13452 @item @code{-gnatyx}
13454 @emph{Check extra parentheses.}
13456 Unnecessary extra level of parentheses (C-style) are not allowed
13457 around conditions in @cite{if} statements, @cite{while} statements and
13458 @cite{exit} statements.
13459 @end table
13461 @geindex -gnatyy (gcc)
13464 @table @asis
13466 @item @code{-gnatyy}
13468 @emph{Set all standard style check options}
13470 This is equivalent to @cite{gnaty3aAbcefhiklmnprst}, that is all checking
13471 options enabled with the exception of @emph{-gnatyB}, @emph{-gnatyd},
13472 @emph{-gnatyI}, @emph{-gnatyLnnn}, @emph{-gnatyo}, @emph{-gnatyO},
13473 @emph{-gnatyS}, @emph{-gnatyu}, and @emph{-gnatyx}.
13474 @end table
13476 @geindex -gnaty- (gcc)
13479 @table @asis
13481 @item @code{-gnaty-}
13483 @emph{Remove style check options}
13485 This causes any subsequent options in the string to act as canceling the
13486 corresponding style check option. To cancel maximum nesting level control,
13487 use @emph{L} parameter witout any integer value after that, because any
13488 digit following @emph{-} in the parameter string of the @emph{-gnaty}
13489 option will be threated as canceling indentation check. The same is true
13490 for @emph{M} parameter. @emph{y} and @emph{N} parameters are not
13491 allowed after @emph{-}.
13492 @end table
13494 @geindex -gnaty+ (gcc)
13497 @table @asis
13499 @item @code{-gnaty+}
13501 @emph{Enable style check options}
13503 This causes any subsequent options in the string to enable the corresponding
13504 style check option. That is, it cancels the effect of a previous -,
13505 if any.
13506 @end table
13508 @c end of switch description (leave this comment to ease automatic parsing for
13510 @c GPS
13512 In the above rules, appearing in column one is always permitted, that is,
13513 counts as meeting either a requirement for a required preceding space,
13514 or as meeting a requirement for no preceding space.
13516 Appearing at the end of a line is also always permitted, that is, counts
13517 as meeting either a requirement for a following space, or as meeting
13518 a requirement for no following space.
13520 If any of these style rules is violated, a message is generated giving
13521 details on the violation. The initial characters of such messages are
13522 always '@cite{(style)}'. Note that these messages are treated as warning
13523 messages, so they normally do not prevent the generation of an object
13524 file. The @emph{-gnatwe} switch can be used to treat warning messages,
13525 including style messages, as fatal errors.
13527 The switch @code{-gnaty} on its own (that is not
13528 followed by any letters or digits) is equivalent
13529 to the use of @emph{-gnatyy} as described above, that is all
13530 built-in standard style check options are enabled.
13532 The switch @code{-gnatyN} clears any previously set style checks.
13534 @node Run-Time Checks,Using gcc for Syntax Checking,Style Checking,Compiler Switches
13535 @anchor{gnat_ugn/building_executable_programs_with_gnat run-time-checks}@anchor{fe}@anchor{gnat_ugn/building_executable_programs_with_gnat id19}@anchor{109}
13536 @subsection Run-Time Checks
13539 @geindex Division by zero
13541 @geindex Access before elaboration
13543 @geindex Checks
13544 @geindex division by zero
13546 @geindex Checks
13547 @geindex access before elaboration
13549 @geindex Checks
13550 @geindex stack overflow checking
13552 By default, the following checks are suppressed: integer overflow
13553 checks, stack overflow checks, and checks for access before
13554 elaboration on subprogram calls. All other checks, including range
13555 checks and array bounds checks, are turned on by default. The
13556 following @emph{gcc} switches refine this default behavior.
13558 @geindex -gnatp (gcc)
13561 @table @asis
13563 @item @code{-gnatp}
13565 @geindex Suppressing checks
13567 @geindex Checks
13568 @geindex suppressing
13570 This switch causes the unit to be compiled
13571 as though @cite{pragma Suppress (All_checks)}
13572 had been present in the source. Validity checks are also eliminated (in
13573 other words @emph{-gnatp} also implies @emph{-gnatVn}.
13574 Use this switch to improve the performance
13575 of the code at the expense of safety in the presence of invalid data or
13576 program bugs.
13578 Note that when checks are suppressed, the compiler is allowed, but not
13579 required, to omit the checking code. If the run-time cost of the
13580 checking code is zero or near-zero, the compiler will generate it even
13581 if checks are suppressed. In particular, if the compiler can prove
13582 that a certain check will necessarily fail, it will generate code to
13583 do an unconditional 'raise', even if checks are suppressed. The
13584 compiler warns in this case. Another case in which checks may not be
13585 eliminated is when they are embedded in certain run time routines such
13586 as math library routines.
13588 Of course, run-time checks are omitted whenever the compiler can prove
13589 that they will not fail, whether or not checks are suppressed.
13591 Note that if you suppress a check that would have failed, program
13592 execution is erroneous, which means the behavior is totally
13593 unpredictable. The program might crash, or print wrong answers, or
13594 do anything else. It might even do exactly what you wanted it to do
13595 (and then it might start failing mysteriously next week or next
13596 year). The compiler will generate code based on the assumption that
13597 the condition being checked is true, which can result in erroneous
13598 execution if that assumption is wrong.
13600 The checks subject to suppression include all the checks defined by
13601 the Ada standard, the additional implementation defined checks
13602 @cite{Alignment_Check},
13603 @cite{Duplicated_Tag_Check}, @cite{Predicate_Check}, and
13604 @cite{Validity_Check}, as well as any checks introduced using
13605 @cite{pragma Check_Name}. Note that @cite{Atomic_Synchronization}
13606 is not automatically suppressed by use of this option.
13608 If the code depends on certain checks being active, you can use
13609 pragma @cite{Unsuppress} either as a configuration pragma or as
13610 a local pragma to make sure that a specified check is performed
13611 even if @emph{gnatp} is specified.
13613 The @emph{-gnatp} switch has no effect if a subsequent
13614 @emph{-gnat-p} switch appears.
13615 @end table
13617 @geindex -gnat-p (gcc)
13619 @geindex Suppressing checks
13621 @geindex Checks
13622 @geindex suppressing
13624 @geindex Suppress
13627 @table @asis
13629 @item @code{-gnat-p}
13631 This switch cancels the effect of a previous @emph{gnatp} switch.
13632 @end table
13634 @geindex -gnato?? (gcc)
13636 @geindex Overflow checks
13638 @geindex Overflow mode
13640 @geindex Check
13641 @geindex overflow
13644 @table @asis
13646 @item @code{-gnato??}
13648 This switch controls the mode used for computing intermediate
13649 arithmetic integer operations, and also enables overflow checking.
13650 For a full description of overflow mode and checking control, see
13651 the 'Overflow Check Handling in GNAT' appendix in this
13652 User's Guide.
13654 Overflow checks are always enabled by this switch. The argument
13655 controls the mode, using the codes
13658 @table @asis
13660 @item @emph{1 = STRICT}
13662 In STRICT mode, intermediate operations are always done using the
13663 base type, and overflow checking ensures that the result is within
13664 the base type range.
13666 @item @emph{2 = MINIMIZED}
13668 In MINIMIZED mode, overflows in intermediate operations are avoided
13669 where possible by using a larger integer type for the computation
13670 (typically @cite{Long_Long_Integer}). Overflow checking ensures that
13671 the result fits in this larger integer type.
13673 @item @emph{3 = ELIMINATED}
13675 In ELIMINATED mode, overflows in intermediate operations are avoided
13676 by using multi-precision arithmetic. In this case, overflow checking
13677 has no effect on intermediate operations (since overflow is impossible).
13678 @end table
13680 If two digits are present after @emph{-gnato} then the first digit
13681 sets the mode for expressions outside assertions, and the second digit
13682 sets the mode for expressions within assertions. Here assertions is used
13683 in the technical sense (which includes for example precondition and
13684 postcondition expressions).
13686 If one digit is present, the corresponding mode is applicable to both
13687 expressions within and outside assertion expressions.
13689 If no digits are present, the default is to enable overflow checks
13690 and set STRICT mode for both kinds of expressions. This is compatible
13691 with the use of @emph{-gnato} in previous versions of GNAT.
13693 @geindex Machine_Overflows
13695 Note that the @emph{-gnato??} switch does not affect the code generated
13696 for any floating-point operations; it applies only to integer semantics.
13697 For floating-point, GNAT has the @cite{Machine_Overflows}
13698 attribute set to @cite{False} and the normal mode of operation is to
13699 generate IEEE NaN and infinite values on overflow or invalid operations
13700 (such as dividing 0.0 by 0.0).
13702 The reason that we distinguish overflow checking from other kinds of
13703 range constraint checking is that a failure of an overflow check, unlike
13704 for example the failure of a range check, can result in an incorrect
13705 value, but cannot cause random memory destruction (like an out of range
13706 subscript), or a wild jump (from an out of range case value). Overflow
13707 checking is also quite expensive in time and space, since in general it
13708 requires the use of double length arithmetic.
13710 Note again that the default is @emph{-gnato00},
13711 so overflow checking is not performed in default mode. This means that out of
13712 the box, with the default settings, GNAT does not do all the checks
13713 expected from the language description in the Ada Reference Manual.
13714 If you want all constraint checks to be performed, as described in this Manual,
13715 then you must explicitly use the @emph{-gnato??}
13716 switch either on the @emph{gnatmake} or @emph{gcc} command.
13717 @end table
13719 @geindex -gnatE (gcc)
13721 @geindex Elaboration checks
13723 @geindex Check
13724 @geindex elaboration
13727 @table @asis
13729 @item @code{-gnatE}
13731 Enables dynamic checks for access-before-elaboration
13732 on subprogram calls and generic instantiations.
13733 Note that @emph{-gnatE} is not necessary for safety, because in the
13734 default mode, GNAT ensures statically that the checks would not fail.
13735 For full details of the effect and use of this switch,
13736 @ref{1e,,Compiling with gcc}.
13737 @end table
13739 @geindex -fstack-check (gcc)
13741 @geindex Stack Overflow Checking
13743 @geindex Checks
13744 @geindex stack overflow checking
13747 @table @asis
13749 @item @code{-fstack-check}
13751 Activates stack overflow checking. For full details of the effect and use of
13752 this switch see @ref{f9,,Stack Overflow Checking}.
13753 @end table
13755 @geindex Unsuppress
13757 The setting of these switches only controls the default setting of the
13758 checks. You may modify them using either @cite{Suppress} (to remove
13759 checks) or @cite{Unsuppress} (to add back suppressed checks) pragmas in
13760 the program source.
13762 @node Using gcc for Syntax Checking,Using gcc for Semantic Checking,Run-Time Checks,Compiler Switches
13763 @anchor{gnat_ugn/building_executable_programs_with_gnat id20}@anchor{10a}@anchor{gnat_ugn/building_executable_programs_with_gnat using-gcc-for-syntax-checking}@anchor{10b}
13764 @subsection Using @emph{gcc} for Syntax Checking
13767 @geindex -gnats (gcc)
13770 @table @asis
13772 @item @code{-gnats}
13774 The @cite{s} stands for 'syntax'.
13776 Run GNAT in syntax checking only mode. For
13777 example, the command
13779 @example
13780 $ gcc -c -gnats x.adb
13781 @end example
13783 compiles file @code{x.adb} in syntax-check-only mode. You can check a
13784 series of files in a single command
13785 , and can use wild cards to specify such a group of files.
13786 Note that you must specify the @emph{-c} (compile
13787 only) flag in addition to the @emph{-gnats} flag.
13789 You may use other switches in conjunction with @emph{-gnats}. In
13790 particular, @emph{-gnatl} and @emph{-gnatv} are useful to control the
13791 format of any generated error messages.
13793 When the source file is empty or contains only empty lines and/or comments,
13794 the output is a warning:
13796 @example
13797 $ gcc -c -gnats -x ada toto.txt
13798 toto.txt:1:01: warning: empty file, contains no compilation units
13800 @end example
13802 Otherwise, the output is simply the error messages, if any. No object file or
13803 ALI file is generated by a syntax-only compilation. Also, no units other
13804 than the one specified are accessed. For example, if a unit @cite{X}
13805 @emph{with}s a unit @cite{Y}, compiling unit @cite{X} in syntax
13806 check only mode does not access the source file containing unit
13807 @cite{Y}.
13809 @geindex Multiple units
13810 @geindex syntax checking
13812 Normally, GNAT allows only a single unit in a source file. However, this
13813 restriction does not apply in syntax-check-only mode, and it is possible
13814 to check a file containing multiple compilation units concatenated
13815 together. This is primarily used by the @cite{gnatchop} utility
13816 (@ref{38,,Renaming Files with gnatchop}).
13817 @end table
13819 @node Using gcc for Semantic Checking,Compiling Different Versions of Ada,Using gcc for Syntax Checking,Compiler Switches
13820 @anchor{gnat_ugn/building_executable_programs_with_gnat id21}@anchor{10c}@anchor{gnat_ugn/building_executable_programs_with_gnat using-gcc-for-semantic-checking}@anchor{10d}
13821 @subsection Using @emph{gcc} for Semantic Checking
13824 @geindex -gnatc (gcc)
13827 @table @asis
13829 @item @code{-gnatc}
13831 The @cite{c} stands for 'check'.
13832 Causes the compiler to operate in semantic check mode,
13833 with full checking for all illegalities specified in the
13834 Ada Reference Manual, but without generation of any object code
13835 (no object file is generated).
13837 Because dependent files must be accessed, you must follow the GNAT
13838 semantic restrictions on file structuring to operate in this mode:
13841 @itemize *
13843 @item 
13844 The needed source files must be accessible
13845 (see @ref{8e,,Search Paths and the Run-Time Library (RTL)}).
13847 @item 
13848 Each file must contain only one compilation unit.
13850 @item 
13851 The file name and unit name must match (@ref{54,,File Naming Rules}).
13852 @end itemize
13854 The output consists of error messages as appropriate. No object file is
13855 generated. An @code{ALI} file is generated for use in the context of
13856 cross-reference tools, but this file is marked as not being suitable
13857 for binding (since no object file is generated).
13858 The checking corresponds exactly to the notion of
13859 legality in the Ada Reference Manual.
13861 Any unit can be compiled in semantics-checking-only mode, including
13862 units that would not normally be compiled (subunits,
13863 and specifications where a separate body is present).
13864 @end table
13866 @node Compiling Different Versions of Ada,Character Set Control,Using gcc for Semantic Checking,Compiler Switches
13867 @anchor{gnat_ugn/building_executable_programs_with_gnat compiling-different-versions-of-ada}@anchor{6}@anchor{gnat_ugn/building_executable_programs_with_gnat id22}@anchor{10e}
13868 @subsection Compiling Different Versions of Ada
13871 The switches described in this section allow you to explicitly specify
13872 the version of the Ada language that your programs are written in.
13873 The default mode is Ada 2012,
13874 but you can also specify Ada 95, Ada 2005 mode, or
13875 indicate Ada 83 compatibility mode.
13877 @geindex Compatibility with Ada 83
13879 @geindex -gnat83 (gcc)
13881 @geindex ACVC
13882 @geindex Ada 83 tests
13884 @geindex Ada 83 mode
13887 @table @asis
13889 @item @code{-gnat83 (Ada 83 Compatibility Mode)}
13891 Although GNAT is primarily an Ada 95 / Ada 2005 compiler, this switch
13892 specifies that the program is to be compiled in Ada 83 mode. With
13893 @emph{-gnat83}, GNAT rejects most post-Ada 83 extensions and applies Ada 83
13894 semantics where this can be done easily.
13895 It is not possible to guarantee this switch does a perfect
13896 job; some subtle tests, such as are
13897 found in earlier ACVC tests (and that have been removed from the ACATS suite
13898 for Ada 95), might not compile correctly.
13899 Nevertheless, this switch may be useful in some circumstances, for example
13900 where, due to contractual reasons, existing code needs to be maintained
13901 using only Ada 83 features.
13903 With few exceptions (most notably the need to use @cite{<>} on
13904 .. index:: Generic formal parameters
13906 unconstrained generic formal parameters, the use of the new Ada 95 / Ada 2005
13907 reserved words, and the use of packages
13908 with optional bodies), it is not necessary to specify the
13909 @emph{-gnat83} switch when compiling Ada 83 programs, because, with rare
13910 exceptions, Ada 95 and Ada 2005 are upwardly compatible with Ada 83. Thus
13911 a correct Ada 83 program is usually also a correct program
13912 in these later versions of the language standard. For further information
13913 please refer to the @cite{Compatibility_and_Porting_Guide} chapter in the
13914 @cite{GNAT Reference Manual}.
13915 @end table
13917 @geindex -gnat95 (gcc)
13919 @geindex Ada 95 mode
13922 @table @asis
13924 @item @code{-gnat95} (Ada 95 mode)
13926 This switch directs the compiler to implement the Ada 95 version of the
13927 language.
13928 Since Ada 95 is almost completely upwards
13929 compatible with Ada 83, Ada 83 programs may generally be compiled using
13930 this switch (see the description of the @emph{-gnat83} switch for further
13931 information about Ada 83 mode).
13932 If an Ada 2005 program is compiled in Ada 95 mode,
13933 uses of the new Ada 2005 features will cause error
13934 messages or warnings.
13936 This switch also can be used to cancel the effect of a previous
13937 @emph{-gnat83}, @emph{-gnat05/2005}, or @emph{-gnat12/2012}
13938 switch earlier in the command line.
13939 @end table
13941 @geindex -gnat05 (gcc)
13943 @geindex -gnat2005 (gcc)
13945 @geindex Ada 2005 mode
13948 @table @asis
13950 @item @code{-gnat05} or @code{-gnat2005} (Ada 2005 mode)
13952 This switch directs the compiler to implement the Ada 2005 version of the
13953 language, as documented in the official Ada standards document.
13954 Since Ada 2005 is almost completely upwards
13955 compatible with Ada 95 (and thus also with Ada 83), Ada 83 and Ada 95 programs
13956 may generally be compiled using this switch (see the description of the
13957 @emph{-gnat83} and @emph{-gnat95} switches for further
13958 information).
13959 @end table
13961 @geindex -gnat12 (gcc)
13963 @geindex -gnat2012 (gcc)
13965 @geindex Ada 2012 mode
13968 @table @asis
13970 @item @code{-gnat12} or @code{-gnat2012} (Ada 2012 mode)
13972 This switch directs the compiler to implement the Ada 2012 version of the
13973 language (also the default).
13974 Since Ada 2012 is almost completely upwards
13975 compatible with Ada 2005 (and thus also with Ada 83, and Ada 95),
13976 Ada 83 and Ada 95 programs
13977 may generally be compiled using this switch (see the description of the
13978 @emph{-gnat83}, @emph{-gnat95}, and @emph{-gnat05/2005} switches
13979 for further information).
13980 @end table
13982 @geindex -gnatX (gcc)
13984 @geindex Ada language extensions
13986 @geindex GNAT extensions
13989 @table @asis
13991 @item @code{-gnatX} (Enable GNAT Extensions)
13993 This switch directs the compiler to implement the latest version of the
13994 language (currently Ada 2012) and also to enable certain GNAT implementation
13995 extensions that are not part of any Ada standard. For a full list of these
13996 extensions, see the GNAT reference manual.
13997 @end table
13999 @node Character Set Control,File Naming Control,Compiling Different Versions of Ada,Compiler Switches
14000 @anchor{gnat_ugn/building_executable_programs_with_gnat id23}@anchor{10f}@anchor{gnat_ugn/building_executable_programs_with_gnat character-set-control}@anchor{4a}
14001 @subsection Character Set Control
14004 @geindex -gnati (gcc)
14007 @table @asis
14009 @item @code{-gnati@emph{c}}
14011 Normally GNAT recognizes the Latin-1 character set in source program
14012 identifiers, as described in the Ada Reference Manual.
14013 This switch causes
14014 GNAT to recognize alternate character sets in identifiers. @cite{c} is a
14015 single character  indicating the character set, as follows:
14018 @multitable {xxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} 
14019 @item
14021 @emph{1}
14023 @tab
14025 ISO 8859-1 (Latin-1) identifiers
14027 @item
14029 @emph{2}
14031 @tab
14033 ISO 8859-2 (Latin-2) letters allowed in identifiers
14035 @item
14037 @emph{3}
14039 @tab
14041 ISO 8859-3 (Latin-3) letters allowed in identifiers
14043 @item
14045 @emph{4}
14047 @tab
14049 ISO 8859-4 (Latin-4) letters allowed in identifiers
14051 @item
14053 @emph{5}
14055 @tab
14057 ISO 8859-5 (Cyrillic) letters allowed in identifiers
14059 @item
14061 @emph{9}
14063 @tab
14065 ISO 8859-15 (Latin-9) letters allowed in identifiers
14067 @item
14069 @emph{p}
14071 @tab
14073 IBM PC letters (code page 437) allowed in identifiers
14075 @item
14077 @emph{8}
14079 @tab
14081 IBM PC letters (code page 850) allowed in identifiers
14083 @item
14085 @emph{f}
14087 @tab
14089 Full upper-half codes allowed in identifiers
14091 @item
14093 @emph{n}
14095 @tab
14097 No upper-half codes allowed in identifiers
14099 @item
14101 @emph{w}
14103 @tab
14105 Wide-character codes (that is, codes greater than 255)
14106 allowed in identifiers
14108 @end multitable
14111 See @ref{40,,Foreign Language Representation} for full details on the
14112 implementation of these character sets.
14113 @end table
14115 @geindex -gnatW (gcc)
14118 @table @asis
14120 @item @code{-gnatW@emph{e}}
14122 Specify the method of encoding for wide characters.
14123 @cite{e} is one of the following:
14126 @multitable {xxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} 
14127 @item
14129 @emph{h}
14131 @tab
14133 Hex encoding (brackets coding also recognized)
14135 @item
14137 @emph{u}
14139 @tab
14141 Upper half encoding (brackets encoding also recognized)
14143 @item
14145 @emph{s}
14147 @tab
14149 Shift/JIS encoding (brackets encoding also recognized)
14151 @item
14153 @emph{e}
14155 @tab
14157 EUC encoding (brackets encoding also recognized)
14159 @item
14161 @emph{8}
14163 @tab
14165 UTF-8 encoding (brackets encoding also recognized)
14167 @item
14169 @emph{b}
14171 @tab
14173 Brackets encoding only (default value)
14175 @end multitable
14178 For full details on these encoding
14179 methods see @ref{50,,Wide_Character Encodings}.
14180 Note that brackets coding is always accepted, even if one of the other
14181 options is specified, so for example @emph{-gnatW8} specifies that both
14182 brackets and UTF-8 encodings will be recognized. The units that are
14183 with'ed directly or indirectly will be scanned using the specified
14184 representation scheme, and so if one of the non-brackets scheme is
14185 used, it must be used consistently throughout the program. However,
14186 since brackets encoding is always recognized, it may be conveniently
14187 used in standard libraries, allowing these libraries to be used with
14188 any of the available coding schemes.
14190 Note that brackets encoding only applies to program text. Within comments,
14191 brackets are considered to be normal graphic characters, and bracket sequences
14192 are never recognized as wide characters.
14194 If no @emph{-gnatW?} parameter is present, then the default
14195 representation is normally Brackets encoding only. However, if the
14196 first three characters of the file are 16#EF# 16#BB# 16#BF# (the standard
14197 byte order mark or BOM for UTF-8), then these three characters are
14198 skipped and the default representation for the file is set to UTF-8.
14200 Note that the wide character representation that is specified (explicitly
14201 or by default) for the main program also acts as the default encoding used
14202 for Wide_Text_IO files if not specifically overridden by a WCEM form
14203 parameter.
14204 @end table
14206 When no @emph{-gnatW?} is specified, then characters (other than wide
14207 characters represented using brackets notation) are treated as 8-bit
14208 Latin-1 codes. The codes recognized are the Latin-1 graphic characters,
14209 and ASCII format effectors (CR, LF, HT, VT). Other lower half control
14210 characters in the range 16#00#..16#1F# are not accepted in program text
14211 or in comments. Upper half control characters (16#80#..16#9F#) are rejected
14212 in program text, but allowed and ignored in comments. Note in particular
14213 that the Next Line (NEL) character whose encoding is 16#85# is not recognized
14214 as an end of line in this default mode. If your source program contains
14215 instances of the NEL character used as a line terminator,
14216 you must use UTF-8 encoding for the whole
14217 source program. In default mode, all lines must be ended by a standard
14218 end of line sequence (CR, CR/LF, or LF).
14220 Note that the convention of simply accepting all upper half characters in
14221 comments means that programs that use standard ASCII for program text, but
14222 UTF-8 encoding for comments are accepted in default mode, providing that the
14223 comments are ended by an appropriate (CR, or CR/LF, or LF) line terminator.
14224 This is a common mode for many programs with foreign language comments.
14226 @node File Naming Control,Subprogram Inlining Control,Character Set Control,Compiler Switches
14227 @anchor{gnat_ugn/building_executable_programs_with_gnat file-naming-control}@anchor{110}@anchor{gnat_ugn/building_executable_programs_with_gnat id24}@anchor{111}
14228 @subsection File Naming Control
14231 @geindex -gnatk (gcc)
14234 @table @asis
14236 @item @code{-gnatk@emph{n}}
14238 Activates file name 'krunching'. @cite{n}, a decimal integer in the range
14239 1-999, indicates the maximum allowable length of a file name (not
14240 including the @code{.ads} or @code{.adb} extension). The default is not
14241 to enable file name krunching.
14243 For the source file naming rules, @ref{54,,File Naming Rules}.
14244 @end table
14246 @node Subprogram Inlining Control,Auxiliary Output Control,File Naming Control,Compiler Switches
14247 @anchor{gnat_ugn/building_executable_programs_with_gnat subprogram-inlining-control}@anchor{112}@anchor{gnat_ugn/building_executable_programs_with_gnat id25}@anchor{113}
14248 @subsection Subprogram Inlining Control
14251 @geindex -gnatn (gcc)
14254 @table @asis
14256 @item @code{-gnatn[12]}
14258 The @cite{n} here is intended to suggest the first syllable of the
14259 word 'inline'.
14260 GNAT recognizes and processes @cite{Inline} pragmas. However, for the
14261 inlining to actually occur, optimization must be enabled and, in order
14262 to enable inlining of subprograms specified by pragma @cite{Inline},
14263 you must also specify this switch.
14264 In the absence of this switch, GNAT does not attempt
14265 inlining and does not need to access the bodies of
14266 subprograms for which @cite{pragma Inline} is specified if they are not
14267 in the current unit.
14269 You can optionally specify the inlining level: 1 for moderate inlining across
14270 modules, which is a good compromise between compilation times and performances
14271 at run time, or 2 for full inlining across modules, which may bring about
14272 longer compilation times. If no inlining level is specified, the compiler will
14273 pick it based on the optimization level: 1 for @emph{-O1}, @emph{-O2} or
14274 @emph{-Os} and 2 for @emph{-O3}.
14276 If you specify this switch the compiler will access these bodies,
14277 creating an extra source dependency for the resulting object file, and
14278 where possible, the call will be inlined.
14279 For further details on when inlining is possible
14280 see @ref{114,,Inlining of Subprograms}.
14281 @end table
14283 @geindex -gnatN (gcc)
14286 @table @asis
14288 @item @code{-gnatN}
14290 This switch activates front-end inlining which also
14291 generates additional dependencies.
14293 When using a gcc-based back end (in practice this means using any version
14294 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
14295 @emph{-gnatN} is deprecated, and the use of @emph{-gnatn} is preferred.
14296 Historically front end inlining was more extensive than the gcc back end
14297 inlining, but that is no longer the case.
14298 @end table
14300 @node Auxiliary Output Control,Debugging Control,Subprogram Inlining Control,Compiler Switches
14301 @anchor{gnat_ugn/building_executable_programs_with_gnat auxiliary-output-control}@anchor{115}@anchor{gnat_ugn/building_executable_programs_with_gnat id26}@anchor{116}
14302 @subsection Auxiliary Output Control
14305 @geindex -gnatt (gcc)
14307 @geindex Writing internal trees
14309 @geindex Internal trees
14310 @geindex writing to file
14313 @table @asis
14315 @item @code{-gnatt}
14317 Causes GNAT to write the internal tree for a unit to a file (with the
14318 extension @code{.adt}.
14319 This not normally required, but is used by separate analysis tools.
14320 Typically
14321 these tools do the necessary compilations automatically, so you should
14322 not have to specify this switch in normal operation.
14323 Note that the combination of switches @emph{-gnatct}
14324 generates a tree in the form required by ASIS applications.
14325 @end table
14327 @geindex -gnatu (gcc)
14330 @table @asis
14332 @item @code{-gnatu}
14334 Print a list of units required by this compilation on @code{stdout}.
14335 The listing includes all units on which the unit being compiled depends
14336 either directly or indirectly.
14337 @end table
14339 @geindex -pass-exit-codes (gcc)
14342 @table @asis
14344 @item @code{-pass-exit-codes}
14346 If this switch is not used, the exit code returned by @emph{gcc} when
14347 compiling multiple files indicates whether all source files have
14348 been successfully used to generate object files or not.
14350 When @emph{-pass-exit-codes} is used, @emph{gcc} exits with an extended
14351 exit status and allows an integrated development environment to better
14352 react to a compilation failure. Those exit status are:
14355 @multitable {xxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} 
14356 @item
14358 @emph{5}
14360 @tab
14362 There was an error in at least one source file.
14364 @item
14366 @emph{3}
14368 @tab
14370 At least one source file did not generate an object file.
14372 @item
14374 @emph{2}
14376 @tab
14378 The compiler died unexpectedly (internal error for example).
14380 @item
14382 @emph{0}
14384 @tab
14386 An object file has been generated for every source file.
14388 @end multitable
14390 @end table
14392 @node Debugging Control,Exception Handling Control,Auxiliary Output Control,Compiler Switches
14393 @anchor{gnat_ugn/building_executable_programs_with_gnat debugging-control}@anchor{117}@anchor{gnat_ugn/building_executable_programs_with_gnat id27}@anchor{118}
14394 @subsection Debugging Control
14397 @quotation
14399 @geindex Debugging options
14400 @end quotation
14402 @geindex -gnatd (gcc)
14405 @table @asis
14407 @item @code{-gnatd@emph{x}}
14409 Activate internal debugging switches. @cite{x} is a letter or digit, or
14410 string of letters or digits, which specifies the type of debugging
14411 outputs desired. Normally these are used only for internal development
14412 or system debugging purposes. You can find full documentation for these
14413 switches in the body of the @cite{Debug} unit in the compiler source
14414 file @code{debug.adb}.
14415 @end table
14417 @geindex -gnatG (gcc)
14420 @table @asis
14422 @item @code{-gnatG[=@emph{nn}]}
14424 This switch causes the compiler to generate auxiliary output containing
14425 a pseudo-source listing of the generated expanded code. Like most Ada
14426 compilers, GNAT works by first transforming the high level Ada code into
14427 lower level constructs. For example, tasking operations are transformed
14428 into calls to the tasking run-time routines. A unique capability of GNAT
14429 is to list this expanded code in a form very close to normal Ada source.
14430 This is very useful in understanding the implications of various Ada
14431 usage on the efficiency of the generated code. There are many cases in
14432 Ada (e.g., the use of controlled types), where simple Ada statements can
14433 generate a lot of run-time code. By using @emph{-gnatG} you can identify
14434 these cases, and consider whether it may be desirable to modify the coding
14435 approach to improve efficiency.
14437 The optional parameter @cite{nn} if present after -gnatG specifies an
14438 alternative maximum line length that overrides the normal default of 72.
14439 This value is in the range 40-999999, values less than 40 being silently
14440 reset to 40. The equal sign is optional.
14442 The format of the output is very similar to standard Ada source, and is
14443 easily understood by an Ada programmer. The following special syntactic
14444 additions correspond to low level features used in the generated code that
14445 do not have any exact analogies in pure Ada source form. The following
14446 is a partial list of these special constructions. See the spec
14447 of package @cite{Sprint} in file @code{sprint.ads} for a full list.
14449 @geindex -gnatL (gcc)
14451 If the switch @emph{-gnatL} is used in conjunction with
14452 @emph{-gnatG}, then the original source lines are interspersed
14453 in the expanded source (as comment lines with the original line number).
14456 @table @asis
14458 @item @code{new @emph{xxx} [storage_pool = @emph{yyy}]}
14460 Shows the storage pool being used for an allocator.
14462 @item @code{at end @emph{procedure-name};}
14464 Shows the finalization (cleanup) procedure for a scope.
14466 @item @code{(if @emph{expr} then @emph{expr} else @emph{expr})}
14468 Conditional expression equivalent to the @cite{x?y:z} construction in C.
14470 @item @code{@emph{target}^(@emph{source})}
14472 A conversion with floating-point truncation instead of rounding.
14474 @item @code{@emph{target}?(@emph{source})}
14476 A conversion that bypasses normal Ada semantic checking. In particular
14477 enumeration types and fixed-point types are treated simply as integers.
14479 @item @code{@emph{target}?^(@emph{source})}
14481 Combines the above two cases.
14482 @end table
14484 @code{@emph{x} #/ @emph{y}}
14486 @code{@emph{x} #mod @emph{y}}
14488 @code{@emph{x} # @emph{y}}
14491 @table @asis
14493 @item @code{@emph{x} #rem @emph{y}}
14495 A division or multiplication of fixed-point values which are treated as
14496 integers without any kind of scaling.
14498 @item @code{free @emph{expr} [storage_pool = @emph{xxx}]}
14500 Shows the storage pool associated with a @cite{free} statement.
14502 @item @code{[subtype or type declaration]}
14504 Used to list an equivalent declaration for an internally generated
14505 type that is referenced elsewhere in the listing.
14507 @item @code{freeze @emph{type-name} [@emph{actions}]}
14509 Shows the point at which @cite{type-name} is frozen, with possible
14510 associated actions to be performed at the freeze point.
14512 @item @code{reference @emph{itype}}
14514 Reference (and hence definition) to internal type @cite{itype}.
14516 @item @code{@emph{function-name}! (@emph{arg}, @emph{arg}, @emph{arg})}
14518 Intrinsic function call.
14520 @item @code{@emph{label-name} : label}
14522 Declaration of label @cite{labelname}.
14524 @item @code{#$ @emph{subprogram-name}}
14526 An implicit call to a run-time support routine
14527 (to meet the requirement of H.3.1(9) in a
14528 convenient manner).
14530 @item @code{@emph{expr} && @emph{expr} && @emph{expr} ... && @emph{expr}}
14532 A multiple concatenation (same effect as @cite{expr} & @cite{expr} &
14533 @cite{expr}, but handled more efficiently).
14535 @item @code{[constraint_error]}
14537 Raise the @cite{Constraint_Error} exception.
14539 @item @code{@emph{expression}'reference}
14541 A pointer to the result of evaluating @{expression@}.
14543 @item @code{@emph{target-type}!(@emph{source-expression})}
14545 An unchecked conversion of @cite{source-expression} to @cite{target-type}.
14547 @item @code{[@emph{numerator}/@emph{denominator}]}
14549 Used to represent internal real literals (that) have no exact
14550 representation in base 2-16 (for example, the result of compile time
14551 evaluation of the expression 1.0/27.0).
14552 @end table
14553 @end table
14555 @geindex -gnatD (gcc)
14558 @table @asis
14560 @item @code{-gnatD[=nn]}
14562 When used in conjunction with @emph{-gnatG}, this switch causes
14563 the expanded source, as described above for
14564 @emph{-gnatG} to be written to files with names
14565 @code{xxx.dg}, where @code{xxx} is the normal file name,
14566 instead of to the standard output file. For
14567 example, if the source file name is @code{hello.adb}, then a file
14568 @code{hello.adb.dg} will be written.  The debugging
14569 information generated by the @emph{gcc} @emph{-g} switch
14570 will refer to the generated @code{xxx.dg} file. This allows
14571 you to do source level debugging using the generated code which is
14572 sometimes useful for complex code, for example to find out exactly
14573 which part of a complex construction raised an exception. This switch
14574 also suppress generation of cross-reference information (see
14575 @emph{-gnatx}) since otherwise the cross-reference information
14576 would refer to the @code{.dg} file, which would cause
14577 confusion since this is not the original source file.
14579 Note that @emph{-gnatD} actually implies @emph{-gnatG}
14580 automatically, so it is not necessary to give both options.
14581 In other words @emph{-gnatD} is equivalent to @emph{-gnatDG}).
14583 @geindex -gnatL (gcc)
14585 If the switch @emph{-gnatL} is used in conjunction with
14586 @emph{-gnatDG}, then the original source lines are interspersed
14587 in the expanded source (as comment lines with the original line number).
14589 The optional parameter @cite{nn} if present after -gnatD specifies an
14590 alternative maximum line length that overrides the normal default of 72.
14591 This value is in the range 40-999999, values less than 40 being silently
14592 reset to 40. The equal sign is optional.
14593 @end table
14595 @geindex -gnatr (gcc)
14597 @geindex pragma Restrictions
14600 @table @asis
14602 @item @code{-gnatr}
14604 This switch causes pragma Restrictions to be treated as Restriction_Warnings
14605 so that violation of restrictions causes warnings rather than illegalities.
14606 This is useful during the development process when new restrictions are added
14607 or investigated. The switch also causes pragma Profile to be treated as
14608 Profile_Warnings, and pragma Restricted_Run_Time and pragma Ravenscar set
14609 restriction warnings rather than restrictions.
14610 @end table
14612 @geindex -gnatR (gcc)
14615 @table @asis
14617 @item @code{-gnatR[0|1|2|3[s]]}
14619 This switch controls output from the compiler of a listing showing
14620 representation information for declared types and objects. For
14621 @emph{-gnatR0}, no information is output (equivalent to omitting
14622 the @emph{-gnatR} switch). For @emph{-gnatR1} (which is the default,
14623 so @emph{-gnatR} with no parameter has the same effect), size and alignment
14624 information is listed for declared array and record types. For
14625 @emph{-gnatR2}, size and alignment information is listed for all
14626 declared types and objects. The @cite{Linker_Section} is also listed for any
14627 entity for which the @cite{Linker_Section} is set explicitly or implicitly (the
14628 latter case occurs for objects of a type for which a @cite{Linker_Section}
14629 is set).
14631 Finally @emph{-gnatR3} includes symbolic
14632 expressions for values that are computed at run time for
14633 variant records. These symbolic expressions have a mostly obvious
14634 format with #n being used to represent the value of the n'th
14635 discriminant. See source files @code{repinfo.ads/adb} in the
14636 @cite{GNAT} sources for full details on the format of @emph{-gnatR3}
14637 output. If the switch is followed by an s (e.g., @emph{-gnatR2s}), then
14638 the output is to a file with the name @code{file.rep} where
14639 file is the name of the corresponding source file.
14641 @item @code{-gnatRm[s]}
14643 This form of the switch controls output of subprogram conventions
14644 and parameter passing mechanisms for all subprograms. A following
14645 @cite{s} means output to a file as described above.
14647 Note that it is possible for record components to have zero size. In
14648 this case, the component clause uses an obvious extension of permitted
14649 Ada syntax, for example @cite{at 0 range 0 .. -1}.
14651 Representation information requires that code be generated (since it is the
14652 code generator that lays out complex data structures). If an attempt is made
14653 to output representation information when no code is generated, for example
14654 when a subunit is compiled on its own, then no information can be generated
14655 and the compiler outputs a message to this effect.
14656 @end table
14658 @geindex -gnatS (gcc)
14661 @table @asis
14663 @item @code{-gnatS}
14665 The use of the switch @emph{-gnatS} for an
14666 Ada compilation will cause the compiler to output a
14667 representation of package Standard in a form very
14668 close to standard Ada. It is not quite possible to
14669 do this entirely in standard Ada (since new
14670 numeric base types cannot be created in standard
14671 Ada), but the output is easily
14672 readable to any Ada programmer, and is useful to
14673 determine the characteristics of target dependent
14674 types in package Standard.
14675 @end table
14677 @geindex -gnatx (gcc)
14680 @table @asis
14682 @item @code{-gnatx}
14684 Normally the compiler generates full cross-referencing information in
14685 the @code{ALI} file. This information is used by a number of tools,
14686 including @cite{gnatfind} and @cite{gnatxref}. The @emph{-gnatx} switch
14687 suppresses this information. This saves some space and may slightly
14688 speed up compilation, but means that these tools cannot be used.
14689 @end table
14691 @node Exception Handling Control,Units to Sources Mapping Files,Debugging Control,Compiler Switches
14692 @anchor{gnat_ugn/building_executable_programs_with_gnat id28}@anchor{119}@anchor{gnat_ugn/building_executable_programs_with_gnat exception-handling-control}@anchor{11a}
14693 @subsection Exception Handling Control
14696 GNAT uses two methods for handling exceptions at run-time. The
14697 @cite{setjmp/longjmp} method saves the context when entering
14698 a frame with an exception handler. Then when an exception is
14699 raised, the context can be restored immediately, without the
14700 need for tracing stack frames. This method provides very fast
14701 exception propagation, but introduces significant overhead for
14702 the use of exception handlers, even if no exception is raised.
14704 The other approach is called 'zero cost' exception handling.
14705 With this method, the compiler builds static tables to describe
14706 the exception ranges. No dynamic code is required when entering
14707 a frame containing an exception handler. When an exception is
14708 raised, the tables are used to control a back trace of the
14709 subprogram invocation stack to locate the required exception
14710 handler. This method has considerably poorer performance for
14711 the propagation of exceptions, but there is no overhead for
14712 exception handlers if no exception is raised. Note that in this
14713 mode and in the context of mixed Ada and C/C++ programming,
14714 to propagate an exception through a C/C++ code, the C/C++ code
14715 must be compiled with the @emph{-funwind-tables} GCC's
14716 option.
14718 The following switches may be used to control which of the
14719 two exception handling methods is used.
14721 @geindex --RTS=sjlj (gnatmake)
14724 @table @asis
14726 @item @code{--RTS=sjlj}
14728 This switch causes the setjmp/longjmp run-time (when available) to be used
14729 for exception handling. If the default
14730 mechanism for the target is zero cost exceptions, then
14731 this switch can be used to modify this default, and must be
14732 used for all units in the partition.
14733 This option is rarely used. One case in which it may be
14734 advantageous is if you have an application where exception
14735 raising is common and the overall performance of the
14736 application is improved by favoring exception propagation.
14737 @end table
14739 @geindex --RTS=zcx (gnatmake)
14741 @geindex Zero Cost Exceptions
14744 @table @asis
14746 @item @code{--RTS=zcx}
14748 This switch causes the zero cost approach to be used
14749 for exception handling. If this is the default mechanism for the
14750 target (see below), then this switch is unneeded. If the default
14751 mechanism for the target is setjmp/longjmp exceptions, then
14752 this switch can be used to modify this default, and must be
14753 used for all units in the partition.
14754 This option can only be used if the zero cost approach
14755 is available for the target in use, otherwise it will generate an error.
14756 @end table
14758 The same option @emph{--RTS} must be used both for @emph{gcc}
14759 and @emph{gnatbind}. Passing this option to @emph{gnatmake}
14760 (@ref{df,,Switches for gnatmake}) will ensure the required consistency
14761 through the compilation and binding steps.
14763 @node Units to Sources Mapping Files,Code Generation Control,Exception Handling Control,Compiler Switches
14764 @anchor{gnat_ugn/building_executable_programs_with_gnat id29}@anchor{11b}@anchor{gnat_ugn/building_executable_programs_with_gnat units-to-sources-mapping-files}@anchor{fc}
14765 @subsection Units to Sources Mapping Files
14768 @geindex -gnatem (gcc)
14771 @table @asis
14773 @item @code{-gnatem=@emph{path}}
14775 A mapping file is a way to communicate to the compiler two mappings:
14776 from unit names to file names (without any directory information) and from
14777 file names to path names (with full directory information). These mappings
14778 are used by the compiler to short-circuit the path search.
14780 The use of mapping files is not required for correct operation of the
14781 compiler, but mapping files can improve efficiency, particularly when
14782 sources are read over a slow network connection. In normal operation,
14783 you need not be concerned with the format or use of mapping files,
14784 and the @emph{-gnatem} switch is not a switch that you would use
14785 explicitly. It is intended primarily for use by automatic tools such as
14786 @emph{gnatmake} running under the project file facility. The
14787 description here of the format of mapping files is provided
14788 for completeness and for possible use by other tools.
14790 A mapping file is a sequence of sets of three lines. In each set, the
14791 first line is the unit name, in lower case, with @cite{%s} appended
14792 for specs and @cite{%b} appended for bodies; the second line is the
14793 file name; and the third line is the path name.
14795 Example:
14797 @example
14798 main%b
14799 main.2.ada
14800 /gnat/project1/sources/main.2.ada
14801 @end example
14803 When the switch @emph{-gnatem} is specified, the compiler will
14804 create in memory the two mappings from the specified file. If there is
14805 any problem (nonexistent file, truncated file or duplicate entries),
14806 no mapping will be created.
14808 Several @emph{-gnatem} switches may be specified; however, only the
14809 last one on the command line will be taken into account.
14811 When using a project file, @emph{gnatmake} creates a temporary
14812 mapping file and communicates it to the compiler using this switch.
14813 @end table
14815 @node Code Generation Control,,Units to Sources Mapping Files,Compiler Switches
14816 @anchor{gnat_ugn/building_executable_programs_with_gnat code-generation-control}@anchor{11c}@anchor{gnat_ugn/building_executable_programs_with_gnat id30}@anchor{11d}
14817 @subsection Code Generation Control
14820 The GCC technology provides a wide range of target dependent
14821 @code{-m} switches for controlling
14822 details of code generation with respect to different versions of
14823 architectures. This includes variations in instruction sets (e.g.,
14824 different members of the power pc family), and different requirements
14825 for optimal arrangement of instructions (e.g., different members of
14826 the x86 family). The list of available @emph{-m} switches may be
14827 found in the GCC documentation.
14829 Use of these @emph{-m} switches may in some cases result in improved
14830 code performance.
14832 The GNAT technology is tested and qualified without any
14833 @code{-m} switches,
14834 so generally the most reliable approach is to avoid the use of these
14835 switches. However, we generally expect most of these switches to work
14836 successfully with GNAT, and many customers have reported successful
14837 use of these options.
14839 Our general advice is to avoid the use of @emph{-m} switches unless
14840 special needs lead to requirements in this area. In particular,
14841 there is no point in using @emph{-m} switches to improve performance
14842 unless you actually see a performance improvement.
14844 @node Binding with gnatbind,Linking with gnatlink,Compiler Switches,Building Executable Programs with GNAT
14845 @anchor{gnat_ugn/building_executable_programs_with_gnat binding-with-gnatbind}@anchor{1f}@anchor{gnat_ugn/building_executable_programs_with_gnat id31}@anchor{11e}
14846 @section Binding with @cite{gnatbind}
14849 @geindex gnatbind
14851 This chapter describes the GNAT binder, @cite{gnatbind}, which is used
14852 to bind compiled GNAT objects.
14854 Note: to invoke @cite{gnatbind} with a project file, use the @cite{gnat}
14855 driver (see @ref{11f,,The GNAT Driver and Project Files}).
14857 The @cite{gnatbind} program performs four separate functions:
14860 @itemize *
14862 @item 
14863 Checks that a program is consistent, in accordance with the rules in
14864 Chapter 10 of the Ada Reference Manual. In particular, error
14865 messages are generated if a program uses inconsistent versions of a
14866 given unit.
14868 @item 
14869 Checks that an acceptable order of elaboration exists for the program
14870 and issues an error message if it cannot find an order of elaboration
14871 that satisfies the rules in Chapter 10 of the Ada Language Manual.
14873 @item 
14874 Generates a main program incorporating the given elaboration order.
14875 This program is a small Ada package (body and spec) that
14876 must be subsequently compiled
14877 using the GNAT compiler. The necessary compilation step is usually
14878 performed automatically by @emph{gnatlink}. The two most important
14879 functions of this program
14880 are to call the elaboration routines of units in an appropriate order
14881 and to call the main program.
14883 @item 
14884 Determines the set of object files required by the given main program.
14885 This information is output in the forms of comments in the generated program,
14886 to be read by the @emph{gnatlink} utility used to link the Ada application.
14887 @end itemize
14889 @menu
14890 * Running gnatbind:: 
14891 * Switches for gnatbind:: 
14892 * Command-Line Access:: 
14893 * Search Paths for gnatbind:: 
14894 * Examples of gnatbind Usage:: 
14896 @end menu
14898 @node Running gnatbind,Switches for gnatbind,,Binding with gnatbind
14899 @anchor{gnat_ugn/building_executable_programs_with_gnat running-gnatbind}@anchor{120}@anchor{gnat_ugn/building_executable_programs_with_gnat id32}@anchor{121}
14900 @subsection Running @cite{gnatbind}
14903 The form of the @cite{gnatbind} command is
14905 @example
14906 $ gnatbind [`switches`] `mainprog`[.ali] [`switches`]
14907 @end example
14909 where @code{mainprog.adb} is the Ada file containing the main program
14910 unit body. @cite{gnatbind} constructs an Ada
14911 package in two files whose names are
14912 @code{b~mainprog.ads}, and @code{b~mainprog.adb}.
14913 For example, if given the
14914 parameter @code{hello.ali}, for a main program contained in file
14915 @code{hello.adb}, the binder output files would be @code{b~hello.ads}
14916 and @code{b~hello.adb}.
14918 When doing consistency checking, the binder takes into consideration
14919 any source files it can locate. For example, if the binder determines
14920 that the given main program requires the package @cite{Pack}, whose
14921 @code{.ALI}
14922 file is @code{pack.ali} and whose corresponding source spec file is
14923 @code{pack.ads}, it attempts to locate the source file @code{pack.ads}
14924 (using the same search path conventions as previously described for the
14925 @emph{gcc} command). If it can locate this source file, it checks that
14926 the time stamps
14927 or source checksums of the source and its references to in @code{ALI} files
14928 match. In other words, any @code{ALI} files that mentions this spec must have
14929 resulted from compiling this version of the source file (or in the case
14930 where the source checksums match, a version close enough that the
14931 difference does not matter).
14933 @geindex Source files
14934 @geindex use by binder
14936 The effect of this consistency checking, which includes source files, is
14937 that the binder ensures that the program is consistent with the latest
14938 version of the source files that can be located at bind time. Editing a
14939 source file without compiling files that depend on the source file cause
14940 error messages to be generated by the binder.
14942 For example, suppose you have a main program @code{hello.adb} and a
14943 package @cite{P}, from file @code{p.ads} and you perform the following
14944 steps:
14947 @itemize *
14949 @item 
14950 Enter @cite{gcc -c hello.adb} to compile the main program.
14952 @item 
14953 Enter @cite{gcc -c p.ads} to compile package @cite{P}.
14955 @item 
14956 Edit file @code{p.ads}.
14958 @item 
14959 Enter @cite{gnatbind hello}.
14960 @end itemize
14962 At this point, the file @code{p.ali} contains an out-of-date time stamp
14963 because the file @code{p.ads} has been edited. The attempt at binding
14964 fails, and the binder generates the following error messages:
14966 @example
14967 error: "hello.adb" must be recompiled ("p.ads" has been modified)
14968 error: "p.ads" has been modified and must be recompiled
14969 @end example
14971 Now both files must be recompiled as indicated, and then the bind can
14972 succeed, generating a main program. You need not normally be concerned
14973 with the contents of this file, but for reference purposes a sample
14974 binder output file is given in @ref{10,,Example of Binder Output File}.
14976 In most normal usage, the default mode of @emph{gnatbind} which is to
14977 generate the main package in Ada, as described in the previous section.
14978 In particular, this means that any Ada programmer can read and understand
14979 the generated main program. It can also be debugged just like any other
14980 Ada code provided the @emph{-g} switch is used for
14981 @emph{gnatbind} and @emph{gnatlink}.
14983 @node Switches for gnatbind,Command-Line Access,Running gnatbind,Binding with gnatbind
14984 @anchor{gnat_ugn/building_executable_programs_with_gnat id33}@anchor{122}@anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gnatbind}@anchor{123}
14985 @subsection Switches for @emph{gnatbind}
14988 The following switches are available with @cite{gnatbind}; details will
14989 be presented in subsequent sections.
14991 @geindex --version (gnatbind)
14994 @table @asis
14996 @item @code{--version}
14998 Display Copyright and version, then exit disregarding all other options.
14999 @end table
15001 @geindex --help (gnatbind)
15004 @table @asis
15006 @item @code{--help}
15008 If @emph{--version} was not used, display usage, then exit disregarding
15009 all other options.
15010 @end table
15012 @geindex -a (gnatbind)
15015 @table @asis
15017 @item @code{-a}
15019 Indicates that, if supported by the platform, the adainit procedure should
15020 be treated as an initialisation routine by the linker (a constructor). This
15021 is intended to be used by the Project Manager to automatically initialize
15022 shared Stand-Alone Libraries.
15023 @end table
15025 @geindex -aO (gnatbind)
15028 @table @asis
15030 @item @code{-aO}
15032 Specify directory to be searched for ALI files.
15033 @end table
15035 @geindex -aI (gnatbind)
15038 @table @asis
15040 @item @code{-aI}
15042 Specify directory to be searched for source file.
15043 @end table
15045 @geindex -A (gnatbind)
15048 @table @asis
15050 @item @code{-A[=@emph{filename}]}
15052 Output ALI list (to standard output or to the named file).
15053 @end table
15055 @geindex -b (gnatbind)
15058 @table @asis
15060 @item @code{-b}
15062 Generate brief messages to @code{stderr} even if verbose mode set.
15063 @end table
15065 @geindex -c (gnatbind)
15068 @table @asis
15070 @item @code{-c}
15072 Check only, no generation of binder output file.
15073 @end table
15075 @geindex -dnn[k|m] (gnatbind)
15078 @table @asis
15080 @item @code{-d@emph{nn}[k|m]}
15082 This switch can be used to change the default task stack size value
15083 to a specified size @cite{nn}, which is expressed in bytes by default, or
15084 in kilobytes when suffixed with @cite{k} or in megabytes when suffixed
15085 with @cite{m}.
15086 In the absence of a @code{[k|m]} suffix, this switch is equivalent,
15087 in effect, to completing all task specs with
15089 @example
15090 pragma Storage_Size (nn);
15091 @end example
15093 When they do not already have such a pragma.
15094 @end table
15096 @geindex -D (gnatbind)
15099 @table @asis
15101 @item @code{-D@emph{nn}[k|m]}
15103 This switch can be used to change the default secondary stack size value
15104 to a specified size @cite{nn}, which is expressed in bytes by default, or
15105 in kilobytes when suffixed with @cite{k} or in megabytes when suffixed
15106 with @cite{m}.
15108 The secondary stack is used to deal with functions that return a variable
15109 sized result, for example a function returning an unconstrained
15110 String. There are two ways in which this secondary stack is allocated.
15112 For most targets, the secondary stack is growing on demand and is allocated
15113 as a chain of blocks in the heap. The -D option is not very
15114 relevant. It only give some control over the size of the allocated
15115 blocks (whose size is the minimum of the default secondary stack size value,
15116 and the actual size needed for the current allocation request).
15118 For certain targets, notably VxWorks 653,
15119 the secondary stack is allocated by carving off a fixed ratio chunk of the
15120 primary task stack. The -D option is used to define the
15121 size of the environment task's secondary stack.
15122 @end table
15124 @geindex -e (gnatbind)
15127 @table @asis
15129 @item @code{-e}
15131 Output complete list of elaboration-order dependencies.
15132 @end table
15134 @geindex -E (gnatbind)
15137 @table @asis
15139 @item @code{-E}
15141 Store tracebacks in exception occurrences when the target supports it.
15143 See also the packages @cite{GNAT.Traceback} and
15144 @cite{GNAT.Traceback.Symbolic} for more information.
15145 Note that on x86 ports, you must not use @emph{-fomit-frame-pointer}
15146 @emph{gcc} option.
15147 @end table
15149 @geindex -F (gnatbind)
15152 @table @asis
15154 @item @code{-F}
15156 Force the checks of elaboration flags. @emph{gnatbind} does not normally
15157 generate checks of elaboration flags for the main executable, except when
15158 a Stand-Alone Library is used. However, there are cases when this cannot be
15159 detected by gnatbind. An example is importing an interface of a Stand-Alone
15160 Library through a pragma Import and only specifying through a linker switch
15161 this Stand-Alone Library. This switch is used to guarantee that elaboration
15162 flag checks are generated.
15163 @end table
15165 @geindex -h (gnatbind)
15168 @table @asis
15170 @item @code{-h}
15172 Output usage (help) information
15174 @geindex -H32 (gnatbind)
15176 @item @code{-H32}
15178 Use 32-bit allocations for @cite{__gnat_malloc} (and thus for access types).
15179 For further details see @ref{124,,Dynamic Allocation Control}.
15181 @geindex -H64 (gnatbind)
15183 @geindex __gnat_malloc
15185 @item @code{-H64}
15187 Use 64-bit allocations for @cite{__gnat_malloc} (and thus for access types).
15188 For further details see @ref{124,,Dynamic Allocation Control}.
15190 @geindex -I (gnatbind)
15192 @item @code{-I}
15194 Specify directory to be searched for source and ALI files.
15196 @geindex -I- (gnatbind)
15198 @item @code{-I-}
15200 Do not look for sources in the current directory where @cite{gnatbind} was
15201 invoked, and do not look for ALI files in the directory containing the
15202 ALI file named in the @cite{gnatbind} command line.
15204 @geindex -l (gnatbind)
15206 @item @code{-l}
15208 Output chosen elaboration order.
15210 @geindex -L (gnatbind)
15212 @item @code{-L@emph{xxx}}
15214 Bind the units for library building. In this case the adainit and
15215 adafinal procedures (@ref{ba,,Binding with Non-Ada Main Programs})
15216 are renamed to @cite{xxx`init and `xxx`final. Implies -n. (:ref:`GNAT_and_Libraries}, for more details.)
15218 @geindex -M (gnatbind)
15220 @item @code{-M@emph{xyz}}
15222 Rename generated main program from main to xyz. This option is
15223 supported on cross environments only.
15225 @geindex -m (gnatbind)
15227 @item @code{-m@emph{n}}
15229 Limit number of detected errors or warnings to @cite{n}, where @cite{n} is
15230 in the range 1..999999. The default value if no switch is
15231 given is 9999. If the number of warnings reaches this limit, then a
15232 message is output and further warnings are suppressed, the bind
15233 continues in this case. If the number of errors reaches this
15234 limit, then a message is output and the bind is abandoned.
15235 A value of zero means that no limit is enforced. The equal
15236 sign is optional.
15238 @geindex -n (gnatbind)
15240 @item @code{-n}
15242 No main program.
15244 @geindex -nostdinc (gnatbind)
15246 @item @code{-nostdinc}
15248 Do not look for sources in the system default directory.
15250 @geindex -nostdlib (gnatbind)
15252 @item @code{-nostdlib}
15254 Do not look for library files in the system default directory.
15256 @geindex --RTS (gnatbind)
15258 @item @code{--RTS=@emph{rts-path}}
15260 Specifies the default location of the runtime library. Same meaning as the
15261 equivalent @emph{gnatmake} flag (@ref{df,,Switches for gnatmake}).
15263 @geindex -o (gnatbind)
15265 @item @code{-o @emph{file}}
15267 Name the output file @cite{file} (default is @code{b~`xxx}.adb`).
15268 Note that if this option is used, then linking must be done manually,
15269 gnatlink cannot be used.
15271 @geindex -O (gnatbind)
15273 @item @code{-O[=@emph{filename}]}
15275 Output object list (to standard output or to the named file).
15277 @geindex -p (gnatbind)
15279 @item @code{-p}
15281 Pessimistic (worst-case) elaboration order
15283 @geindex -P (gnatbind)
15285 @item @code{-P}
15287 Generate binder file suitable for CodePeer.
15289 @geindex -R (gnatbind)
15291 @item @code{-R}
15293 Output closure source list, which includes all non-run-time units that are
15294 included in the bind.
15296 @geindex -Ra (gnatbind)
15298 @item @code{-Ra}
15300 Like @emph{-R} but the list includes run-time units.
15302 @geindex -s (gnatbind)
15304 @item @code{-s}
15306 Require all source files to be present.
15308 @geindex -S (gnatbind)
15310 @item @code{-S@emph{xxx}}
15312 Specifies the value to be used when detecting uninitialized scalar
15313 objects with pragma Initialize_Scalars.
15314 The @cite{xxx} string specified with the switch is one of:
15317 @itemize *
15319 @item 
15320 @code{in} for an invalid value*.
15322 If zero is invalid for the discrete type in question,
15323 then the scalar value is set to all zero bits.
15324 For signed discrete types, the largest possible negative value of
15325 the underlying scalar is set (i.e. a one bit followed by all zero bits).
15326 For unsigned discrete types, the underlying scalar value is set to all
15327 one bits. For floating-point types, a NaN value is set
15328 (see body of package System.Scalar_Values for exact values).
15330 @item 
15331 @code{lo} for low value.
15333 If zero is invalid for the discrete type in question,
15334 then the scalar value is set to all zero bits.
15335 For signed discrete types, the largest possible negative value of
15336 the underlying scalar is set (i.e. a one bit followed by all zero bits).
15337 For unsigned discrete types, the underlying scalar value is set to all
15338 zero bits. For floating-point, a small value is set
15339 (see body of package System.Scalar_Values for exact values).
15341 @item 
15342 @code{hi} for high value.
15344 If zero is invalid for the discrete type in question,
15345 then the scalar value is set to all one bits.
15346 For signed discrete types, the largest possible positive value of
15347 the underlying scalar is set (i.e. a zero bit followed by all one bits).
15348 For unsigned discrete types, the underlying scalar value is set to all
15349 one bits. For floating-point, a large value is set
15350 (see body of package System.Scalar_Values for exact values).
15352 @item 
15353 @cite{xx} for hex value (two hex digits).
15355 The underlying scalar is set to a value consisting of repeated bytes, whose
15356 value corresponds to the given value. For example if @code{BF} is given,
15357 then a 32-bit scalar value will be set to the bit patterm @code{16#BFBFBFBF#}.
15358 @end itemize
15360 @geindex GNAT_INIT_SCALARS
15362 In addition, you can specify @emph{-Sev} to indicate that the value is
15363 to be set at run time. In this case, the program will look for an environment
15364 variable of the form @code{GNAT_INIT_SCALARS=@emph{yy}}, where @cite{yy} is one
15365 of @emph{in/lo/hi/`xx*` with the same meanings as above.
15366 If no environment variable is found, or if it does not have a valid value,
15367 then the default is *in} (invalid values).
15368 @end table
15370 @geindex -static (gnatbind)
15373 @table @asis
15375 @item @code{-static}
15377 Link against a static GNAT run time.
15379 @geindex -shared (gnatbind)
15381 @item @code{-shared}
15383 Link against a shared GNAT run time when available.
15385 @geindex -t (gnatbind)
15387 @item @code{-t}
15389 Tolerate time stamp and other consistency errors
15391 @geindex -T (gnatbind)
15393 @item @code{-T@emph{n}}
15395 Set the time slice value to @cite{n} milliseconds. If the system supports
15396 the specification of a specific time slice value, then the indicated value
15397 is used. If the system does not support specific time slice values, but
15398 does support some general notion of round-robin scheduling, then any
15399 nonzero value will activate round-robin scheduling.
15401 A value of zero is treated specially. It turns off time
15402 slicing, and in addition, indicates to the tasking run time that the
15403 semantics should match as closely as possible the Annex D
15404 requirements of the Ada RM, and in particular sets the default
15405 scheduling policy to @cite{FIFO_Within_Priorities}.
15407 @geindex -u (gnatbind)
15409 @item @code{-u@emph{n}}
15411 Enable dynamic stack usage, with @cite{n} results stored and displayed
15412 at program termination. A result is generated when a task
15413 terminates. Results that can't be stored are displayed on the fly, at
15414 task termination. This option is currently not supported on Itanium
15415 platforms. (See @ref{125,,Dynamic Stack Usage Analysis} for details.)
15417 @geindex -v (gnatbind)
15419 @item @code{-v}
15421 Verbose mode. Write error messages, header, summary output to
15422 @code{stdout}.
15424 @geindex -w (gnatbind)
15426 @item @code{-w@emph{x}}
15428 Warning mode; @cite{x} = s/e for suppress/treat as error
15430 @geindex -Wx (gnatbind)
15432 @item @code{-Wx@emph{e}}
15434 Override default wide character encoding for standard Text_IO files.
15436 @geindex -x (gnatbind)
15438 @item @code{-x}
15440 Exclude source files (check object consistency only).
15442 @geindex -Xnnn (gnatbind)
15444 @item @code{-X@emph{nnn}}
15446 Set default exit status value, normally 0 for POSIX compliance.
15448 @geindex -y (gnatbind)
15450 @item @code{-y}
15452 Enable leap seconds support in @cite{Ada.Calendar} and its children.
15454 @geindex -z (gnatbind)
15456 @item @code{-z}
15458 No main subprogram.
15459 @end table
15461 You may obtain this listing of switches by running @cite{gnatbind} with
15462 no arguments.
15464 @menu
15465 * Consistency-Checking Modes:: 
15466 * Binder Error Message Control:: 
15467 * Elaboration Control:: 
15468 * Output Control:: 
15469 * Dynamic Allocation Control:: 
15470 * Binding with Non-Ada Main Programs:: 
15471 * Binding Programs with No Main Subprogram:: 
15473 @end menu
15475 @node Consistency-Checking Modes,Binder Error Message Control,,Switches for gnatbind
15476 @anchor{gnat_ugn/building_executable_programs_with_gnat consistency-checking-modes}@anchor{126}@anchor{gnat_ugn/building_executable_programs_with_gnat id34}@anchor{127}
15477 @subsubsection Consistency-Checking Modes
15480 As described earlier, by default @cite{gnatbind} checks
15481 that object files are consistent with one another and are consistent
15482 with any source files it can locate. The following switches control binder
15483 access to sources.
15485 @quotation
15487 @geindex -s (gnatbind)
15488 @end quotation
15491 @table @asis
15493 @item @code{-s}
15495 Require source files to be present. In this mode, the binder must be
15496 able to locate all source files that are referenced, in order to check
15497 their consistency. In normal mode, if a source file cannot be located it
15498 is simply ignored. If you specify this switch, a missing source
15499 file is an error.
15501 @geindex -Wx (gnatbind)
15503 @item @code{-Wx@emph{e}}
15505 Override default wide character encoding for standard Text_IO files.
15506 Normally the default wide character encoding method used for standard
15507 [Wide_[Wide_]]Text_IO files is taken from the encoding specified for
15508 the main source input (see description of switch
15509 @emph{-gnatWx} for the compiler). The
15510 use of this switch for the binder (which has the same set of
15511 possible arguments) overrides this default as specified.
15513 @geindex -x (gnatbind)
15515 @item @code{-x}
15517 Exclude source files. In this mode, the binder only checks that ALI
15518 files are consistent with one another. Source files are not accessed.
15519 The binder runs faster in this mode, and there is still a guarantee that
15520 the resulting program is self-consistent.
15521 If a source file has been edited since it was last compiled, and you
15522 specify this switch, the binder will not detect that the object
15523 file is out of date with respect to the source file. Note that this is the
15524 mode that is automatically used by @emph{gnatmake} because in this
15525 case the checking against sources has already been performed by
15526 @emph{gnatmake} in the course of compilation (i.e., before binding).
15527 @end table
15529 @node Binder Error Message Control,Elaboration Control,Consistency-Checking Modes,Switches for gnatbind
15530 @anchor{gnat_ugn/building_executable_programs_with_gnat id35}@anchor{128}@anchor{gnat_ugn/building_executable_programs_with_gnat binder-error-message-control}@anchor{129}
15531 @subsubsection Binder Error Message Control
15534 The following switches provide control over the generation of error
15535 messages from the binder:
15537 @quotation
15539 @geindex -v (gnatbind)
15540 @end quotation
15543 @table @asis
15545 @item @code{-v}
15547 Verbose mode. In the normal mode, brief error messages are generated to
15548 @code{stderr}. If this switch is present, a header is written
15549 to @code{stdout} and any error messages are directed to @code{stdout}.
15550 All that is written to @code{stderr} is a brief summary message.
15552 @geindex -b (gnatbind)
15554 @item @code{-b}
15556 Generate brief error messages to @code{stderr} even if verbose mode is
15557 specified. This is relevant only when used with the
15558 @emph{-v} switch.
15560 @geindex -m (gnatbind)
15562 @item @code{-m@emph{n}}
15564 Limits the number of error messages to @cite{n}, a decimal integer in the
15565 range 1-999. The binder terminates immediately if this limit is reached.
15567 @geindex -M (gnatbind)
15569 @item @code{-M@emph{xxx}}
15571 Renames the generated main program from @cite{main} to @cite{xxx}.
15572 This is useful in the case of some cross-building environments, where
15573 the actual main program is separate from the one generated
15574 by @cite{gnatbind}.
15576 @geindex -ws (gnatbind)
15578 @geindex Warnings
15580 @item @code{-ws}
15582 Suppress all warning messages.
15584 @geindex -we (gnatbind)
15586 @item @code{-we}
15588 Treat any warning messages as fatal errors.
15590 @geindex -t (gnatbind)
15592 @geindex Time stamp checks
15593 @geindex in binder
15595 @geindex Binder consistency checks
15597 @geindex Consistency checks
15598 @geindex in binder
15600 @item @code{-t}
15602 The binder performs a number of consistency checks including:
15605 @itemize *
15607 @item 
15608 Check that time stamps of a given source unit are consistent
15610 @item 
15611 Check that checksums of a given source unit are consistent
15613 @item 
15614 Check that consistent versions of @cite{GNAT} were used for compilation
15616 @item 
15617 Check consistency of configuration pragmas as required
15618 @end itemize
15620 Normally failure of such checks, in accordance with the consistency
15621 requirements of the Ada Reference Manual, causes error messages to be
15622 generated which abort the binder and prevent the output of a binder
15623 file and subsequent link to obtain an executable.
15625 The @emph{-t} switch converts these error messages
15626 into warnings, so that
15627 binding and linking can continue to completion even in the presence of such
15628 errors. The result may be a failed link (due to missing symbols), or a
15629 non-functional executable which has undefined semantics.
15631 @cartouche
15632 @quotation Note 
15633 This means that @emph{-t} should be used only in unusual situations,
15634 with extreme care.
15635 @end quotation
15636 @end cartouche
15637 @end table
15639 @node Elaboration Control,Output Control,Binder Error Message Control,Switches for gnatbind
15640 @anchor{gnat_ugn/building_executable_programs_with_gnat id36}@anchor{12a}@anchor{gnat_ugn/building_executable_programs_with_gnat elaboration-control}@anchor{12b}
15641 @subsubsection Elaboration Control
15644 The following switches provide additional control over the elaboration
15645 order. For full details see @ref{11,,Elaboration Order Handling in GNAT}.
15647 @quotation
15649 @geindex -p (gnatbind)
15650 @end quotation
15653 @table @asis
15655 @item @code{-p}
15657 Normally the binder attempts to choose an elaboration order that is
15658 likely to minimize the likelihood of an elaboration order error resulting
15659 in raising a @cite{Program_Error} exception. This switch reverses the
15660 action of the binder, and requests that it deliberately choose an order
15661 that is likely to maximize the likelihood of an elaboration error.
15662 This is useful in ensuring portability and avoiding dependence on
15663 accidental fortuitous elaboration ordering.
15665 Normally it only makes sense to use the @emph{-p}
15666 switch if dynamic
15667 elaboration checking is used (@emph{-gnatE} switch used for compilation).
15668 This is because in the default static elaboration mode, all necessary
15669 @cite{Elaborate} and @cite{Elaborate_All} pragmas are implicitly inserted.
15670 These implicit pragmas are still respected by the binder in
15671 @emph{-p} mode, so a
15672 safe elaboration order is assured.
15674 Note that @emph{-p} is not intended for
15675 production use; it is more for debugging/experimental use.
15676 @end table
15678 @node Output Control,Dynamic Allocation Control,Elaboration Control,Switches for gnatbind
15679 @anchor{gnat_ugn/building_executable_programs_with_gnat output-control}@anchor{12c}@anchor{gnat_ugn/building_executable_programs_with_gnat id37}@anchor{12d}
15680 @subsubsection Output Control
15683 The following switches allow additional control over the output
15684 generated by the binder.
15686 @quotation
15688 @geindex -c (gnatbind)
15689 @end quotation
15692 @table @asis
15694 @item @code{-c}
15696 Check only. Do not generate the binder output file. In this mode the
15697 binder performs all error checks but does not generate an output file.
15699 @geindex -e (gnatbind)
15701 @item @code{-e}
15703 Output complete list of elaboration-order dependencies, showing the
15704 reason for each dependency. This output can be rather extensive but may
15705 be useful in diagnosing problems with elaboration order. The output is
15706 written to @code{stdout}.
15708 @geindex -h (gnatbind)
15710 @item @code{-h}
15712 Output usage information. The output is written to @code{stdout}.
15714 @geindex -K (gnatbind)
15716 @item @code{-K}
15718 Output linker options to @code{stdout}. Includes library search paths,
15719 contents of pragmas Ident and Linker_Options, and libraries added
15720 by @cite{gnatbind}.
15722 @geindex -l (gnatbind)
15724 @item @code{-l}
15726 Output chosen elaboration order. The output is written to @code{stdout}.
15728 @geindex -O (gnatbind)
15730 @item @code{-O}
15732 Output full names of all the object files that must be linked to provide
15733 the Ada component of the program. The output is written to @code{stdout}.
15734 This list includes the files explicitly supplied and referenced by the user
15735 as well as implicitly referenced run-time unit files. The latter are
15736 omitted if the corresponding units reside in shared libraries. The
15737 directory names for the run-time units depend on the system configuration.
15739 @geindex -o (gnatbind)
15741 @item @code{-o @emph{file}}
15743 Set name of output file to @cite{file} instead of the normal
15744 @code{b~`mainprog}.adb` default. Note that @cite{file} denote the Ada
15745 binder generated body filename.
15746 Note that if this option is used, then linking must be done manually.
15747 It is not possible to use gnatlink in this case, since it cannot locate
15748 the binder file.
15750 @geindex -r (gnatbind)
15752 @item @code{-r}
15754 Generate list of @cite{pragma Restrictions} that could be applied to
15755 the current unit. This is useful for code audit purposes, and also may
15756 be used to improve code generation in some cases.
15757 @end table
15759 @node Dynamic Allocation Control,Binding with Non-Ada Main Programs,Output Control,Switches for gnatbind
15760 @anchor{gnat_ugn/building_executable_programs_with_gnat dynamic-allocation-control}@anchor{124}@anchor{gnat_ugn/building_executable_programs_with_gnat id38}@anchor{12e}
15761 @subsubsection Dynamic Allocation Control
15764 The heap control switches -- @emph{-H32} and @emph{-H64} --
15765 determine whether dynamic allocation uses 32-bit or 64-bit memory.
15766 They only affect compiler-generated allocations via @cite{__gnat_malloc};
15767 explicit calls to @cite{malloc} and related functions from the C
15768 run-time library are unaffected.
15771 @table @asis
15773 @item @code{-H32}
15775 Allocate memory on 32-bit heap
15777 @item @code{-H64}
15779 Allocate memory on 64-bit heap.  This is the default
15780 unless explicitly overridden by a @cite{'Size} clause on the access type.
15781 @end table
15783 These switches are only effective on VMS platforms.
15785 @node Binding with Non-Ada Main Programs,Binding Programs with No Main Subprogram,Dynamic Allocation Control,Switches for gnatbind
15786 @anchor{gnat_ugn/building_executable_programs_with_gnat binding-with-non-ada-main-programs}@anchor{ba}@anchor{gnat_ugn/building_executable_programs_with_gnat id39}@anchor{12f}
15787 @subsubsection Binding with Non-Ada Main Programs
15790 The description so far has assumed that the main
15791 program is in Ada, and that the task of the binder is to generate a
15792 corresponding function @cite{main} that invokes this Ada main
15793 program. GNAT also supports the building of executable programs where
15794 the main program is not in Ada, but some of the called routines are
15795 written in Ada and compiled using GNAT (@ref{46,,Mixed Language Programming}).
15796 The following switch is used in this situation:
15798 @quotation
15800 @geindex -n (gnatbind)
15801 @end quotation
15804 @table @asis
15806 @item @code{-n}
15808 No main program. The main program is not in Ada.
15809 @end table
15811 In this case, most of the functions of the binder are still required,
15812 but instead of generating a main program, the binder generates a file
15813 containing the following callable routines:
15815 @quotation
15817 @geindex adainit
15820 @table @asis
15822 @item @emph{adainit}
15824 You must call this routine to initialize the Ada part of the program by
15825 calling the necessary elaboration routines. A call to @cite{adainit} is
15826 required before the first call to an Ada subprogram.
15828 Note that it is assumed that the basic execution environment must be setup
15829 to be appropriate for Ada execution at the point where the first Ada
15830 subprogram is called. In particular, if the Ada code will do any
15831 floating-point operations, then the FPU must be setup in an appropriate
15832 manner. For the case of the x86, for example, full precision mode is
15833 required. The procedure GNAT.Float_Control.Reset may be used to ensure
15834 that the FPU is in the right state.
15835 @end table
15837 @geindex adafinal
15840 @table @asis
15842 @item @emph{adafinal}
15844 You must call this routine to perform any library-level finalization
15845 required by the Ada subprograms. A call to @cite{adafinal} is required
15846 after the last call to an Ada subprogram, and before the program
15847 terminates.
15848 @end table
15849 @end quotation
15851 @geindex -n (gnatbind)
15853 @geindex Binder
15854 @geindex multiple input files
15856 If the @emph{-n} switch
15857 is given, more than one ALI file may appear on
15858 the command line for @cite{gnatbind}. The normal @emph{closure}
15859 calculation is performed for each of the specified units. Calculating
15860 the closure means finding out the set of units involved by tracing
15861 @emph{with} references. The reason it is necessary to be able to
15862 specify more than one ALI file is that a given program may invoke two or
15863 more quite separate groups of Ada units.
15865 The binder takes the name of its output file from the last specified ALI
15866 file, unless overridden by the use of the @emph{-o file}.
15868 @geindex -o (gnatbind)
15870 The output is an Ada unit in source form that can be compiled with GNAT.
15871 This compilation occurs automatically as part of the @emph{gnatlink}
15872 processing.
15874 Currently the GNAT run time requires a FPU using 80 bits mode
15875 precision. Under targets where this is not the default it is required to
15876 call GNAT.Float_Control.Reset before using floating point numbers (this
15877 include float computation, float input and output) in the Ada code. A
15878 side effect is that this could be the wrong mode for the foreign code
15879 where floating point computation could be broken after this call.
15881 @node Binding Programs with No Main Subprogram,,Binding with Non-Ada Main Programs,Switches for gnatbind
15882 @anchor{gnat_ugn/building_executable_programs_with_gnat binding-programs-with-no-main-subprogram}@anchor{130}@anchor{gnat_ugn/building_executable_programs_with_gnat id40}@anchor{131}
15883 @subsubsection Binding Programs with No Main Subprogram
15886 It is possible to have an Ada program which does not have a main
15887 subprogram. This program will call the elaboration routines of all the
15888 packages, then the finalization routines.
15890 The following switch is used to bind programs organized in this manner:
15892 @quotation
15894 @geindex -z (gnatbind)
15895 @end quotation
15898 @table @asis
15900 @item @code{-z}
15902 Normally the binder checks that the unit name given on the command line
15903 corresponds to a suitable main subprogram. When this switch is used,
15904 a list of ALI files can be given, and the execution of the program
15905 consists of elaboration of these units in an appropriate order. Note
15906 that the default wide character encoding method for standard Text_IO
15907 files is always set to Brackets if this switch is set (you can use
15908 the binder switch
15909 @emph{-Wx} to override this default).
15910 @end table
15912 @node Command-Line Access,Search Paths for gnatbind,Switches for gnatbind,Binding with gnatbind
15913 @anchor{gnat_ugn/building_executable_programs_with_gnat id41}@anchor{132}@anchor{gnat_ugn/building_executable_programs_with_gnat command-line-access}@anchor{133}
15914 @subsection Command-Line Access
15917 The package @cite{Ada.Command_Line} provides access to the command-line
15918 arguments and program name. In order for this interface to operate
15919 correctly, the two variables
15921 @example
15922 int gnat_argc;
15923 char **gnat_argv;
15924 @end example
15926 @geindex gnat_argv
15928 @geindex gnat_argc
15930 are declared in one of the GNAT library routines. These variables must
15931 be set from the actual @cite{argc} and @cite{argv} values passed to the
15932 main program. With no @emph{n} present, @cite{gnatbind}
15933 generates the C main program to automatically set these variables.
15934 If the @emph{n} switch is used, there is no automatic way to
15935 set these variables. If they are not set, the procedures in
15936 @cite{Ada.Command_Line} will not be available, and any attempt to use
15937 them will raise @cite{Constraint_Error}. If command line access is
15938 required, your main program must set @cite{gnat_argc} and
15939 @cite{gnat_argv} from the @cite{argc} and @cite{argv} values passed to
15942 @node Search Paths for gnatbind,Examples of gnatbind Usage,Command-Line Access,Binding with gnatbind
15943 @anchor{gnat_ugn/building_executable_programs_with_gnat search-paths-for-gnatbind}@anchor{91}@anchor{gnat_ugn/building_executable_programs_with_gnat id42}@anchor{134}
15944 @subsection Search Paths for @cite{gnatbind}
15947 The binder takes the name of an ALI file as its argument and needs to
15948 locate source files as well as other ALI files to verify object consistency.
15950 For source files, it follows exactly the same search rules as @emph{gcc}
15951 (see @ref{8e,,Search Paths and the Run-Time Library (RTL)}). For ALI files the
15952 directories searched are:
15955 @itemize *
15957 @item 
15958 The directory containing the ALI file named in the command line, unless
15959 the switch @emph{-I-} is specified.
15961 @item 
15962 All directories specified by @emph{-I}
15963 switches on the @cite{gnatbind}
15964 command line, in the order given.
15966 @geindex ADA_PRJ_OBJECTS_FILE
15968 @item 
15969 Each of the directories listed in the text file whose name is given
15970 by the 
15971 @geindex ADA_PRJ_OBJECTS_FILE
15972 @geindex environment variable; ADA_PRJ_OBJECTS_FILE
15973 @code{ADA_PRJ_OBJECTS_FILE} environment variable.
15975 @geindex ADA_PRJ_OBJECTS_FILE
15976 @geindex environment variable; ADA_PRJ_OBJECTS_FILE
15977 @code{ADA_PRJ_OBJECTS_FILE} is normally set by gnatmake or by the gnat
15978 driver when project files are used. It should not normally be set
15979 by other means.
15981 @geindex ADA_OBJECTS_PATH
15983 @item 
15984 Each of the directories listed in the value of the
15985 @geindex ADA_OBJECTS_PATH
15986 @geindex environment variable; ADA_OBJECTS_PATH
15987 @code{ADA_OBJECTS_PATH} environment variable.
15988 Construct this value
15989 exactly as the 
15990 @geindex PATH
15991 @geindex environment variable; PATH
15992 @code{PATH} environment variable: a list of directory
15993 names separated by colons (semicolons when working with the NT version
15994 of GNAT).
15996 @item 
15997 The content of the @code{ada_object_path} file which is part of the GNAT
15998 installation tree and is used to store standard libraries such as the
15999 GNAT Run Time Library (RTL) unless the switch @emph{-nostdlib} is
16000 specified. See @ref{8b,,Installing a library}
16001 @end itemize
16003 @geindex -I (gnatbind)
16005 @geindex -aI (gnatbind)
16007 @geindex -aO (gnatbind)
16009 In the binder the switch @emph{-I}
16010 is used to specify both source and
16011 library file paths. Use @emph{-aI}
16012 instead if you want to specify
16013 source paths only, and @emph{-aO}
16014 if you want to specify library paths
16015 only. This means that for the binder
16016 @code{-I@emph{dir}} is equivalent to
16017 @code{-aI@emph{dir}}
16018 @code{-aO`@emph{dir}}.
16019 The binder generates the bind file (a C language source file) in the
16020 current working directory.
16022 @geindex Ada
16024 @geindex System
16026 @geindex Interfaces
16028 @geindex GNAT
16030 The packages @cite{Ada}, @cite{System}, and @cite{Interfaces} and their
16031 children make up the GNAT Run-Time Library, together with the package
16032 GNAT and its children, which contain a set of useful additional
16033 library functions provided by GNAT. The sources for these units are
16034 needed by the compiler and are kept together in one directory. The ALI
16035 files and object files generated by compiling the RTL are needed by the
16036 binder and the linker and are kept together in one directory, typically
16037 different from the directory containing the sources. In a normal
16038 installation, you need not specify these directory names when compiling
16039 or binding. Either the environment variables or the built-in defaults
16040 cause these files to be found.
16042 Besides simplifying access to the RTL, a major use of search paths is
16043 in compiling sources from multiple directories. This can make
16044 development environments much more flexible.
16046 @node Examples of gnatbind Usage,,Search Paths for gnatbind,Binding with gnatbind
16047 @anchor{gnat_ugn/building_executable_programs_with_gnat examples-of-gnatbind-usage}@anchor{135}@anchor{gnat_ugn/building_executable_programs_with_gnat id43}@anchor{136}
16048 @subsection Examples of @cite{gnatbind} Usage
16051 Here are some examples of @cite{gnatbind} invovations:
16053 @quotation
16055 @example
16056 gnatbind hello
16057 @end example
16059 The main program @cite{Hello} (source program in @code{hello.adb}) is
16060 bound using the standard switch settings. The generated main program is
16061 @code{b~hello.adb}. This is the normal, default use of the binder.
16063 @example
16064 gnatbind hello -o mainprog.adb
16065 @end example
16067 The main program @cite{Hello} (source program in @code{hello.adb}) is
16068 bound using the standard switch settings. The generated main program is
16069 @code{mainprog.adb} with the associated spec in
16070 @code{mainprog.ads}. Note that you must specify the body here not the
16071 spec. Note that if this option is used, then linking must be done manually,
16072 since gnatlink will not be able to find the generated file.
16073 @end quotation
16075 @node Linking with gnatlink,Using the GNU make Utility,Binding with gnatbind,Building Executable Programs with GNAT
16076 @anchor{gnat_ugn/building_executable_programs_with_gnat id44}@anchor{137}@anchor{gnat_ugn/building_executable_programs_with_gnat linking-with-gnatlink}@anchor{20}
16077 @section Linking with @emph{gnatlink}
16080 @c index: ! gnatlink
16082 This chapter discusses @emph{gnatlink}, a tool that links
16083 an Ada program and builds an executable file. This utility
16084 invokes the system linker (via the @emph{gcc} command)
16085 with a correct list of object files and library references.
16086 @emph{gnatlink} automatically determines the list of files and
16087 references for the Ada part of a program. It uses the binder file
16088 generated by the @emph{gnatbind} to determine this list.
16090 Note: to invoke @cite{gnatlink} with a project file, use the @cite{gnat}
16091 driver (see @ref{11f,,The GNAT Driver and Project Files}).
16093 @menu
16094 * Running gnatlink:: 
16095 * Switches for gnatlink:: 
16097 @end menu
16099 @node Running gnatlink,Switches for gnatlink,,Linking with gnatlink
16100 @anchor{gnat_ugn/building_executable_programs_with_gnat id45}@anchor{138}@anchor{gnat_ugn/building_executable_programs_with_gnat running-gnatlink}@anchor{139}
16101 @subsection Running @emph{gnatlink}
16104 The form of the @emph{gnatlink} command is
16106 @example
16107 $ gnatlink [`switches`] `mainprog`[.ali]
16108            [`non-Ada objects`] [`linker options`]
16109 @end example
16111 The arguments of @emph{gnatlink} (switches, main @code{ALI} file,
16112 non-Ada objects
16113 or linker options) may be in any order, provided that no non-Ada object may
16114 be mistaken for a main @code{ALI} file.
16115 Any file name @code{F} without the @code{.ali}
16116 extension will be taken as the main @code{ALI} file if a file exists
16117 whose name is the concatenation of @code{F} and @code{.ali}.
16119 @code{mainprog.ali} references the ALI file of the main program.
16120 The @code{.ali} extension of this file can be omitted. From this
16121 reference, @emph{gnatlink} locates the corresponding binder file
16122 @code{b~mainprog.adb} and, using the information in this file along
16123 with the list of non-Ada objects and linker options, constructs a
16124 linker command file to create the executable.
16126 The arguments other than the @emph{gnatlink} switches and the main
16127 @code{ALI} file are passed to the linker uninterpreted.
16128 They typically include the names of
16129 object files for units written in other languages than Ada and any library
16130 references required to resolve references in any of these foreign language
16131 units, or in @cite{Import} pragmas in any Ada units.
16133 @cite{linker options} is an optional list of linker specific
16134 switches.
16135 The default linker called by gnatlink is @emph{gcc} which in
16136 turn calls the appropriate system linker.
16138 One useful option for the linker is @emph{-s}: it reduces the size of the
16139 executable by removing all symbol table and relocation information from the
16140 executable.
16142 Standard options for the linker such as @emph{-lmy_lib} or
16143 @emph{-Ldir} can be added as is.
16144 For options that are not recognized by
16145 @emph{gcc} as linker options, use the @emph{gcc} switches
16146 @emph{-Xlinker} or @emph{-Wl,}.
16148 Refer to the GCC documentation for
16149 details.
16151 Here is an example showing how to generate a linker map:
16153 @example
16154 $ gnatlink my_prog -Wl,-Map,MAPFILE
16155 @end example
16157 Using @cite{linker options} it is possible to set the program stack and
16158 heap size.
16159 See @ref{13a,,Setting Stack Size from gnatlink} and
16160 @ref{13b,,Setting Heap Size from gnatlink}.
16162 @emph{gnatlink} determines the list of objects required by the Ada
16163 program and prepends them to the list of objects passed to the linker.
16164 @emph{gnatlink} also gathers any arguments set by the use of
16165 @cite{pragma Linker_Options} and adds them to the list of arguments
16166 presented to the linker.
16168 @node Switches for gnatlink,,Running gnatlink,Linking with gnatlink
16169 @anchor{gnat_ugn/building_executable_programs_with_gnat id46}@anchor{13c}@anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gnatlink}@anchor{13d}
16170 @subsection Switches for @emph{gnatlink}
16173 The following switches are available with the @emph{gnatlink} utility:
16175 @geindex --version (gnatlink)
16178 @table @asis
16180 @item @code{--version}
16182 Display Copyright and version, then exit disregarding all other options.
16183 @end table
16185 @geindex --help (gnatlink)
16188 @table @asis
16190 @item @code{--help}
16192 If @emph{--version} was not used, display usage, then exit disregarding
16193 all other options.
16194 @end table
16196 @geindex Command line length
16198 @geindex -f (gnatlink)
16201 @table @asis
16203 @item @code{-f}
16205 On some targets, the command line length is limited, and @emph{gnatlink}
16206 will generate a separate file for the linker if the list of object files
16207 is too long.
16208 The @emph{-f} switch forces this file
16209 to be generated even if
16210 the limit is not exceeded. This is useful in some cases to deal with
16211 special situations where the command line length is exceeded.
16212 @end table
16214 @geindex Debugging information
16215 @geindex including
16217 @geindex -g (gnatlink)
16220 @table @asis
16222 @item @code{-g}
16224 The option to include debugging information causes the Ada bind file (in
16225 other words, @code{b~mainprog.adb}) to be compiled with @emph{-g}.
16226 In addition, the binder does not delete the @code{b~mainprog.adb},
16227 @code{b~mainprog.o} and @code{b~mainprog.ali} files.
16228 Without @emph{-g}, the binder removes these files by default.
16229 @end table
16231 @geindex -n (gnatlink)
16234 @table @asis
16236 @item @code{-n}
16238 Do not compile the file generated by the binder. This may be used when
16239 a link is rerun with different options, but there is no need to recompile
16240 the binder file.
16241 @end table
16243 @geindex -v (gnatlink)
16246 @table @asis
16248 @item @code{-v}
16250 Verbose mode. Causes additional information to be output, including a full
16251 list of the included object files.
16252 This switch option is most useful when you want
16253 to see what set of object files are being used in the link step.
16254 @end table
16256 @geindex -v -v (gnatlink)
16259 @table @asis
16261 @item @code{-v -v}
16263 Very verbose mode. Requests that the compiler operate in verbose mode when
16264 it compiles the binder file, and that the system linker run in verbose mode.
16265 @end table
16267 @geindex -o (gnatlink)
16270 @table @asis
16272 @item @code{-o @emph{exec-name}}
16274 @cite{exec-name} specifies an alternate name for the generated
16275 executable program. If this switch is omitted, the executable has the same
16276 name as the main unit. For example, @cite{gnatlink try.ali} creates
16277 an executable called @code{try}.
16278 @end table
16280 @geindex -b (gnatlink)
16283 @table @asis
16285 @item @code{-b @emph{target}}
16287 Compile your program to run on @cite{target}, which is the name of a
16288 system configuration. You must have a GNAT cross-compiler built if
16289 @cite{target} is not the same as your host system.
16290 @end table
16292 @geindex -B (gnatlink)
16295 @table @asis
16297 @item @code{-B@emph{dir}}
16299 Load compiler executables (for example, @cite{gnat1}, the Ada compiler)
16300 from @cite{dir} instead of the default location. Only use this switch
16301 when multiple versions of the GNAT compiler are available.
16302 See the @cite{Directory Options} section in @cite{The_GNU_Compiler_Collection}
16303 for further details. You would normally use the @emph{-b} or
16304 @emph{-V} switch instead.
16305 @end table
16307 @geindex -M (gnatlink)
16310 @table @asis
16312 @item @code{-M}
16314 When linking an executable, create a map file. The name of the map file
16315 has the same name as the executable with extension ".map".
16316 @end table
16318 @geindex -M= (gnatlink)
16321 @table @asis
16323 @item @code{-M=@emph{mapfile}}
16325 When linking an executable, create a map file. The name of the map file is
16326 @cite{mapfile}.
16327 @end table
16329 @geindex --GCC=compiler_name (gnatlink)
16332 @table @asis
16334 @item @code{--GCC=@emph{compiler_name}}
16336 Program used for compiling the binder file. The default is
16337 @code{gcc}. You need to use quotes around @cite{compiler_name} if
16338 @cite{compiler_name} contains spaces or other separator characters.
16339 As an example @code{--GCC="foo -x -y"} will instruct @emph{gnatlink} to
16340 use @code{foo -x -y} as your compiler. Note that switch @code{-c} is always
16341 inserted after your command name. Thus in the above example the compiler
16342 command that will be used by @emph{gnatlink} will be @code{foo -c -x -y}.
16343 A limitation of this syntax is that the name and path name of the executable
16344 itself must not include any embedded spaces. If the compiler executable is
16345 different from the default one (gcc or <prefix>-gcc), then the back-end
16346 switches in the ALI file are not used to compile the binder generated source.
16347 For example, this is the case with @code{--GCC="foo -x -y"}. But the back end
16348 switches will be used for @code{--GCC="gcc -gnatv"}. If several
16349 @code{--GCC=compiler_name} are used, only the last @cite{compiler_name}
16350 is taken into account. However, all the additional switches are also taken
16351 into account. Thus,
16352 @code{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
16353 @code{--GCC="bar -x -y -z -t"}.
16354 @end table
16356 @geindex --LINK= (gnatlink)
16359 @table @asis
16361 @item @code{--LINK=@emph{name}}
16363 @cite{name} is the name of the linker to be invoked. This is especially
16364 useful in mixed language programs since languages such as C++ require
16365 their own linker to be used. When this switch is omitted, the default
16366 name for the linker is @emph{gcc}. When this switch is used, the
16367 specified linker is called instead of @emph{gcc} with exactly the same
16368 parameters that would have been passed to @emph{gcc} so if the desired
16369 linker requires different parameters it is necessary to use a wrapper
16370 script that massages the parameters before invoking the real linker. It
16371 may be useful to control the exact invocation by using the verbose
16372 switch.
16373 @end table
16375 @node Using the GNU make Utility,,Linking with gnatlink,Building Executable Programs with GNAT
16376 @anchor{gnat_ugn/building_executable_programs_with_gnat id47}@anchor{13e}@anchor{gnat_ugn/building_executable_programs_with_gnat using-the-gnu-make-utility}@anchor{21}
16377 @section Using the GNU @cite{make} Utility
16380 @geindex make (GNU)
16381 @geindex GNU make
16383 This chapter offers some examples of makefiles that solve specific
16384 problems. It does not explain how to write a makefile, nor does it try to replace the
16385 @emph{gnatmake} utility (@ref{1d,,Building with gnatmake}).
16387 All the examples in this section are specific to the GNU version of
16388 make. Although @emph{make} is a standard utility, and the basic language
16389 is the same, these examples use some advanced features found only in
16390 @cite{GNU make}.
16392 @menu
16393 * Using gnatmake in a Makefile:: 
16394 * Automatically Creating a List of Directories:: 
16395 * Generating the Command Line Switches:: 
16396 * Overcoming Command Line Length Limits:: 
16398 @end menu
16400 @node Using gnatmake in a Makefile,Automatically Creating a List of Directories,,Using the GNU make Utility
16401 @anchor{gnat_ugn/building_executable_programs_with_gnat using-gnatmake-in-a-makefile}@anchor{13f}@anchor{gnat_ugn/building_executable_programs_with_gnat id48}@anchor{140}
16402 @subsection Using gnatmake in a Makefile
16405 @c index makefile (GNU make)
16407 Complex project organizations can be handled in a very powerful way by
16408 using GNU make combined with gnatmake. For instance, here is a Makefile
16409 which allows you to build each subsystem of a big project into a separate
16410 shared library. Such a makefile allows you to significantly reduce the link
16411 time of very big applications while maintaining full coherence at
16412 each step of the build process.
16414 The list of dependencies are handled automatically by
16415 @emph{gnatmake}. The Makefile is simply used to call gnatmake in each of
16416 the appropriate directories.
16418 Note that you should also read the example on how to automatically
16419 create the list of directories
16420 (@ref{141,,Automatically Creating a List of Directories})
16421 which might help you in case your project has a lot of subdirectories.
16423 @example
16424 ## This Makefile is intended to be used with the following directory
16425 ## configuration:
16426 ##  - The sources are split into a series of csc (computer software components)
16427 ##    Each of these csc is put in its own directory.
16428 ##    Their name are referenced by the directory names.
16429 ##    They will be compiled into shared library (although this would also work
16430 ##    with static libraries
16431 ##  - The main program (and possibly other packages that do not belong to any
16432 ##    csc is put in the top level directory (where the Makefile is).
16433 ##       toplevel_dir __ first_csc  (sources) __ lib (will contain the library)
16434 ##                    \\_ second_csc (sources) __ lib (will contain the library)
16435 ##                    \\_ ...
16436 ## Although this Makefile is build for shared library, it is easy to modify
16437 ## to build partial link objects instead (modify the lines with -shared and
16438 ## gnatlink below)
16440 ## With this makefile, you can change any file in the system or add any new
16441 ## file, and everything will be recompiled correctly (only the relevant shared
16442 ## objects will be recompiled, and the main program will be re-linked).
16444 # The list of computer software component for your project. This might be
16445 # generated automatically.
16446 CSC_LIST=aa bb cc
16448 # Name of the main program (no extension)
16449 MAIN=main
16451 # If we need to build objects with -fPIC, uncomment the following line
16452 #NEED_FPIC=-fPIC
16454 # The following variable should give the directory containing libgnat.so
16455 # You can get this directory through 'gnatls -v'. This is usually the last
16456 # directory in the Object_Path.
16457 GLIB=...
16459 # The directories for the libraries
16460 # (This macro expands the list of CSC to the list of shared libraries, you
16461 # could simply use the expanded form:
16462 # LIB_DIR=aa/lib/libaa.so bb/lib/libbb.so cc/lib/libcc.so
16463 LIB_DIR=$@{foreach dir,$@{CSC_LIST@},$@{dir@}/lib/lib$@{dir@}.so@}
16465 $@{MAIN@}: objects $@{LIB_DIR@}
16466     gnatbind $@{MAIN@} $@{CSC_LIST:%=-aO%/lib@} -shared
16467     gnatlink $@{MAIN@} $@{CSC_LIST:%=-l%@}
16469 objects::
16470     # recompile the sources
16471     gnatmake -c -i $@{MAIN@}.adb $@{NEED_FPIC@} $@{CSC_LIST:%=-I%@}
16473 # Note: In a future version of GNAT, the following commands will be simplified
16474 # by a new tool, gnatmlib
16475 $@{LIB_DIR@}:
16476     mkdir -p $@{dir $@@ @}
16477     cd $@{dir $@@ @} && gcc -shared -o $@{notdir $@@ @} ../*.o -L$@{GLIB@} -lgnat
16478     cd $@{dir $@@ @} && cp -f ../*.ali .
16480 # The dependencies for the modules
16481 # Note that we have to force the expansion of *.o, since in some cases
16482 # make won't be able to do it itself.
16483 aa/lib/libaa.so: $@{wildcard aa/*.o@}
16484 bb/lib/libbb.so: $@{wildcard bb/*.o@}
16485 cc/lib/libcc.so: $@{wildcard cc/*.o@}
16487 # Make sure all of the shared libraries are in the path before starting the
16488 # program
16489 run::
16490     LD_LIBRARY_PATH=`pwd`/aa/lib:`pwd`/bb/lib:`pwd`/cc/lib ./$@{MAIN@}
16492 clean::
16493     $@{RM@} -rf $@{CSC_LIST:%=%/lib@}
16494     $@{RM@} $@{CSC_LIST:%=%/*.ali@}
16495     $@{RM@} $@{CSC_LIST:%=%/*.o@}
16496     $@{RM@} *.o *.ali $@{MAIN@}
16497 @end example
16499 @node Automatically Creating a List of Directories,Generating the Command Line Switches,Using gnatmake in a Makefile,Using the GNU make Utility
16500 @anchor{gnat_ugn/building_executable_programs_with_gnat automatically-creating-a-list-of-directories}@anchor{141}@anchor{gnat_ugn/building_executable_programs_with_gnat id49}@anchor{142}
16501 @subsection Automatically Creating a List of Directories
16504 In most makefiles, you will have to specify a list of directories, and
16505 store it in a variable. For small projects, it is often easier to
16506 specify each of them by hand, since you then have full control over what
16507 is the proper order for these directories, which ones should be
16508 included.
16510 However, in larger projects, which might involve hundreds of
16511 subdirectories, it might be more convenient to generate this list
16512 automatically.
16514 The example below presents two methods. The first one, although less
16515 general, gives you more control over the list. It involves wildcard
16516 characters, that are automatically expanded by @emph{make}. Its
16517 shortcoming is that you need to explicitly specify some of the
16518 organization of your project, such as for instance the directory tree
16519 depth, whether some directories are found in a separate tree, etc.
16521 The second method is the most general one. It requires an external
16522 program, called @emph{find}, which is standard on all Unix systems. All
16523 the directories found under a given root directory will be added to the
16524 list.
16526 @example
16527 # The examples below are based on the following directory hierarchy:
16528 # All the directories can contain any number of files
16529 # ROOT_DIRECTORY ->  a  ->  aa  ->  aaa
16530 #                       ->  ab
16531 #                       ->  ac
16532 #                ->  b  ->  ba  ->  baa
16533 #                       ->  bb
16534 #                       ->  bc
16535 # This Makefile creates a variable called DIRS, that can be reused any time
16536 # you need this list (see the other examples in this section)
16538 # The root of your project's directory hierarchy
16539 ROOT_DIRECTORY=.
16541 ####
16542 # First method: specify explicitly the list of directories
16543 # This allows you to specify any subset of all the directories you need.
16544 ####
16546 DIRS := a/aa/ a/ab/ b/ba/
16548 ####
16549 # Second method: use wildcards
16550 # Note that the argument(s) to wildcard below should end with a '/'.
16551 # Since wildcards also return file names, we have to filter them out
16552 # to avoid duplicate directory names.
16553 # We thus use make's `dir` and `sort` functions.
16554 # It sets DIRs to the following value (note that the directories aaa and baa
16555 # are not given, unless you change the arguments to wildcard).
16556 # DIRS= ./a/a/ ./b/ ./a/aa/ ./a/ab/ ./a/ac/ ./b/ba/ ./b/bb/ ./b/bc/
16557 ####
16559 DIRS := $@{sort $@{dir $@{wildcard $@{ROOT_DIRECTORY@}/*/
16560                     $@{ROOT_DIRECTORY@}/*/*/@}@}@}
16562 ####
16563 # Third method: use an external program
16564 # This command is much faster if run on local disks, avoiding NFS slowdowns.
16565 # This is the most complete command: it sets DIRs to the following value:
16566 # DIRS= ./a ./a/aa ./a/aa/aaa ./a/ab ./a/ac ./b ./b/ba ./b/ba/baa ./b/bb ./b/bc
16567 ####
16569 DIRS := $@{shell find $@{ROOT_DIRECTORY@} -type d -print@}
16570 @end example
16572 @node Generating the Command Line Switches,Overcoming Command Line Length Limits,Automatically Creating a List of Directories,Using the GNU make Utility
16573 @anchor{gnat_ugn/building_executable_programs_with_gnat id50}@anchor{143}@anchor{gnat_ugn/building_executable_programs_with_gnat generating-the-command-line-switches}@anchor{144}
16574 @subsection Generating the Command Line Switches
16577 Once you have created the list of directories as explained in the
16578 previous section (@ref{141,,Automatically Creating a List of Directories}),
16579 you can easily generate the command line arguments to pass to gnatmake.
16581 For the sake of completeness, this example assumes that the source path
16582 is not the same as the object path, and that you have two separate lists
16583 of directories.
16585 @example
16586 # see "Automatically creating a list of directories" to create
16587 # these variables
16588 SOURCE_DIRS=
16589 OBJECT_DIRS=
16591 GNATMAKE_SWITCHES := $@{patsubst %,-aI%,$@{SOURCE_DIRS@}@}
16592 GNATMAKE_SWITCHES += $@{patsubst %,-aO%,$@{OBJECT_DIRS@}@}
16594 all:
16595         gnatmake $@{GNATMAKE_SWITCHES@} main_unit
16596 @end example
16598 @node Overcoming Command Line Length Limits,,Generating the Command Line Switches,Using the GNU make Utility
16599 @anchor{gnat_ugn/building_executable_programs_with_gnat overcoming-command-line-length-limits}@anchor{145}@anchor{gnat_ugn/building_executable_programs_with_gnat id51}@anchor{146}
16600 @subsection Overcoming Command Line Length Limits
16603 One problem that might be encountered on big projects is that many
16604 operating systems limit the length of the command line. It is thus hard to give
16605 gnatmake the list of source and object directories.
16607 This example shows how you can set up environment variables, which will
16608 make @emph{gnatmake} behave exactly as if the directories had been
16609 specified on the command line, but have a much higher length limit (or
16610 even none on most systems).
16612 It assumes that you have created a list of directories in your Makefile,
16613 using one of the methods presented in
16614 @ref{141,,Automatically Creating a List of Directories}.
16615 For the sake of completeness, we assume that the object
16616 path (where the ALI files are found) is different from the sources patch.
16618 Note a small trick in the Makefile below: for efficiency reasons, we
16619 create two temporary variables (SOURCE_LIST and OBJECT_LIST), that are
16620 expanded immediately by @cite{make}. This way we overcome the standard
16621 make behavior which is to expand the variables only when they are
16622 actually used.
16624 On Windows, if you are using the standard Windows command shell, you must
16625 replace colons with semicolons in the assignments to these variables.
16627 @example
16628 # In this example, we create both ADA_INCLUDE_PATH and ADA_OBJECTS_PATH.
16629 # This is the same thing as putting the -I arguments on the command line.
16630 # (the equivalent of using -aI on the command line would be to define
16631 #  only ADA_INCLUDE_PATH, the equivalent of -aO is ADA_OBJECTS_PATH).
16632 # You can of course have different values for these variables.
16634 # Note also that we need to keep the previous values of these variables, since
16635 # they might have been set before running 'make' to specify where the GNAT
16636 # library is installed.
16638 # see "Automatically creating a list of directories" to create these
16639 # variables
16640 SOURCE_DIRS=
16641 OBJECT_DIRS=
16643 empty:=
16644 space:=$@{empty@} $@{empty@}
16645 SOURCE_LIST := $@{subst $@{space@},:,$@{SOURCE_DIRS@}@}
16646 OBJECT_LIST := $@{subst $@{space@},:,$@{OBJECT_DIRS@}@}
16647 ADA_INCLUDE_PATH += $@{SOURCE_LIST@}
16648 ADA_OBJECTS_PATH += $@{OBJECT_LIST@}
16649 export ADA_INCLUDE_PATH
16650 export ADA_OBJECTS_PATH
16652 all:
16653         gnatmake main_unit
16654 @end example
16656 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
16658 @node GNAT Project Manager,Tools Supporting Project Files,Building Executable Programs with GNAT,Top
16659 @anchor{gnat_ugn/gnat_project_manager doc}@anchor{147}@anchor{gnat_ugn/gnat_project_manager gnat-project-manager}@anchor{b}@anchor{gnat_ugn/gnat_project_manager id1}@anchor{148}
16660 @chapter GNAT Project Manager
16663 @menu
16664 * Introduction:: 
16665 * Building With Projects:: 
16666 * Organizing Projects into Subsystems:: 
16667 * Scenarios in Projects:: 
16668 * Library Projects:: 
16669 * Project Extension:: 
16670 * Aggregate Projects:: 
16671 * Aggregate Library Projects:: 
16672 * Project File Reference:: 
16674 @end menu
16676 @node Introduction,Building With Projects,,GNAT Project Manager
16677 @anchor{gnat_ugn/gnat_project_manager introduction}@anchor{149}@anchor{gnat_ugn/gnat_project_manager gnat-project-manager-introduction}@anchor{14a}
16678 @section Introduction
16681 This chapter describes GNAT's @emph{Project Manager}, a facility that allows
16682 you to manage complex builds involving a number of source files, directories,
16683 and options for different system configurations. In particular,
16684 project files allow you to specify:
16687 @itemize *
16689 @item 
16690 The directory or set of directories containing the source files, and/or the
16691 names of the specific source files themselves
16693 @item 
16694 The directory in which the compiler's output
16695 (@code{ALI} files, object files, tree files, etc.) is to be placed
16697 @item 
16698 The directory in which the executable programs are to be placed
16700 @item 
16701 Switch settings for any of the project-enabled tools;
16702 you can apply these settings either globally or to individual compilation units.
16704 @item 
16705 The source files containing the main subprogram(s) to be built
16707 @item 
16708 The source programming language(s)
16710 @item 
16711 Source file naming conventions; you can specify these either globally or for
16712 individual compilation units (see @ref{14b,,Naming Schemes}).
16714 @item 
16715 Change any of the above settings depending on external values, thus enabling
16716 the reuse of the projects in various @strong{scenarios} (see @ref{14c,,Scenarios in Projects}).
16718 @item 
16719 Automatically build libraries as part of the build process
16720 (see @ref{8a,,Library Projects}).
16721 @end itemize
16723 Project files are written in a syntax close to that of Ada, using familiar
16724 notions such as packages, context clauses, declarations, default values,
16725 assignments, and inheritance (see @ref{14d,,Project File Reference}).
16727 Project files can be built hierarchically from other project files, simplifying
16728 complex system integration and project reuse (see @ref{14e,,Organizing Projects into Subsystems}).
16731 @itemize *
16733 @item 
16734 One project can import other projects containing needed source files.
16735 More generally, the Project Manager lets you structure large development
16736 efforts into hierarchical subsystems, where build decisions are delegated
16737 to the subsystem level, and thus different compilation environments
16738 (switch settings) used for different subsystems.
16740 @item 
16741 You can organize GNAT projects in a hierarchy: a child project
16742 can extend a parent project, inheriting the parent's source files and
16743 optionally overriding any of them with alternative versions
16744 (see @ref{14f,,Project Extension}).
16745 @end itemize
16747 Several tools support project files, generally in addition to specifying
16748 the information on the command line itself). They share common switches
16749 to control the loading of the project (in particular
16750 @code{-P@emph{projectfile}} and
16751 @code{-X@emph{vbl}=@emph{value}}).
16753 The Project Manager supports a wide range of development strategies,
16754 for systems of all sizes.  Here are some typical practices that are
16755 easily handled:
16758 @itemize *
16760 @item 
16761 Using a common set of source files and generating object files in different
16762 directories via different switch settings. It can be used for instance, for
16763 generating separate sets of object files for debugging and for production.
16765 @item 
16766 Using a mostly-shared set of source files with different versions of
16767 some units or subunits. It can be used for instance, for grouping and hiding
16768 all OS dependencies in a small number of implementation units.
16769 @end itemize
16771 Project files can be used to achieve some of the effects of a source
16772 versioning system (for example, defining separate projects for
16773 the different sets of sources that comprise different releases) but the
16774 Project Manager is independent of any source configuration management tool
16775 that might be used by the developers.
16777 The various sections below introduce the different concepts related to
16778 projects. Each section starts with examples and use cases, and then goes into
16779 the details of related project file capabilities.
16781 @node Building With Projects,Organizing Projects into Subsystems,Introduction,GNAT Project Manager
16782 @anchor{gnat_ugn/gnat_project_manager building-with-projects}@anchor{150}@anchor{gnat_ugn/gnat_project_manager id2}@anchor{151}
16783 @section Building With Projects
16786 In its simplest form, a unique project is used to build a single executable.
16787 This section concentrates on such a simple setup. Later sections will extend
16788 this basic model to more complex setups.
16790 The following concepts are the foundation of project files, and will be further
16791 detailed later in this documentation. They are summarized here as a reference.
16794 @table @asis
16796 @item @strong{Project file}:
16798 A text file using an Ada-like syntax, generally using the @code{.gpr}
16799 extension. It defines build-related characteristics of an application.
16800 The characteristics include the list of sources, the location of those
16801 sources, the location for the generated object files, the name of
16802 the main program, and the options for the various tools involved in the
16803 build process.
16805 @item @strong{Project attribute}:
16807 A specific project characteristic is defined by an attribute clause. Its
16808 value is a string or a sequence of strings. All settings in a project
16809 are defined through a list of predefined attributes with precise
16810 semantics. See @ref{152,,Attributes}.
16812 @item @strong{Package in a project}:
16814 Global attributes are defined at the top level of a project.
16815 Attributes affecting specific tools are grouped in a
16816 package whose name is related to tool's function. The most common
16817 packages are @cite{Builder}, @cite{Compiler}, @cite{Binder},
16818 and @cite{Linker}. See @ref{153,,Packages}.
16820 @item @strong{Project variables}:
16822 In addition to attributes, a project can use variables to store intermediate
16823 values and avoid duplication in complex expressions. It can be initialized
16824 with a value coming from the environment.
16825 A frequent use of variables is to define scenarios.
16826 See @ref{154,,External Values}, @ref{14c,,Scenarios in Projects}, and @ref{155,,Variables}.
16828 @item @strong{Source files} and @strong{source directories}:
16830 A source file is associated with a language through a naming convention. For
16831 instance, @cite{foo.c} is typically the name of a C source file;
16832 @cite{bar.ads} or @cite{bar.1.ada} are two common naming conventions for a
16833 file containing an Ada spec. A compilation unit is often composed of a main
16834 source file and potentially several auxiliary ones, such as header files in C.
16835 The naming conventions can be user defined @ref{14b,,Naming Schemes}, and will
16836 drive the builder to call the appropriate compiler for the given source file.
16837 Source files are searched for in the source directories associated with the
16838 project through the @strong{Source_Dirs} attribute. By default, all the files (in
16839 these source directories) following the naming conventions associated with the
16840 declared languages are considered to be part of the project. It is also
16841 possible to limit the list of source files using the @strong{Source_Files} or
16842 @strong{Source_List_File} attributes. Note that those last two attributes only
16843 accept basenames with no directory information.
16845 @item @strong{Object files} and @strong{object directory}:
16847 An object file is an intermediate file produced by the compiler from a
16848 compilation unit. It is used by post-compilation tools to produce
16849 final executables or libraries. Object files produced in the context of
16850 a given project are stored in a single directory that can be specified by the
16851 @strong{Object_Dir} attribute. In order to store objects in
16852 two or more object directories, the system must be split into
16853 distinct subsystems with their own project file.
16854 @end table
16856 The following subsections introduce gradually all the attributes of interest
16857 for simple build needs. Here is the simple setup that will be used in the
16858 following examples.
16860 The Ada source files @code{pack.ads}, @code{pack.adb}, and @code{proc.adb} are in
16861 the @code{common/} directory. The file @code{proc.adb} contains an Ada main
16862 subprogram @cite{Proc} that @emph{with}s package @cite{Pack}. We want to compile
16863 these source files with the switch
16864 @emph{-O2}, and put the resulting files in
16865 the directory @code{obj/}.
16867 @example
16868 common/
16869   pack.ads
16870   pack.adb
16871   proc.adb
16872 common/obj/
16873   proc.ali, proc.o pack.ali, pack.o
16874 @end example
16876 Our project is to be called @emph{Build}. The name of the
16877 file is the name of the project (case-insensitive) with the
16878 @code{.gpr} extension, therefore the project file name is @code{build.gpr}. This
16879 is not mandatory, but a warning is issued when this convention is not followed.
16881 This is a very simple example, and as stated above, a single project
16882 file is enough for it. We will thus create a new file, that for now
16883 should contain the following code:
16885 @example
16886 project Build is
16887 end Build;
16888 @end example
16890 @menu
16891 * Source Files and Directories:: 
16892 * Duplicate Sources in Projects:: 
16893 * Object and Exec Directory:: 
16894 * Main Subprograms:: 
16895 * Tools Options in Project Files:: 
16896 * Compiling with Project Files:: 
16897 * Executable File Names:: 
16898 * Avoid Duplication With Variables:: 
16899 * Naming Schemes:: 
16900 * Installation:: 
16901 * Distributed support:: 
16903 @end menu
16905 @node Source Files and Directories,Duplicate Sources in Projects,,Building With Projects
16906 @anchor{gnat_ugn/gnat_project_manager id3}@anchor{156}@anchor{gnat_ugn/gnat_project_manager source-files-and-directories}@anchor{157}
16907 @subsection Source Files and Directories
16910 When you create a new project, the first thing to describe is how to find the
16911 corresponding source files. These are the only settings that are needed by all
16912 the tools that will use this project (builder, compiler, binder and linker for
16913 the compilation, IDEs to edit the source files,...).
16915 @geindex Source directories (GNAT Project Manager)
16917 The first step is to declare the source directories, which are the directories
16918 to be searched to find source files. In the case of the example,
16919 the @code{common} directory is the only source directory.
16921 @geindex Source_Dirs (GNAT Project Manager)
16923 There are several ways of defining source directories:
16926 @itemize *
16928 @item 
16929 When the attribute @strong{Source_Dirs} is not used, a project contains a
16930 single source directory which is the one where the project file itself
16931 resides. In our example, if @code{build.gpr} is placed in the @code{common}
16932 directory, the project has the needed implicit source directory.
16934 @item 
16935 The attribute @strong{Source_Dirs} can be set to a list of path names, one
16936 for each of the source directories. Such paths can either be absolute
16937 names (for instance @code{"/usr/local/common/"} on UNIX), or relative to the
16938 directory in which the project file resides (for instance "." if
16939 @code{build.gpr} is inside @code{common/}, or "common" if it is one level up).
16940 Each of the source directories must exist and be readable.
16942 @geindex portability of path names (GNAT Project Manager)
16944 The syntax for directories is platform specific. For portability, however,
16945 the project manager will always properly translate UNIX-like path names to
16946 the native format of the specific platform. For instance, when the same
16947 project file is to be used both on Unix and Windows, "/" should be used as
16948 the directory separator rather than "\".
16950 @item 
16951 The attribute @strong{Source_Dirs} can automatically include subdirectories
16952 using a special syntax inspired by some UNIX shells. If any of the paths in
16953 the list ends with "@code{**}", then that path and all its subdirectories
16954 (recursively) are included in the list of source directories. For instance,
16955 @code{**} and @code{./**} represent the complete directory tree rooted at
16956 the directory in which the project file resides.
16958 @geindex Source directories (GNAT Project Manager)
16960 @geindex Excluded_Source_Dirs (GNAT Project Manager)
16962 When using that construct, it can sometimes be convenient to also use the
16963 attribute @strong{Excluded_Source_Dirs}, which is also a list of paths. Each entry
16964 specifies a directory whose immediate content, not including subdirs, is to
16965 be excluded. It is also possible to exclude a complete directory subtree
16966 using the "**" notation.
16968 @geindex Ignore_Source_Sub_Dirs (GNAT Project Manager)
16970 It is often desirable to remove, from the source directories, directory
16971 subtrees rooted at some subdirectories. An example is the subdirectories
16972 created by a Version Control System such as Subversion that creates directory
16973 subtrees rooted at subdirectories ".svn". To do that, attribute
16974 @strong{Ignore_Source_Sub_Dirs} can be used. It specifies the list of simple
16975 file names for the roots of these undesirable directory subtrees.
16977 @c code-block: ada-project
16978 @c 
16979 @c for Source_Dirs use ("./**");
16980 @c for Ignore_Source_Sub_Dirs use (".svn");
16981 @end itemize
16983 When applied to the simple example, and because we generally prefer to have
16984 the project file at the toplevel directory rather than mixed with the sources,
16985 we will create the following file
16987 @c code-block: ada-project
16988 @c 
16989 @c build.gpr
16990 @c project Build is
16991 @c    for Source_Dirs use ("common");  --  <<<<
16992 @c end Build;
16994 Once source directories have been specified, one may need to indicate
16995 source files of interest. By default, all source files present in the source
16996 directories are considered by the project manager. When this is not desired,
16997 it is possible to specify the list of sources to consider explicitly.
16998 In such a case, only source file base names are indicated and not
16999 their absolute or relative path names. The project manager is in charge of
17000 locating the specified source files in the specified source directories.
17003 @itemize *
17005 @item 
17006 By default, the project manager searches for all source files of all
17007 specified languages in all the source directories.
17009 Since the project manager was initially developed for Ada environments, the
17010 default language is usually Ada and the above project file is complete: it
17011 defines without ambiguity the sources composing the project: that is to say,
17012 all the sources in subdirectory "common" for the default language (Ada) using
17013 the default naming convention.
17015 @geindex Languages (GNAT Project Manager)
17017 However, when compiling a multi-language application, or a pure C
17018 application, the project manager must be told which languages are of
17019 interest, which is done by setting the @strong{Languages} attribute to a list of
17020 strings, each of which is the name of a language. Tools like
17021 @emph{gnatmake} only know about Ada, while other tools like
17022 @emph{gprbuild} know about many more languages such as C, C++, Fortran,
17023 assembly and others can be added dynamically.
17025 @geindex Naming scheme (GNAT Project Manager)
17027 Even when using only Ada, the default naming might not be suitable. Indeed,
17028 how does the project manager recognizes an "Ada file" from any other
17029 file? Project files can describe the naming scheme used for source files,
17030 and override the default (see @ref{14b,,Naming Schemes}). The default is the
17031 standard GNAT extension (@code{.adb} for bodies and @code{.ads} for
17032 specs), which is what is used in our example, explaining why no naming scheme
17033 is explicitly specified.
17034 See @ref{14b,,Naming Schemes}.
17036 @geindex Source_Files (GNAT Project Manager)
17038 @item 
17039 @cite{Source_Files}.
17040 In some cases, source directories might contain files that should not be
17041 included in a project. One can specify the explicit list of file names to
17042 be considered through the @strong{Source_Files} attribute.
17043 When this attribute is defined, instead of looking at every file in the
17044 source directories, the project manager takes only those names into
17045 consideration  reports  errors if they cannot be found in the source
17046 directories or does not correspond to the naming scheme.
17048 @item 
17049 For various reasons, it is sometimes useful to have a project with no
17050 sources (most of the time because the attributes defined in the project
17051 file will be reused in other projects, as explained in
17052 @ref{14e,,Organizing Projects into Subsystems}. To do this, the attribute
17053 @emph{Source_Files} is set to the empty list, i.e. @cite{()}. Alternatively,
17054 @emph{Source_Dirs} can be set to the empty list, with the same
17055 result.
17057 @geindex Source_List_File (GNAT Project Manager)
17059 @item 
17060 @cite{Source_List_File}.
17061 If there is a great number of files, it might be more convenient to use
17062 the attribute @strong{Source_List_File}, which specifies the full path of a file.
17063 This file must contain a list of source file names (one per line, no
17064 directory information) that are searched as if they had been defined
17065 through @emph{Source_Files}. Such a file can easily be created through
17066 external tools.
17068 A warning is issued if both attributes @cite{Source_Files} and
17069 @cite{Source_List_File} are given explicit values. In this case, the
17070 attribute @cite{Source_Files} prevails.
17072 @geindex Excluded_Source_Files (GNAT Project Manager)
17074 @geindex Locally_Removed_Files (GNAT Project Manager)
17076 @geindex Excluded_Source_List_File (GNAT Project Manager)
17078 @item 
17079 @cite{Excluded_Source_Files}.
17080 Specifying an explicit list of files is not always convenient.It might be
17081 more convenient to use the default search rules with specific exceptions.
17082 This can be done thanks to the attribute @strong{Excluded_Source_Files}
17083 (or its synonym @strong{Locally_Removed_Files}).
17084 Its value is the list of file names that should not be taken into account.
17085 This attribute is often used when extending a project,
17086 see @ref{14f,,Project Extension}. A similar attribute
17087 @strong{Excluded_Source_List_File} plays the same
17088 role but takes the name of file containing file names similarly to
17089 @cite{Source_List_File}.
17090 @end itemize
17092 In most simple cases, such as the above example, the default source file search
17093 behavior provides the expected result, and we do not need to add anything after
17094 setting @cite{Source_Dirs}. The project manager automatically finds
17095 @code{pack.ads}, @code{pack.adb}, and @code{proc.adb} as source files of the
17096 project.
17098 Note that by default a warning is issued when a project has no sources attached
17099 to it and this is not explicitly indicated in the project file.
17101 @node Duplicate Sources in Projects,Object and Exec Directory,Source Files and Directories,Building With Projects
17102 @anchor{gnat_ugn/gnat_project_manager duplicate-sources-in-projects}@anchor{158}@anchor{gnat_ugn/gnat_project_manager id4}@anchor{159}
17103 @subsection Duplicate Sources in Projects
17106 If the order of the source directories is known statically, that is if
17107 @cite{"/**"} is not used in the string list @cite{Source_Dirs}, then there may
17108 be several files with the same name sitting in different directories of the
17109 project. In this case, only the file in the first directory is considered as a
17110 source of the project and the others are hidden. If @cite{"/**"} is used in the
17111 string list @cite{Source_Dirs}, it is an error to have several files with the
17112 same name in the same directory @cite{"/**"} subtree, since there would be an
17113 ambiguity as to which one should be used. However, two files with the same name
17114 may exist in two single directories or directory subtrees. In this case, the
17115 one in the first directory or directory subtree is a source of the project.
17117 If there are two sources in different directories of the same @cite{"/**"}
17118 subtree, one way to resolve the problem is to exclude the directory of the
17119 file that should not be used as a source of the project.
17121 @node Object and Exec Directory,Main Subprograms,Duplicate Sources in Projects,Building With Projects
17122 @anchor{gnat_ugn/gnat_project_manager object-and-exec-directory}@anchor{15a}@anchor{gnat_ugn/gnat_project_manager id5}@anchor{15b}
17123 @subsection Object and Exec Directory
17126 The next step when writing a project is to indicate where the compiler should
17127 put the object files. In fact, the compiler and other tools might create
17128 several different kind of files (for GNAT, there is the object file and the ALI
17129 file for instance). One of the important concepts in projects is that most
17130 tools may consider source directories as read-only and do not attempt to create
17131 new or temporary files there. Instead, all files are created in the object
17132 directory. It is of course not true for project-aware IDEs, whose purpose it is
17133 to create the source files.
17135 @geindex Object_Dir (GNAT Project Manager)
17137 The object directory is specified through the @strong{Object_Dir} attribute.
17138 Its value is the path to the object directory, either absolute or
17139 relative to the directory containing the project file. This
17140 directory must already exist and be readable and writable, although
17141 some tools have a switch to create the directory if needed (See
17142 the switch @cite{-p} for @emph{gnatmake}
17143 and @emph{gprbuild}).
17145 If the attribute @cite{Object_Dir} is not specified, it defaults to
17146 the project directory, that is the directory containing the project file.
17148 For our example, we can specify the object dir in this way:
17150 @c code-block: ada-project
17151 @c 
17152 @c project Build is
17153 @c    for Source_Dirs use ("common");
17154 @c    for Object_Dir use "obj";   --  <<<<
17155 @c end Build;
17157 As mentioned earlier, there is a single object directory per project. As a
17158 result, if you have an existing system where the object files are spread across
17159 several directories, you can either move all of them into the same directory if
17160 you want to build it with a single project file, or study the section on
17161 subsystems (see @ref{14e,,Organizing Projects into Subsystems}) to see how each
17162 separate object directory can be associated with one of the subsystems
17163 constituting the application.
17165 When the @emph{linker} is called, it usually creates an executable. By
17166 default, this executable is placed in the object directory of the project. It
17167 might be convenient to store it in its own directory.
17169 @geindex Exec_Dir (GNAT Project Manager)
17171 This can be done through the @cite{Exec_Dir} attribute, which, like
17172 @emph{Object_Dir} contains a single absolute or relative path and must point to
17173 an existing and writable directory, unless you ask the tool to create it on
17174 your behalf. When not specified, It defaults to the object directory and
17175 therefore to the project file's directory if neither @emph{Object_Dir} nor
17176 @emph{Exec_Dir} was specified.
17178 In the case of the example, let's place the executable in the root
17179 of the hierarchy, ie the same directory as @code{build.gpr}. Hence
17180 the project file is now
17182 @c code-block: ada-project
17183 @c 
17184 @c project Build is
17185 @c    for Source_Dirs use ("common");
17186 @c    for Object_Dir use "obj";
17187 @c    for Exec_Dir use ".";  --   <<<<
17188 @c end Build;
17190 @node Main Subprograms,Tools Options in Project Files,Object and Exec Directory,Building With Projects
17191 @anchor{gnat_ugn/gnat_project_manager id6}@anchor{15c}@anchor{gnat_ugn/gnat_project_manager main-subprograms}@anchor{15d}
17192 @subsection Main Subprograms
17195 In the previous section, executables were mentioned. The project manager needs
17196 to be taught what they are. In a project file, an executable is indicated by
17197 pointing to the source file of a main subprogram. In C this is the file that
17198 contains the @cite{main} function, and in Ada the file that contains the main
17199 unit.
17201 There can be any number of such main files within a given project, and thus
17202 several executables can be built in the context of a single project file. Of
17203 course, one given executable might not (and in fact will not) need all the
17204 source files referenced by the project. As opposed to other build environments
17205 such as @emph{makefile}, one does not need to specify the list of
17206 dependencies of each executable, the project-aware builder knows enough of the
17207 semantics of the languages to build and link only the necessary elements.
17209 @geindex Main (GNAT Project Manager)
17211 The list of main files is specified via the @strong{Main} attribute. It contains
17212 a list of file names (no directories). If a project defines this
17213 attribute, it is not necessary to identify  main files on the
17214 command line when invoking a builder, and editors like
17215 @emph{GPS} will be able to create extra menus to spawn or debug the
17216 corresponding executables.
17218 @c code-block: ada-project
17219 @c 
17220 @c project Build is
17221 @c    for Source_Dirs use ("common");
17222 @c    for Object_Dir use "obj";
17223 @c    for Exec_Dir use ".";
17224 @c    for Main use ("proc.adb");  --   <<<<
17225 @c end Build;
17227 If this attribute is defined in the project, then spawning the builder
17228 with a command such as
17230 @example
17231 gprbuild -Pbuild
17232 @end example
17234 automatically builds all the executables corresponding to the files
17235 listed in the @emph{Main} attribute. It is possible to specify one
17236 or more executables on the command line to build a subset of them.
17238 @node Tools Options in Project Files,Compiling with Project Files,Main Subprograms,Building With Projects
17239 @anchor{gnat_ugn/gnat_project_manager tools-options-in-project-files}@anchor{15e}@anchor{gnat_ugn/gnat_project_manager id7}@anchor{15f}
17240 @subsection Tools Options in Project Files
17243 We now have a project file that fully describes our environment, and can be
17244 used to build the application with a simple @emph{gprbuild} command as seen
17245 in the previous section. In fact, the empty project we showed immediately at
17246 the beginning (with no attribute at all) could already fulfill that need if it
17247 was put in the @code{common} directory.
17249 Of course, we might want more control. This section shows you how to specify
17250 the compilation switches that the various tools involved in the building of the
17251 executable should use.
17253 @geindex command line length (GNAT Project Manager)
17255 Since source names and locations are described in the project file, it is not
17256 necessary to use switches on the command line for this purpose (switches such
17257 as -I for gcc). This removes a major source of command line length overflow.
17258 Clearly, the builders will have to communicate this information one way or
17259 another to the underlying compilers and tools they call but they usually use
17260 response files for this and thus are not subject to command line overflows.
17262 Several tools participate to the creation of an executable: the compiler
17263 produces object files from the source files; the binder (in the Ada case)
17264 creates a "source" file that takes care, among other things, of elaboration
17265 issues and global variable initialization; and the linker gathers everything
17266 into a single executable that users can execute. All these tools are known to
17267 the project manager and will be called with user defined switches from the
17268 project files. However, we need to introduce a new project file concept to
17269 express the switches to be used for any of the tools involved in the build.
17271 @geindex project file packages (GNAT Project Manager)
17273 A project file is subdivided into zero or more @strong{packages}, each of which
17274 contains the attributes specific to one tool (or one set of tools). Project
17275 files use an Ada-like syntax for packages. Package names permitted in project
17276 files are restricted to a predefined set (see @ref{153,,Packages}), and the contents
17277 of packages are limited to a small set of constructs and attributes
17278 (see @ref{152,,Attributes}).
17280 Our example project file can be extended with the following empty packages. At
17281 this stage, they could all be omitted since they are empty, but they show which
17282 packages would be involved in the build process.
17284 @c code-block: ada-project
17285 @c 
17286 @c project Build is
17287 @c    for Source_Dirs use ("common");
17288 @c    for Object_Dir use "obj";
17289 @c    for Exec_Dir use ".";
17290 @c    for Main use ("proc.adb");
17291 @c 
17292 @c    package Builder is  --<<<  for gnatmake and gprbuild
17293 @c    end Builder;
17294 @c 
17295 @c    package Compiler is --<<<  for the compiler
17296 @c    end Compiler;
17297 @c 
17298 @c    package Binder is   --<<<  for the binder
17299 @c    end Binder;
17300 @c 
17301 @c    package Linker is   --<<<  for the linker
17302 @c    end Linker;
17303 @c end Build;
17305 Let's first examine the compiler switches. As stated in the initial description
17306 of the example, we want to compile all files with @emph{-O2}. This is a
17307 compiler switch, although it is usual, on the command line, to pass it to the
17308 builder which then passes it to the compiler. It is recommended to use directly
17309 the right package, which will make the setup easier to understand for other
17310 people.
17312 Several attributes can be used to specify the switches:
17314 @geindex Default_Switches (GNAT Project Manager)
17316 @strong{Default_Switches}:
17318 @quotation
17320 This is the first mention in this manual of an @strong{indexed attribute}. When
17321 this attribute is defined, one must supply an @emph{index} in the form of a
17322 literal string.
17323 In the case of @emph{Default_Switches}, the index is the name of the
17324 language to which the switches apply (since a different compiler will
17325 likely be used for each language, and each compiler has its own set of
17326 switches). The value of the attribute is a list of switches.
17328 In this example, we want to compile all Ada source files with the switch
17329 @emph{-O2}, and the resulting project file is as follows
17330 (only the @cite{Compiler} package is shown):
17332 @c code-block: ada-project
17333 @c 
17334 @c package Compiler is
17335 @c   for Default_Switches ("Ada") use ("-O2");
17336 @c end Compiler;
17337 @end quotation
17339 @geindex Switches (GNAT Project Manager)
17341 @strong{Switches}:
17343 @quotation
17345 In some cases, we might want to use specific switches
17346 for one or more files. For instance, compiling @code{proc.adb} might not be
17347 possible at high level of optimization because of a compiler issue.
17348 In such a case, the @emph{Switches}
17349 attribute (indexed on the file name) can be used and will override the
17350 switches defined by @emph{Default_Switches}. Our project file would
17351 become:
17353 @c code-block: ada-project
17354 @c 
17355 @c 
17356 @c package Compiler is
17357 @c    for Default_Switches ("Ada")
17358 @c        use ("-O2");
17359 @c    for Switches ("proc.adb")
17360 @c        use ("-O0");
17361 @c end Compiler;
17363 @cite{Switches} may take a pattern as an index, such as in:
17365 @c code-block: ada-project
17366 @c 
17367 @c package Compiler is
17368 @c   for Default_Switches ("Ada")
17369 @c       use ("-O2");
17370 @c   for Switches ("pkg*")
17371 @c       use ("-O0");
17372 @c end Compiler;
17374 Sources @code{pkg.adb} and @code{pkg-child.adb} would be compiled with -O0,
17375 not -O2.
17377 @cite{Switches} can also be given a language name as index instead of a file
17378 name in which case it has the same semantics as @emph{Default_Switches}.
17379 However, indexes with wild cards are never valid for language name.
17380 @end quotation
17382 @geindex Local_Configuration_Pragmas (GNAT Project Manager)
17384 @strong{Local_Configuration_Pragmas}:
17386 @quotation
17388 This attribute may specify the path
17389 of a file containing configuration pragmas for use by the Ada compiler,
17390 such as @cite{pragma Restrictions (No_Tasking)}. These pragmas will be
17391 used for all the sources of the project.
17392 @end quotation
17394 The switches for the other tools are defined in a similar manner through the
17395 @strong{Default_Switches} and @strong{Switches} attributes, respectively in the
17396 @emph{Builder} package (for @emph{gnatmake} and @emph{gprbuild}),
17397 the @emph{Binder} package (binding Ada executables) and the @emph{Linker}
17398 package (for linking executables).
17400 @node Compiling with Project Files,Executable File Names,Tools Options in Project Files,Building With Projects
17401 @anchor{gnat_ugn/gnat_project_manager compiling-with-project-files}@anchor{160}@anchor{gnat_ugn/gnat_project_manager id8}@anchor{161}
17402 @subsection Compiling with Project Files
17405 Now that our project files are written, let's build our executable.
17406 Here is the command we would use from the command line:
17408 @example
17409 gnatmake -Pbuild
17410 @end example
17412 This will automatically build the executables specified through the
17413 @emph{Main} attribute: for each, it will compile or recompile the
17414 sources for which the object file does not exist or is not up-to-date; it
17415 will then run the binder; and finally run the linker to create the
17416 executable itself.
17418 @emph{gnatmake} only knows how to handle Ada files. By using
17419 @emph{gprbuild} as a builder, you could automatically manage C files the
17420 same way: create the file @code{utils.c} in the @code{common} directory,
17421 set the attribute @emph{Languages} to @cite{"(Ada@comma{} C)"}, and run
17423 @example
17424 gprbuild -Pbuild
17425 @end example
17427 Gprbuild knows how to recompile the C files and will
17428 recompile them only if one of their dependencies has changed. No direct
17429 indication on how to build the various elements is given in the
17430 project file, which describes the project properties rather than a
17431 set of actions to be executed. Here is the invocation of
17432 @emph{gprbuild} when building a multi-language program:
17434 @example
17435 $ gprbuild -Pbuild
17436 gcc -c proc.adb
17437 gcc -c pack.adb
17438 gcc -c utils.c
17439 gprbind proc
17441 gcc proc.o -o proc
17442 @end example
17444 Notice the three steps described earlier:
17447 @itemize *
17449 @item 
17450 The first three gcc commands correspond to the compilation phase.
17452 @item 
17453 The gprbind command corresponds to the post-compilation phase.
17455 @item 
17456 The last gcc command corresponds to the final link.
17457 @end itemize
17459 @geindex -v option (for GPRbuild)
17461 The default output of GPRbuild's execution is kept reasonably simple and easy
17462 to understand. In particular, some of the less frequently used commands are not
17463 shown, and some parameters are abbreviated. So it is not possible to rerun the
17464 effect of the @emph{gprbuild} command by cut-and-pasting its output.
17465 GPRbuild's option @cite{-v} provides a much more verbose output which includes,
17466 among other information, more complete compilation, post-compilation and link
17467 commands.
17469 @node Executable File Names,Avoid Duplication With Variables,Compiling with Project Files,Building With Projects
17470 @anchor{gnat_ugn/gnat_project_manager executable-file-names}@anchor{162}@anchor{gnat_ugn/gnat_project_manager id9}@anchor{163}
17471 @subsection Executable File Names
17474 @geindex Executable (GNAT Project Manager)
17476 By default, the executable name corresponding to a main file is
17477 computed from the main source file name. Through the attribute
17478 @strong{Builder.Executable}, it is possible to change this default.
17480 For instance, instead of building @emph{proc} (or @emph{proc.exe}
17481 on Windows), we could configure our project file to build "proc1"
17482 (resp proc1.exe) with the following addition:
17484 @example
17485 project Build is
17486    ...  --  same as before
17487    package Builder is
17488       for Executable ("proc.adb") use "proc1";
17489    end Builder
17490 end Build;
17491 @end example
17493 @geindex Executable_Suffix (GNAT Project Manager)
17495 Attribute @strong{Executable_Suffix}, when specified, may change the suffix
17496 of the executable files, when no attribute @cite{Executable} applies:
17497 its value replaces the platform-specific executable suffix.
17498 The default executable suffix is empty on UNIX and ".exe" on Windows.
17500 It is also possible to change the name of the produced executable by using the
17501 command line switch @emph{-o}. When several mains are defined in the project,
17502 it is not possible to use the @emph{-o} switch and the only way to change the
17503 names of the executable is provided by Attributes @cite{Executable} and
17504 @cite{Executable_Suffix}.
17506 @node Avoid Duplication With Variables,Naming Schemes,Executable File Names,Building With Projects
17507 @anchor{gnat_ugn/gnat_project_manager id10}@anchor{164}@anchor{gnat_ugn/gnat_project_manager avoid-duplication-with-variables}@anchor{165}
17508 @subsection Avoid Duplication With Variables
17511 To illustrate some other project capabilities, here is a slightly more complex
17512 project using similar sources and a main program in C:
17514 @example
17515 project C_Main is
17516    for Languages    use ("Ada", "C");
17517    for Source_Dirs  use ("common");
17518    for Object_Dir   use  "obj";
17519    for Main         use ("main.c");
17520    package Compiler is
17521       C_Switches := ("-pedantic");
17522       for Default_Switches ("C")   use C_Switches;
17523       for Default_Switches ("Ada") use ("-gnaty");
17524       for Switches ("main.c") use C_Switches & ("-g");
17525    end Compiler;
17526 end C_Main;
17527 @end example
17529 This project has many similarities with the previous one.
17530 As expected, its @cite{Main} attribute now refers to a C source.
17531 The attribute @emph{Exec_Dir} is now omitted, thus the resulting
17532 executable will be put in the directory @code{obj}.
17534 The most noticeable difference is the use of a variable in the
17535 @emph{Compiler} package to store settings used in several attributes.
17536 This avoids text duplication, and eases maintenance (a single place to
17537 modify if we want to add new switches for C files). We will revisit
17538 the use of variables in the context of scenarios (see @ref{14c,,Scenarios in Projects}).
17540 In this example, we see how the file @code{main.c} can be compiled with
17541 the switches used for all the other C files, plus @emph{-g}.
17542 In this specific situation the use of a variable could have been
17543 replaced by a reference to the @cite{Default_Switches} attribute:
17545 @example
17546 for Switches ("c_main.c") use Compiler'Default_Switches ("C") & ("-g");
17547 @end example
17549 Note the tick (@emph{'}) used to refer to attributes defined in a package.
17551 Here is the output of the GPRbuild command using this project:
17553 @example
17554 $ gprbuild -Pc_main
17555 gcc -c -pedantic -g main.c
17556 gcc -c -gnaty proc.adb
17557 gcc -c -gnaty pack.adb
17558 gcc -c -pedantic utils.c
17559 gprbind main.bexch
17561 gcc main.o -o main
17562 @end example
17564 The default switches for Ada sources,
17565 the default switches for C sources (in the compilation of @code{lib.c}),
17566 and the specific switches for @code{main.c} have all been taken into
17567 account.
17569 @node Naming Schemes,Installation,Avoid Duplication With Variables,Building With Projects
17570 @anchor{gnat_ugn/gnat_project_manager id11}@anchor{166}@anchor{gnat_ugn/gnat_project_manager naming-schemes}@anchor{14b}
17571 @subsection Naming Schemes
17574 Sometimes an Ada software system is ported from one compilation environment to
17575 another (say GNAT), and the file are not named using the default GNAT
17576 conventions. Instead of changing all the file names, which for a variety of
17577 reasons might not be possible, you can define the relevant file naming scheme
17578 in the @strong{Naming} package of your project file.
17580 The naming scheme has two distinct goals for the project manager: it
17581 allows finding of source files when searching in the source
17582 directories, and given a source file name it makes it possible to guess
17583 the associated language, and thus the compiler to use.
17585 Note that the use by the Ada compiler of pragmas Source_File_Name is not
17586 supported when using project files. You must use the features described in this
17587 paragraph. You can however specify other configuration pragmas.
17589 The following attributes can be defined in package @cite{Naming}:
17591 @geindex Casing (GNAT Project Manager)
17593 @strong{Casing}:
17595 @quotation
17597 Its value must be one of @cite{"lowercase"} (the default if
17598 unspecified), @cite{"uppercase"} or @cite{"mixedcase"}. It describes the
17599 casing of file names with regards to the Ada unit name. Given an Ada unit
17600 My_Unit, the file name will respectively be @code{my_unit.adb} (lowercase),
17601 @code{MY_UNIT.ADB} (uppercase) or @code{My_Unit.adb} (mixedcase).
17602 On Windows, file names are case insensitive, so this attribute is
17603 irrelevant.
17604 @end quotation
17606 @geindex Dot_Replacement (GNAT Project Manager)
17608 @strong{Dot_Replacement}:
17610 @quotation
17612 This attribute specifies the string that should replace the "." in unit
17613 names. Its default value is @cite{"-"} so that a unit
17614 @cite{Parent.Child} is expected to be found in the file
17615 @code{parent-child.adb}. The replacement string must satisfy the following
17616 requirements to avoid ambiguities in the naming scheme:
17619 @itemize *
17621 @item 
17622 It must not be empty
17624 @item 
17625 It cannot start or end with an alphanumeric character
17627 @item 
17628 It cannot be a single underscore
17630 @item 
17631 It cannot start with an underscore followed by an alphanumeric
17633 @item 
17634 It cannot contain a dot @cite{'.'} except if the entire string is @cite{"."}
17635 @end itemize
17636 @end quotation
17638 @geindex Spec_Suffix (GNAT Project Manager)
17640 @geindex Specification_Suffix (GNAT Project Manager)
17642 @strong{Spec_Suffix} and @strong{Specification_Suffix}:
17644 @quotation
17646 For Ada, these attributes give the suffix used in file names that contain
17647 specifications. For other languages, they give the extension for files
17648 that contain declaration (header files in C for instance). The attribute
17649 is indexed on the language.
17650 The two attributes are equivalent, but the latter is obsolescent.
17652 If the value of the attribute is the empty string, it indicates to the
17653 Project Manager that the only specifications/header files for the language
17654 are those specified with attributes @cite{Spec} or
17655 @cite{Specification_Exceptions}.
17657 If @cite{Spec_Suffix ("Ada")} is not specified, then the default is
17658 @cite{".ads"}.
17660 A non empty value must satisfy the following requirements:
17663 @itemize *
17665 @item 
17666 It must include at least one dot
17668 @item 
17669 If @cite{Dot_Replacement} is a single dot, then it cannot include
17670 more than one dot.
17671 @end itemize
17672 @end quotation
17674 @geindex Body_Suffix (GNAT Project Manager)
17676 @geindex Implementation_Suffix (GNAT Project Manager)
17678 @strong{Body_Suffix} and @strong{Implementation_Suffix}:
17680 @quotation
17682 These attributes give the extension used for file names that contain
17683 code (bodies in Ada). They are indexed on the language. The second
17684 version is obsolescent and fully replaced by the first attribute.
17686 For each language of a project, one of these two attributes need to be
17687 specified, either in the project itself or in the configuration project file.
17689 If the value of the attribute is the empty string, it indicates to the
17690 Project Manager that the only source files for the language
17691 are those specified with attributes @cite{Body} or
17692 @cite{Implementation_Exceptions}.
17694 These attributes must satisfy the same requirements as @cite{Spec_Suffix}.
17695 In addition, they must be different from any of the values in
17696 @cite{Spec_Suffix}.
17697 If @cite{Body_Suffix ("Ada")} is not specified, then the default is
17698 @cite{".adb"}.
17700 If @cite{Body_Suffix ("Ada")} and @cite{Spec_Suffix ("Ada")} end with the
17701 same string, then a file name that ends with the longest of these two
17702 suffixes will be a body if the longest suffix is @cite{Body_Suffix ("Ada")}
17703 or a spec if the longest suffix is @cite{Spec_Suffix ("Ada")}.
17705 If the suffix does not start with a '.', a file with a name exactly equal to
17706 the suffix will also be part of the project (for instance if you define the
17707 suffix as @cite{Makefile.in}, a file called @code{Makefile.in} will be part
17708 of the project. This capability is usually not interesting when building.
17709 However, it might become useful when a project is also used to
17710 find the list of source files in an editor, like the GNAT Programming System
17711 (GPS).
17712 @end quotation
17714 @geindex Separate_Suffix (GNAT Project Manager)
17716 @strong{Separate_Suffix}:
17718 @quotation
17720 This attribute is specific to Ada. It denotes the suffix used in file names
17721 that contain separate bodies. If it is not specified, then it defaults to
17722 same value as @cite{Body_Suffix ("Ada")}.
17724 The value of this attribute cannot be the empty string.
17726 Otherwise, the same rules apply as for the
17727 @cite{Body_Suffix} attribute. The only accepted index is "Ada".
17728 @end quotation
17730 @strong{Spec} or @strong{Specification}:
17732 @quotation
17734 @geindex Spec (GNAT Project Manager)
17736 @geindex Specification (GNAT Project Manager)
17738 This attribute @cite{Spec} can be used to define the source file name for a
17739 given Ada compilation unit's spec. The index is the literal name of the Ada
17740 unit (case insensitive). The value is the literal base name of the file that
17741 contains this unit's spec (case sensitive or insensitive depending on the
17742 operating system). This attribute allows the definition of exceptions to the
17743 general naming scheme, in case some files do not follow the usual
17744 convention.
17746 When a source file contains several units, the relative position of the unit
17747 can be indicated. The first unit in the file is at position 1
17749 @example
17750 for Spec ("MyPack.MyChild") use "mypack.mychild.spec";
17751 for Spec ("top") use "foo.a" at 1;
17752 for Spec ("foo") use "foo.a" at 2;
17753 @end example
17754 @end quotation
17756 @geindex Body (GNAT Project Manager)
17758 @geindex Implementation (GNAT Project Manager)
17760 @strong{Body} or @strong{Implementation}:
17762 @quotation
17764 These attribute play the same role as @emph{Spec} for Ada bodies.
17765 @end quotation
17767 @geindex Specification_Exceptions (GNAT Project Manager)
17769 @geindex Implementation_Exceptions (GNAT Project Manager)
17771 @strong{Specification_Exceptions} and @strong{Implementation_Exceptions}:
17773 @quotation
17775 These attributes define exceptions to the naming scheme for languages
17776 other than Ada. They are indexed on the language name, and contain
17777 a list of file names respectively for headers and source code.
17778 @end quotation
17780 For example, the following package models the Apex file naming rules:
17782 @example
17783 package Naming is
17784   for Casing               use "lowercase";
17785   for Dot_Replacement      use ".";
17786   for Spec_Suffix ("Ada")  use ".1.ada";
17787   for Body_Suffix ("Ada")  use ".2.ada";
17788 end Naming;
17789 @end example
17791 @node Installation,Distributed support,Naming Schemes,Building With Projects
17792 @anchor{gnat_ugn/gnat_project_manager id12}@anchor{167}@anchor{gnat_ugn/gnat_project_manager installation}@anchor{168}
17793 @subsection Installation
17796 After building an application or a library it is often required to
17797 install it into the development environment. For instance this step is
17798 required if the library is to be used by another application.
17799 The @emph{gprinstall} tool provides an easy way to install
17800 libraries, executable or object code generated during the build. The
17801 @strong{Install} package can be used to change the default locations.
17803 The following attributes can be defined in package @cite{Install}:
17805 @geindex Active (GNAT Project Manager)
17808 @table @asis
17810 @item @strong{Active}
17812 Whether the project is to be installed, values are @cite{true}
17813 (default) or @cite{false}.
17814 @end table
17816 @geindex Artifacts (GNAT Project Manager)
17818 @strong{Artifacts}
17820 @quotation
17822 An array attribute to declare a set of files not part of the sources
17823 to be installed. The array discriminant is the directory where the
17824 file is to be installed. If a relative directory then Prefix (see
17825 below) is prepended.
17826 @end quotation
17828 @geindex Prefix (GNAT Project Manager)
17830 @strong{Prefix}:
17832 @quotation
17834 Root directory for the installation.
17835 @end quotation
17837 @strong{Exec_Subdir}
17839 @quotation
17841 Subdirectory of @strong{Prefix} where executables are to be
17842 installed. Default is @strong{bin}.
17843 @end quotation
17845 @strong{Lib_Subdir}
17847 @quotation
17849 Subdirectory of @strong{Prefix} where directory with the library or object
17850 files is to be installed. Default is @strong{lib}.
17851 @end quotation
17853 @strong{Sources_Subdir}
17855 @quotation
17857 Subdirectory of @strong{Prefix} where directory with sources is to be
17858 installed. Default is @strong{include}.
17859 @end quotation
17861 @strong{Project_Subdir}
17863 @quotation
17865 Subdirectory of @strong{Prefix} where the generated project file is to be
17866 installed. Default is @strong{share/gpr}.
17867 @end quotation
17869 @strong{Mode}
17871 @quotation
17873 The installation mode, it is either @strong{dev} (default) or @strong{usage}.
17874 See @strong{gprbuild} user's guide for details.
17875 @end quotation
17877 @strong{Install_Name}
17879 @quotation
17881 Specify the name to use for recording the installation. The default is
17882 the project name without the extension.
17883 @end quotation
17885 @node Distributed support,,Installation,Building With Projects
17886 @anchor{gnat_ugn/gnat_project_manager id13}@anchor{169}@anchor{gnat_ugn/gnat_project_manager distributed-support}@anchor{16a}
17887 @subsection Distributed support
17890 For large projects the compilation time can become a limitation in
17891 the development cycle. To cope with that, GPRbuild supports
17892 distributed compilation.
17894 The following attributes can be defined in package @cite{Remote}:
17896 @geindex Root_Dir (GNAT Project Manager)
17898 @strong{Root_Dir}:
17900 @quotation
17902 Root directory of the project's sources. The default value is the
17903 project's directory.
17904 @end quotation
17906 @node Organizing Projects into Subsystems,Scenarios in Projects,Building With Projects,GNAT Project Manager
17907 @anchor{gnat_ugn/gnat_project_manager organizing-projects-into-subsystems}@anchor{14e}@anchor{gnat_ugn/gnat_project_manager id14}@anchor{16b}
17908 @section Organizing Projects into Subsystems
17911 A @strong{subsystem} is a coherent part of the complete system to be built. It is
17912 represented by a set of sources and one single object directory. A system can
17913 be composed of a single subsystem when it is simple as we have seen in the
17914 first section. Complex systems are usually composed of several interdependent
17915 subsystems. A subsystem is dependent on another subsystem if knowledge of the
17916 other one is required to build it, and in particular if visibility on some of
17917 the sources of this other subsystem is required. Each subsystem is usually
17918 represented by its own project file.
17920 In this section, the previous example is being extended. Let's assume some
17921 sources of our @cite{Build} project depend on other sources.
17922 For instance, when building a graphical interface, it is usual to depend upon
17923 a graphical library toolkit such as GtkAda. Furthermore, we also need
17924 sources from a logging module we had previously written.
17926 @menu
17927 * Project Dependencies:: 
17928 * Cyclic Project Dependencies:: 
17929 * Sharing Between Projects:: 
17930 * Global Attributes:: 
17932 @end menu
17934 @node Project Dependencies,Cyclic Project Dependencies,,Organizing Projects into Subsystems
17935 @anchor{gnat_ugn/gnat_project_manager project-dependencies}@anchor{16c}@anchor{gnat_ugn/gnat_project_manager id15}@anchor{16d}
17936 @subsection Project Dependencies
17939 GtkAda comes with its own project file (appropriately called
17940 @code{gtkada.gpr}), and we will assume we have already built a project
17941 called @code{logging.gpr} for the logging module. With the information provided
17942 so far in @code{build.gpr}, building the application would fail with an error
17943 indicating that the gtkada and logging units that are relied upon by the sources
17944 of this project cannot be found.
17946 This is solved by adding the following @strong{with} clauses at the beginning of our
17947 project:
17949 @example
17950 with "gtkada.gpr";
17951 with "a/b/logging.gpr";
17952 project Build is
17953   ...  --  as before
17954 end Build;
17955 @end example
17957 @geindex Externally_Built (GNAT Project Manager)
17959 When such a project is compiled, @emph{gprbuild} will automatically check
17960 the other projects and recompile their sources when needed. It will also
17961 recompile the sources from @cite{Build} when needed, and finally create the
17962 executable. In some cases, the implementation units needed to recompile a
17963 project are not available, or come from some third party and you do not want to
17964 recompile it yourself. In this case, set the attribute @strong{Externally_Built} to
17965 "true", indicating to the builder that this project can be assumed to be
17966 up-to-date, and should not be considered for recompilation. In Ada, if the
17967 sources of this externally built project were compiled with another version of
17968 the compiler or with incompatible options, the binder will issue an error.
17970 The project's @emph{with} clause has several effects. It provides source
17971 visibility between projects during the compilation process. It also guarantees
17972 that the necessary object files from @cite{Logging} and @cite{GtkAda} are
17973 available when linking @cite{Build}.
17975 As can be seen in this example, the syntax for importing projects is similar
17976 to the syntax for importing compilation units in Ada. However, project files
17977 use literal strings instead of names, and the @emph{with} clause identifies
17978 project files rather than packages.
17980 Each literal string after @emph{with} is the path
17981 (absolute or relative) to a project file. The @cite{.gpr} extension is
17982 optional, although we recommend adding it. If no extension is specified,
17983 and no project file with the @code{.gpr} extension is found, then
17984 the file is searched for exactly as written in the @emph{with} clause,
17985 that is with no extension.
17987 As mentioned above, the path after a @emph{with} has to be a literal
17988 string, and you cannot use concatenation, or lookup the value of external
17989 variables to change the directories from which a project is loaded.
17990 A solution if you need something like this is to use aggregate projects
17991 (see @ref{16e,,Aggregate Projects}).
17993 @geindex project path (GNAT Project Manager)
17995 When a relative path or a base name is used, the
17996 project files are searched relative to each of the directories in the
17997 @strong{project path}. This path includes all the directories found with the
17998 following algorithm, in this order; the first matching file is used:
18001 @itemize *
18003 @item 
18004 First, the file is searched relative to the directory that contains the
18005 current project file.
18007 @geindex GPR_PROJECT_PATH_FILE (GNAT Project Manager)
18009 @geindex GPR_PROJECT_PATH (GNAT Project Manager)
18011 @geindex ADA_PROJECT_PATH (GNAT Project Manager)
18013 @item 
18014 Then it is searched relative to all the directories specified in the
18015 environment variables @strong{GPR_PROJECT_PATH_FILE},
18016 @strong{GPR_PROJECT_PATH} and @strong{ADA_PROJECT_PATH} (in that order) if they exist.
18017 The value of @strong{GPR_PROJECT_PATH_FILE}, when defined, is the path name of
18018 a text file that contains project directory path names, one per line.
18019 @strong{GPR_PROJECT_PATH} and @strong{ADA_PROJECT_PATH}, when defined, contain
18020 project directory path names separated by directory separators.
18021 @strong{ADA_PROJECT_PATH} is used for compatibility, it is recommended to
18022 use @strong{GPR_PROJECT_PATH_FILE} or @strong{GPR_PROJECT_PATH}.
18024 @item 
18025 Finally, it is searched relative to the default project directories.
18026 Such directories depend on the tool used. The locations searched in the
18027 specified order are:
18030 @itemize *
18032 @item 
18033 @code{<prefix>/<target>/lib/gnat}
18034 (for @emph{gnatmake} in all cases, and for @emph{gprbuild} if option
18035 @emph{--target} is specified)
18037 @item 
18038 @code{<prefix>/<target>/share/gpr}
18039 (for @emph{gnatmake} in all cases, and for @emph{gprbuild} if option
18040 @emph{--target} is specified)
18042 @item 
18043 @code{<prefix>/share/gpr/}
18044 (for @emph{gnatmake} and @emph{gprbuild})
18046 @item 
18047 @code{<prefix>/lib/gnat/}
18048 (for @emph{gnatmake} and @emph{gprbuild})
18049 @end itemize
18051 In our example, @code{gtkada.gpr} is found in the predefined directory if
18052 it was installed at the same root as GNAT.
18053 @end itemize
18055 Some tools also support extending the project path from the command line,
18056 generally through the @emph{-aP}. You can see the value of the project
18057 path by using the @emph{gnatls -v} command.
18059 Any symbolic link will be fully resolved in the directory of the
18060 importing project file before the imported project file is examined.
18062 Any source file in the imported project can be used by the sources of the
18063 importing project, transitively.
18064 Thus if @cite{A} imports @cite{B}, which imports @cite{C}, the sources of
18065 @cite{A} may depend on the sources of @cite{C}, even if @cite{A} does not
18066 import @cite{C} explicitly. However, this is not recommended, because if
18067 and when @cite{B} ceases to import @cite{C}, some sources in @cite{A} will
18068 no longer compile. @emph{gprbuild} has a switch @emph{--no-indirect-imports}
18069 that will report such indirect dependencies.
18071 @cartouche
18072 @quotation Note 
18073 One very important aspect of a project hierarchy is that
18074 @strong{a given source can only belong to one project} (otherwise the project manager
18075 would not know which settings apply to it and when to recompile it). It means
18076 that different project files do not usually share source directories or
18077 when they do, they need to specify precisely which project owns which sources
18078 using attribute @cite{Source_Files} or equivalent. By contrast, 2 projects
18079 can each own a source with the same base file name as long as they live in
18080 different directories. The latter is not true for Ada Sources because of the
18081 correlation between source files and Ada units.
18082 @end quotation
18083 @end cartouche
18085 @node Cyclic Project Dependencies,Sharing Between Projects,Project Dependencies,Organizing Projects into Subsystems
18086 @anchor{gnat_ugn/gnat_project_manager id16}@anchor{16f}@anchor{gnat_ugn/gnat_project_manager cyclic-project-dependencies}@anchor{170}
18087 @subsection Cyclic Project Dependencies
18090 Cyclic dependencies are mostly forbidden:
18091 if @cite{A} imports @cite{B} (directly or indirectly) then @cite{B}
18092 is not allowed to import @cite{A}. However, there are cases when cyclic
18093 dependencies would be beneficial. For these cases, another form of import
18094 between projects exists: the @strong{limited with}.  A project @cite{A} that
18095 imports a project @cite{B} with a straight @emph{with} may also be imported,
18096 directly or indirectly, by @cite{B} through a @cite{limited with}.
18098 The difference between straight @emph{with} and @cite{limited with} is that
18099 the name of a project imported with a @cite{limited with} cannot be used in the
18100 project importing it. In particular, its packages cannot be renamed and
18101 its variables cannot be referred to.
18103 @example
18104 with "b.gpr";
18105 with "c.gpr";
18106 project A is
18107     for Exec_Dir use B'Exec_Dir; -- ok
18108 end A;
18110 limited with "a.gpr";   --  Cyclic dependency: A -> B -> A
18111 project B is
18112    for Exec_Dir use A'Exec_Dir; -- not ok
18113 end B;
18115 with "d.gpr";
18116 project C is
18117 end C;
18119 limited with "a.gpr";  --  Cyclic dependency: A -> C -> D -> A
18120 project D is
18121    for Exec_Dir use A'Exec_Dir; -- not ok
18122 end D;
18123 @end example
18125 @node Sharing Between Projects,Global Attributes,Cyclic Project Dependencies,Organizing Projects into Subsystems
18126 @anchor{gnat_ugn/gnat_project_manager sharing-between-projects}@anchor{171}@anchor{gnat_ugn/gnat_project_manager id17}@anchor{172}
18127 @subsection Sharing Between Projects
18130 When building an application, it is common to have similar needs in several of
18131 the projects corresponding to the subsystems under construction. For instance,
18132 they will all have the same compilation switches.
18134 As seen before (see @ref{15e,,Tools Options in Project Files}), setting compilation
18135 switches for all sources of a subsystem is simple: it is just a matter of
18136 adding a @cite{Compiler.Default_Switches} attribute to each project files with
18137 the same value. Of course, that means duplication of data, and both places need
18138 to be changed in order to recompile the whole application with different
18139 switches. It can become a real problem if there are many subsystems and thus
18140 many project files to edit.
18142 There are two main approaches to avoiding this duplication:
18145 @itemize *
18147 @item 
18148 Since @code{build.gpr} imports @code{logging.gpr}, we could change it
18149 to reference the attribute in Logging, either through a package renaming,
18150 or by referencing the attribute. The following example shows both cases:
18152 @example
18153 project Logging is
18154    package Compiler is
18155       for Switches ("Ada")
18156           use ("-O2");
18157    end Compiler;
18158    package Binder is
18159       for Switches ("Ada")
18160           use ("-E");
18161    end Binder;
18162 end Logging;
18164 with "logging.gpr";
18165 project Build is
18166    package Compiler renames Logging.Compiler;
18167    package Binder is
18168       for Switches ("Ada") use Logging.Binder'Switches ("Ada");
18169    end Binder;
18170 end Build;
18171 @end example
18173 The solution used for @cite{Compiler} gets the same value for all
18174 attributes of the package, but you cannot modify anything from the
18175 package (adding extra switches or some exceptions). The second
18176 version is more flexible, but more verbose.
18178 If you need to refer to the value of a variable in an imported
18179 project, rather than an attribute, the syntax is similar but uses
18180 a "." rather than an apostrophe. For instance:
18182 @example
18183 with "imported";
18184 project Main is
18185    Var1 := Imported.Var;
18186 end Main;
18187 @end example
18189 @item 
18190 The second approach is to define the switches in a third project.
18191 That project is set up without any sources (so that, as opposed to
18192 the first example, none of the project plays a special role), and
18193 will only be used to define the attributes. Such a project is
18194 typically called @code{shared.gpr}.
18196 @example
18197 abstract project Shared is
18198    for Source_Files use ();   --  no sources
18199    package Compiler is
18200       for Switches ("Ada")
18201           use ("-O2");
18202    end Compiler;
18203 end Shared;
18205 with "shared.gpr";
18206 project Logging is
18207    package Compiler renames Shared.Compiler;
18208 end Logging;
18210 with "shared.gpr";
18211 project Build is
18212    package Compiler renames Shared.Compiler;
18213 end Build;
18214 @end example
18216 As for the first example, we could have chosen to set the attributes
18217 one by one rather than to rename a package. The reason we explicitly
18218 indicate that @cite{Shared} has no sources is so that it can be created
18219 in any directory and we are sure it shares no sources with @cite{Build}
18220 or @cite{Logging}, which of course would be invalid.
18222 @geindex project qualifier (GNAT Project Manager)
18224 Note the additional use of the @strong{abstract} qualifier in @code{shared.gpr}.
18225 This qualifier is optional, but helps convey the message that we do not
18226 intend this project to have sources (see @ref{173,,Qualified Projects} for
18227 more qualifiers).
18228 @end itemize
18230 @node Global Attributes,,Sharing Between Projects,Organizing Projects into Subsystems
18231 @anchor{gnat_ugn/gnat_project_manager global-attributes}@anchor{174}@anchor{gnat_ugn/gnat_project_manager id18}@anchor{175}
18232 @subsection Global Attributes
18235 We have already seen many examples of attributes used to specify a special
18236 option of one of the tools involved in the build process. Most of those
18237 attributes are project specific. That it to say, they only affect the invocation
18238 of tools on the sources of the project where they are defined.
18240 There are a few additional attributes that apply to all projects in a
18241 hierarchy as long as they are defined on the "main" project.
18242 The main project is the project explicitly mentioned on the command-line.
18243 The project hierarchy is the "with"-closure of the main project.
18245 Here is a list of commonly used global attributes:
18247 @geindex Global_Configuration_Pragmas (GNAT Project Manager)
18249 @strong{Builder.Global_Configuration_Pragmas}:
18251 @quotation
18253 This attribute points to a file that contains configuration pragmas
18254 to use when building executables. These pragmas apply for all
18255 executables built from this project hierarchy. As we have seen before,
18256 additional pragmas can be specified on a per-project basis by setting the
18257 @cite{Compiler.Local_Configuration_Pragmas} attribute.
18258 @end quotation
18260 @geindex Global_Compilation_Switches (GNAT Project Manager)
18262 @strong{Builder.Global_Compilation_Switches}:
18264 @quotation
18266 This attribute is a list of compiler switches to use when compiling any
18267 source file in the project hierarchy. These switches are used in addition
18268 to the ones defined in the @cite{Compiler} package, which only apply to
18269 the sources of the corresponding project. This attribute is indexed on
18270 the name of the language.
18271 @end quotation
18273 Using such global capabilities is convenient. It can also lead to unexpected
18274 behavior. Especially when several subsystems are shared among different main
18275 projects and the different global attributes are not
18276 compatible. Note that using aggregate projects can be a safer and more powerful
18277 replacement to global attributes.
18279 @node Scenarios in Projects,Library Projects,Organizing Projects into Subsystems,GNAT Project Manager
18280 @anchor{gnat_ugn/gnat_project_manager id19}@anchor{176}@anchor{gnat_ugn/gnat_project_manager scenarios-in-projects}@anchor{14c}
18281 @section Scenarios in Projects
18284 Various aspects of the projects can be modified based on @strong{scenarios}. These
18285 are user-defined modes that change the behavior of a project. Typical
18286 examples are the setup of platform-specific compiler options, or the use of
18287 a debug and a release mode (the former would activate the generation of debug
18288 information, while the second will focus on improving code optimization).
18290 Let's enhance our example to support debug and release modes. The issue is to
18291 let the user choose what kind of system he is building: use @emph{-g} as
18292 compiler switches in debug mode and @emph{-O2} in release mode. We will also
18293 set up the projects so that we do not share the same object directory in both
18294 modes; otherwise switching from one to the other might trigger more
18295 recompilations than needed or mix objects from the two modes.
18297 One naive approach is to create two different project files, say
18298 @code{build_debug.gpr} and @code{build_release.gpr}, that set the appropriate
18299 attributes as explained in previous sections. This solution does not scale
18300 well, because in the presence of multiple projects depending on each other, you
18301 will also have to duplicate the complete hierarchy and adapt the project files
18302 to point to the right copies.
18304 @geindex scenarios (GNAT Project Manager)
18306 Instead, project files support the notion of scenarios controlled
18307 by external values. Such values can come from several sources (in decreasing
18308 order of priority):
18310 @geindex -X (usage with GNAT Project Manager)
18313 @table @asis
18315 @item @strong{Command line}:
18317 When launching @emph{gnatmake} or @emph{gprbuild}, the user can pass
18318 extra @emph{-X} switches to define the external value. In
18319 our case, the command line might look like
18321 @example
18322 gnatmake -Pbuild.gpr -Xmode=debug
18323 @end example
18327 @example
18328 gnatmake -Pbuild.gpr -Xmode=release
18329 @end example
18331 @item @strong{Environment variables}:
18333 When the external value does not come from the command line, it can come from
18334 the value of environment variables of the appropriate name.
18335 In our case, if an environment variable called "mode"
18336 exists, its value will be taken into account.
18337 @end table
18339 @geindex external (GNAT Project Manager)
18341 @strong{External function second parameter}.
18343 We now need to get that value in the project. The general form is to use
18344 the predefined function @strong{external} which returns the current value of
18345 the external. For instance, we could set up the object directory to point to
18346 either @code{obj/debug} or @code{obj/release} by changing our project to
18348 @example
18349 project Build is
18350     for Object_Dir use "obj/" & external ("mode", "debug");
18351     ... --  as before
18352 end Build;
18353 @end example
18355 The second parameter to @cite{external} is optional, and is the default
18356 value to use if "mode" is not set from the command line or the environment.
18358 In order to set the switches according to the different scenarios, other
18359 constructs have to be introduced such as typed variables and case constructions.
18361 @geindex typed variable (GNAT Project Manager)
18363 @geindex case construction (GNAT Project Manager)
18365 A @strong{typed variable} is a variable that
18366 can take only a limited number of values, similar to an enumeration in Ada.
18367 Such a variable can then be used in a @strong{case construction} and create conditional
18368 sections in the project. The following example shows how this can be done:
18370 @example
18371 project Build is
18372    type Mode_Type is ("debug", "release");  --  all possible values
18373    Mode : Mode_Type := external ("mode", "debug"); -- a typed variable
18375    package Compiler is
18376       case Mode is
18377          when "debug" =>
18378             for Switches ("Ada")
18379                 use ("-g");
18380          when "release" =>
18381             for Switches ("Ada")
18382                 use ("-O2");
18383       end case;
18384    end Compiler;
18385 end Build;
18386 @end example
18388 The project has suddenly grown in size, but has become much more flexible.
18389 @cite{Mode_Type} defines the only valid values for the @cite{mode} variable. If
18390 any other value is read from the environment, an error is reported and the
18391 project is considered as invalid.
18393 The @cite{Mode} variable is initialized with an external value
18394 defaulting to @cite{"debug"}. This default could be omitted and that would
18395 force the user to define the value. Finally, we can use a case construction to set the
18396 switches depending on the scenario the user has chosen.
18398 Most aspects of the projects can depend on scenarios. The notable exception
18399 are project dependencies (@emph{with} clauses), which cannot depend on a scenario.
18401 Scenarios work the same way with @strong{project hierarchies}: you can either
18402 duplicate a variable similar to @cite{Mode} in each of the project (as long
18403 as the first argument to @cite{external} is always the same and the type is
18404 the same), or simply set the variable in the @code{shared.gpr} project
18405 (see @ref{171,,Sharing Between Projects}).
18407 @node Library Projects,Project Extension,Scenarios in Projects,GNAT Project Manager
18408 @anchor{gnat_ugn/gnat_project_manager library-projects}@anchor{8a}@anchor{gnat_ugn/gnat_project_manager id20}@anchor{177}
18409 @section Library Projects
18412 So far, we have seen examples of projects that create executables. However,
18413 it is also possible to create libraries instead. A @strong{library} is a specific
18414 type of subsystem where, for convenience, objects are grouped together
18415 using system-specific means such as archives or windows DLLs.
18417 Library projects provide a system- and language-independent way of building
18418 both @strong{static} and @strong{dynamic} libraries. They also support the concept of
18419 @strong{standalone libraries} (SAL) which offer two significant properties: the
18420 elaboration (e.g. initialization) of the library is either automatic or
18421 very simple; a change in the
18422 implementation part of the library implies minimal post-compilation actions on
18423 the complete system and potentially no action at all for the rest of the
18424 system in the case of dynamic SALs.
18426 There is a restriction on shared library projects: by default, they are only
18427 allowed to import other shared library projects. They are not allowed to
18428 import non library projects or static library projects.
18430 The GNAT Project Manager takes complete care of the library build, rebuild and
18431 installation tasks, including recompilation of the source files for which
18432 objects do not exist or are not up to date, assembly of the library archive, and
18433 installation of the library (i.e., copying associated source, object and
18434 @code{ALI} files to the specified location).
18436 @menu
18437 * Building Libraries:: 
18438 * Using Library Projects:: 
18439 * Stand-alone Library Projects:: 
18440 * Installing a library with project files:: 
18442 @end menu
18444 @node Building Libraries,Using Library Projects,,Library Projects
18445 @anchor{gnat_ugn/gnat_project_manager id21}@anchor{178}@anchor{gnat_ugn/gnat_project_manager building-libraries}@anchor{179}
18446 @subsection Building Libraries
18449 Let's enhance our example and transform the @cite{logging} subsystem into a
18450 library.  In order to do so, a few changes need to be made to
18451 @code{logging.gpr}.  Some attributes need to be defined: at least
18452 @cite{Library_Name} and @cite{Library_Dir}; in addition, some other attributes
18453 can be used to specify specific aspects of the library. For readability, it is
18454 also recommended (although not mandatory), to use the qualifier @cite{library}
18455 in front of the @cite{project} keyword.
18457 @geindex Library_Name (GNAT Project Manager)
18459 @strong{Library_Name}:
18461 @quotation
18463 This attribute is the name of the library to be built. There is no
18464 restriction on the name of a library imposed by the project manager, except
18465 for stand-alone libraries whose names must follow the syntax of Ada
18466 identifiers; however, there may be system-specific restrictions on the name.
18467 In general, it is recommended to stick to alphanumeric characters (and
18468 possibly single underscores) to help portability.
18469 @end quotation
18471 @geindex Library_Dir (GNAT Project Manager)
18473 @strong{Library_Dir}:
18475 @quotation
18477 This attribute  is the path (absolute or relative) of the directory where
18478 the library is to be installed. In the process of building a library,
18479 the sources are compiled, the object files end up  in the explicit or
18480 implicit @cite{Object_Dir} directory. When all sources of a library
18481 are compiled, some of the compilation artifacts, including the library itself,
18482 are copied to the library_dir directory. This directory must exist and be
18483 writable. It must also be different from the object directory so that cleanup
18484 activities in the Library_Dir do not affect recompilation needs.
18485 @end quotation
18487 Here is the new version of @code{logging.gpr} that makes it a library:
18489 @example
18490 library project Logging is          --  "library" is optional
18491    for Library_Name use "logging";  --  will create "liblogging.a" on Unix
18492    for Object_Dir   use "obj";
18493    for Library_Dir  use "lib";      --  different from object_dir
18494 end Logging;
18495 @end example
18497 Once the above two attributes are defined, the library project is valid and
18498 is enough for building a library with default characteristics.
18499 Other library-related attributes can be used to change the defaults:
18501 @geindex Library_Kind (GNAT Project Manager)
18503 @strong{Library_Kind}:
18505 @quotation
18507 The value of this attribute must be either @cite{"static"}, @cite{"dynamic"} or
18508 @cite{"relocatable"} (the latter is a synonym for dynamic). It indicates
18509 which kind of library should be built (the default is to build a
18510 static library, that is an archive of object files that can potentially
18511 be linked into a static executable). When the library is set to be dynamic,
18512 a separate image is created that will be loaded independently, usually
18513 at the start of the main program execution. Support for dynamic libraries is
18514 very platform specific, for instance on Windows it takes the form of a DLL
18515 while on GNU/Linux, it is a dynamic elf image whose suffix is usually
18516 @code{.so}. Library project files, on the other hand, can be written in
18517 a platform independent way so that the same project file can be used to build
18518 a library on different operating systems.
18520 If you need to build both a static and a dynamic library, it is recommended
18521 to use two different object directories, since in some cases some extra code
18522 needs to be generated for the latter. For such cases, one can either define
18523 two different project files, or a single one that uses scenarios to indicate
18524 the various kinds of library to be built and their corresponding object_dir.
18525 @end quotation
18527 @geindex Library_ALI_Dir (GNAT Project Manager)
18529 @strong{Library_ALI_Dir}:
18531 @quotation
18533 This attribute may be specified to indicate the directory where the ALI
18534 files of the library are installed. By default, they are copied into the
18535 @cite{Library_Dir} directory, but as for the executables where we have a
18536 separate @cite{Exec_Dir} attribute, you might want to put them in a separate
18537 directory since there can be hundreds of them. The same restrictions as for
18538 the @cite{Library_Dir} attribute apply.
18539 @end quotation
18541 @geindex Library_Version (GNAT Project Manager)
18543 @strong{Library_Version}:
18545 @quotation
18547 This attribute is platform dependent, and has no effect on Windows.
18548 On Unix, it is used only for dynamic libraries as the internal
18549 name of the library (the @cite{"soname"}). If the library file name (built
18550 from the @cite{Library_Name}) is different from the @cite{Library_Version},
18551 then the library file will be a symbolic link to the actual file whose name
18552 will be @cite{Library_Version}. This follows the usual installation schemes
18553 for dynamic libraries on many Unix systems.
18555 @example
18556 project Logging is
18557    Version := "1";
18558    for Library_Dir use "lib";
18559    for Library_Name use "logging";
18560    for Library_Kind use "dynamic";
18561    for Library_Version use "liblogging.so." & Version;
18562 end Logging;
18563 @end example
18565 After the compilation, the directory @code{lib} will contain both a
18566 @code{libdummy.so.1} library and a symbolic link to it called
18567 @code{libdummy.so}.
18568 @end quotation
18570 @geindex Library_GCC (GNAT Project Manager)
18572 @strong{Library_GCC}:
18574 @quotation
18576 This attribute is the name of the tool to use instead of "gcc" to link shared
18577 libraries. A common use of this attribute is to define a wrapper script that
18578 accomplishes specific actions before calling gcc (which itself calls the
18579 linker to build the library image).
18580 @end quotation
18582 @geindex Library_Options (GNAT Project Manager)
18584 @strong{Library_Options}:
18586 @quotation
18588 This attribute may be used to specify additional switches (last switches)
18589 when linking a shared library.
18591 It may also be used to add foreign object files to a static library.
18592 Each string in Library_Options is an absolute or relative path of an object
18593 file. When a relative path, it is relative to the object directory.
18594 @end quotation
18596 @geindex Leading_Library_Options (GNAT Project Manager)
18598 @strong{Leading_Library_Options}:
18600 @quotation
18602 This attribute, that is taken into account only by @emph{gprbuild}, may be
18603 used to specified leading options (first switches) when linking a shared
18604 library.
18605 @end quotation
18607 @geindex Linker_Options (GNAT Project Manager)
18609 @strong{Linker.Linker_Options}:
18611 @quotation
18613 This attribute specifies additional switches to be given to the linker when
18614 linking an executable. It is ignored when defined in the main project and
18615 taken into account in all other projects that are imported directly or
18616 indirectly. These switches complement the @cite{Linker.Switches}
18617 defined in the main project. This is useful when a particular subsystem
18618 depends on an external library: adding this dependency as a
18619 @cite{Linker_Options} in the project of the subsystem is more convenient than
18620 adding it to all the @cite{Linker.Switches} of the main projects that depend
18621 upon this subsystem.
18622 @end quotation
18624 @node Using Library Projects,Stand-alone Library Projects,Building Libraries,Library Projects
18625 @anchor{gnat_ugn/gnat_project_manager id22}@anchor{17a}@anchor{gnat_ugn/gnat_project_manager using-library-projects}@anchor{17b}
18626 @subsection Using Library Projects
18629 When the builder detects that a project file is a library project file, it
18630 recompiles all sources of the project that need recompilation and rebuild the
18631 library if any of the sources have been recompiled. It then groups all object
18632 files into a single file, which is a shared or a static library. This library
18633 can later on be linked with multiple executables. Note that the use
18634 of shard libraries reduces the size of the final executable and can also reduce
18635 the memory footprint at execution time when the library is shared among several
18636 executables.
18638 It is also possible to build @strong{multi-language libraries}. When using
18639 @emph{gprbuild} as a builder, multi-language library projects allow naturally
18640 the creation of multi-language libraries . @emph{gnatmake}, does not try to
18641 compile non Ada sources. However, when the project is multi-language, it will
18642 automatically link all object files found in the object directory, whether or
18643 not they were compiled from an Ada source file. This specific behavior does not
18644 apply to Ada-only projects which only take into account the objects
18645 corresponding to the sources of the project.
18647 A non-library project can import a library project. When the builder is invoked
18648 on the former, the library of the latter is only rebuilt when absolutely
18649 necessary. For instance, if a unit of the library is not up-to-date but none of
18650 the executables need this unit, then the unit is not recompiled and the library
18651 is not reassembled.  For instance, let's assume in our example that logging has
18652 the following sources: @code{log1.ads}, @code{log1.adb}, @code{log2.ads} and
18653 @code{log2.adb}. If @code{log1.adb} has been modified, then the library
18654 @code{liblogging} will be rebuilt when compiling all the sources of
18655 @cite{Build} only if @code{proc.ads}, @code{pack.ads} or @code{pack.adb}
18656 include a @cite{"with Log1"}.
18658 To ensure that all the sources in the @cite{Logging} library are
18659 up to date, and that all the sources of @cite{Build} are also up to date,
18660 the following two commands need to be used:
18662 @example
18663 gnatmake -Plogging.gpr
18664 gnatmake -Pbuild.gpr
18665 @end example
18667 All @code{ALI} files will also be copied from the object directory to the
18668 library directory. To build executables, @emph{gnatmake} will use the
18669 library rather than the individual object files.
18671 Library projects can also be useful to describe a library that needs to be used
18672 but, for some reason, cannot be rebuilt. For instance, it is the case when some
18673 of the library sources are not available. Such library projects need to use the
18674 @cite{Externally_Built} attribute as in the example below:
18676 @c code-block: ada-project
18677 @c 
18678 @c library project Extern_Lib is
18679 @c    for Languages    use ("Ada", "C");
18680 @c    for Source_Dirs  use ("lib_src");
18681 @c    for Library_Dir  use "lib2";
18682 @c    for Library_Kind use "dynamic";
18683 @c    for Library_Name use "l2";
18684 @c    for Externally_Built use "true";  --  <<<<
18685 @c end Extern_Lib;
18687 In the case of externally built libraries, the @cite{Object_Dir}
18688 attribute does not need to be specified because it will never be
18689 used.
18691 The main effect of using such an externally built library project is mostly to
18692 affect the linker command in order to reference the desired library. It can
18693 also be achieved by using @cite{Linker.Linker_Options} or @cite{Linker.Switches}
18694 in the project corresponding to the subsystem needing this external library.
18695 This latter method is more straightforward in simple cases but when several
18696 subsystems depend upon the same external library, finding the proper place
18697 for the @cite{Linker.Linker_Options} might not be easy and if it is
18698 not placed properly, the final link command is likely to present ordering issues.
18699 In such a situation, it is better to use the externally built library project
18700 so that all other subsystems depending on it can declare this dependency thanks
18701 to a project @emph{with} clause, which in turn will trigger the builder to find
18702 the proper order of libraries in the final link command.
18704 @node Stand-alone Library Projects,Installing a library with project files,Using Library Projects,Library Projects
18705 @anchor{gnat_ugn/gnat_project_manager id23}@anchor{17c}@anchor{gnat_ugn/gnat_project_manager stand-alone-library-projects}@anchor{97}
18706 @subsection Stand-alone Library Projects
18709 @geindex standalone libraries (usage with GNAT Project Manager)
18711 A @strong{stand-alone library} is a library that contains the necessary code to
18712 elaborate the Ada units that are included in the library. A stand-alone
18713 library is a convenient way to add an Ada subsystem to a more global system
18714 whose main is not in Ada since it makes the elaboration of the Ada part mostly
18715 transparent. However, stand-alone libraries are also useful when the main is in
18716 Ada: they provide a means for minimizing relinking & redeployment of complex
18717 systems when localized changes are made.
18719 The name of a stand-alone library, specified with attribute
18720 @cite{Library_Name}, must have the syntax of an Ada identifier.
18722 The most prominent characteristic of a stand-alone library is that it offers a
18723 distinction between interface units and implementation units. Only the former
18724 are visible to units outside the library. A stand-alone library project is thus
18725 characterised by a third attribute, usually @strong{Library_Interface}, in addition
18726 to the two attributes that make a project a Library Project
18727 (@cite{Library_Name} and @cite{Library_Dir}). This third attribute may also be
18728 @strong{Interfaces}. @strong{Library_Interface} only works when the interface is in Ada
18729 and takes a list of units as parameter. @strong{Interfaces} works for any supported
18730 language and takes a list of sources as parameter.
18732 @geindex Library_Interface (GNAT Project Manager)
18734 @strong{Library_Interface}:
18736 @quotation
18738 This attribute defines an explicit subset of the units of the project. Units
18739 from projects importing this library project may only "with" units whose
18740 sources are listed in the @cite{Library_Interface}. Other sources are
18741 considered implementation units.
18743 @example
18744 for Library_Dir use "lib";
18745 for Library_Name use "logging";
18746 for Library_Interface use ("lib1", "lib2");  --  unit names
18747 @end example
18748 @end quotation
18750 @strong{Interfaces}
18752 @quotation
18754 This attribute defines an explicit subset of the source files of a project.
18755 Sources from projects importing this project, can only depend on sources from
18756 this subset. This attribute can be used on non library projects. It can also
18757 be used as a replacement for attribute @cite{Library_Interface}, in which
18758 case, units have to be replaced by source files. For multi-language library
18759 projects, it is the only way to make the project a Stand-Alone Library project
18760 whose interface is not purely Ada.
18761 @end quotation
18763 @geindex Library_Standalone (GNAT Project Manager)
18765 @strong{Library_Standalone}:
18767 @quotation
18769 This attribute defines the kind of standalone library to
18770 build. Values are either @cite{standard} (the default), @cite{no} or
18771 @cite{encapsulated}. When @cite{standard} is used the code to elaborate and
18772 finalize the library is embedded, when @cite{encapsulated} is used the
18773 library can furthermore depend only on static libraries (including
18774 the GNAT runtime). This attribute can be set to @cite{no} to make it clear
18775 that the library should not be standalone in which case the
18776 @cite{Library_Interface} should not defined. Note that this attribute
18777 only applies to shared libraries, so @cite{Library_Kind} must be set
18778 to @cite{dynamic}.
18780 @example
18781 for Library_Dir use "lib";
18782 for Library_Name use "logging";
18783 for Library_Kind use "dynamic";
18784 for Library_Interface use ("lib1", "lib2");  --  unit names
18785 for Library_Standalone use "encapsulated";
18786 @end example
18787 @end quotation
18789 In order to include the elaboration code in the stand-alone library, the binder
18790 is invoked on the closure of the library units creating a package whose name
18791 depends on the library name (b~logging.ads/b in the example).
18792 This binder-generated package includes @strong{initialization} and @strong{finalization}
18793 procedures whose names depend on the library name (@cite{logginginit} and
18794 @cite{loggingfinal} in the example). The object corresponding to this package is
18795 included in the library.
18797 @geindex Library_Auto_Init (GNAT Project Manager)
18799 @strong{Library_Auto_Init}:
18801 @quotation
18803 A dynamic stand-alone Library is automatically initialized
18804 if automatic initialization of Stand-alone Libraries is supported on the
18805 platform and if attribute @strong{Library_Auto_Init} is not specified or
18806 is specified with the value "true". A static Stand-alone Library is never
18807 automatically initialized. Specifying "false" for this attribute
18808 prevents automatic initialization.
18810 When a non-automatically initialized stand-alone library is used in an
18811 executable, its initialization procedure must be called before any service of
18812 the library is used. When the main subprogram is in Ada, it may mean that the
18813 initialization procedure has to be called during elaboration of another
18814 package.
18815 @end quotation
18817 @geindex Library_Dir (GNAT Project Manager)
18819 @strong{Library_Dir}:
18821 @quotation
18823 For a stand-alone library, only the @code{ALI} files of the interface units
18824 (those that are listed in attribute @cite{Library_Interface}) are copied to
18825 the library directory. As a consequence, only the interface units may be
18826 imported from Ada units outside of the library. If other units are imported,
18827 the binding phase will fail.
18828 @end quotation
18830 @strong{Binder.Default_Switches}:
18832 @quotation
18834 When a stand-alone library is bound, the switches that are specified in
18835 the attribute @strong{Binder.Default_Switches ("Ada")} are
18836 used in the call to @emph{gnatbind}.
18837 @end quotation
18839 @geindex Library_Src_Dir (GNAT Project Manager)
18841 @strong{Library_Src_Dir}:
18843 @quotation
18845 This attribute defines the location (absolute or relative to the project
18846 directory) where the sources of the interface units are copied at
18847 installation time.
18848 These sources includes the specs of the interface units along with the
18849 closure of sources necessary to compile them successfully. That may include
18850 bodies and subunits, when pragmas @cite{Inline} are used, or when there are
18851 generic units in specs. This directory cannot point to the object directory
18852 or one of the source directories, but it can point to the library directory,
18853 which is the default value for this attribute.
18854 @end quotation
18856 @geindex Library_Symbol_Policy (GNAT Project Manager)
18858 @strong{Library_Symbol_Policy}:
18860 @quotation
18862 This attribute controls the export of symbols and, on some platforms (like
18863 VMS) that have the notions of major and minor IDs built in the library
18864 files, it controls the setting of these IDs. It is not supported on all
18865 platforms (where it will just have no effect). It may have one of the
18866 following values:
18869 @itemize *
18871 @item 
18872 @cite{"autonomous"} or @cite{"default"}: exported symbols are not controlled
18874 @item 
18875 @cite{"compliant"}: if attribute @strong{Library_Reference_Symbol_File}
18876 is not defined, then it is equivalent to policy "autonomous". If there
18877 are exported symbols in the reference symbol file that are not in the
18878 object files of the interfaces, the major ID of the library is increased.
18879 If there are symbols in the object files of the interfaces that are not
18880 in the reference symbol file, these symbols are put at the end of the list
18881 in the newly created symbol file and the minor ID is increased.
18883 @item 
18884 @cite{"controlled"}: the attribute @strong{Library_Reference_Symbol_File} must be
18885 defined. The library will fail to build if the exported symbols in the
18886 object files of the interfaces do not match exactly the symbol in the
18887 symbol file.
18889 @item 
18890 @cite{"restricted"}: The attribute @strong{Library_Symbol_File} must be defined.
18891 The library will fail to build if there are symbols in the symbol file that
18892 are not in the exported symbols of the object files of the interfaces.
18893 Additional symbols in the object files are not added to the symbol file.
18895 @item 
18896 @cite{"direct"}: The attribute @strong{Library_Symbol_File} must be defined and
18897 must designate an existing file in the object directory. This symbol file
18898 is passed directly to the underlying linker without any symbol processing.
18899 @end itemize
18900 @end quotation
18902 @geindex Library_Reference_Symbol_File (GNAT Project Manager)
18904 @strong{Library_Reference_Symbol_File}
18906 @quotation
18908 This attribute may define the path name of a reference symbol file that is
18909 read when the symbol policy is either "compliant" or "controlled", on
18910 platforms that support symbol control, such as VMS, when building a
18911 stand-alone library. The path may be an absolute path or a path relative
18912 to the project directory.
18913 @end quotation
18915 @geindex Library_Symbol_File (GNAT Project Manager)
18917 @strong{Library_Symbol_File}
18919 @quotation
18921 This attribute may define the name of the symbol file to be created when
18922 building a stand-alone library when the symbol policy is either "compliant",
18923 "controlled" or "restricted", on platforms that support symbol control,
18924 such as VMS. When symbol policy is "direct", then a file with this name
18925 must exist in the object directory.
18926 @end quotation
18928 @node Installing a library with project files,,Stand-alone Library Projects,Library Projects
18929 @anchor{gnat_ugn/gnat_project_manager installing-a-library-with-project-files}@anchor{8d}@anchor{gnat_ugn/gnat_project_manager id24}@anchor{17d}
18930 @subsection Installing a library with project files
18933 When using project files, a usable version of the library is created in the
18934 directory specified by the @cite{Library_Dir} attribute of the library
18935 project file. Thus no further action is needed in order to make use of
18936 the libraries that are built as part of the general application build.
18938 You may want to install a library in a context different from where the library
18939 is built. This situation arises with third party suppliers, who may want
18940 to distribute a library in binary form where the user is not expected to be
18941 able to recompile the library. The simplest option in this case is to provide
18942 a project file slightly different from the one used to build the library, by
18943 using the @cite{externally_built} attribute. See @ref{17b,,Using Library Projects}
18945 Another option is to use @emph{gprinstall} to install the library in a
18946 different context than the build location. @emph{gprinstall} automatically
18947 generates a project to use this library, and also copies the minimum set of
18948 sources needed to use the library to the install location.
18949 @ref{168,,Installation}
18951 @node Project Extension,Aggregate Projects,Library Projects,GNAT Project Manager
18952 @anchor{gnat_ugn/gnat_project_manager id25}@anchor{17e}@anchor{gnat_ugn/gnat_project_manager project-extension}@anchor{14f}
18953 @section Project Extension
18956 During development of a large system, it is sometimes necessary to use
18957 modified versions of some of the source files, without changing the original
18958 sources. This can be achieved through the @strong{project extension} facility.
18960 Suppose for instance that our example @cite{Build} project is built every night
18961 for the whole team, in some shared directory. A developer usually needs to work
18962 on a small part of the system, and might not want to have a copy of all the
18963 sources and all the object files (mostly because that would require too much
18964 disk space, time to recompile everything). He prefers to be able to override
18965 some of the source files in his directory, while taking advantage of all the
18966 object files generated at night.
18968 Another example can be taken from large software systems, where it is common to have
18969 multiple implementations of a common interface; in Ada terms, multiple
18970 versions of a package body for the same spec.  For example, one implementation
18971 might be safe for use in tasking programs, while another might be used only
18972 in sequential applications.  This can be modeled in GNAT using the concept
18973 of @emph{project extension}.  If one project (the 'child') @emph{extends}
18974 another project (the 'parent') then by default all source files of the
18975 parent project are inherited by the child, but the child project can
18976 override any of the parent's source files with new versions, and can also
18977 add new files or remove unnecessary ones.
18978 This facility is the project analog of a type extension in
18979 object-oriented programming.  Project hierarchies are permitted (an extending
18980 project may itself be extended), and a project that
18981 extends a project can also import other projects.
18983 A third example is that of using project extensions to provide different
18984 versions of the same system. For instance, assume that a @cite{Common}
18985 project is used by two development branches. One of the branches has now
18986 been frozen, and no further change can be done to it or to @cite{Common}.
18987 However, the other development branch still needs evolution of @cite{Common}.
18988 Project extensions provide a flexible solution to create a new version
18989 of a subsystem while sharing and reusing as much as possible from the original
18990 one.
18992 A project extension implicitly inherits all the sources and objects from the
18993 project it extends. It is possible to create a new version of some of the
18994 sources in one of the additional source directories of the extending
18995 project. Those new versions hide the original versions. Adding new sources or
18996 removing existing ones is also possible. Here is an example on how to extend
18997 the project @cite{Build} from previous examples:
18999 @example
19000 project Work extends "../bld/build.gpr" is
19001 end Work;
19002 @end example
19004 The project after @strong{extends} is the one being extended. As usual, it can be
19005 specified using an absolute path, or a path relative to any of the directories
19006 in the project path (see @ref{16c,,Project Dependencies}). This project does not
19007 specify source or object directories, so the default values for these
19008 attributes will be used that is to say the current directory (where project
19009 @cite{Work} is placed). We can compile that project with
19011 @example
19012 gprbuild -Pwork
19013 @end example
19015 If no sources have been placed in the current directory, this command
19016 won't do anything, since this project does not change the
19017 sources it inherited from @cite{Build}, therefore all the object files
19018 in @cite{Build} and its dependencies are still valid and are reused
19019 automatically.
19021 Suppose we now want to supply an alternate version of @code{pack.adb} but use
19022 the existing versions of @code{pack.ads} and @code{proc.adb}.  We can create
19023 the new file in Work's current directory (likely by copying the one from the
19024 @cite{Build} project and making changes to it. If new packages are needed at
19025 the same time, we simply create new files in the source directory of the
19026 extending project.
19028 When we recompile, @emph{gprbuild} will now automatically recompile
19029 this file (thus creating @code{pack.o} in the current directory) and
19030 any file that depends on it (thus creating @code{proc.o}). Finally, the
19031 executable is also linked locally.
19033 Note that we could have obtained the desired behavior using project import
19034 rather than project inheritance. A @cite{base} project would contain the
19035 sources for @code{pack.ads} and @code{proc.adb}, and @cite{Work} would
19036 import @cite{base} and add @code{pack.adb}. In this scenario,  @cite{base}
19037 cannot contain the original version of @code{pack.adb} otherwise there would be
19038 2 versions of the same unit in the closure of the project and this is not
19039 allowed. Generally speaking, it is not recommended to put the spec and the
19040 body of a unit in different projects since this affects their autonomy and
19041 reusability.
19043 In a project file that extends another project, it is possible to
19044 indicate that an inherited source is @strong{not part} of the sources of the
19045 extending project. This is necessary sometimes when a package spec has
19046 been overridden and no longer requires a body: in this case, it is
19047 necessary to indicate that the inherited body is not part of the sources
19048 of the project, otherwise there will be a compilation error
19049 when compiling the spec.
19051 @geindex Excluded_Source_Files (GNAT Project Manager)
19053 @geindex Excluded_Source_List_File (GNAT Project Manager)
19055 For that purpose, the attribute @strong{Excluded_Source_Files} is used.
19056 Its value is a list of file names.
19057 It is also possible to use attribute @cite{Excluded_Source_List_File}.
19058 Its value is the path of a text file containing one file name per
19059 line.
19061 @example
19062 project Work extends "../bld/build.gpr" is
19063    for Source_Files use ("pack.ads");
19064    --  New spec of Pkg does not need a completion
19065    for Excluded_Source_Files use ("pack.adb");
19066 end Work;
19067 @end example
19069 All packages that are not declared in the extending project are inherited from
19070 the project being extended, with their attributes, with the exception of
19071 @cite{Linker'Linker_Options} which is never inherited. In particular, an
19072 extending project retains all the switches specified in the project being
19073 extended.
19075 At the project level, if they are not declared in the extending project, some
19076 attributes are inherited from the project being extended. They are:
19077 @cite{Languages}, @cite{Main} (for a root non library project) and
19078 @cite{Library_Name} (for a project extending a library project).
19080 @menu
19081 * Project Hierarchy Extension:: 
19083 @end menu
19085 @node Project Hierarchy Extension,,,Project Extension
19086 @anchor{gnat_ugn/gnat_project_manager project-hierarchy-extension}@anchor{17f}@anchor{gnat_ugn/gnat_project_manager id26}@anchor{180}
19087 @subsection Project Hierarchy Extension
19090 One of the fundamental restrictions in project extension is the following:
19091 @strong{A project is not allowed to import directly or indirectly at the same time an extending project and one of its ancestors}.
19093 For example, consider the following hierarchy of projects.
19095 @example
19096 a.gpr  contains package A1
19097 b.gpr, imports a.gpr and contains B1, which depends on A1
19098 c.gpr, imports b.gpr and contains C1, which depends on B1
19099 @end example
19101 If we want to locally extend the packages @cite{A1} and @cite{C1}, we need to
19102 create several extending projects:
19104 @example
19105 a_ext.gpr which extends a.gpr, and overrides A1
19106 b_ext.gpr which extends b.gpr and imports a_ext.gpr
19107 c_ext.gpr which extends c.gpr, imports b_ext.gpr and overrides C1
19108 @end example
19110 @example
19111 project A_Ext extends "a.gpr" is
19112    for Source_Files use ("a1.adb", "a1.ads");
19113 end A_Ext;
19115 with "a_ext.gpr";
19116 project B_Ext extends "b.gpr" is
19117 end B_Ext;
19119 with "b_ext.gpr";
19120 project C_Ext extends "c.gpr" is
19121    for Source_Files use ("c1.adb");
19122 end C_Ext;
19123 @end example
19125 The extension @code{b_ext.gpr} is required, even though we are not overriding
19126 any of the sources of @code{b.gpr} because otherwise @code{c_expr.gpr} would
19127 import @code{b.gpr} which itself knows nothing about @code{a_ext.gpr}.
19129 @geindex extends all (GNAT Project Manager)
19131 When extending a large system spanning multiple projects, it is often
19132 inconvenient to extend every project in the hierarchy that is impacted by a
19133 small change introduced in a low layer. In such cases, it is possible to create
19134 an @strong{implicit extension} of an entire hierarchy using @strong{extends all}
19135 relationship.
19137 When the project is extended using @cite{extends all} inheritance, all projects
19138 that are imported by it, both directly and indirectly, are considered virtually
19139 extended. That is, the project manager creates implicit projects
19140 that extend every project in the hierarchy; all these implicit projects do not
19141 control sources on their own and use the object directory of
19142 the "extending all" project.
19144 It is possible to explicitly extend one or more projects in the hierarchy
19145 in order to modify the sources. These extending projects must be imported by
19146 the "extending all" project, which will replace the corresponding virtual
19147 projects with the explicit ones.
19149 When building such a project hierarchy extension, the project manager will
19150 ensure that both modified sources and sources in implicit extending projects
19151 that depend on them are recompiled.
19153 Thus, in our example we could create the following projects instead:
19155 @example
19156 a_ext.gpr, extends a.gpr and overrides A1
19157 c_ext.gpr, "extends all" c.gpr, imports a_ext.gpr and overrides C1
19158 @end example
19160 @example
19161 project A_Ext extends "a.gpr" is
19162    for Source_Files use ("a1.adb", "a1.ads");
19163 end A_Ext;
19165 with "a_ext.gpr";
19166 project C_Ext extends all "c.gpr" is
19167   for Source_Files use ("c1.adb");
19168 end C_Ext;
19169 @end example
19171 When building project @code{c_ext.gpr}, the entire modified project space is
19172 considered for recompilation, including the sources of @code{b.gpr} that are
19173 impacted by the changes in @cite{A1} and @cite{C1}.
19175 @node Aggregate Projects,Aggregate Library Projects,Project Extension,GNAT Project Manager
19176 @anchor{gnat_ugn/gnat_project_manager aggregate-projects}@anchor{16e}@anchor{gnat_ugn/gnat_project_manager id27}@anchor{181}
19177 @section Aggregate Projects
19180 Aggregate projects are an extension of the project paradigm, and are
19181 meant to solve a few specific use cases that cannot be solved directly
19182 using standard projects. This section will go over a few of these use
19183 cases to try to explain what you can use aggregate projects for.
19185 @menu
19186 * Building all main programs from a single project tree:: 
19187 * Building a set of projects with a single command:: 
19188 * Define a build environment:: 
19189 * Performance improvements in builder:: 
19190 * Syntax of aggregate projects:: 
19191 * package Builder in aggregate projects:: 
19193 @end menu
19195 @node Building all main programs from a single project tree,Building a set of projects with a single command,,Aggregate Projects
19196 @anchor{gnat_ugn/gnat_project_manager id28}@anchor{182}@anchor{gnat_ugn/gnat_project_manager building-all-main-programs-from-a-single-project-tree}@anchor{183}
19197 @subsection Building all main programs from a single project tree
19200 Most often, an application is organized into modules and submodules,
19201 which are very conveniently represented as a project tree or graph
19202 (the root project A @emph{with}s the projects for each modules (say B and C),
19203 which in turn @emph{with} projects for submodules.
19205 Very often, modules will build their own executables (for testing
19206 purposes for instance), or libraries (for easier reuse in various
19207 contexts).
19209 However, if you build your project through @emph{gnatmake} or
19210 @emph{gprbuild}, using a syntax similar to
19212 @example
19213 gprbuild -PA.gpr
19214 @end example
19216 this will only rebuild the main programs of project A, not those of the
19217 imported projects B and C. Therefore you have to spawn several
19218 @emph{gnatmake} commands, one per project, to build all executables.
19219 This is a little inconvenient, but more importantly is inefficient
19220 because @emph{gnatmake} needs to do duplicate work to ensure that sources are
19221 up-to-date, and cannot easily compile things in parallel when using
19222 the -j switch.
19224 Also libraries are always rebuilt when building a project.
19226 You could therefore define an aggregate project Agg that groups A, B
19227 and C. Then, when you build with
19229 @example
19230 gprbuild -PAgg.gpr
19231 @end example
19233 this will build all mains from A, B and C.
19235 @example
19236 aggregate project Agg is
19237    for Project_Files use ("a.gpr", "b.gpr", "c.gpr");
19238 end Agg;
19239 @end example
19241 If B or C do not define any main program (through their Main
19242 attribute), all their sources are built. When you do not group them
19243 in the aggregate project, only those sources that are needed by A
19244 will be built.
19246 If you add a main to a project P not already explicitly referenced in the
19247 aggregate project, you will need to add "p.gpr" in the list of project
19248 files for the aggregate project, or the main will not be built when
19249 building the aggregate project.
19251 Aggregate projects are supported only with @emph{gprbuild}, not with
19252 @emph{gnatmake}.
19254 @node Building a set of projects with a single command,Define a build environment,Building all main programs from a single project tree,Aggregate Projects
19255 @anchor{gnat_ugn/gnat_project_manager building-a-set-of-projects-with-a-single-command}@anchor{184}@anchor{gnat_ugn/gnat_project_manager id29}@anchor{185}
19256 @subsection Building a set of projects with a single command
19259 One other case is when you have multiple applications and libraries
19260 that are built independently from each other (but can be built in
19261 parallel). For instance, you have a project tree rooted at A, and
19262 another one (which might share some subprojects) rooted at B.
19264 Using only @emph{gprbuild}, you could do
19266 @example
19267 gprbuild -PA.gpr
19268 gprbuild -PB.gpr
19269 @end example
19271 to build both. But again, @emph{gprbuild} has to do some duplicate work for
19272 those files that are shared between the two, and cannot truly build
19273 things in parallel efficiently.
19275 If the two projects are really independent, share no sources other
19276 than through a common subproject, and have no source files with a
19277 common basename, you could create a project C that imports A and
19278 B. But these restrictions are often too strong, and one has to build
19279 them independently. An aggregate project does not have these
19280 limitations and can aggregate two project trees that have common
19281 sources.
19283 This scenario is particularly useful in environments like VxWorks 653
19284 where the applications running in the multiple partitions can be built
19285 in parallel through a single @emph{gprbuild} command. This also works nicely
19286 with Annex E.
19288 @node Define a build environment,Performance improvements in builder,Building a set of projects with a single command,Aggregate Projects
19289 @anchor{gnat_ugn/gnat_project_manager id30}@anchor{186}@anchor{gnat_ugn/gnat_project_manager define-a-build-environment}@anchor{187}
19290 @subsection Define a build environment
19293 The environment variables at the time you launch @emph{gprbuild}
19294 will influence the view these tools have of the project
19295 (PATH to find the compiler, ADA_PROJECT_PATH or GPR_PROJECT_PATH to find the
19296 projects, environment variables that are referenced in project files
19297 through the "external" built-in function, ...). Several command line switches
19298 can be used to override those (-X or -aP), but on some systems and
19299 with some projects, this might make the command line too long, and on
19300 all systems often make it hard to read.
19302 An aggregate project can be used to set the environment for all
19303 projects built through that aggregate. One of the nice aspects is that
19304 you can put the aggregate project under configuration management, and
19305 make sure all your user have a consistent environment when
19306 building. The syntax looks like
19308 @example
19309 aggregate project Agg is
19310    for Project_Files use ("A.gpr", "B.gpr");
19311    for Project_Path use ("../dir1", "../dir1/dir2");
19312    for External ("BUILD") use "PRODUCTION";
19314    package Builder is
19315       for Switches ("Ada") use ("-q");
19316    end Builder;
19317 end Agg;
19318 @end example
19320 One of the often requested features in projects is to be able to
19321 reference external variables in @emph{with} declarations, as in
19323 @example
19324 with external("SETUP") & "path/prj.gpr";   --  ILLEGAL
19325 project MyProject is
19326    ...
19327 end MyProject;
19328 @end example
19330 For various reasons, this is not allowed. But using aggregate projects provide
19331 an elegant solution. For instance, you could use a project file like:
19333 @example
19334 aggregate project Agg is
19335     for Project_Path use (external("SETUP") & "path");
19336     for Project_Files use ("myproject.gpr");
19337 end Agg;
19339 with "prj.gpr";  --  searched on Agg'Project_Path
19340 project MyProject is
19341    ...
19342 end MyProject;
19343 @end example
19345 @node Performance improvements in builder,Syntax of aggregate projects,Define a build environment,Aggregate Projects
19346 @anchor{gnat_ugn/gnat_project_manager performance-improvements-in-builder}@anchor{188}@anchor{gnat_ugn/gnat_project_manager id31}@anchor{189}
19347 @subsection Performance improvements in builder
19350 The loading of aggregate projects is optimized in @emph{gprbuild},
19351 so that all files are searched for only once on the disk
19352 (thus reducing the number of system calls and contributing to faster
19353 compilation times, especially on systems with sources on remote
19354 servers). As part of the loading, @emph{gprbuild}
19355 computes how and where a source file should be compiled, and even if it is
19356 found several times in the aggregated projects it will be compiled only
19357 once.
19359 Since there is no ambiguity as to which switches should be used, files
19360 can be compiled in parallel (through the usual -j switch) and this can
19361 be done while maximizing the use of CPUs (compared to launching
19362 multiple @emph{gprbuild} and @emph{gnatmake} commands in parallel).
19364 @node Syntax of aggregate projects,package Builder in aggregate projects,Performance improvements in builder,Aggregate Projects
19365 @anchor{gnat_ugn/gnat_project_manager id32}@anchor{18a}@anchor{gnat_ugn/gnat_project_manager syntax-of-aggregate-projects}@anchor{18b}
19366 @subsection Syntax of aggregate projects
19369 An aggregate project follows the general syntax of project files. The
19370 recommended extension is still @code{.gpr}. However, a special
19371 @cite{aggregate} qualifier must be put before the keyword
19372 @cite{project}.
19374 An aggregate project cannot @emph{with} any other project (standard or
19375 aggregate), except an abstract project which can be used to share attribute
19376 values. Also, aggregate projects cannot be extended or imported though a
19377 @emph{with} clause by any other project. Building other aggregate projects from
19378 an aggregate project is done through the Project_Files attribute (see below).
19380 An aggregate project does not have any source files directly (only
19381 through other standard projects). Therefore a number of the standard
19382 attributes and packages are forbidden in an aggregate project. Here is the
19383 (non exhaustive) list:
19386 @itemize *
19388 @item 
19389 Languages
19391 @item 
19392 Source_Files, Source_List_File and other attributes dealing with
19393 list of sources.
19395 @item 
19396 Source_Dirs, Exec_Dir and Object_Dir
19398 @item 
19399 Library_Dir, Library_Name and other library-related attributes
19401 @item 
19402 Main
19404 @item 
19405 Roots
19407 @item 
19408 Externally_Built
19410 @item 
19411 Inherit_Source_Path
19413 @item 
19414 Excluded_Source_Dirs
19416 @item 
19417 Locally_Removed_Files
19419 @item 
19420 Excluded_Source_Files
19422 @item 
19423 Excluded_Source_List_File
19425 @item 
19426 Interfaces
19427 @end itemize
19429 The only package that is authorized (albeit optional) is
19430 Builder. Other packages (in particular Compiler, Binder and Linker)
19431 are forbidden.
19433 The following three attributes can be used only in an aggregate project:
19435 @geindex Project_Files (GNAT Project Manager)
19437 @strong{Project_Files}:
19439 @quotation
19441 This attribute is compulsory (or else we are not aggregating any project,
19442 and thus not doing anything). It specifies a list of @code{.gpr} files
19443 that are grouped in the aggregate. The list may be empty. The project
19444 files can be either other aggregate projects, or standard projects. When
19445 grouping standard projects, you can have both the root of a project tree
19446 (and you do not need to specify all its imported projects), and any project
19447 within the tree.
19449 Basically, the idea is to specify all those projects that have
19450 main programs you want to build and link, or libraries you want to
19451 build. You can even specify projects that do not use the Main
19452 attribute nor the @cite{Library_*} attributes, and the result will be to
19453 build all their source files (not just the ones needed by other
19454 projects).
19456 The file can include paths (absolute or relative). Paths are relative to
19457 the location of the aggregate project file itself (if you use a base name,
19458 we expect to find the .gpr file in the same directory as the aggregate
19459 project file). The environment variables @cite{ADA_PROJECT_PATH},
19460 @cite{GPR_PROJECT_PATH} and @cite{GPR_PROJECT_PATH_FILE} are not used to find
19461 the project files. The extension @code{.gpr} is mandatory, since this attribute
19462 contains file names, not project names.
19464 Paths can also include the @cite{"*"} and @cite{"**"} globbing patterns. The
19465 latter indicates that any subdirectory (recursively) will be
19466 searched for matching files. The latter (@cite{"**"}) can only occur at the
19467 last position in the directory part (ie @cite{"a/**/*.gpr"} is supported, but
19468 not @cite{"**/a/*.gpr"}). Starting the pattern with @cite{"**"} is equivalent
19469 to starting with @cite{"./**"}.
19471 For now, the pattern @cite{"*"} is only allowed in the filename part, not
19472 in the directory part. This is mostly for efficiency reasons to limit the
19473 number of system calls that are needed.
19475 Here are a few valid examples:
19477 @example
19478 for Project_Files use ("a.gpr", "subdir/b.gpr");
19479 --  two specific projects relative to the directory of agg.gpr
19481 for Project_Files use ("/.gpr");
19482 --  all projects recursively
19483 @end example
19484 @end quotation
19486 @geindex Project_Path (GNAT Project Manager)
19488 @strong{Project_Path}:
19490 @quotation
19492 This attribute can be used to specify a list of directories in
19493 which to look for project files in @emph{with} declarations.
19495 When you specify a project in Project_Files (say @cite{x/y/a.gpr}), and
19496 @cite{a.gpr} imports a project @cite{b.gpr}, only @cite{b.gpr} is searched in
19497 the project path. @cite{a.gpr} must be exactly at
19498 @cite{<dir of the aggregate>/x/y/a.gpr}.
19500 This attribute, however, does not affect the search for the aggregated
19501 project files specified with @cite{Project_Files}.
19503 Each aggregate project has its own @cite{Project_Path} (that is if
19504 @cite{agg1.gpr} includes @cite{agg2.gpr}, they can potentially both have a
19505 different @cite{Project_Path}).
19507 This project path is defined as the concatenation, in that order, of:
19510 @itemize *
19512 @item 
19513 the current directory;
19515 @item 
19516 followed by the command line -aP switches;
19518 @item 
19519 then the directories from the GPR_PROJECT_PATH and ADA_PROJECT_PATH environment
19520 variables;
19522 @item 
19523 then the directories from the Project_Path attribute;
19525 @item 
19526 and finally the predefined directories.
19527 @end itemize
19529 In the example above, agg2.gpr's project path is not influenced by
19530 the attribute agg1'Project_Path, nor is agg1 influenced by
19531 agg2'Project_Path.
19533 This can potentially lead to errors. Consider the following example:
19535 @example
19537 --  +---------------+                  +----------------+
19538 --  | Agg1.gpr      |-=--includes--=-->| Agg2.gpr       |
19539 --  |  'project_path|                  |  'project_path |
19540 --  |               |                  |                |
19541 --  +---------------+                  +----------------+
19542 --        :                                   :
19543 --        includes                        includes
19544 --        :                                   :
19545 --        v                                   v
19546 --    +-------+                          +---------+
19547 --    | P.gpr |<---------- withs --------|  Q.gpr  |
19548 --    +-------+---------\                +---------+
19549 --        |             |
19550 --        withs         |
19551 --        |             |
19552 --        v             v
19553 --    +-------+      +---------+
19554 --    | R.gpr |      | R'.gpr  |
19555 --    +-------+      +---------+
19556 @end example
19558 When looking for p.gpr, both aggregates find the same physical file on
19559 the disk. However, it might happen that with their different project
19560 paths, both aggregate projects would in fact find a different r.gpr.
19561 Since we have a common project (p.gpr) "with"ing two different r.gpr,
19562 this will be reported as an error by the builder.
19564 Directories are relative to the location of the aggregate project file.
19566 Example:
19568 @example
19569 for Project_Path use ("/usr/local/gpr", "gpr/");
19570 @end example
19571 @end quotation
19573 @geindex External (GNAT Project Manager)
19575 @strong{External}:
19577 @quotation
19579 This attribute can be used to set the value of environment
19580 variables as retrieved through the @cite{external} function
19581 in projects. It does not affect the environment variables
19582 themselves (so for instance you cannot use it to change the value
19583 of your PATH as seen from the spawned compiler).
19585 This attribute affects the external values as seen in the rest of
19586 the aggregate project, and in the aggregated projects.
19588 The exact value of external a variable comes from one of three
19589 sources (each level overrides the previous levels):
19592 @itemize *
19594 @item 
19595 An External attribute in aggregate project, for instance
19596 @cite{for External ("BUILD_MODE") use "DEBUG"};
19598 @item 
19599 Environment variables.
19600 These override the value given by the attribute, so that
19601 users can override the value set in the (presumably shared
19602 with others team members) aggregate project.
19604 @item 
19605 The -X command line switch to @emph{gprbuild}.
19606 This always takes precedence.
19607 @end itemize
19609 This attribute is only taken into account in the main aggregate
19610 project (i.e. the one specified on the command line to @emph{gprbuild}),
19611 and ignored in other aggregate projects. It is invalid
19612 in standard projects.
19613 The goal is to have a consistent value in all
19614 projects that are built through the aggregate, which would not
19615 be the case in the diamond case: A groups the aggregate
19616 projects B and C, which both (either directly or indirectly)
19617 build the project P. If B and C could set different values for
19618 the environment variables, we would have two different views of
19619 P, which in particular might impact the list of source files in P.
19620 @end quotation
19622 @node package Builder in aggregate projects,,Syntax of aggregate projects,Aggregate Projects
19623 @anchor{gnat_ugn/gnat_project_manager package-builder-in-aggregate-projects}@anchor{18c}@anchor{gnat_ugn/gnat_project_manager id33}@anchor{18d}
19624 @subsection package Builder in aggregate projects
19627 As mentioned above, only the package Builder can be specified in
19628 an aggregate project. In this package, only the following attributes
19629 are valid:
19631 @geindex Switches (GNAT Project Manager)
19633 @strong{Switches}:
19635 @quotation
19637 This attribute gives the list of switches to use for @emph{gprbuild}.
19638 Because no mains can be specified for aggregate projects, the only possible
19639 index for attribute @cite{Switches} is @cite{others}. All other indexes will
19640 be ignored.
19642 Example:
19644 @example
19645 for Switches (others) use ("-v", "-k", "-j8");
19646 @end example
19648 These switches are only read from the main aggregate project (the
19649 one passed on the command line), and ignored in all other aggregate
19650 projects or projects.
19652 It can only contain builder switches, not compiler switches.
19653 @end quotation
19655 @geindex Global_Compilation_Switches (GNAT Project Manager)
19657 @strong{Global_Compilation_Switches}
19659 @quotation
19661 This attribute gives the list of compiler switches for the various
19662 languages. For instance,
19664 @example
19665 for Global_Compilation_Switches ("Ada") use ("O1", "-g");
19666 for Global_Compilation_Switches ("C")   use ("-O2");
19667 @end example
19669 This attribute is only taken into account in the aggregate project
19670 specified on the command line, not in other aggregate projects.
19672 In the projects grouped by that aggregate, the attribute
19673 Builder.Global_Compilation_Switches is also ignored. However, the
19674 attribute Compiler.Default_Switches will be taken into account (but
19675 that of the aggregate have higher priority). The attribute
19676 Compiler.Switches is also taken into account and can be used to
19677 override the switches for a specific file. As a result, it always
19678 has priority.
19680 The rules are meant to avoid ambiguities when compiling. For
19681 instance, aggregate project Agg groups the projects A and B, that
19682 both depend on C. Here is an extra for all of these projects:
19684 @example
19685 aggregate project Agg is
19686     for Project_Files use ("a.gpr", "b.gpr");
19687     package Builder is
19688        for Global_Compilation_Switches ("Ada") use ("-O2");
19689     end Builder;
19690 end Agg;
19692 with "c.gpr";
19693 project A is
19694     package Builder is
19695        for Global_Compilation_Switches ("Ada") use ("-O1");
19696        --  ignored
19697     end Builder;
19699     package Compiler is
19700        for Default_Switches ("Ada")
19701            use ("-O1", "-g");
19702        for Switches ("a_file1.adb")
19703            use ("-O0");
19704     end Compiler;
19705 end A;
19707 with "c.gpr";
19708 project B is
19709     package Compiler is
19710        for Default_Switches ("Ada") use ("-O0");
19711     end Compiler;
19712 end B;
19714 project C is
19715     package Compiler is
19716        for Default_Switches ("Ada")
19717            use ("-O3",
19718                 "-gnatn");
19719        for Switches ("c_file1.adb")
19720            use ("-O0", "-g");
19721     end Compiler;
19722 end C;
19723 @end example
19725 then the following switches are used:
19728 @itemize *
19730 @item 
19731 all files from project A except a_file1.adb are compiled
19732 with "-O2 -g", since the aggregate project has priority.
19734 @item 
19735 the file a_file1.adb is compiled with
19736 "-O0", since the Compiler.Switches has priority
19738 @item 
19739 all files from project B are compiled with
19740 "-O2", since the aggregate project has priority
19742 @item 
19743 all files from C are compiled with "-O2 -gnatn", except for
19744 c_file1.adb which is compiled with "-O0 -g"
19745 @end itemize
19747 Even though C is seen through two paths (through A and through
19748 B), the switches used by the compiler are unambiguous.
19749 @end quotation
19751 @geindex Global_Configuration_Pragmas (GNAT Project Manager)
19753 @strong{Global_Configuration_Pragmas}
19755 @quotation
19757 This attribute can be used to specify a file containing
19758 configuration pragmas, to be passed to the Ada compiler.  Since we
19759 ignore the package Builder in other aggregate projects and projects,
19760 only those pragmas defined in the main aggregate project will be
19761 taken into account.
19763 Projects can locally add to those by using the
19764 @cite{Compiler.Local_Configuration_Pragmas} attribute if they need.
19765 @end quotation
19767 @geindex Global_Config_File (GNAT Project Manager)
19769 @strong{Global_Config_File}
19771 @quotation
19773 This attribute, indexed with a language name, can be used to specify a config
19774 when compiling sources of the language. For Ada, these files are configuration
19775 pragmas files.
19776 @end quotation
19778 For projects that are built through the aggregate, the package Builder
19779 is ignored, except for the Executable attribute which specifies the
19780 name of the executables resulting from the link of the main programs, and
19781 for the Executable_Suffix.
19783 @node Aggregate Library Projects,Project File Reference,Aggregate Projects,GNAT Project Manager
19784 @anchor{gnat_ugn/gnat_project_manager id34}@anchor{18e}@anchor{gnat_ugn/gnat_project_manager aggregate-library-projects}@anchor{18f}
19785 @section Aggregate Library Projects
19788 Aggregate library projects make it possible to build a single library
19789 using object files built using other standard or library
19790 projects. This gives the flexibility to describe an application as
19791 having multiple modules (a GUI, database access, ...) using different
19792 project files (so possibly built with different compiler options) and
19793 yet create a single library (static or relocatable) out of the
19794 corresponding object files.
19796 @menu
19797 * Building aggregate library projects:: 
19798 * Syntax of aggregate library projects:: 
19800 @end menu
19802 @node Building aggregate library projects,Syntax of aggregate library projects,,Aggregate Library Projects
19803 @anchor{gnat_ugn/gnat_project_manager building-aggregate-library-projects}@anchor{190}@anchor{gnat_ugn/gnat_project_manager id35}@anchor{191}
19804 @subsection Building aggregate library projects
19807 For example, we can define an aggregate project Agg that groups A, B
19808 and C:
19810 @example
19811 aggregate library project Agg is
19812    for Project_Files use ("a.gpr", "b.gpr", "c.gpr");
19813    for Library_Name use ("agg");
19814    for Library_Dir use ("lagg");
19815 end Agg;
19816 @end example
19818 Then, when you build with:
19820 @example
19821 gprbuild agg.gpr
19822 @end example
19824 This will build all units from projects A, B and C and will create a
19825 static library named @code{libagg.a} in the @code{lagg}
19826 directory. An aggregate library project has the same set of
19827 restriction as a standard library project.
19829 Note that a shared aggregate library project cannot aggregate a
19830 static library project. In platforms where a compiler option is
19831 required to create relocatable object files, a Builder package in the
19832 aggregate library project may be used:
19834 @example
19835 aggregate library project Agg is
19836    for Project_Files use ("a.gpr", "b.gpr", "c.gpr");
19837    for Library_Name use ("agg");
19838    for Library_Dir use ("lagg");
19839    for Library_Kind use "relocatable";
19841    package Builder is
19842       for Global_Compilation_Switches ("Ada") use ("-fPIC");
19843    end Builder;
19844 end Agg;
19845 @end example
19847 With the above aggregate library Builder package, the @cite{-fPIC}
19848 option will be passed to the compiler when building any source code
19849 from projects @code{a.gpr}, @code{b.gpr} and @code{c.gpr}.
19851 @node Syntax of aggregate library projects,,Building aggregate library projects,Aggregate Library Projects
19852 @anchor{gnat_ugn/gnat_project_manager syntax-of-aggregate-library-projects}@anchor{192}@anchor{gnat_ugn/gnat_project_manager id36}@anchor{193}
19853 @subsection Syntax of aggregate library projects
19856 An aggregate library project follows the general syntax of project
19857 files. The recommended extension is still @code{.gpr}. However, a special
19858 @cite{aggregate library} qualifier must be put before the keyword
19859 @cite{project}.
19861 An aggregate library project cannot @emph{with} any other project
19862 (standard or aggregate), except an abstract project which can be used
19863 to share attribute values.
19865 An aggregate library project does not have any source files directly (only
19866 through other standard projects). Therefore a number of the standard
19867 attributes and packages are forbidden in an aggregate library
19868 project. Here is the (non exhaustive) list:
19871 @itemize *
19873 @item 
19874 Languages
19876 @item 
19877 Source_Files, Source_List_File and other attributes dealing with
19878 list of sources.
19880 @item 
19881 Source_Dirs, Exec_Dir and Object_Dir
19883 @item 
19884 Main
19886 @item 
19887 Roots
19889 @item 
19890 Externally_Built
19892 @item 
19893 Inherit_Source_Path
19895 @item 
19896 Excluded_Source_Dirs
19898 @item 
19899 Locally_Removed_Files
19901 @item 
19902 Excluded_Source_Files
19904 @item 
19905 Excluded_Source_List_File
19907 @item 
19908 Interfaces
19909 @end itemize
19911 The only package that is authorized (albeit optional) is Builder.
19913 The Project_Files attribute (See @ref{16e,,Aggregate Projects}) is used to
19914 described the aggregated projects whose object files have to be
19915 included into the aggregate library. The environment variables
19916 @cite{ADA_PROJECT_PATH}, @cite{GPR_PROJECT_PATH} and
19917 @cite{GPR_PROJECT_PATH_FILE} are not used to find the project files.
19919 @node Project File Reference,,Aggregate Library Projects,GNAT Project Manager
19920 @anchor{gnat_ugn/gnat_project_manager id37}@anchor{194}@anchor{gnat_ugn/gnat_project_manager project-file-reference}@anchor{14d}
19921 @section Project File Reference
19924 This section describes the syntactic structure of project files, the various
19925 constructs that can be used. Finally, it ends with a summary of all available
19926 attributes.
19928 @menu
19929 * Project Declaration:: 
19930 * Qualified Projects:: 
19931 * Declarations:: 
19932 * Packages:: 
19933 * Expressions:: 
19934 * External Values:: 
19935 * Typed String Declaration:: 
19936 * Variables:: 
19937 * Case Constructions:: 
19938 * Attributes:: 
19940 @end menu
19942 @node Project Declaration,Qualified Projects,,Project File Reference
19943 @anchor{gnat_ugn/gnat_project_manager id38}@anchor{195}@anchor{gnat_ugn/gnat_project_manager project-declaration}@anchor{196}
19944 @subsection Project Declaration
19947 Project files have an Ada-like syntax. The minimal project file is:
19949 @example
19950 project Empty is
19951 end Empty;
19952 @end example
19954 The identifier @cite{Empty} is the name of the project.
19955 This project name must be present after the reserved
19956 word @cite{end} at the end of the project file, followed by a semi-colon.
19958 @strong{Identifiers} (i.e., the user-defined names such as project or variable names)
19959 have the same syntax as Ada identifiers: they must start with a letter,
19960 and be followed by zero or more letters, digits or underscore characters;
19961 it is also illegal to have two underscores next to each other. Identifiers
19962 are always case-insensitive ("Name" is the same as "name").
19964 @example
19965 simple_name ::= identifier
19966 name        ::= simple_name @{ . simple_name @}
19967 @end example
19969 @strong{Strings} are used for values of attributes or as indexes for these
19970 attributes. They are in general case sensitive, except when noted
19971 otherwise (in particular, strings representing file names will be case
19972 insensitive on some systems, so that "file.adb" and "File.adb" both
19973 represent the same file).
19975 @strong{Reserved words} are the same as for standard Ada 95, and cannot
19976 be used for identifiers. In particular, the following words are currently
19977 used in project files, but others could be added later on. In bold are the
19978 extra reserved words in project files:
19979 @code{all}, @code{at}, @code{case}, @code{end}, @code{for}, @code{is}, @code{limited},
19980 @code{null}, @code{others}, @code{package}, @code{renames}, @code{type}, @code{use}, @code{when},
19981 @code{with}, @strong{extends}, @strong{external}, @strong{project}.
19983 @strong{Comments} in project files have the same syntax as in Ada, two consecutive
19984 hyphens through the end of the line.
19986 A project may be an @strong{independent project}, entirely defined by a single
19987 project file. Any source file in an independent project depends only
19988 on the predefined library and other source files in the same project.
19989 But a project may also depend on other projects, either by importing them
19990 through @strong{with clauses}, or by @strong{extending} at most one other project. Both
19991 types of dependency can be used in the same project.
19993 A path name denotes a project file. It can be absolute or relative.
19994 An absolute path name includes a sequence of directories, in the syntax of
19995 the host operating system, that identifies uniquely the project file in the
19996 file system. A relative path name identifies the project file, relative
19997 to the directory that contains the current project, or relative to a
19998 directory listed in the environment variables ADA_PROJECT_PATH and
19999 GPR_PROJECT_PATH. Path names are case sensitive if file names in the host
20000 operating system are case sensitive. As a special case, the directory
20001 separator can always be "/" even on Windows systems, so that project files
20002 can be made portable across architectures.
20003 The syntax of the environment variables ADA_PROJECT_PATH and
20004 GPR_PROJECT_PATH is a list of directory names separated by colons on UNIX and
20005 semicolons on Windows.
20007 A given project name can appear only once in a context clause.
20009 It is illegal for a project imported by a context clause to refer, directly
20010 or indirectly, to the project in which this context clause appears (the
20011 dependency graph cannot contain cycles), except when one of the with clauses
20012 in the cycle is a @strong{limited with}.
20014 @example
20015 with "other_project.gpr";
20016 project My_Project extends "extended.gpr" is
20017 end My_Project;
20018 @end example
20020 These dependencies form a @strong{directed graph}, potentially cyclic when using
20021 @strong{limited with}. The subgraph reflecting the @strong{extends} relations is a tree.
20023 A project's @strong{immediate sources} are the source files directly defined by
20024 that project, either implicitly by residing in the project source directories,
20025 or explicitly through any of the source-related attributes.
20026 More generally, a project's @strong{sources} are the immediate sources of the
20027 project together with the immediate sources (unless overridden) of any project
20028 on which it depends directly or indirectly.
20030 A @strong{project hierarchy} can be created, where projects are children of
20031 other projects. The name of such a child project must be @cite{Parent.Child},
20032 where @cite{Parent} is the name of the parent project. In particular, this
20033 makes all @emph{with} clauses of the parent project automatically visible
20034 in the child project.
20036 @example
20037 project        ::= context_clause project_declaration
20039 context_clause ::= @{with_clause@}
20040 with_clause    ::= *with* path_name @{ , path_name @} ;
20041 path_name      ::= string_literal
20043 project_declaration ::= simple_project_declaration | project_extension
20044 simple_project_declaration ::=
20045   project <project_>name is
20046     @{declarative_item@}
20047   end <project_>simple_name;
20048 @end example
20050 @node Qualified Projects,Declarations,Project Declaration,Project File Reference
20051 @anchor{gnat_ugn/gnat_project_manager qualified-projects}@anchor{173}@anchor{gnat_ugn/gnat_project_manager id39}@anchor{197}
20052 @subsection Qualified Projects
20055 Before the reserved @cite{project}, there may be one or two @strong{qualifiers}, that
20056 is identifiers or reserved words, to qualify the project.
20057 The current list of qualifiers is:
20060 @table @asis
20062 @item @strong{abstract}:
20064 Qualifies a project with no sources.
20065 Such a   project must either have no declaration of attributes @cite{Source_Dirs},
20066 @cite{Source_Files}, @cite{Languages} or @cite{Source_List_File}, or one of
20067 @cite{Source_Dirs}, @cite{Source_Files}, or @cite{Languages} must be declared
20068 as empty. If it extends another project, the project it extends must also be a
20069 qualified abstract project.
20071 @item @strong{standard}:
20073 A standard project is a non library project with sources.
20074 This is the default (implicit) qualifier.
20076 @item @strong{aggregate}:
20078 A project whose sources are aggregated from other project files.
20080 @item @strong{aggregate library}:
20082 A library whose sources are aggregated from other project
20083 or library project files.
20085 @item @strong{library}:
20087 A library project must declare both attributes
20088 Library_Name` and @cite{Library_Dir}.
20090 @item @strong{configuration}:
20092 A configuration project cannot be in a project tree.
20093 It describes compilers and other tools to @emph{gprbuild}.
20094 @end table
20096 @node Declarations,Packages,Qualified Projects,Project File Reference
20097 @anchor{gnat_ugn/gnat_project_manager declarations}@anchor{198}@anchor{gnat_ugn/gnat_project_manager id40}@anchor{199}
20098 @subsection Declarations
20101 Declarations introduce new entities that denote types, variables, attributes,
20102 and packages. Some declarations can only appear immediately within a project
20103 declaration. Others can appear within a project or within a package.
20105 @example
20106 declarative_item ::= simple_declarative_item
20107   | typed_string_declaration
20108   | package_declaration
20110 simple_declarative_item ::= variable_declaration
20111   | typed_variable_declaration
20112   | attribute_declaration
20113   | case_construction
20114   | empty_declaration
20116 empty_declaration ::= *null* ;
20117 @end example
20119 An empty declaration is allowed anywhere a declaration is allowed. It has
20120 no effect.
20122 @node Packages,Expressions,Declarations,Project File Reference
20123 @anchor{gnat_ugn/gnat_project_manager packages}@anchor{153}@anchor{gnat_ugn/gnat_project_manager id41}@anchor{19a}
20124 @subsection Packages
20127 A project file may contain @strong{packages}, that group attributes (typically
20128 all the attributes that are used by one of the GNAT tools).
20130 A package with a given name may only appear once in a project file.
20131 The following packages are currently supported in project files
20132 (See @ref{152,,Attributes} for the list of attributes that each can contain).
20135 @table @asis
20137 @item @emph{Binder}
20139 This package specifies characteristics useful when invoking the binder either
20140 directly via the @emph{gnat} driver or when using a builder such as
20141 @emph{gnatmake} or @emph{gprbuild}. See @ref{15d,,Main Subprograms}.
20143 @item @emph{Builder}
20145 This package specifies the compilation options used when building an
20146 executable or a library for a project. Most of the options should be
20147 set in one of @cite{Compiler}, @cite{Binder} or @cite{Linker} packages,
20148 but there are some general options that should be defined in this
20149 package. See @ref{15d,,Main Subprograms}, and @ref{162,,Executable File Names} in
20150 particular.
20151 @end table
20155 @table @asis
20157 @item @emph{Clean}
20159 This package specifies the options used when cleaning a project or a project
20160 tree using the tools @emph{gnatclean} or @emph{gprclean}.
20162 @item @emph{Compiler}
20164 This package specifies the compilation options used by the compiler for
20165 each languages. See @ref{15e,,Tools Options in Project Files}.
20167 @item @emph{Cross_Reference}
20169 This package specifies the options used when calling the library tool
20170 @emph{gnatxref} via the @emph{gnat} driver. Its attributes
20171 @strong{Default_Switches} and @strong{Switches} have the same semantics as for the
20172 package @cite{Builder}.
20173 @end table
20177 @table @asis
20179 @item @emph{Finder}
20181 This package specifies the options used when calling the search tool
20182 @emph{gnatfind} via the @emph{gnat} driver. Its attributes
20183 @strong{Default_Switches} and @strong{Switches} have the same semantics as for the
20184 package @cite{Builder}.
20186 @item @emph{Gnatls}
20188 This package specifies the options to use when invoking @emph{gnatls}
20189 via the @emph{gnat} driver.
20190 @end table
20194 @table @asis
20196 @item @emph{IDE}
20198 This package specifies the options used when starting an integrated
20199 development environment, for instance @emph{GPS} or @emph{Gnatbench}.
20201 @item @emph{Install}
20203 This package specifies the options used when installing a project
20204 with @emph{gprinstall}. See @ref{168,,Installation}.
20206 @item @emph{Linker}
20208 This package specifies the options used by the linker.
20209 See @ref{15d,,Main Subprograms}.
20210 @end table
20214 @table @asis
20216 @item @emph{Naming}
20218 @quotation
20220 This package specifies the naming conventions that apply
20221 to the source files in a project. In particular, these conventions are
20222 used to automatically find all source files in the source directories,
20223 or given a file name to find out its language for proper processing.
20224 See @ref{14b,,Naming Schemes}.
20225 @end quotation
20227 @c only: PRO or GPL
20228 @c 
20229 @c *Pretty_Printer*
20230 @c   This package specifies the options used when calling the formatting tool
20231 @c   *gnatpp* via the *gnat* driver. Its attributes
20232 @c   **Default_Switches** and **Switches** have the same semantics as for the
20233 @c   package `Builder`.
20235 @item @emph{Remote}
20237 This package is used by @emph{gprbuild} to describe how distributed
20238 compilation should be done.
20240 @item @emph{Stack}
20242 This package specifies the options used when calling the tool
20243 @emph{gnatstack} via the @emph{gnat} driver. Its attributes
20244 @strong{Default_Switches} and @strong{Switches} have the same semantics as for the
20245 package @cite{Builder}.
20247 @item @emph{Synchronize}
20249 This package specifies the options used when calling the tool
20250 @emph{gnatsync} via the @emph{gnat} driver.
20251 @end table
20253 In its simplest form, a package may be empty:
20255 @example
20256 project Simple is
20257   package Builder is
20258   end Builder;
20259 end Simple;
20260 @end example
20262 A package may contain @strong{attribute declarations},
20263 @strong{variable declarations} and @strong{case constructions}, as will be
20264 described below.
20266 When there is ambiguity between a project name and a package name,
20267 the name always designates the project. To avoid possible confusion, it is
20268 always a good idea to avoid naming a project with one of the
20269 names allowed for packages or any name that starts with @cite{gnat}.
20271 A package can also be defined by a @strong{renaming declaration}. The new package
20272 renames a package declared in a different project file, and has the same
20273 attributes as the package it renames. The name of the renamed package
20274 must be the same as the name of the renaming package. The project must
20275 contain a package declaration with this name, and the project
20276 must appear in the context clause of the current project, or be its parent
20277 project. It is not possible to add or override attributes to the renaming
20278 project. If you need to do so, you should use an @strong{extending declaration}
20279 (see below).
20281 Packages that are renamed in other project files often come from project files
20282 that have no sources: they are just used as templates. Any modification in the
20283 template will be reflected automatically in all the project files that rename
20284 a package from the template. This is a very common way to share settings
20285 between projects.
20287 Finally, a package can also be defined by an @strong{extending declaration}. This is
20288 similar to a @strong{renaming declaration}, except that it is possible to add or
20289 override attributes.
20291 @example
20292 package_declaration ::= package_spec | package_renaming | package_extension
20293 package_spec ::=
20294   package <package_>simple_name is
20295     @{simple_declarative_item@}
20296   end package_identifier ;
20297 package_renaming ::==
20298   package <package_>simple_name renames <project_>simple_name.package_identifier ;
20299 package_extension ::==
20300   package <package_>simple_name extends <project_>simple_name.package_identifier is
20301     @{simple_declarative_item@}
20302   end package_identifier ;
20303 @end example
20305 @node Expressions,External Values,Packages,Project File Reference
20306 @anchor{gnat_ugn/gnat_project_manager expressions}@anchor{19b}@anchor{gnat_ugn/gnat_project_manager id42}@anchor{19c}
20307 @subsection Expressions
20310 An expression is any value that can be assigned to an attribute or a
20311 variable. It is either a literal value, or a construct requiring runtime
20312 computation by the project manager. In a project file, the computed value of
20313 an expression is either a string or a list of strings.
20315 A string value is one of:
20318 @itemize *
20320 @item 
20321 A literal string, for instance @cite{"comm/my_proj.gpr"}
20323 @item 
20324 The name of a variable that evaluates to a string (see @ref{155,,Variables})
20326 @item 
20327 The name of an attribute that evaluates to a string (see @ref{152,,Attributes})
20329 @item 
20330 An external reference (see @ref{154,,External Values})
20332 @item 
20333 A concatenation of the above, as in @cite{"prefix_" & Var}.
20334 @end itemize
20336 A list of strings is one of the following:
20339 @itemize *
20341 @item 
20342 A parenthesized comma-separated list of zero or more string expressions, for
20343 instance @cite{(File_Name@comma{} "gnat.adc"@comma{} File_Name & ".orig")} or @cite{()}.
20345 @item 
20346 The name of a variable that evaluates to a list of strings
20348 @item 
20349 The name of an attribute that evaluates to a list of strings
20351 @item 
20352 A concatenation of a list of strings and a string (as defined above), for
20353 instance @cite{("A"@comma{} "B") & "C"}
20355 @item 
20356 A concatenation of two lists of strings
20357 @end itemize
20359 The following is the grammar for expressions
20361 @example
20362 string_literal ::= "@{string_element@}"  --  Same as Ada
20363 string_expression ::= string_literal
20364     | *variable_*name
20365     | external_value
20366     | attribute_reference
20367     | ( string_expression @{ & string_expression @} )
20368 string_list  ::= ( string_expression @{ , string_expression @} )
20369    | *string_variable*_name
20370    | *string_*attribute_reference
20371 term ::= string_expression | string_list
20372 expression ::= term @{ & term @}     --  Concatenation
20373 @end example
20375 Concatenation involves strings and list of strings. As soon as a list of
20376 strings is involved, the result of the concatenation is a list of strings. The
20377 following Ada declarations show the existing operators:
20379 @example
20380 function "&" (X : String;      Y : String)      return String;
20381 function "&" (X : String_List; Y : String)      return String_List;
20382 function "&" (X : String_List; Y : String_List) return String_List;
20383 @end example
20385 Here are some specific examples:
20387 @example
20388 List := () & File_Name; --  One string in this list
20389 List2 := List & (File_Name & ".orig"); -- Two strings
20390 Big_List := List & Lists2;  --  Three strings
20391 Illegal := "gnat.adc" & List2;  --  Illegal, must start with list
20392 @end example
20394 @node External Values,Typed String Declaration,Expressions,Project File Reference
20395 @anchor{gnat_ugn/gnat_project_manager external-values}@anchor{154}@anchor{gnat_ugn/gnat_project_manager id43}@anchor{19d}
20396 @subsection External Values
20399 An external value is an expression whose value is obtained from the command
20400 that invoked the processing of the current project file (typically a
20401 @emph{gnatmake} or @emph{gprbuild} command).
20403 There are two kinds of external values, one that returns a single string, and
20404 one that returns a string list.
20406 The syntax of a single string external value is:
20408 @example
20409 external_value ::= *external* ( string_literal [, string_literal] )
20410 @end example
20412 The first string_literal is the string to be used on the command line or
20413 in the environment to specify the external value. The second string_literal,
20414 if present, is the default to use if there is no specification for this
20415 external value either on the command line or in the environment.
20417 Typically, the external value will either exist in the
20418 environment variables
20419 or be specified on the command line through the
20420 @code{-X@emph{vbl}=@emph{value}} switch. If both
20421 are specified, then the command line value is used, so that a user can more
20422 easily override the value.
20424 The function @cite{external} always returns a string. It is an error if the
20425 value was not found in the environment and no default was specified in the
20426 call to @cite{external}.
20428 An external reference may be part of a string expression or of a string
20429 list expression, and can therefore appear in a variable declaration or
20430 an attribute declaration.
20432 Most of the time, this construct is used to initialize typed variables, which
20433 are then used in @strong{case} constructions to control the value assigned to
20434 attributes in various scenarios. Thus such variables are often called
20435 @strong{scenario variables}.
20437 The syntax for a string list external value is:
20439 @example
20440 external_value ::= *external_as_list* ( string_literal , string_literal )
20441 @end example
20443 The first string_literal is the string to be used on the command line or
20444 in the environment to specify the external value. The second string_literal is
20445 the separator between each component of the string list.
20447 If the external value does not exist in the environment or on the command line,
20448 the result is an empty list. This is also the case, if the separator is an
20449 empty string or if the external value is only one separator.
20451 Any separator at the beginning or at the end of the external value is
20452 discarded. Then, if there is no separator in the external value, the result is
20453 a string list with only one string. Otherwise, any string between the beginning
20454 and the first separator, between two consecutive separators and between the
20455 last separator and the end are components of the string list.
20457 @example
20458 *external_as_list* ("SWITCHES", ",")
20459 @end example
20461 If the external value is "-O2,-g",
20462 the result is ("-O2", "-g").
20464 If the external value is ",-O2,-g,",
20465 the result is also ("-O2", "-g").
20467 if the external value is "-gnatv",
20468 the result is ("-gnatv").
20470 If the external value is ",,", the result is ("").
20472 If the external value is ",", the result is (), the empty string list.
20474 @node Typed String Declaration,Variables,External Values,Project File Reference
20475 @anchor{gnat_ugn/gnat_project_manager id44}@anchor{19e}@anchor{gnat_ugn/gnat_project_manager typed-string-declaration}@anchor{19f}
20476 @subsection Typed String Declaration
20479 A @strong{type declaration} introduces a discrete set of string literals.
20480 If a string variable is declared to have this type, its value
20481 is restricted to the given set of literals. These are the only named
20482 types in project files. A string type may only be declared at the project
20483 level, not inside a package.
20485 @example
20486 typed_string_declaration ::=
20487   *type* *<typed_string_>*_simple_name *is* ( string_literal @{, string_literal@} );
20488 @end example
20490 The string literals in the list are case sensitive and must all be different.
20491 They may include any graphic characters allowed in Ada, including spaces.
20492 Here is an example of a string type declaration:
20494 @example
20495 type OS is ("NT", "nt", "Unix", "GNU/Linux", "other OS");
20496 @end example
20498 Variables of a string type are called @strong{typed variables}; all other
20499 variables are called @strong{untyped variables}. Typed variables are
20500 particularly useful in @cite{case} constructions, to support conditional
20501 attribute declarations. (See @ref{1a0,,Case Constructions}).
20503 A string type may be referenced by its name if it has been declared in the same
20504 project file, or by an expanded name whose prefix is the name of the project
20505 in which it is declared.
20507 @node Variables,Case Constructions,Typed String Declaration,Project File Reference
20508 @anchor{gnat_ugn/gnat_project_manager variables}@anchor{155}@anchor{gnat_ugn/gnat_project_manager id45}@anchor{1a1}
20509 @subsection Variables
20512 @strong{Variables} store values (strings or list of strings) and can appear
20513 as part of an expression. The declaration of a variable creates the
20514 variable and assigns the value of the expression to it. The name of the
20515 variable is available immediately after the assignment symbol, if you
20516 need to reuse its old value to compute the new value. Before the completion
20517 of its first declaration, the value of a variable defaults to the empty
20518 string ("").
20520 A @strong{typed} variable can be used as part of a @strong{case} expression to
20521 compute the value, but it can only be declared once in the project file,
20522 so that all case constructions see the same value for the variable. This
20523 provides more consistency and makes the project easier to understand.
20524 The syntax for its declaration is identical to the Ada syntax for an
20525 object declaration. In effect, a typed variable acts as a constant.
20527 An @strong{untyped} variable can be declared and overridden multiple times
20528 within the same project. It is declared implicitly through an Ada
20529 assignment. The first declaration establishes the kind of the variable
20530 (string or list of strings) and successive declarations must respect
20531 the initial kind. Assignments are executed in the order in which they
20532 appear, so the new value replaces the old one and any subsequent reference
20533 to the variable uses the new value.
20535 A variable may be declared at the project file level, or within a package.
20537 @example
20538 typed_variable_declaration ::=
20539   *<typed_variable_>*simple_name : *<typed_string_>*name := string_expression;
20541 variable_declaration ::= *<variable_>*simple_name := expression;
20542 @end example
20544 Here are some examples of variable declarations:
20546 @example
20547 This_OS : OS := external ("OS"); --  a typed variable declaration
20548 That_OS := "GNU/Linux";          --  an untyped variable declaration
20550 Name      := "readme.txt";
20551 Save_Name := Name & ".saved";
20553 Empty_List := ();
20554 List_With_One_Element := ("-gnaty");
20555 List_With_Two_Elements := List_With_One_Element & "-gnatg";
20556 Long_List := ("main.ada", "pack1_.ada", "pack1.ada", "pack2_.ada");
20557 @end example
20559 A @strong{variable reference} may take several forms:
20562 @itemize *
20564 @item 
20565 The simple variable name, for a variable in the current package (if any)
20566 or in the current project
20568 @item 
20569 An expanded name, whose prefix is a context name.
20570 @end itemize
20572 A @strong{context} may be one of the following:
20575 @itemize *
20577 @item 
20578 The name of an existing package in the current project
20580 @item 
20581 The name of an imported project of the current project
20583 @item 
20584 The name of an ancestor project (i.e., a project extended by the current
20585 project, either directly or indirectly)
20587 @item 
20588 An expanded name whose prefix is an imported/parent project name, and
20589 whose selector is a package name in that project.
20590 @end itemize
20592 @node Case Constructions,Attributes,Variables,Project File Reference
20593 @anchor{gnat_ugn/gnat_project_manager id46}@anchor{1a2}@anchor{gnat_ugn/gnat_project_manager case-constructions}@anchor{1a0}
20594 @subsection Case Constructions
20597 A @strong{case} construction is used in a project file to effect conditional
20598 behavior. Through this construction, you can set the value of attributes
20599 and variables depending on the value previously assigned to a typed
20600 variable.
20602 All choices in a choice list must be distinct. Unlike Ada, the choice
20603 lists of all alternatives do not need to include all values of the type.
20604 An @cite{others} choice must appear last in the list of alternatives.
20606 The syntax of a @cite{case} construction is based on the Ada case construction
20607 (although the @cite{null} declaration for empty alternatives is optional).
20609 The case expression must be a string variable, either typed or not, whose value
20610 is often given by an external reference (see @ref{154,,External Values}).
20612 Each alternative starts with the reserved word @cite{when}, either a list of
20613 literal strings separated by the @cite{"|"} character or the reserved word
20614 @cite{others}, and the @cite{"=>"} token.
20615 When the case expression is a typed string variable, each literal string must
20616 belong to the string type that is the type of the case variable.
20617 After each @cite{=>}, there are zero or more declarations.  The only
20618 declarations allowed in a case construction are other case constructions,
20619 attribute declarations and variable declarations. String type declarations and
20620 package declarations are not allowed. Variable declarations are restricted to
20621 variables that have already been declared before the case construction.
20623 @example
20624 case_construction ::=
20625   *case* *<variable_>*name *is* @{case_item@} *end case* ;
20627 case_item ::=
20628   *when* discrete_choice_list =>
20629     @{case_declaration
20630       | attribute_declaration
20631       | variable_declaration
20632       | empty_declaration@}
20634 discrete_choice_list ::= string_literal @{| string_literal@} | *others*
20635 @end example
20637 Here is a typical example, with a typed string variable:
20639 @example
20640 project MyProj is
20641    type OS_Type is ("GNU/Linux", "Unix", "NT", "VMS");
20642    OS : OS_Type := external ("OS", "GNU/Linux");
20644    package Compiler is
20645      case OS is
20646        when "GNU/Linux" | "Unix" =>
20647          for Switches ("Ada")
20648              use ("-gnath");
20649        when "NT" =>
20650          for Switches ("Ada")
20651              use ("-gnatP");
20652        when others =>
20653          null;
20654      end case;
20655    end Compiler;
20656 end MyProj;
20657 @end example
20659 @node Attributes,,Case Constructions,Project File Reference
20660 @anchor{gnat_ugn/gnat_project_manager id47}@anchor{1a3}@anchor{gnat_ugn/gnat_project_manager attributes}@anchor{152}
20661 @subsection Attributes
20664 A project (and its packages) may have @strong{attributes} that define
20665 the project's properties.  Some attributes have values that are strings;
20666 others have values that are string lists.
20668 @example
20669 attribute_declaration ::=
20670    simple_attribute_declaration | indexed_attribute_declaration
20672 simple_attribute_declaration ::= *for* attribute_designator *use* expression ;
20674 indexed_attribute_declaration ::=
20675   *for* *<indexed_attribute_>*simple_name ( string_literal) *use* expression ;
20677 attribute_designator ::=
20678   *<simple_attribute_>*simple_name
20679   | *<indexed_attribute_>*simple_name ( string_literal )
20680 @end example
20682 There are two categories of attributes: @strong{simple attributes}
20683 and @strong{indexed attributes}.
20684 Each simple attribute has a default value: the empty string (for string
20685 attributes) and the empty list (for string list attributes).
20686 An attribute declaration defines a new value for an attribute, and overrides
20687 the previous value. The syntax of a simple attribute declaration is similar to
20688 that of an attribute definition clause in Ada.
20690 Some attributes are indexed. These attributes are mappings whose
20691 domain is a set of strings. They are declared one association
20692 at a time, by specifying a point in the domain and the corresponding image
20693 of the attribute.
20694 Like untyped variables and simple attributes, indexed attributes
20695 may be declared several times. Each declaration supplies a new value for the
20696 attribute, and replaces the previous setting.
20698 Here are some examples of attribute declarations:
20700 @example
20701 --  simple attributes
20702 for Object_Dir use "objects";
20703 for Source_Dirs use ("units", "test/drivers");
20705 --  indexed attributes
20706 for Body ("main") use "Main.ada";
20707 for Switches ("main.ada")
20708     use ("-v", "-gnatv");
20709 for Switches ("main.ada") use Builder'Switches ("main.ada") & "-g";
20711 --  indexed attributes copy (from package Builder in project Default)
20712 --  The package name must always be specified, even if it is the current
20713 --  package.
20714 for Default_Switches use Default.Builder'Default_Switches;
20715 @end example
20717 Attributes references may appear anywhere in expressions, and are used
20718 to retrieve the value previously assigned to the attribute. If an attribute
20719 has not been set in a given package or project, its value defaults to the
20720 empty string or the empty list, with some exceptions.
20722 @example
20723 attribute_reference ::=
20724   attribute_prefix ' *<simple_attribute>_*simple_name [ (string_literal) ]
20725 attribute_prefix ::= *project*
20726   | *<project_>*simple_name
20727   | package_identifier
20728   | *<project_>*simple_name . package_identifier
20729 @end example
20731 Examples are:
20733 @example
20734 <project>'Object_Dir
20735 Naming'Dot_Replacement
20736 Imported_Project'Source_Dirs
20737 Imported_Project.Naming'Casing
20738 Builder'Default_Switches ("Ada")
20739 @end example
20741 The exceptions to the empty defaults are:
20744 @itemize *
20746 @item 
20747 Object_Dir: default is "."
20749 @item 
20750 Exec_Dir: default is 'Object_Dir, that is the value of attribute
20751 Object_Dir in the same project, declared or defaulted.
20753 @item 
20754 Source_Dirs: default is (".")
20755 @end itemize
20757 The prefix of an attribute may be:
20760 @itemize *
20762 @item 
20763 @cite{project} for an attribute of the current project
20765 @item 
20766 The name of an existing package of the current project
20768 @item 
20769 The name of an imported project
20771 @item 
20772 The name of a parent project that is extended by the current project
20774 @item 
20775 An expanded name whose prefix is imported/parent project name,
20776 and whose selector is a package name
20777 @end itemize
20779 In the following sections, all predefined attributes are succinctly described,
20780 first the project level attributes, that is those attributes that are not in a
20781 package, then the attributes in the different packages.
20783 It is possible for different tools to dynamically create new packages with
20784 attributes, or new attributes in predefined packages. These attributes are
20785 not documented here.
20787 The attributes under Configuration headings are usually found only in
20788 configuration project files.
20790 The characteristics of each attribute are indicated as follows:
20793 @itemize *
20795 @item 
20796 @strong{Type of value}
20798 The value of an attribute may be a single string, indicated by the word
20799 "single", or a string list, indicated by the word "list".
20801 @item 
20802 @strong{Read-only}
20804 When the attribute is read-only, that is when it is not allowed to declare
20805 the attribute, this is indicated by the words "read-only".
20807 @item 
20808 @strong{Optional index}
20810 If it is allowed in the value of the attribute (both single and list) to have
20811 an optional index, this is indicated by the words "optional index".
20813 @item 
20814 @strong{Indexed attribute}
20816 When it is an indexed attribute, this is indicated by the word "indexed".
20818 @item 
20819 @strong{Case-sensitivity of the index}
20821 For an indexed attribute, if the index is case-insensitive, this is indicated
20822 by the words "case-insensitive index".
20824 @item 
20825 @strong{File name index}
20827 For an indexed attribute, when the index is a file name, this is indicated by
20828 the words "file name index". The index may or may not be case-sensitive,
20829 depending on the platform.
20831 @item 
20832 @strong{others allowed in index}
20834 For an indexed attribute, if it is allowed to use @strong{others} as the index,
20835 this is indicated by the words "others allowed".
20837 When @strong{others} is used as the index of an indexed attribute, the value of
20838 the attribute indexed by @strong{others} is used when no other index would apply.
20839 @end itemize
20841 @menu
20842 * Project Level Attributes:: 
20843 * Package Binder Attributes:: 
20844 * Package Builder Attributes:: 
20845 * Package Clean Attributes:: 
20846 * Package Compiler Attributes:: 
20847 * Package Cross_Reference Attributes:: 
20848 * Package Finder Attributes:: 
20849 * Package gnatls Attributes:: 
20850 * Package IDE Attributes:: 
20851 * Package Install Attributes:: 
20852 * Package Linker Attributes:: 
20853 * Package Naming Attributes:: 
20854 * Package Remote Attributes:: 
20855 * Package Stack Attributes:: 
20856 * Package Synchronize Attributes:: 
20858 @end menu
20860 @node Project Level Attributes,Package Binder Attributes,,Attributes
20861 @anchor{gnat_ugn/gnat_project_manager project-level-attributes}@anchor{1a4}@anchor{gnat_ugn/gnat_project_manager id48}@anchor{1a5}
20862 @subsubsection Project Level Attributes
20866 @itemize *
20868 @item 
20869 @strong{General}
20872 @itemize *
20874 @item 
20875 @strong{Name}: single, read-only
20877 The name of the project.
20879 @item 
20880 @strong{Project_Dir}: single, read-only
20882 The path name of the project directory.
20884 @item 
20885 @strong{Main}: list, optional index
20887 The list of main sources for the executables.
20889 @item 
20890 @strong{Languages}: list
20892 The list of languages of the sources of the project.
20894 @item 
20895 @strong{Roots}: list, indexed, file name index
20897 The index is the file name of an executable source. Indicates the list of units
20898 from the main project that need to be bound and linked with their closures
20899 with the executable. The index is either a file name, a language name or "*".
20900 The roots for an executable source are those in @strong{Roots} with an index that
20901 is the executable source file name, if declared. Otherwise, they are those in
20902 @strong{Roots} with an index that is the language name of the executable source,
20903 if present. Otherwise, they are those in @strong{Roots ("*")}, if declared. If none
20904 of these three possibilities are declared, then there are no roots for the
20905 executable source.
20907 @item 
20908 @strong{Externally_Built}: single
20910 Indicates if the project is externally built.
20911 Only case-insensitive values allowed are "true" and "false", the default.
20912 @end itemize
20914 @item 
20915 @strong{Directories}
20918 @itemize *
20920 @item 
20921 @strong{Object_Dir}: single
20923 Indicates the object directory for the project.
20925 @item 
20926 @strong{Exec_Dir}: single
20928 Indicates the exec directory for the project, that is the directory where the
20929 executables are.
20931 @item 
20932 @strong{Source_Dirs}: list
20934 The list of source directories of the project.
20936 @item 
20937 @strong{Inherit_Source_Path}: list, indexed, case-insensitive index
20939 Index is a language name. Value is a list of language names. Indicates that
20940 in the source search path of the index language the source directories of
20941 the languages in the list should be included.
20943 Example:
20945 @example
20946 for Inherit_Source_Path ("C++") use ("C");
20947 @end example
20949 @item 
20950 @strong{Exclude_Source_Dirs}: list
20952 The list of directories that are included in Source_Dirs but are not source
20953 directories of the project.
20955 @item 
20956 @strong{Ignore_Source_Sub_Dirs}: list
20958 Value is a list of simple names for subdirectories that are removed from the
20959 list of source directories, including theur subdirectories.
20960 @end itemize
20962 @item 
20963 @strong{Source Files}
20966 @itemize *
20968 @item 
20969 @strong{Source_Files}: list
20971 Value is a list of source file simple names.
20973 @item 
20974 @strong{Locally_Removed_Files}: list
20976 Obsolescent. Equivalent to Excluded_Source_Files.
20978 @item 
20979 @strong{Excluded_Source_Files}: list
20981 Value is a list of simple file names that are not sources of the project.
20982 Allows to remove sources that are inherited or found in the source directories
20983 and that match the naming scheme.
20985 @item 
20986 @strong{Source_List_File}: single
20988 Value is a text file name that contains a list of source file simple names,
20989 one on each line.
20991 @item 
20992 @strong{Excluded_Source_List_File}: single
20994 Value is a text file name that contains a list of file simple names that
20995 are not sources of the project.
20997 @item 
20998 @strong{Interfaces}: list
21000 Value is a list of file names that constitutes the interfaces of the project.
21001 @end itemize
21003 @item 
21004 @strong{Aggregate Projects}
21007 @itemize *
21009 @item 
21010 @strong{Project_Files}: list
21012 Value is the list of aggregated projects.
21014 @item 
21015 @strong{Project_Path}: list
21017 Value is a list of directories that are added to the project search path when
21018 looking for the aggregated projects.
21020 @item 
21021 @strong{External}: single, indexed
21023 Index is the name of an external reference. Value is the value of the
21024 external reference to be used when parsing the aggregated projects.
21025 @end itemize
21027 @item 
21028 @strong{Libraries}
21031 @itemize *
21033 @item 
21034 @strong{Library_Dir}: single
21036 Value is the name of the library directory. This attribute needs to be
21037 declared for each library project.
21039 @item 
21040 @strong{Library_Name}: single
21042 Value is the name of the library. This attribute needs to be declared or
21043 inherited for each library project.
21045 @item 
21046 @strong{Library_Kind}: single
21048 Specifies the kind of library: static library (archive) or shared library.
21049 Case-insensitive values must be one of "static" for archives (the default) or
21050 "dynamic" or "relocatable" for shared libraries.
21052 @item 
21053 @strong{Library_Version}: single
21055 Value is the name of the library file.
21057 @item 
21058 @strong{Library_Interface}: list
21060 Value is the list of unit names that constitutes the interfaces
21061 of a Stand-Alone Library project.
21063 @item 
21064 @strong{Library_Standalone}: single
21066 Specifies if a Stand-Alone Library (SAL) is encapsulated or not.
21067 Only authorized case-insensitive values are "standard" for non encapsulated
21068 SALs, "encapsulated" for encapsulated SALs or "no" for non SAL library project.
21070 @item 
21071 @strong{Library_Encapsulated_Options}: list
21073 Value is a list of options that need to be used when linking an encapsulated
21074 Stand-Alone Library.
21076 @item 
21077 @strong{Library_Encapsulated_Supported}: single
21079 Indicates if encapsulated Stand-Alone Libraries are supported. Only
21080 authorized case-insensitive values are "true" and "false" (the default).
21082 @item 
21083 @strong{Library_Auto_Init}: single
21085 Indicates if a Stand-Alone Library is auto-initialized. Only authorized
21086 case-insentive values are "true" and "false".
21088 @item 
21089 @strong{Leading_Library_Options}: list
21091 Value is a list of options that are to be used at the beginning of
21092 the command line when linking a shared library.
21094 @item 
21095 @strong{Library_Options}: list
21097 Value is a list of options that are to be used when linking a shared library.
21099 @item 
21100 @strong{Library_Rpath_Options}: list, indexed, case-insensitive index
21102 Index is a language name. Value is a list of options for an invocation of the
21103 compiler of the language. This invocation is done for a shared library project
21104 with sources of the language. The output of the invocation is the path name
21105 of a shared library file. The directory name is to be put in the run path
21106 option switch when linking the shared library for the project.
21108 @item 
21109 @strong{Library_Src_Dir}: single
21111 Value is the name of the directory where copies of the sources of the
21112 interfaces of a Stand-Alone Library are to be copied.
21114 @item 
21115 @strong{Library_ALI_Dir}: single
21117 Value is the name of the directory where the ALI files of the interfaces
21118 of a Stand-Alone Library are to be copied. When this attribute is not declared,
21119 the directory is the library directory.
21121 @item 
21122 @strong{Library_gcc}: single
21124 Obsolescent attribute. Specify the linker driver used to link a shared library.
21125 Use instead attribute Linker'Driver.
21127 @item 
21128 @strong{Library_Symbol_File}: single
21130 Value is the name of the library symbol file.
21132 @item 
21133 @strong{Library_Symbol_Policy}: single
21135 Indicates the symbol policy kind. Only authorized case-insensitive values are
21136 "autonomous", "default", "compliant", "controlled" or "direct".
21138 @item 
21139 @strong{Library_Reference_Symbol_File}: single
21141 Value is the name of the reference symbol file.
21142 @end itemize
21144 @item 
21145 @strong{Configuration - General}
21148 @itemize *
21150 @item 
21151 @strong{Default_Language}: single
21153 Value is the case-insensitive name of the language of a project when attribute
21154 Languages is not specified.
21156 @item 
21157 @strong{Run_Path_Option}: list
21159 Value is the list of switches to be used when specifying the run path option
21160 in an executable.
21162 @item 
21163 @strong{Run_Path_Origin}: single
21165 Value is the the string that may replace the path name of the executable
21166 directory in the run path options.
21168 @item 
21169 @strong{Separate_Run_Path_Options}: single
21171 Indicates if there may be several run path options specified when linking an
21172 executable. Only authorized case-insensitive values are "true" or "false" (the
21173 default).
21175 @item 
21176 @strong{Toolchain_Version}: single, indexed, case-insensitive index
21178 Index is a language name. Specify the version of a toolchain for a language.
21180 @item 
21181 @strong{Toolchain_Description}: single, indexed, case-insensitive index
21183 Obsolescent. No longer used.
21185 @item 
21186 @strong{Object_Generated}: single, indexed, case-insensitive index
21188 Index is a language name. Indicates if invoking the compiler for a language
21189 produces an object file. Only authorized case-insensitive values are "false"
21190 and "true" (the default).
21192 @item 
21193 @strong{Objects_Linked}: single, indexed, case-insensitive index
21195 Index is a language name. Indicates if the object files created by the compiler
21196 for a language need to be linked in the executable. Only authorized
21197 case-insensitive values are "false" and "true" (the default).
21199 @item 
21200 @strong{Target}: single
21202 Value is the name of the target platform. Taken into account only in the main
21203 project.
21205 @item 
21206 @strong{Runtime}: single, indexed, case-insensitive index
21208 Index is a language name. Indicates the runtime directory that is to be used
21209 when using the compiler of the language. Taken into account only in the main
21210 project.
21211 @end itemize
21213 @item 
21214 @strong{Configuration - Libraries}
21217 @itemize *
21219 @item 
21220 @strong{Library_Builder}: single
21222 Value is the path name of the application that is to be used to build
21223 libraries. Usually the path name of "gprlib".
21225 @item 
21226 @strong{Library_Support}: single
21228 Indicates the level of support of libraries. Only authorized case-insensitive
21229 values are "static_only", "full" or "none" (the default).
21230 @end itemize
21232 @item 
21233 @strong{Configuration - Archives}
21236 @itemize *
21238 @item 
21239 @strong{Archive_Builder}: list
21241 Value is the name of the application to be used to create a static library
21242 (archive), followed by the options to be used.
21244 @item 
21245 @strong{Archive_Builder_Append_Option}: list
21247 Value is the list of options to be used when invoking the archive builder
21248 to add project files into an archive.
21250 @item 
21251 @strong{Archive_Indexer}: list
21253 Value is the name of the archive indexer, followed by the required options.
21255 @item 
21256 @strong{Archive_Suffix}: single
21258 Value is the extension of archives. When not declared, the extension is ".a".
21260 @item 
21261 @strong{Library_Partial_Linker}: list
21263 Value is the name of the partial linker executable, followed by the required
21264 options.
21265 @end itemize
21267 @item 
21268 @strong{Configuration - Shared Libraries}
21271 @itemize *
21273 @item 
21274 @strong{Shared_Library_Prefix}: single
21276 Value is the prefix in the name of shared library files. When not declared,
21277 the prefix is "lib".
21279 @item 
21280 @strong{Shared_Library_Suffix}: single
21282 Value is the the extension of the name of shared library files. When not
21283 declared, the extension is ".so".
21285 @item 
21286 @strong{Symbolic_Link_Supported}: single
21288 Indicates if symbolic links are supported on the platform. Only authorized
21289 case-insensitive values are "true" and "false" (the default).
21291 @item 
21292 @strong{Library_Major_Minor_Id_Supported}: single
21294 Indicates if major and minor ids for shared library names are supported on
21295 the platform. Only authorized case-insensitive values are "true" and "false"
21296 (the default).
21298 @item 
21299 @strong{Library_Auto_Init_Supported}: single
21301 Indicates if auto-initialization of Stand-Alone Libraries is supported. Only
21302 authorized case-insensitive values are "true" and "false" (the default).
21304 @item 
21305 @strong{Shared_Library_Minimum_Switches}: list
21307 Value is the list of required switches when linking a shared library.
21309 @item 
21310 @strong{Library_Version_Switches}: list
21312 Value is the list of switches to specify a internal name for a shared library.
21314 @item 
21315 @strong{Library_Install_Name_Option}: single
21317 Value is the name of the option that needs to be used, concatenated with the
21318 path name of the library file, when linking a shared library.
21320 @item 
21321 @strong{Runtime_Library_Dir}: single, indexed, case-insensitive index
21323 Index is a language name. Value is the path name of the directory where the
21324 runtime libraries are located.
21326 @item 
21327 @strong{Runtime_Source_Dir}: single, indexed, case-insensitive index
21329 Index is a language name. Value is the path name of the directory where the
21330 sources of runtime libraries are located.
21331 @end itemize
21332 @end itemize
21334 @node Package Binder Attributes,Package Builder Attributes,Project Level Attributes,Attributes
21335 @anchor{gnat_ugn/gnat_project_manager package-binder-attributes}@anchor{1a6}@anchor{gnat_ugn/gnat_project_manager id49}@anchor{1a7}
21336 @subsubsection Package Binder Attributes
21340 @itemize *
21342 @item 
21343 @strong{General}
21346 @itemize *
21348 @item 
21349 @strong{Default_Switches}: list, indexed, case-insensitive index
21351 Index is a language name. Value is the list of switches to be used when binding
21352 code of the language, if there is no applicable attribute Switches.
21354 @item 
21355 @strong{Switches}: list, optional index, indexed,
21356 case-insensitive index, others allowed
21358 Index is either a language name or a source file name. Value is the list of
21359 switches to be used when binding code. Index is either the source file name
21360 of the executable to be bound or the language name of the code to be bound.
21361 @end itemize
21363 @item 
21364 @strong{Configuration - Binding}
21367 @itemize *
21369 @item 
21370 @strong{Driver}: single, indexed, case-insensitive index
21372 Index is a language name. Value is the name of the application to be used when
21373 binding code of the language.
21375 @item 
21376 @strong{Required_Switches}: list, indexed, case-insensitive index
21378 Index is a language name. Value is the list of the required switches to be
21379 used when binding code of the language.
21381 @item 
21382 @strong{Prefix}: single, indexed, case-insensitive index
21384 Index is a language name. Value is a prefix to be used for the binder exchange
21385 file name for the language. Used to have different binder exchange file names
21386 when binding different languages.
21388 @item 
21389 @strong{Objects_Path}: single,indexed, case-insensitive index
21391 Index is a language name. Value is the name of the environment variable that
21392 contains the path for the object directories.
21394 @item 
21395 @strong{Object_Path_File}: single,indexed, case-insensitive index
21397 Index is a language name. Value is the name of the environment variable. The
21398 value of the environment variable is the path name of a text file that
21399 contains the list of object directories.
21400 @end itemize
21401 @end itemize
21403 @node Package Builder Attributes,Package Clean Attributes,Package Binder Attributes,Attributes
21404 @anchor{gnat_ugn/gnat_project_manager package-builder-attributes}@anchor{1a8}@anchor{gnat_ugn/gnat_project_manager id50}@anchor{1a9}
21405 @subsubsection Package Builder Attributes
21409 @itemize *
21411 @item 
21412 @strong{Default_Switches}: list, indexed, case-insensitive index
21414 Index is a language name. Value is the list of builder switches to be used when
21415 building an executable of the language, if there is no applicable attribute
21416 Switches.
21418 @item 
21419 @strong{Switches}: list, optional index, indexed, case-insensitive index,
21420 others allowed
21422 Index is either a language name or a source file name. Value is the list of
21423 builder switches to be used when building an executable. Index is either the
21424 source file name of the executable to be built or its language name.
21426 @item 
21427 @strong{Global_Compilation_Switches}: list, optional index, indexed,
21428 case-insensitive index
21430 Index is either a language name or a source file name. Value is the list of
21431 compilation switches to be used when building an executable. Index is either
21432 the source file name of the executable to be built or its language name.
21434 @item 
21435 @strong{Executable}: single, indexed, case-insensitive index
21437 Index is an executable source file name. Value is the simple file name of the
21438 executable to be built.
21440 @item 
21441 @strong{Executable_Suffix}: single
21443 Value is the extension of the file names of executable. When not specified,
21444 the extension is the default extension of executables on the platform.
21446 @item 
21447 @strong{Global_Configuration_Pragmas}: single
21449 Value is the file name of a configuration pragmas file that is specified to
21450 the Ada compiler when compiling any Ada source in the project tree.
21452 @item 
21453 @strong{Global_Config_File}: single, indexed, case-insensitive index
21455 Index is a language name. Value is the file name of a configuration file that
21456 is specified to the compiler when compiling any source of the language in the
21457 project tree.
21458 @end itemize
21461 @node Package Clean Attributes,Package Compiler Attributes,Package Builder Attributes,Attributes
21462 @anchor{gnat_ugn/gnat_project_manager package-clean-attributes}@anchor{1aa}@anchor{gnat_ugn/gnat_project_manager id52}@anchor{1ab}
21463 @subsubsection Package Clean Attributes
21467 @itemize *
21469 @item 
21470 @strong{Switches}: list
21472 Value is a list of switches to be used by the cleaning application.
21474 @item 
21475 @strong{Source_Artifact_Extensions}: list, indexed, case-insensitive index
21477 Index is a language names. Value is the list of extensions for file names
21478 derived from object file names that need to be cleaned in the object
21479 directory of the project.
21481 @item 
21482 @strong{Object_Artifact_Extensions}: list, indexed, case-insensitive index
21484 Index is a language names. Value is the list of extensions for file names
21485 derived from source file names that need to be cleaned in the object
21486 directory of the project.
21488 @item 
21489 @strong{Artifacts_In_Object_Dir}: single
21491 Value is a list of file names expressed as regular expressions that are to be
21492 deleted by gprclean in the object directory of the project.
21494 @item 
21495 @strong{Artifacts_In_Exec_Dir}: single
21497 Value is list of file names expressed as regular expressions that are to be
21498 deleted by gprclean in the exec directory of the main project.
21499 @end itemize
21501 @node Package Compiler Attributes,Package Cross_Reference Attributes,Package Clean Attributes,Attributes
21502 @anchor{gnat_ugn/gnat_project_manager id53}@anchor{1ac}@anchor{gnat_ugn/gnat_project_manager package-compiler-attributes}@anchor{1ad}
21503 @subsubsection Package Compiler Attributes
21507 @itemize *
21509 @item 
21510 @strong{General}
21513 @itemize *
21515 @item 
21516 @strong{Default_Switches}: list, indexed, case-insensitive index
21518 Index is a language name. Value is a list of switches to be used when invoking
21519 the compiler for the language for a source of the project, if there is no
21520 applicable attribute Switches.
21522 @item 
21523 @strong{Switches}: list, optional index, indexed, case-insensitive index,
21524 others allowed
21526 Index is a source file name or a language name. Value is the list of switches
21527 to be used when invoking the compiler for the source or for its language.
21529 @item 
21530 @strong{Local_Configuration_Pragmas}: single
21532 Value is the file name of a configuration pragmas file that is specified to
21533 the Ada compiler when compiling any Ada source in the project.
21535 @item 
21536 @strong{Local_Config_File}: single, indexed, case-insensitive index
21538 Index is a language name. Value is the file name of a configuration file that
21539 is specified to the compiler when compiling any source of the language in the
21540 project.
21541 @end itemize
21543 @item 
21544 @strong{Configuration - Compiling}
21547 @itemize *
21549 @item 
21550 @strong{Driver}: single, indexed, case-insensitive index
21552 Index is a language name. Value is the name of the executable for the compiler
21553 of the language.
21555 @item 
21556 @strong{Language_Kind}: single, indexed, case-insensitive index
21558 Index is a language name. Indicates the kind of the language, either file based
21559 or unit based. Only authorized case-insensitive values are "unit_based" and
21560 "file_based" (the default).
21562 @item 
21563 @strong{Dependency_Kind}: single, indexed, case-insensitive index
21565 Index is a language name. Indicates how the dependencies are handled for the
21566 language. Only authorized case-insensitive values are "makefile", "ali_file",
21567 "ali_closure" or "none" (the default).
21569 @item 
21570 @strong{Required_Switches}: list, indexed, case-insensitive index
21572 Equivalent to attribute Leading_Required_Switches.
21574 @item 
21575 @strong{Leading_Required_Switches}: list, indexed, case-insensitive index
21577 Index is a language name. Value is the list of the minimum switches to be used
21578 at the beginning of the command line when invoking the compiler for the
21579 language.
21581 @item 
21582 @strong{Trailing_Required_Switches}: list, indexed, case-insensitive index
21584 Index is a language name. Value is the list of the minimum switches to be used
21585 at the end of the command line when invoking the compiler for the language.
21587 @item 
21588 @strong{PIC_Option}: list, indexed, case-insensitive index
21590 Index is a language name. Value is the list of switches to be used when
21591 compiling a source of the language when the project is a shared library
21592 project.
21594 @item 
21595 @strong{Path_Syntax}: single, indexed, case-insensitive index
21597 Index is a language name. Value is the kind of path syntax to be used when
21598 invoking the compiler for the language. Only authorized case-insensitive
21599 values are "canonical" and "host" (the default).
21601 @item 
21602 @strong{Source_File_Switches}: single, indexed, case-insensitive index
21604 Index is a language name. Value is a list of switches to be used just before
21605 the path name of the source to compile when invoking the compiler for a source
21606 of the language.
21608 @item 
21609 @strong{Object_File_Suffix}: single, indexed, case-insensitive index
21611 Index is a language name. Value is the extension of the object files created
21612 by the compiler of the language. When not specified, the extension is the
21613 default one for the platform.
21615 @item 
21616 @strong{Object_File_Switches}: list, indexed, case-insensitive index
21618 Index is a language name. Value is the list of switches to be used by the
21619 compiler of the language to specify the path name of the object file. When not
21620 specified, the switch used is "-o".
21622 @item 
21623 @strong{Multi_Unit_Switches}: list, indexed, case-insensitive index
21625 Index is a language name. Value is the list of switches to be used to compile
21626 a unit in a multi unit source of the language. The index of the unit in the
21627 source is concatenated with the last switches in the list.
21629 @item 
21630 @strong{Multi_Unit_Object_Separator}: single, indexed, case-insensitive index
21632 Index is a language name. Value is the string to be used in the object file
21633 name before the index of the unit, when compiling a unit in a multi unit source
21634 of the language.
21635 @end itemize
21637 @item 
21638 @strong{Configuration - Mapping Files}
21641 @itemize *
21643 @item 
21644 @strong{Mapping_File_Switches}: list, indexed, case-insensitive index
21646 Index is a language name. Value is the list of switches to be used to specify
21647 a mapping file when invoking the compiler for a source of the language.
21649 @item 
21650 @strong{Mapping_Spec_Suffix}: single, indexed, case-insensitive index
21652 Index is a language name. Value is the suffix to be used in a mapping file
21653 to indicate that the source is a spec.
21655 @item 
21656 @strong{Mapping_Body_Suffix}: single, indexed, case-insensitive index
21658 Index is a language name. Value is the suffix to be used in a mapping file
21659 to indicate that the source is a body.
21660 @end itemize
21662 @item 
21663 @strong{Configuration - Config Files}
21666 @itemize *
21668 @item 
21669 @strong{Config_File_Switches}: list: single, indexed, case-insensitive index
21671 Index is a language name. Value is the list of switches to specify to the
21672 compiler of the language a configuration file.
21674 @item 
21675 @strong{Config_Body_File_Name}: single, indexed, case-insensitive index
21677 Index is a language name. Value is the template to be used to indicate a
21678 configuration specific to a body of the language in a configuration
21679 file.
21681 @item 
21682 @strong{Config_Body_File_Name_Index}: single, indexed, case-insensitive index
21684 Index is a language name. Value is the template to be used to indicate a
21685 configuration specific to the body a unit in a multi unit source of the
21686 language in a configuration file.
21688 @item 
21689 @strong{Config_Body_File_Name_Pattern}: single, indexed,
21690 case-insensitive index
21692 Index is a language name. Value is the template to be used to indicate a
21693 configuration for all bodies of the languages in a configuration file.
21695 @item 
21696 @strong{Config_Spec_File_Name}: single, indexed, case-insensitive index
21698 Index is a language name. Value is the template to be used to indicate a
21699 configuration specific to a spec of the language in a configuration
21700 file.
21702 @item 
21703 @strong{Config_Spec_File_Name_Index}: single, indexed, case-insensitive index
21705 Index is a language name. Value is the template to be used to indicate a
21706 configuration specific to the spec a unit in a multi unit source of the
21707 language in a configuration file.
21709 @item 
21710 @strong{Config_Spec_File_Name_Pattern}: single, indexed,
21711 case-insensitive index
21713 Index is a language name. Value is the template to be used to indicate a
21714 configuration for all specs of the languages in a configuration file.
21716 @item 
21717 @strong{Config_File_Unique}: single, indexed, case-insensitive index
21719 Index is a language name. Indicates if there should be only one configuration
21720 file specified to the compiler of the language. Only authorized
21721 case-insensitive values are "true" and "false" (the default).
21722 @end itemize
21724 @item 
21725 @strong{Configuration - Dependencies}
21728 @itemize *
21730 @item 
21731 @strong{Dependency_Switches}: list, indexed, case-insensitive index
21733 Index is a language name. Value is the list of switches to be used to specify
21734 to the compiler the dependency file when the dependency kind of the language is
21735 file based, and when Dependency_Driver is not specified for the language.
21737 @item 
21738 @strong{Dependency_Driver}: list, indexed, case-insensitive index
21740 Index is a language name. Value is the name of the executable to be used to
21741 create the dependency file for a source of the language, followed by the
21742 required switches.
21743 @end itemize
21745 @item 
21746 @strong{Configuration - Search Paths}
21749 @itemize *
21751 @item 
21752 @strong{Include_Switches}: list, indexed, case-insensitive index
21754 Index is a language name. Value is the list of switches to specify to the
21755 compiler of the language to indicate a directory to look for sources.
21757 @item 
21758 @strong{Include_Path}: single, indexed, case-insensitive index
21760 Index is a language name. Value is the name of an environment variable that
21761 contains the path of all the directories that the compiler of the language
21762 may search for sources.
21764 @item 
21765 @strong{Include_Path_File}: single, indexed, case-insensitive index
21767 Index is a language name. Value is the name of an environment variable the
21768 value of which is the path name of a text file that contains the directories
21769 that the compiler of the language may search for sources.
21771 @item 
21772 @strong{Object_Path_Switches}: list, indexed, case-insensitive index
21774 Index is a language name. Value is the list of switches to specify to the
21775 compiler of the language the name of a text file that contains the list of
21776 object directories. When this attribute is not declared, the text file is
21777 not created.
21778 @end itemize
21779 @end itemize
21781 @node Package Cross_Reference Attributes,Package Finder Attributes,Package Compiler Attributes,Attributes
21782 @anchor{gnat_ugn/gnat_project_manager id54}@anchor{1ae}@anchor{gnat_ugn/gnat_project_manager package-cross-reference-attributes}@anchor{1af}
21783 @subsubsection Package Cross_Reference Attributes
21787 @itemize *
21789 @item 
21790 @strong{Default_Switches}: list, indexed, case-insensitive index
21792 Index is a language name. Value is a list of switches to be used when invoking
21793 @cite{gnatxref} for a source of the language, if there is no applicable
21794 attribute Switches.
21796 @item 
21797 @strong{Switches}: list, optional index, indexed, case-insensitive index,
21798 others allowed
21800 Index is a source file name. Value is the list of switches to be used when
21801 invoking @cite{gnatxref} for the source.
21802 @end itemize
21805 @node Package Finder Attributes,Package gnatls Attributes,Package Cross_Reference Attributes,Attributes
21806 @anchor{gnat_ugn/gnat_project_manager id56}@anchor{1b0}@anchor{gnat_ugn/gnat_project_manager package-finder-attributes}@anchor{1b1}
21807 @subsubsection Package Finder Attributes
21811 @itemize *
21813 @item 
21814 @strong{Default_Switches}: list, indexed, case-insensitive index
21816 Index is a language name. Value is a list of switches to be used when invoking
21817 @cite{gnatfind} for a source of the language, if there is no applicable
21818 attribute Switches.
21820 @item 
21821 @strong{Switches}: list, optional index, indexed, case-insensitive index,
21822 others allowed
21824 Index is a source file name. Value is the list of switches to be used when
21825 invoking @cite{gnatfind} for the source.
21826 @end itemize
21828 @node Package gnatls Attributes,Package IDE Attributes,Package Finder Attributes,Attributes
21829 @anchor{gnat_ugn/gnat_project_manager package-gnatls-attributes}@anchor{1b2}@anchor{gnat_ugn/gnat_project_manager id57}@anchor{1b3}
21830 @subsubsection Package gnatls Attributes
21834 @itemize *
21836 @item 
21837 @strong{Switches}: list
21839 Value is a list of switches to be used when invoking @cite{gnatls}.
21840 @end itemize
21843 @node Package IDE Attributes,Package Install Attributes,Package gnatls Attributes,Attributes
21844 @anchor{gnat_ugn/gnat_project_manager id58}@anchor{1b4}@anchor{gnat_ugn/gnat_project_manager package-ide-attributes}@anchor{1b5}
21845 @subsubsection Package IDE Attributes
21849 @itemize *
21851 @item 
21852 @strong{Default_Switches}: list, indexed
21854 Index is the name of an external tool that the GNAT Programming System (GPS)
21855 is supporting. Value is a list of switches to use when invoking that tool.
21857 @item 
21858 @strong{Remote_Host}: single
21860 Value is a string that designates the remote host in a cross-compilation
21861 environment, to be used for remote compilation and debugging. This attribute
21862 should not be specified when running on the local machine.
21864 @item 
21865 @strong{Program_Host}: single
21867 Value is a string that specifies the name of IP address of the embedded target
21868 in a cross-compilation environment, on which the program should execute.
21870 @item 
21871 @strong{Communication_Protocol}: single
21873 Value is the name of the protocol to use to communicate with the target
21874 in a cross-compilation environment, for example @cite{"wtx"} or
21875 @cite{"vxworks"}.
21877 @item 
21878 @strong{Compiler_Command}: single, indexed, case-insensitive index
21880 Index is a language Name. Value is a string that denotes the command to be
21881 used to invoke the compiler. The value of @cite{Compiler_Command ("Ada")} is
21882 expected to be compatible with @emph{gnatmake}, in particular in
21883 the handling of switches.
21885 @item 
21886 @strong{Debugger_Command}: single
21888 Value is a string that specifies the name of the debugger to be used, such as
21889 gdb, powerpc-wrs-vxworks-gdb or gdb-4.
21891 @item 
21892 @strong{gnatlist}: single
21894 Value is a string that specifies the name of the @emph{gnatls} utility
21895 to be used to retrieve information about the predefined path; for example,
21896 @cite{"gnatls"}, @cite{"powerpc-wrs-vxworks-gnatls"}.
21898 @item 
21899 @strong{VCS_Kind}: single
21901 Value is a string used to specify the Version Control System (VCS) to be used
21902 for this project, for example "Subversion", "ClearCase". If the
21903 value is set to "Auto", the IDE will try to detect the actual VCS used
21904 on the list of supported ones.
21906 @item 
21907 @strong{VCS_File_Check}: single
21909 Value is a string that specifies the command used by the VCS to check
21910 the validity of a file, either when the user explicitly asks for a check,
21911 or as a sanity check before doing the check-in.
21913 @item 
21914 @strong{VCS_Log_Check}: single
21916 Value is a string that specifies the command used by the VCS to check
21917 the validity of a log file.
21919 @item 
21920 @strong{Documentation_Dir}: single
21922 Value is the directory used to generate the documentation of source code.
21923 @end itemize
21925 @node Package Install Attributes,Package Linker Attributes,Package IDE Attributes,Attributes
21926 @anchor{gnat_ugn/gnat_project_manager package-install-attributes}@anchor{1b6}@anchor{gnat_ugn/gnat_project_manager id59}@anchor{1b7}
21927 @subsubsection Package Install Attributes
21931 @itemize *
21933 @item 
21934 @strong{Artifacts}: list, indexed
21936 An array attribute to declare a set of files not part of the sources
21937 to be installed. The array discriminant is the directory where the
21938 file is to be installed. If a relative directory then Prefix (see
21939 below) is prepended.
21941 @item 
21942 @strong{Prefix}: single
21944 Value is the install destination directory.
21946 @item 
21947 @strong{Sources_Subdir}: single
21949 Value is the sources directory or subdirectory of Prefix.
21951 @item 
21952 @strong{Exec_Subdir}: single
21954 Value is the executables directory or subdirectory of Prefix.
21956 @item 
21957 @strong{Lib_Subdir}: single
21959 Value is library directory or subdirectory of Prefix.
21961 @item 
21962 @strong{Project_Subdir}: single
21964 Value is the project directory or subdirectory of Prefix.
21966 @item 
21967 @strong{Active}: single
21969 Indicates that the project is to be installed or not. Case-insensitive value
21970 "false" means that the project is not to be installed, all other values mean
21971 that the project is to be installed.
21973 @item 
21974 @strong{Mode}: single
21976 Value is the installation mode, it is either @strong{dev} (default) or @strong{usage}.
21978 @item 
21979 @strong{Install_Name}: single
21981 Specify the name to use for recording the installation. The default is
21982 the project name without the extension.
21983 @end itemize
21985 @node Package Linker Attributes,Package Naming Attributes,Package Install Attributes,Attributes
21986 @anchor{gnat_ugn/gnat_project_manager id60}@anchor{1b8}@anchor{gnat_ugn/gnat_project_manager package-linker-attributes}@anchor{1b9}
21987 @subsubsection Package Linker Attributes
21991 @itemize *
21993 @item 
21994 @strong{General}
21997 @itemize *
21999 @item 
22000 @strong{Required_Switches}: list
22002 Value is a list of switches that are required when invoking the linker to link
22003 an executable.
22005 @item 
22006 @strong{Default_Switches}: list, indexed, case-insensitive index
22008 Index is a language name. Value is a list of switches for the linker when
22009 linking an executable for a main source of the language, when there is no
22010 applicable Switches.
22012 @item 
22013 @strong{Leading_Switches}: list, optional index, indexed,
22014 case-insensitive index, others allowed
22016 Index is a source file name or a language name. Value is the list of switches
22017 to be used at the beginning of the command line when invoking the linker to
22018 build an executable for the source or for its language.
22020 @item 
22021 @strong{Switches}: list, optional index, indexed, case-insensitive index,
22022 others allowed
22024 Index is a source file name or a language name. Value is the list of switches
22025 to be used when invoking the linker to build an executable for the source or
22026 for its language.
22028 @item 
22029 @strong{Trailing_Switches}: list, optional index, indexed,
22030 case-insensitive index, others allowed
22032 Index is a source file name or a language name. Value is the list of switches
22033 to be used at the end of the command line when invoking the linker to
22034 build an executable for the source or for its language. These switches may
22035 override the Required_Switches.
22037 @item 
22038 @strong{Linker_Options}: list
22040 Value is a list of switches/options that are to be added when linking an
22041 executable from a project importing the current project directly or indirectly.
22042 Linker_Options are not used when linking an executable from the current
22043 project.
22045 @item 
22046 @strong{Map_File_Option}: single
22048 Value is the switch to specify the map file name that the linker needs to
22049 create.
22050 @end itemize
22052 @item 
22053 @strong{Configuration - Linking}
22056 @itemize *
22058 @item 
22059 @strong{Driver}: single
22061 Value is the name of the linker executable.
22062 @end itemize
22064 @item 
22065 @strong{Configuration - Response Files}
22068 @itemize *
22070 @item 
22071 @strong{Max_Command_Line_Length}: single
22073 Value is the maximum number of character in the command line when invoking
22074 the linker to link an executable.
22076 @item 
22077 @strong{Response_File_Format}: single
22079 Indicates the kind of response file to create when the length of the linking
22080 command line is too large. Only authorized case-insensitive values are "none",
22081 "gnu", "object_list", "gcc_gnu", "gcc_option_list" and "gcc_object_list".
22083 @item 
22084 @strong{Response_File_Switches}: list
22086 Value is the list of switches to specify a response file to the linker.
22087 @end itemize
22088 @end itemize
22090 @c only PRO or GPL
22091 @c 
22092 @c .. _Package_Metrics_Attribute:
22093 @c 
22094 @c Package Metrics Attribute
22095 @c ^^^^^^^^^^^^^^^^^^^^^^^^^
22096 @c 
22097 @c * **Default_Switches**: list, indexed, case-insensitive index
22098 @c 
22099 @c   Index is a language name. Value is a list of switches to be used when invoking
22100 @c   `gnatmetric` for a source of the language, if there is no applicable
22101 @c   attribute Switches.
22102 @c 
22103 @c * **Switches**: list, optional index, indexed, case-insensitive index,
22104 @c   others allowed
22105 @c 
22106 @c   Index is a source file name. Value is the list of switches to be used when
22107 @c   invoking `gnatmetric` for the source.
22109 @node Package Naming Attributes,Package Remote Attributes,Package Linker Attributes,Attributes
22110 @anchor{gnat_ugn/gnat_project_manager package-naming-attributes}@anchor{1ba}@anchor{gnat_ugn/gnat_project_manager id61}@anchor{1bb}
22111 @subsubsection Package Naming Attributes
22115 @itemize *
22117 @item 
22118 @strong{Specification_Suffix}: single, indexed, case-insensitive index
22120 Equivalent to attribute Spec_Suffix.
22122 @item 
22123 @strong{Spec_Suffix}: single, indexed, case-insensitive index
22125 Index is a language name. Value is the extension of file names for specs of
22126 the language.
22128 @item 
22129 @strong{Implementation_Suffix}: single, indexed, case-insensitive index
22131 Equivalent to attribute Body_Suffix.
22133 @item 
22134 @strong{Body_Suffix}: single, indexed, case-insensitive index
22136 Index is a language name. Value is the extension of file names for bodies of
22137 the language.
22139 @item 
22140 @strong{Separate_Suffix}: single
22142 Value is the extension of file names for subunits of Ada.
22144 @item 
22145 @strong{Casing}: single
22147 Indicates the casing of sources of the Ada language. Only authorized
22148 case-insensitive values are "lowercase", "uppercase" and "mixedcase".
22150 @item 
22151 @strong{Dot_Replacement}: single
22153 Value is the string that replace the dot of unit names in the source file names
22154 of the Ada language.
22156 @item 
22157 @strong{Specification}: single, optional index, indexed,
22158 case-insensitive index
22160 Equivalent to attribute Spec.
22162 @item 
22163 @strong{Spec}: single, optional index, indexed, case-insensitive index
22165 Index is a unit name. Value is the file name of the spec of the unit.
22167 @item 
22168 @strong{Implementation}: single, optional index, indexed,
22169 case-insensitive index
22171 Equivalent to attribute Body.
22173 @item 
22174 @strong{Body}: single, optional index, indexed, case-insensitive index
22176 Index is a unit name. Value is the file name of the body of the unit.
22178 @item 
22179 @strong{Specification_Exceptions}: list, indexed, case-insensitive index
22181 Index is a language name. Value is a list of specs for the language that do not
22182 necessarily follow the naming scheme for the language and that may or may not
22183 be found in the source directories of the project.
22185 @item 
22186 @strong{Implementation_Exceptions}: list, indexed, case-insensitive index
22188 Index is a language name. Value is a list of bodies for the language that do not
22189 necessarily follow the naming scheme for the language and that may or may not
22190 be found in the source directories of the project.
22191 @end itemize
22194 @node Package Remote Attributes,Package Stack Attributes,Package Naming Attributes,Attributes
22195 @anchor{gnat_ugn/gnat_project_manager package-remote-attributes}@anchor{1bc}@anchor{gnat_ugn/gnat_project_manager id63}@anchor{1bd}
22196 @subsubsection Package Remote Attributes
22200 @itemize *
22202 @item 
22203 @strong{Included_Patterns}: list
22205 If this attribute is defined it sets the patterns to
22206 synchronized from the master to the slaves. It is exclusive
22207 with Excluded_Patterns, that is it is an error to define
22208 both.
22210 @item 
22211 @strong{Included_Artifact_Patterns}: list
22213 If this attribute is defined it sets the patterns of compilation
22214 artifacts to synchronized from the slaves to the build master.
22215 This attribute replace the default hard-coded patterns.
22217 @item 
22218 @strong{Excluded_Patterns}: list
22220 Set of patterns to ignore when synchronizing sources from the build
22221 master to the slaves. A set of predefined patterns are supported
22222 (e.g. *.o, *.ali, *.exe, etc.), this attributes make it possible to
22223 add some more patterns.
22225 @item 
22226 @strong{Root_Dir}: single
22228 Value is the root directory used by the slave machines.
22229 @end itemize
22231 @node Package Stack Attributes,Package Synchronize Attributes,Package Remote Attributes,Attributes
22232 @anchor{gnat_ugn/gnat_project_manager id64}@anchor{1be}@anchor{gnat_ugn/gnat_project_manager package-stack-attributes}@anchor{1bf}
22233 @subsubsection Package Stack Attributes
22237 @itemize *
22239 @item 
22240 @strong{Switches}: list
22242 Value is the list of switches to be used when invoking @cite{gnatstack}.
22243 @end itemize
22245 @node Package Synchronize Attributes,,Package Stack Attributes,Attributes
22246 @anchor{gnat_ugn/gnat_project_manager package-synchronize-attributes}@anchor{1c0}
22247 @subsubsection Package Synchronize Attributes
22251 @itemize *
22253 @item 
22254 @strong{Default_Switches}: list, indexed, case-insensitive index
22256 Index is a language name. Value is a list of switches to be used when invoking
22257 @cite{gnatsync} for a source of the language, if there is no applicable
22258 attribute Switches.
22260 @item 
22261 @strong{Switches}: list, optional index, indexed, case-insensitive index,
22262 others allowed
22264 Index is a source file name. Value is the list of switches to be used when
22265 invoking @cite{gnatsync} for the source.
22266 @end itemize
22268 @node Tools Supporting Project Files,GNAT Utility Programs,GNAT Project Manager,Top
22269 @anchor{gnat_ugn/tools_supporting_project_files doc}@anchor{1c1}@anchor{gnat_ugn/tools_supporting_project_files tools-supporting-project-files}@anchor{c}@anchor{gnat_ugn/tools_supporting_project_files id1}@anchor{1c2}
22270 @chapter Tools Supporting Project Files
22273 This section describes how project files can be used in conjunction with a number of
22274 GNAT tools.
22276 @menu
22277 * gnatmake and Project Files:: 
22278 * The GNAT Driver and Project Files:: 
22280 @end menu
22282 @node gnatmake and Project Files,The GNAT Driver and Project Files,,Tools Supporting Project Files
22283 @anchor{gnat_ugn/tools_supporting_project_files id2}@anchor{1c3}@anchor{gnat_ugn/tools_supporting_project_files gnatmake-and-project-files}@anchor{e1}
22284 @section gnatmake and Project Files
22287 This section covers several topics related to @emph{gnatmake} and
22288 project files: defining switches for @emph{gnatmake}
22289 and for the tools that it invokes; specifying configuration pragmas;
22290 the use of the @cite{Main} attribute; building and rebuilding library project
22291 files.
22293 @menu
22294 * Switches Related to Project Files:: 
22295 * Switches and Project Files:: 
22296 * Specifying Configuration Pragmas:: 
22297 * Project Files and Main Subprograms:: 
22298 * Library Project Files:: 
22300 @end menu
22302 @node Switches Related to Project Files,Switches and Project Files,,gnatmake and Project Files
22303 @anchor{gnat_ugn/tools_supporting_project_files switches-related-to-project-files}@anchor{e3}@anchor{gnat_ugn/tools_supporting_project_files id3}@anchor{1c4}
22304 @subsection Switches Related to Project Files
22307 The following switches are used by GNAT tools that support project files:
22309 @quotation
22311 @geindex -P (any project-aware tool)
22312 @end quotation
22315 @table @asis
22317 @item @code{-P@emph{project}}
22319 Indicates the name of a project file. This project file will be parsed with
22320 the verbosity indicated by @emph{-vP*x*},
22321 if any, and using the external references indicated
22322 by @emph{-X} switches, if any.
22323 There may zero, one or more spaces between @emph{-P} and @cite{project}.
22325 There must be only one @emph{-P} switch on the command line.
22327 Since the Project Manager parses the project file only after all the switches
22328 on the command line are checked, the order of the switches
22329 @emph{-P},
22330 @emph{-vP*x*}
22331 or @emph{-X} is not significant.
22333 @geindex -X (any project-aware tool)
22335 @item @code{-X@emph{name}=@emph{value}}
22337 Indicates that external variable @cite{name} has the value @cite{value}.
22338 The Project Manager will use this value for occurrences of
22339 @cite{external(name)} when parsing the project file.
22341 If @cite{name} or @cite{value} includes a space, then @cite{name=value} should be
22342 put between quotes.
22344 @example
22345 -XOS=NT
22346 -X"user=John Doe"
22347 @end example
22349 Several @emph{-X} switches can be used simultaneously.
22350 If several @emph{-X} switches specify the same
22351 @cite{name}, only the last one is used.
22353 An external variable specified with a @emph{-X} switch
22354 takes precedence over the value of the same name in the environment.
22356 @geindex -vP (any project-aware tool)
22358 @item @code{-vP@emph{x}}
22360 Indicates the verbosity of the parsing of GNAT project files.
22362 @emph{-vP0} means Default;
22363 @emph{-vP1} means Medium;
22364 @emph{-vP2} means High.
22366 The default is Default: no output for syntactically correct
22367 project files.
22368 If several @emph{-vP*x*} switches are present,
22369 only the last one is used.
22371 @geindex -aP (any project-aware tool)
22373 @item @code{-aP@emph{dir}}
22375 Add directory @cite{dir} at the beginning of the project search path, in order,
22376 after the current working directory.
22378 @geindex -eL (any project-aware tool)
22380 @item @code{-eL}
22382 Follow all symbolic links when processing project files.
22384 @geindex --subdirs= (gnatmake and gnatclean)
22386 @item @code{--subdirs=@emph{subdir}}
22388 This switch is recognized by @emph{gnatmake} and @emph{gnatclean}. It
22389 indicate that the real directories (except the source directories) are the
22390 subdirectories @cite{subdir} of the directories specified in the project files.
22391 This applies in particular to object directories, library directories and
22392 exec directories. If the subdirectories do not exist, they are created
22393 automatically.
22394 @end table
22396 @node Switches and Project Files,Specifying Configuration Pragmas,Switches Related to Project Files,gnatmake and Project Files
22397 @anchor{gnat_ugn/tools_supporting_project_files id4}@anchor{1c5}@anchor{gnat_ugn/tools_supporting_project_files switches-and-project-files}@anchor{1c6}
22398 @subsection Switches and Project Files
22401 For each of the packages @cite{Builder}, @cite{Compiler}, @cite{Binder}, and
22402 @cite{Linker}, you can specify a @cite{Default_Switches}
22403 attribute, a @cite{Switches} attribute, or both;
22404 as their names imply, these switch-related
22405 attributes affect the switches that are used for each of these GNAT
22406 components when
22407 @emph{gnatmake} is invoked.  As will be explained below, these
22408 component-specific switches precede
22409 the switches provided on the @emph{gnatmake} command line.
22411 The @cite{Default_Switches} attribute is an attribute
22412 indexed by language name (case insensitive) whose value is a string list.
22413 For example:
22415 @quotation
22417 @example
22418 package Compiler is
22419   for Default_Switches ("Ada")
22420       use ("-gnaty",
22421            "-v");
22422 end Compiler;
22423 @end example
22424 @end quotation
22426 The @cite{Switches} attribute is indexed on a file name (which may or may
22427 not be case sensitive, depending
22428 on the operating system) whose value is a string list.  For example:
22430 @quotation
22432 @example
22433 package Builder is
22434    for Switches ("main1.adb")
22435        use ("-O2");
22436    for Switches ("main2.adb")
22437        use ("-g");
22438 end Builder;
22439 @end example
22440 @end quotation
22442 For the @cite{Builder} package, the file names must designate source files
22443 for main subprograms.  For the @cite{Binder} and @cite{Linker} packages, the
22444 file names must designate @code{ALI} or source files for main subprograms.
22445 In each case just the file name without an explicit extension is acceptable.
22447 For each tool used in a program build (@emph{gnatmake}, the compiler, the
22448 binder, and the linker), the corresponding package @@dfn@{contributes@} a set of
22449 switches for each file on which the tool is invoked, based on the
22450 switch-related attributes defined in the package.
22451 In particular, the switches
22452 that each of these packages contributes for a given file @cite{f} comprise:
22455 @itemize *
22457 @item 
22458 the value of attribute @cite{Switches (`f})`,
22459 if it is specified in the package for the given file,
22461 @item 
22462 otherwise, the value of @cite{Default_Switches ("Ada")},
22463 if it is specified in the package.
22464 @end itemize
22466 If neither of these attributes is defined in the package, then the package does
22467 not contribute any switches for the given file.
22469 When @emph{gnatmake} is invoked on a file, the switches comprise
22470 two sets, in the following order: those contributed for the file
22471 by the @cite{Builder} package;
22472 and the switches passed on the command line.
22474 When @emph{gnatmake} invokes a tool (compiler, binder, linker) on a file,
22475 the switches passed to the tool comprise three sets,
22476 in the following order:
22479 @itemize *
22481 @item 
22482 the applicable switches contributed for the file
22483 by the @cite{Builder} package in the project file supplied on the command line;
22485 @item 
22486 those contributed for the file by the package (in the relevant project file --
22487 see below) corresponding to the tool; and
22489 @item 
22490 the applicable switches passed on the command line.
22491 @end itemize
22493 The term @emph{applicable switches} reflects the fact that
22494 @emph{gnatmake} switches may or may not be passed to individual
22495 tools, depending on the individual switch.
22497 @emph{gnatmake} may invoke the compiler on source files from different
22498 projects. The Project Manager will use the appropriate project file to
22499 determine the @cite{Compiler} package for each source file being compiled.
22500 Likewise for the @cite{Binder} and @cite{Linker} packages.
22502 As an example, consider the following package in a project file:
22504 @quotation
22506 @example
22507 project Proj1 is
22508    package Compiler is
22509       for Default_Switches ("Ada")
22510           use ("-g");
22511       for Switches ("a.adb")
22512           use ("-O1");
22513       for Switches ("b.adb")
22514           use ("-O2",
22515                "-gnaty");
22516    end Compiler;
22517 end Proj1;
22518 @end example
22519 @end quotation
22521 If @emph{gnatmake} is invoked with this project file, and it needs to
22522 compile, say, the files @code{a.adb}, @code{b.adb}, and @code{c.adb}, then
22523 @code{a.adb} will be compiled with the switch @emph{-O1},
22524 @code{b.adb} with switches @emph{-O2} and @emph{-gnaty},
22525 and @code{c.adb} with @emph{-g}.
22527 The following example illustrates the ordering of the switches
22528 contributed by different packages:
22530 @quotation
22532 @example
22533 project Proj2 is
22534    package Builder is
22535       for Switches ("main.adb")
22536           use ("-g",
22537                "-O1",
22538                "-f");
22539    end Builder;
22541    package Compiler is
22542       for Switches ("main.adb")
22543           use ("-O2");
22544    end Compiler;
22545 end Proj2;
22546 @end example
22547 @end quotation
22549 If you issue the command:
22551 @quotation
22553 @example
22554 $ gnatmake -Pproj2 -O0 main
22555 @end example
22556 @end quotation
22558 then the compiler will be invoked on @code{main.adb} with the following
22559 sequence of switches
22561 @quotation
22563 @example
22564 -g -O1 -O2 -O0
22565 @end example
22566 @end quotation
22568 with the last @emph{-O}
22569 switch having precedence over the earlier ones;
22570 several other switches
22571 (such as @emph{-c}) are added implicitly.
22573 The switches @emph{-g}
22574 and @emph{-O1} are contributed by package
22575 @cite{Builder},  @emph{-O2} is contributed
22576 by the package @cite{Compiler}
22577 and @emph{-O0} comes from the command line.
22579 The @emph{-g} switch will also be passed in the invocation of
22580 @emph{Gnatlink.}
22582 A final example illustrates switch contributions from packages in different
22583 project files:
22585 @quotation
22587 @example
22588 project Proj3 is
22589    for Source_Files use ("pack.ads", "pack.adb");
22590    package Compiler is
22591       for Default_Switches ("Ada")
22592           use ("-gnata");
22593    end Compiler;
22594 end Proj3;
22596 with "Proj3";
22597 project Proj4 is
22598    for Source_Files use ("foo_main.adb", "bar_main.adb");
22599    package Builder is
22600       for Switches ("foo_main.adb")
22601           use ("-s",
22602                "-g");
22603    end Builder;
22604 end Proj4;
22605 @end example
22607 @example
22608 -- Ada source file:
22609 with Pack;
22610 procedure Foo_Main is
22611    ...
22612 end Foo_Main;
22613 @end example
22614 @end quotation
22616 If the command is
22618 @quotation
22620 @example
22621 $ gnatmake -PProj4 foo_main.adb -cargs -gnato
22622 @end example
22623 @end quotation
22625 then the switches passed to the compiler for @code{foo_main.adb} are
22626 @emph{-g} (contributed by the package @cite{Proj4.Builder}) and
22627 @emph{-gnato} (passed on the command line).
22628 When the imported package @cite{Pack} is compiled, the switches used
22629 are @emph{-g} from @cite{Proj4.Builder},
22630 @emph{-gnata} (contributed from package @cite{Proj3.Compiler},
22631 and @emph{-gnato} from the command line.
22633 When using @emph{gnatmake} with project files, some switches or
22634 arguments may be expressed as relative paths. As the working directory where
22635 compilation occurs may change, these relative paths are converted to absolute
22636 paths. For the switches found in a project file, the relative paths
22637 are relative to the project file directory, for the switches on the command
22638 line, they are relative to the directory where @emph{gnatmake} is invoked.
22639 The switches for which this occurs are:
22643 -aO,
22644 -aL,
22645 -aI, as well as all arguments that are not switches (arguments to
22646 switch
22647 -o, object files specified in package @cite{Linker} or after
22648 -largs on the command line). The exception to this rule is the switch
22649 --RTS= for which a relative path argument is never converted.
22651 @node Specifying Configuration Pragmas,Project Files and Main Subprograms,Switches and Project Files,gnatmake and Project Files
22652 @anchor{gnat_ugn/tools_supporting_project_files id5}@anchor{1c7}@anchor{gnat_ugn/tools_supporting_project_files specifying-configuration-pragmas}@anchor{7d}
22653 @subsection Specifying Configuration Pragmas
22656 When using @emph{gnatmake} with project files, if there exists a file
22657 @code{gnat.adc} that contains configuration pragmas, this file will be
22658 ignored.
22660 Configuration pragmas can be defined by means of the following attributes in
22661 project files: @cite{Global_Configuration_Pragmas} in package @cite{Builder}
22662 and @cite{Local_Configuration_Pragmas} in package @cite{Compiler}.
22664 Both these attributes are single string attributes. Their values is the path
22665 name of a file containing configuration pragmas. If a path name is relative,
22666 then it is relative to the project directory of the project file where the
22667 attribute is defined.
22669 When compiling a source, the configuration pragmas used are, in order,
22670 those listed in the file designated by attribute
22671 @cite{Global_Configuration_Pragmas} in package @cite{Builder} of the main
22672 project file, if it is specified, and those listed in the file designated by
22673 attribute @cite{Local_Configuration_Pragmas} in package @cite{Compiler} of
22674 the project file of the source, if it exists.
22676 @node Project Files and Main Subprograms,Library Project Files,Specifying Configuration Pragmas,gnatmake and Project Files
22677 @anchor{gnat_ugn/tools_supporting_project_files id6}@anchor{1c8}@anchor{gnat_ugn/tools_supporting_project_files project-files-and-main-subprograms}@anchor{e2}
22678 @subsection Project Files and Main Subprograms
22681 When using a project file, you can invoke @emph{gnatmake}
22682 with one or several main subprograms, by specifying their source files on the
22683 command line.
22685 @quotation
22687 @example
22688 $ gnatmake -Pprj main1.adb main2.adb main3.adb
22689 @end example
22690 @end quotation
22692 Each of these needs to be a source file of the same project, except
22693 when the switch @cite{-u} is used.
22695 When @cite{-u} is not used, all the mains need to be sources of the
22696 same project, one of the project in the tree rooted at the project specified
22697 on the command line. The package @cite{Builder} of this common project, the
22698 "main project" is the one that is considered by @emph{gnatmake}.
22700 When @cite{-u} is used, the specified source files may be in projects
22701 imported directly or indirectly by the project specified on the command line.
22702 Note that if such a source file is not part of the project specified on the
22703 command line, the switches found in package @cite{Builder} of the
22704 project specified on the command line, if any, that are transmitted
22705 to the compiler will still be used, not those found in the project file of
22706 the source file.
22708 When using a project file, you can also invoke @emph{gnatmake} without
22709 explicitly specifying any main, and the effect depends on whether you have
22710 defined the @cite{Main} attribute.  This attribute has a string list value,
22711 where each element in the list is the name of a source file (the file
22712 extension is optional) that contains a unit that can be a main subprogram.
22714 If the @cite{Main} attribute is defined in a project file as a non-empty
22715 string list and the switch @emph{-u} is not used on the command
22716 line, then invoking @emph{gnatmake} with this project file but without any
22717 main on the command line is equivalent to invoking @emph{gnatmake} with all
22718 the file names in the @cite{Main} attribute on the command line.
22720 Example:
22722 @quotation
22724 @example
22725 project Prj is
22726    for Main use ("main1.adb", "main2.adb", "main3.adb");
22727 end Prj;
22728 @end example
22729 @end quotation
22731 With this project file, @cite{"gnatmake -Pprj"}
22732 is equivalent to
22733 @cite{"gnatmake -Pprj main1.adb main2.adb main3.adb"}.
22735 When the project attribute @cite{Main} is not specified, or is specified
22736 as an empty string list, or when the switch @emph{-u} is used on the command
22737 line, then invoking @emph{gnatmake} with no main on the command line will
22738 result in all immediate sources of the project file being checked, and
22739 potentially recompiled. Depending on the presence of the switch @emph{-u},
22740 sources from other project files on which the immediate sources of the main
22741 project file depend are also checked and potentially recompiled. In other
22742 words, the @emph{-u} switch is applied to all of the immediate sources of the
22743 main project file.
22745 When no main is specified on the command line and attribute @cite{Main} exists
22746 and includes several mains, or when several mains are specified on the
22747 command line, the default switches in package @cite{Builder} will
22748 be used for all mains, even if there are specific switches
22749 specified for one or several mains.
22751 But the switches from package @cite{Binder} or @cite{Linker} will be
22752 the specific switches for each main, if they are specified.
22754 @node Library Project Files,,Project Files and Main Subprograms,gnatmake and Project Files
22755 @anchor{gnat_ugn/tools_supporting_project_files id7}@anchor{1c9}@anchor{gnat_ugn/tools_supporting_project_files library-project-files}@anchor{1ca}
22756 @subsection Library Project Files
22759 When @emph{gnatmake} is invoked with a main project file that is a library
22760 project file, it is not allowed to specify one or more mains on the command
22761 line.
22763 When a library project file is specified, switches @cite{-b} and
22764 @cite{-l} have special meanings.
22767 @itemize *
22769 @item 
22770 @cite{-b} is only allowed for stand-alone libraries. It indicates
22771 to @emph{gnatmake} that @emph{gnatbind} should be invoked for the
22772 library.
22774 @item 
22775 @cite{-l} may be used for all library projects. It indicates
22776 to @emph{gnatmake} that the binder generated file should be compiled
22777 (in the case of a stand-alone library) and that the library should be built.
22778 @end itemize
22780 @node The GNAT Driver and Project Files,,gnatmake and Project Files,Tools Supporting Project Files
22781 @anchor{gnat_ugn/tools_supporting_project_files id8}@anchor{1cb}@anchor{gnat_ugn/tools_supporting_project_files the-gnat-driver-and-project-files}@anchor{11f}
22782 @section The GNAT Driver and Project Files
22785 A number of GNAT tools beyond @emph{gnatmake}
22786 can benefit from project files:
22790 @itemize *
22792 @item 
22793 @emph{gnatbind}
22795 @item 
22796 @emph{gnatclean}
22798 @item 
22799 @emph{gnatfind}
22801 @item 
22802 @emph{gnatlink}
22804 @item 
22805 @emph{gnatls}
22807 @item 
22808 @emph{gnatxref}
22809 @end itemize
22811 However, none of these tools can be invoked
22812 directly with a project file switch (@emph{-P}).
22813 They must be invoked through the @emph{gnat} driver.
22815 The @emph{gnat} driver is a wrapper that accepts a number of commands and
22816 calls the corresponding tool. It was designed initially for VMS platforms (to
22817 convert VMS qualifiers to Unix-style switches), but it is now available on all
22818 GNAT platforms.
22820 On non-VMS platforms, the @emph{gnat} driver accepts the following commands
22821 (case insensitive):
22825 @itemize *
22827 @item 
22828 BIND to invoke @emph{gnatbind}
22830 @item 
22831 CHOP to invoke @emph{gnatchop}
22833 @item 
22834 CLEAN to invoke @emph{gnatclean}
22836 @item 
22837 COMP or COMPILE to invoke the compiler
22839 @item 
22840 FIND to invoke @emph{gnatfind}
22842 @item 
22843 KR or KRUNCH to invoke @emph{gnatkr}
22845 @item 
22846 LINK to invoke @emph{gnatlink}
22848 @item 
22849 LS or LIST to invoke @emph{gnatls}
22851 @item 
22852 MAKE to invoke @emph{gnatmake}
22854 @item 
22855 NAME to invoke @emph{gnatname}
22857 @item 
22858 PREP or PREPROCESS to invoke @emph{gnatprep}
22860 @item 
22861 XREF to invoke @emph{gnatxref}
22862 @end itemize
22864 Note that the command
22865 @emph{gnatmake -c -f -u} is used to invoke the compiler.
22867 On non-VMS platforms, between @emph{gnat} and the command, two
22868 special switches may be used:
22871 @itemize *
22873 @item 
22874 @emph{-v} to display the invocation of the tool.
22876 @item 
22877 @emph{-dn} to prevent the @emph{gnat} driver from removing
22878 the temporary files it has created. These temporary files are
22879 configuration files and temporary file list files.
22880 @end itemize
22882 The command may be followed by switches and arguments for the invoked
22883 tool.
22885 @quotation
22887 @example
22888 $ gnat bind -C main.ali
22889 $ gnat ls -a main
22890 $ gnat chop foo.txt
22891 @end example
22892 @end quotation
22894 Switches may also be put in text files, one switch per line, and the text
22895 files may be specified with their path name preceded by '@@'.
22897 @quotation
22899 @example
22900 $ gnat bind @@args.txt main.ali
22901 @end example
22902 @end quotation
22904 In addition, for the following commands the project file related switches
22905 (@emph{-P}, @emph{-X} and @emph{-vPx}) may be used in addition to
22906 the switches of the invoking tool:
22910 @itemize *
22912 @item 
22913 BIND
22915 @item 
22916 COMP or COMPILE
22918 @item 
22919 FIND
22921 @item 
22922 LS or LIST
22924 @item 
22925 LINK
22927 @item 
22928 XREF
22929 @end itemize
22932 For each of the following commands, there is optionally a corresponding
22933 package in the main project.
22937 @itemize *
22939 @item 
22940 package @cite{Binder} for command BIND (invoking @cite{gnatbind})
22942 @item 
22943 package @cite{Compiler} for command COMP or COMPILE (invoking the compiler)
22945 @item 
22946 package @cite{Cross_Reference} for command XREF (invoking @cite{gnatxref})
22948 @item 
22949 package @cite{Finder} for command FIND (invoking @cite{gnatfind})
22951 @item 
22952 package @cite{Gnatls} for command LS or LIST (invoking @cite{gnatls})
22954 @item 
22955 package @cite{Linker} for command LINK (invoking @cite{gnatlink})
22956 @end itemize
22958 Package @cite{Gnatls} has a unique attribute @cite{Switches},
22959 a simple variable with a string list value. It contains switches
22960 for the invocation of @cite{gnatls}.
22962 @quotation
22964 @example
22965 project Proj1 is
22966    package gnatls is
22967       for Switches
22968           use ("-a",
22969                "-v");
22970    end gnatls;
22971 end Proj1;
22972 @end example
22973 @end quotation
22975 All other packages have two attribute @cite{Switches} and
22976 @cite{Default_Switches}.
22978 @cite{Switches} is an indexed attribute, indexed by the
22979 source file name, that has a string list value: the switches to be
22980 used when the tool corresponding to the package is invoked for the specific
22981 source file.
22983 @cite{Default_Switches} is an attribute,
22984 indexed by  the programming language that has a string list value.
22985 @cite{Default_Switches ("Ada")} contains the
22986 switches for the invocation of the tool corresponding
22987 to the package, except if a specific @cite{Switches} attribute
22988 is specified for the source file.
22990 @quotation
22992 @example
22993 project Proj is
22995    for Source_Dirs use ("");
22997    package gnatls is
22998       for Switches use
22999           ("-a",
23000            "-v");
23001    end gnatls;
23003    package Compiler is
23004       for Default_Switches ("Ada")
23005           use ("-gnatv",
23006                "-gnatwa");
23007    end Binder;
23009    package Binder is
23010       for Default_Switches ("Ada")
23011           use ("-C",
23012                "-e");
23013    end Binder;
23015    package Linker is
23016       for Default_Switches ("Ada")
23017           use ("-C");
23018       for Switches ("main.adb")
23019           use ("-C",
23020                "-v",
23021                "-v");
23022    end Linker;
23024    package Finder is
23025       for Default_Switches ("Ada")
23026            use ("-a",
23027                 "-f");
23028    end Finder;
23030    package Cross_Reference is
23031       for Default_Switches ("Ada")
23032           use ("-a",
23033                "-f",
23034                "-d",
23035                "-u");
23036    end Cross_Reference;
23037 end Proj;
23038 @end example
23039 @end quotation
23041 With the above project file, commands such as
23043 @quotation
23045 @example
23046 $ gnat comp -Pproj main
23047 $ gnat ls -Pproj main
23048 $ gnat xref -Pproj main
23049 $ gnat bind -Pproj main.ali
23050 $ gnat link -Pproj main.ali
23051 @end example
23052 @end quotation
23054 will set up the environment properly and invoke the tool with the switches
23055 found in the package corresponding to the tool:
23056 @cite{Default_Switches ("Ada")} for all tools,
23057 except @cite{Switches ("main.adb")}
23058 for @cite{gnatlink}.
23061 @node GNAT Utility Programs,GNAT and Program Execution,Tools Supporting Project Files,Top
23062 @anchor{gnat_ugn/gnat_utility_programs doc}@anchor{1cc}@anchor{gnat_ugn/gnat_utility_programs gnat-utility-programs}@anchor{d}@anchor{gnat_ugn/gnat_utility_programs id1}@anchor{1cd}
23063 @chapter GNAT Utility Programs
23066 This chapter describes a number of utility programs:
23070 @itemize *
23072 @item 
23073 @ref{22,,The File Cleanup Utility gnatclean}
23075 @item 
23076 @ref{23,,The GNAT Library Browser gnatls}
23078 @item 
23079 @ref{24,,The Cross-Referencing Tools gnatxref and gnatfind}
23081 @item 
23082 @ref{25,,The Ada to HTML Converter gnathtml}
23083 @end itemize
23085 Other GNAT utilities are described elsewhere in this manual:
23088 @itemize *
23090 @item 
23091 @ref{5b,,Handling Arbitrary File Naming Conventions with gnatname}
23093 @item 
23094 @ref{65,,File Name Krunching with gnatkr}
23096 @item 
23097 @ref{38,,Renaming Files with gnatchop}
23099 @item 
23100 @ref{19,,Preprocessing with gnatprep}
23101 @end itemize
23103 @menu
23104 * The File Cleanup Utility gnatclean:: 
23105 * The GNAT Library Browser gnatls:: 
23106 * The Cross-Referencing Tools gnatxref and gnatfind:: 
23107 * The Ada to HTML Converter gnathtml:: 
23109 @end menu
23111 @node The File Cleanup Utility gnatclean,The GNAT Library Browser gnatls,,GNAT Utility Programs
23112 @anchor{gnat_ugn/gnat_utility_programs id2}@anchor{1ce}@anchor{gnat_ugn/gnat_utility_programs the-file-cleanup-utility-gnatclean}@anchor{22}
23113 @section The File Cleanup Utility @emph{gnatclean}
23116 @geindex File cleanup tool
23118 @geindex gnatclean
23120 @cite{gnatclean} is a tool that allows the deletion of files produced by the
23121 compiler, binder and linker, including ALI files, object files, tree files,
23122 expanded source files, library files, interface copy source files, binder
23123 generated files and executable files.
23125 @menu
23126 * Running gnatclean:: 
23127 * Switches for gnatclean:: 
23129 @end menu
23131 @node Running gnatclean,Switches for gnatclean,,The File Cleanup Utility gnatclean
23132 @anchor{gnat_ugn/gnat_utility_programs running-gnatclean}@anchor{1cf}@anchor{gnat_ugn/gnat_utility_programs id3}@anchor{1d0}
23133 @subsection Running @cite{gnatclean}
23136 The @cite{gnatclean} command has the form:
23138 @quotation
23140 @example
23141 $ gnatclean switches `names`
23142 @end example
23143 @end quotation
23145 where @cite{names} is a list of source file names. Suffixes @code{.ads} and
23146 @code{adb} may be omitted. If a project file is specified using switch
23147 @code{-P}, then @cite{names} may be completely omitted.
23149 In normal mode, @cite{gnatclean} delete the files produced by the compiler and,
23150 if switch @cite{-c} is not specified, by the binder and
23151 the linker. In informative-only mode, specified by switch
23152 @cite{-n}, the list of files that would have been deleted in
23153 normal mode is listed, but no file is actually deleted.
23155 @node Switches for gnatclean,,Running gnatclean,The File Cleanup Utility gnatclean
23156 @anchor{gnat_ugn/gnat_utility_programs id4}@anchor{1d1}@anchor{gnat_ugn/gnat_utility_programs switches-for-gnatclean}@anchor{1d2}
23157 @subsection Switches for @cite{gnatclean}
23160 @cite{gnatclean} recognizes the following switches:
23162 @geindex --version (gnatclean)
23165 @table @asis
23167 @item @code{--version}
23169 Display Copyright and version, then exit disregarding all other options.
23170 @end table
23172 @geindex --help (gnatclean)
23175 @table @asis
23177 @item @code{--help}
23179 If @emph{--version} was not used, display usage, then exit disregarding
23180 all other options.
23182 @item @code{--subdirs=@emph{subdir}}
23184 Actual object directory of each project file is the subdirectory subdir of the
23185 object directory specified or defaulted in the project file.
23187 @item @code{--unchecked-shared-lib-imports}
23189 By default, shared library projects are not allowed to import static library
23190 projects. When this switch is used on the command line, this restriction is
23191 relaxed.
23192 @end table
23194 @geindex -c (gnatclean)
23197 @table @asis
23199 @item @code{-c}
23201 Only attempt to delete the files produced by the compiler, not those produced
23202 by the binder or the linker. The files that are not to be deleted are library
23203 files, interface copy files, binder generated files and executable files.
23204 @end table
23206 @geindex -D (gnatclean)
23209 @table @asis
23211 @item @code{-D @emph{dir}}
23213 Indicate that ALI and object files should normally be found in directory @cite{dir}.
23214 @end table
23216 @geindex -F (gnatclean)
23219 @table @asis
23221 @item @code{-F}
23223 When using project files, if some errors or warnings are detected during
23224 parsing and verbose mode is not in effect (no use of switch
23225 -v), then error lines start with the full path name of the project
23226 file, rather than its simple file name.
23227 @end table
23229 @geindex -h (gnatclean)
23232 @table @asis
23234 @item @code{-h}
23236 Output a message explaining the usage of @cite{gnatclean}.
23237 @end table
23239 @geindex -n (gnatclean)
23242 @table @asis
23244 @item @code{-n}
23246 Informative-only mode. Do not delete any files. Output the list of the files
23247 that would have been deleted if this switch was not specified.
23248 @end table
23250 @geindex -P (gnatclean)
23253 @table @asis
23255 @item @code{-P@emph{project}}
23257 Use project file @cite{project}. Only one such switch can be used.
23258 When cleaning a project file, the files produced by the compilation of the
23259 immediate sources or inherited sources of the project files are to be
23260 deleted. This is not depending on the presence or not of executable names
23261 on the command line.
23262 @end table
23264 @geindex -q (gnatclean)
23267 @table @asis
23269 @item @code{-q}
23271 Quiet output. If there are no errors, do not output anything, except in
23272 verbose mode (switch -v) or in informative-only mode
23273 (switch -n).
23274 @end table
23276 @geindex -r (gnatclean)
23279 @table @asis
23281 @item @code{-r}
23283 When a project file is specified (using switch -P),
23284 clean all imported and extended project files, recursively. If this switch
23285 is not specified, only the files related to the main project file are to be
23286 deleted. This switch has no effect if no project file is specified.
23287 @end table
23289 @geindex -v (gnatclean)
23292 @table @asis
23294 @item @code{-v}
23296 Verbose mode.
23297 @end table
23299 @geindex -vP (gnatclean)
23302 @table @asis
23304 @item @code{-vP@emph{x}}
23306 Indicates the verbosity of the parsing of GNAT project files.
23307 @ref{e3,,Switches Related to Project Files}.
23308 @end table
23310 @geindex -X (gnatclean)
23313 @table @asis
23315 @item @code{-X@emph{name}=@emph{value}}
23317 Indicates that external variable @cite{name} has the value @cite{value}.
23318 The Project Manager will use this value for occurrences of
23319 @cite{external(name)} when parsing the project file.
23320 @ref{e3,,Switches Related to Project Files}.
23321 @end table
23323 @geindex -aO (gnatclean)
23326 @table @asis
23328 @item @code{-aO@emph{dir}}
23330 When searching for ALI and object files, look in directory @cite{dir}.
23331 @end table
23333 @geindex -I (gnatclean)
23336 @table @asis
23338 @item @code{-I@emph{dir}}
23340 Equivalent to @code{-aO@emph{dir}}.
23341 @end table
23343 @geindex -I- (gnatclean)
23345 @geindex Source files
23346 @geindex suppressing search
23349 @table @asis
23351 @item @code{-I-}
23353 Do not look for ALI or object files in the directory
23354 where @cite{gnatclean} was invoked.
23355 @end table
23357 @node The GNAT Library Browser gnatls,The Cross-Referencing Tools gnatxref and gnatfind,The File Cleanup Utility gnatclean,GNAT Utility Programs
23358 @anchor{gnat_ugn/gnat_utility_programs the-gnat-library-browser-gnatls}@anchor{23}@anchor{gnat_ugn/gnat_utility_programs id5}@anchor{1d3}
23359 @section The GNAT Library Browser @cite{gnatls}
23362 @geindex Library browser
23364 @c index: ! gnatls
23366 @cite{gnatls} is a tool that outputs information about compiled
23367 units. It gives the relationship between objects, unit names and source
23368 files. It can also be used to check the source dependencies of a unit
23369 as well as various characteristics.
23371 Note: to invoke @cite{gnatls} with a project file, use the @cite{gnat}
23372 driver (see @ref{11f,,The GNAT Driver and Project Files}).
23374 @menu
23375 * Running gnatls:: 
23376 * Switches for gnatls:: 
23377 * Example of gnatls Usage:: 
23379 @end menu
23381 @node Running gnatls,Switches for gnatls,,The GNAT Library Browser gnatls
23382 @anchor{gnat_ugn/gnat_utility_programs id6}@anchor{1d4}@anchor{gnat_ugn/gnat_utility_programs running-gnatls}@anchor{1d5}
23383 @subsection Running @cite{gnatls}
23386 The @cite{gnatls} command has the form
23388 @quotation
23390 @example
23391 $ gnatls switches `object_or_ali_file`
23392 @end example
23393 @end quotation
23395 The main argument is the list of object or @code{ali} files
23396 (see @ref{44,,The Ada Library Information Files})
23397 for which information is requested.
23399 In normal mode, without additional option, @cite{gnatls} produces a
23400 four-column listing. Each line represents information for a specific
23401 object. The first column gives the full path of the object, the second
23402 column gives the name of the principal unit in this object, the third
23403 column gives the status of the source and the fourth column gives the
23404 full path of the source representing this unit.
23405 Here is a simple example of use:
23407 @quotation
23409 @example
23410 $ gnatls *.o
23411 ./demo1.o            demo1            DIF demo1.adb
23412 ./demo2.o            demo2             OK demo2.adb
23413 ./hello.o            h1                OK hello.adb
23414 ./instr-child.o      instr.child      MOK instr-child.adb
23415 ./instr.o            instr             OK instr.adb
23416 ./tef.o              tef              DIF tef.adb
23417 ./text_io_example.o  text_io_example   OK text_io_example.adb
23418 ./tgef.o             tgef             DIF tgef.adb
23419 @end example
23420 @end quotation
23422 The first line can be interpreted as follows: the main unit which is
23423 contained in
23424 object file @code{demo1.o} is demo1, whose main source is in
23425 @code{demo1.adb}. Furthermore, the version of the source used for the
23426 compilation of demo1 has been modified (DIF). Each source file has a status
23427 qualifier which can be:
23430 @table @asis
23432 @item @emph{OK (unchanged)}
23434 The version of the source file used for the compilation of the
23435 specified unit corresponds exactly to the actual source file.
23437 @item @emph{MOK (slightly modified)}
23439 The version of the source file used for the compilation of the
23440 specified unit differs from the actual source file but not enough to
23441 require recompilation. If you use gnatmake with the qualifier
23442 @emph{-m (minimal recompilation)}, a file marked
23443 MOK will not be recompiled.
23445 @item @emph{DIF (modified)}
23447 No version of the source found on the path corresponds to the source
23448 used to build this object.
23450 @item @emph{??? (file not found)}
23452 No source file was found for this unit.
23454 @item @emph{HID (hidden,  unchanged version not first on PATH)}
23456 The version of the source that corresponds exactly to the source used
23457 for compilation has been found on the path but it is hidden by another
23458 version of the same source that has been modified.
23459 @end table
23461 @node Switches for gnatls,Example of gnatls Usage,Running gnatls,The GNAT Library Browser gnatls
23462 @anchor{gnat_ugn/gnat_utility_programs id7}@anchor{1d6}@anchor{gnat_ugn/gnat_utility_programs switches-for-gnatls}@anchor{1d7}
23463 @subsection Switches for @cite{gnatls}
23466 @cite{gnatls} recognizes the following switches:
23468 @geindex --version (gnatls)
23471 @table @asis
23473 @item @code{--version}
23475 Display Copyright and version, then exit disregarding all other options.
23476 @end table
23478 @geindex --help (gnatls)
23481 @table @asis
23483 @item @code{*--help}
23485 If @emph{--version} was not used, display usage, then exit disregarding
23486 all other options.
23487 @end table
23489 @geindex -a (gnatls)
23492 @table @asis
23494 @item @code{-a}
23496 Consider all units, including those of the predefined Ada library.
23497 Especially useful with @emph{-d}.
23498 @end table
23500 @geindex -d (gnatls)
23503 @table @asis
23505 @item @code{-d}
23507 List sources from which specified units depend on.
23508 @end table
23510 @geindex -h (gnatls)
23513 @table @asis
23515 @item @code{-h}
23517 Output the list of options.
23518 @end table
23520 @geindex -o (gnatls)
23523 @table @asis
23525 @item @code{-o}
23527 Only output information about object files.
23528 @end table
23530 @geindex -s (gnatls)
23533 @table @asis
23535 @item @code{-s}
23537 Only output information about source files.
23538 @end table
23540 @geindex -u (gnatls)
23543 @table @asis
23545 @item @code{-u}
23547 Only output information about compilation units.
23548 @end table
23550 @geindex -files (gnatls)
23553 @table @asis
23555 @item @code{-files=@emph{file}}
23557 Take as arguments the files listed in text file @cite{file}.
23558 Text file @cite{file} may contain empty lines that are ignored.
23559 Each nonempty line should contain the name of an existing file.
23560 Several such switches may be specified simultaneously.
23561 @end table
23563 @geindex -aO (gnatls)
23565 @geindex -aI (gnatls)
23567 @geindex -I (gnatls)
23569 @geindex -I- (gnatls)
23572 @table @asis
23574 @item @code{-aO@emph{dir}}, @code{-aI@emph{dir}}, @code{-I@emph{dir}}, @code{-I-}, @code{-nostdinc}
23576 Source path manipulation. Same meaning as the equivalent @emph{gnatmake}
23577 flags (@ref{df,,Switches for gnatmake}).
23578 @end table
23580 @geindex -aP (gnatls)
23583 @table @asis
23585 @item @code{-aP@emph{dir}}
23587 Add @cite{dir} at the beginning of the project search dir.
23588 @end table
23590 @geindex --RTS (gnatls)
23593 @table @asis
23595 @item @code{--RTS=@emph{rts-path}`}
23597 Specifies the default location of the runtime library. Same meaning as the
23598 equivalent @emph{gnatmake} flag (@ref{df,,Switches for gnatmake}).
23599 @end table
23601 @geindex -v (gnatls)
23604 @table @asis
23606 @item @code{-v}
23608 Verbose mode. Output the complete source, object and project paths. Do not use
23609 the default column layout but instead use long format giving as much as
23610 information possible on each requested units, including special
23611 characteristics such as:
23614 @itemize *
23616 @item 
23617 @emph{Preelaborable}: The unit is preelaborable in the Ada sense.
23619 @item 
23620 @emph{No_Elab_Code}:  No elaboration code has been produced by the compiler for this unit.
23622 @item 
23623 @emph{Pure}: The unit is pure in the Ada sense.
23625 @item 
23626 @emph{Elaborate_Body}: The unit contains a pragma Elaborate_Body.
23628 @item 
23629 @emph{Remote_Types}: The unit contains a pragma Remote_Types.
23631 @item 
23632 @emph{Shared_Passive}: The unit contains a pragma Shared_Passive.
23634 @item 
23635 @emph{Predefined}: This unit is part of the predefined environment and cannot be modified
23636 by the user.
23638 @item 
23639 @emph{Remote_Call_Interface}: The unit contains a pragma Remote_Call_Interface.
23640 @end itemize
23641 @end table
23643 @node Example of gnatls Usage,,Switches for gnatls,The GNAT Library Browser gnatls
23644 @anchor{gnat_ugn/gnat_utility_programs id8}@anchor{1d8}@anchor{gnat_ugn/gnat_utility_programs example-of-gnatls-usage}@anchor{1d9}
23645 @subsection Example of @cite{gnatls} Usage
23648 Example of using the verbose switch. Note how the source and
23649 object paths are affected by the -I switch.
23651 @quotation
23653 @example
23654 $ gnatls -v -I.. demo1.o
23656 GNATLS 5.03w (20041123-34)
23657 Copyright 1997-2004 Free Software Foundation, Inc.
23659 Source Search Path:
23660    <Current_Directory>
23661    ../
23662    /home/comar/local/adainclude/
23664 Object Search Path:
23665    <Current_Directory>
23666    ../
23667    /home/comar/local/lib/gcc-lib/x86-linux/3.4.3/adalib/
23669 Project Search Path:
23670    <Current_Directory>
23671    /home/comar/local/lib/gnat/
23673 ./demo1.o
23674    Unit =>
23675      Name   => demo1
23676      Kind   => subprogram body
23677      Flags  => No_Elab_Code
23678      Source => demo1.adb    modified
23679 @end example
23680 @end quotation
23682 The following is an example of use of the dependency list.
23683 Note the use of the -s switch
23684 which gives a straight list of source files. This can be useful for
23685 building specialized scripts.
23687 @quotation
23689 @example
23690 $ gnatls -d demo2.o
23691 ./demo2.o   demo2        OK demo2.adb
23692                          OK gen_list.ads
23693                          OK gen_list.adb
23694                          OK instr.ads
23695                          OK instr-child.ads
23697 $ gnatls -d -s -a demo1.o
23698 demo1.adb
23699 /home/comar/local/adainclude/ada.ads
23700 /home/comar/local/adainclude/a-finali.ads
23701 /home/comar/local/adainclude/a-filico.ads
23702 /home/comar/local/adainclude/a-stream.ads
23703 /home/comar/local/adainclude/a-tags.ads
23704 gen_list.ads
23705 gen_list.adb
23706 /home/comar/local/adainclude/gnat.ads
23707 /home/comar/local/adainclude/g-io.ads
23708 instr.ads
23709 /home/comar/local/adainclude/system.ads
23710 /home/comar/local/adainclude/s-exctab.ads
23711 /home/comar/local/adainclude/s-finimp.ads
23712 /home/comar/local/adainclude/s-finroo.ads
23713 /home/comar/local/adainclude/s-secsta.ads
23714 /home/comar/local/adainclude/s-stalib.ads
23715 /home/comar/local/adainclude/s-stoele.ads
23716 /home/comar/local/adainclude/s-stratt.ads
23717 /home/comar/local/adainclude/s-tasoli.ads
23718 /home/comar/local/adainclude/s-unstyp.ads
23719 /home/comar/local/adainclude/unchconv.ads
23720 @end example
23721 @end quotation
23723 @node The Cross-Referencing Tools gnatxref and gnatfind,The Ada to HTML Converter gnathtml,The GNAT Library Browser gnatls,GNAT Utility Programs
23724 @anchor{gnat_ugn/gnat_utility_programs the-cross-referencing-tools-gnatxref-and-gnatfind}@anchor{24}@anchor{gnat_ugn/gnat_utility_programs id9}@anchor{1da}
23725 @section The Cross-Referencing Tools @cite{gnatxref} and @cite{gnatfind}
23728 @geindex gnatxref
23730 @geindex gnatfind
23732 The compiler generates cross-referencing information (unless
23733 you set the @code{-gnatx} switch), which are saved in the @code{.ali} files.
23734 This information indicates where in the source each entity is declared and
23735 referenced. Note that entities in package Standard are not included, but
23736 entities in all other predefined units are included in the output.
23738 Before using any of these two tools, you need to compile successfully your
23739 application, so that GNAT gets a chance to generate the cross-referencing
23740 information.
23742 The two tools @cite{gnatxref} and @cite{gnatfind} take advantage of this
23743 information to provide the user with the capability to easily locate the
23744 declaration and references to an entity. These tools are quite similar,
23745 the difference being that @cite{gnatfind} is intended for locating
23746 definitions and/or references to a specified entity or entities, whereas
23747 @cite{gnatxref} is oriented to generating a full report of all
23748 cross-references.
23750 To use these tools, you must not compile your application using the
23751 @emph{-gnatx} switch on the @emph{gnatmake} command line
23752 (see @ref{1d,,Building with gnatmake}). Otherwise, cross-referencing
23753 information will not be generated.
23755 Note: to invoke @cite{gnatxref} or @cite{gnatfind} with a project file,
23756 use the @cite{gnat} driver (see @ref{11f,,The GNAT Driver and Project Files}).
23758 @menu
23759 * gnatxref Switches:: 
23760 * gnatfind Switches:: 
23761 * Project Files for gnatxref and gnatfind:: 
23762 * Regular Expressions in gnatfind and gnatxref:: 
23763 * Examples of gnatxref Usage:: 
23764 * Examples of gnatfind Usage:: 
23766 @end menu
23768 @node gnatxref Switches,gnatfind Switches,,The Cross-Referencing Tools gnatxref and gnatfind
23769 @anchor{gnat_ugn/gnat_utility_programs id10}@anchor{1db}@anchor{gnat_ugn/gnat_utility_programs gnatxref-switches}@anchor{1dc}
23770 @subsection @cite{gnatxref} Switches
23773 The command invocation for @cite{gnatxref} is:
23775 @quotation
23777 @example
23778 $ gnatxref [`switches`] `sourcefile1` [`sourcefile2` ...]
23779 @end example
23780 @end quotation
23782 where
23785 @table @asis
23787 @item @emph{sourcefile1} [, @emph{sourcefile2} ...]
23789 identify the source files for which a report is to be generated. The
23790 'with'ed units will be processed too. You must provide at least one file.
23792 These file names are considered to be regular expressions, so for instance
23793 specifying @code{source*.adb} is the same as giving every file in the current
23794 directory whose name starts with @code{source} and whose extension is
23795 @code{adb}.
23797 You shouldn't specify any directory name, just base names. @emph{gnatxref}
23798 and @emph{gnatfind} will be able to locate these files by themselves using
23799 the source path. If you specify directories, no result is produced.
23800 @end table
23802 The following switches are available for @emph{gnatxref}:
23804 @geindex --version (gnatxref)
23807 @table @asis
23809 @item @code{-version}
23811 Display Copyright and version, then exit disregarding all other options.
23812 @end table
23814 @geindex --help (gnatxref)
23817 @table @asis
23819 @item @code{-help}
23821 If @emph{--version} was not used, display usage, then exit disregarding
23822 all other options.
23823 @end table
23825 @geindex -a (gnatxref)
23828 @table @asis
23830 @item @code{a}
23832 If this switch is present, @cite{gnatfind} and @cite{gnatxref} will parse
23833 the read-only files found in the library search path. Otherwise, these files
23834 will be ignored. This option can be used to protect Gnat sources or your own
23835 libraries from being parsed, thus making @cite{gnatfind} and @cite{gnatxref}
23836 much faster, and their output much smaller. Read-only here refers to access
23837 or permissions status in the file system for the current user.
23838 @end table
23840 @geindex -aIDIR (gnatxref)
23843 @table @asis
23845 @item @code{aI@emph{DIR}}
23847 When looking for source files also look in directory DIR. The order in which
23848 source file search is undertaken is the same as for @emph{gnatmake}.
23849 @end table
23851 @geindex -aODIR (gnatxref)
23854 @table @asis
23856 @item @code{aO@emph{DIR}}
23858 When searching for library and object files, look in directory
23859 DIR. The order in which library files are searched is the same as for
23860 @emph{gnatmake}.
23861 @end table
23863 @geindex -nostdinc (gnatxref)
23866 @table @asis
23868 @item @code{nostdinc}
23870 Do not look for sources in the system default directory.
23871 @end table
23873 @geindex -nostdlib (gnatxref)
23876 @table @asis
23878 @item @code{nostdlib}
23880 Do not look for library files in the system default directory.
23881 @end table
23883 @geindex --ext (gnatxref)
23886 @table @asis
23888 @item @code{-ext=@emph{extension}}
23890 Specify an alternate ali file extension. The default is @cite{ali} and other
23891 extensions (e.g. @cite{gli} for C/C++ sources when using @emph{-fdump-xref})
23892 may be specified via this switch. Note that if this switch overrides the
23893 default, which means that only the new extension will be considered.
23894 @end table
23896 @geindex --RTS (gnatxref)
23899 @table @asis
23901 @item @code{-RTS=@emph{rts-path}}
23903 Specifies the default location of the runtime library. Same meaning as the
23904 equivalent @emph{gnatmake} flag (@ref{df,,Switches for gnatmake}).
23905 @end table
23907 @geindex -d (gnatxref)
23910 @table @asis
23912 @item @code{d}
23914 If this switch is set @cite{gnatxref} will output the parent type
23915 reference for each matching derived types.
23916 @end table
23918 @geindex -f (gnatxref)
23921 @table @asis
23923 @item @code{f}
23925 If this switch is set, the output file names will be preceded by their
23926 directory (if the file was found in the search path). If this switch is
23927 not set, the directory will not be printed.
23928 @end table
23930 @geindex -g (gnatxref)
23933 @table @asis
23935 @item @code{g}
23937 If this switch is set, information is output only for library-level
23938 entities, ignoring local entities. The use of this switch may accelerate
23939 @cite{gnatfind} and @cite{gnatxref}.
23940 @end table
23942 @geindex -IDIR (gnatxref)
23945 @table @asis
23947 @item @code{I@emph{DIR}}
23949 Equivalent to @code{-aODIR -aIDIR}.
23950 @end table
23952 @geindex -pFILE (gnatxref)
23955 @table @asis
23957 @item @code{p@emph{FILE}}
23959 Specify a project file to use @ref{b,,GNAT Project Manager}.
23960 If you need to use the @code{.gpr}
23961 project files, you should use gnatxref through the GNAT driver
23962 (@emph{gnat xref -Pproject}).
23964 By default, @cite{gnatxref} and @cite{gnatfind} will try to locate a
23965 project file in the current directory.
23967 If a project file is either specified or found by the tools, then the content
23968 of the source directory and object directory lines are added as if they
23969 had been specified respectively by @code{-aI}
23970 and @code{-aO}.
23972 @item @code{u}
23974 Output only unused symbols. This may be really useful if you give your
23975 main compilation unit on the command line, as @cite{gnatxref} will then
23976 display every unused entity and 'with'ed package.
23978 @item @code{v}
23980 Instead of producing the default output, @cite{gnatxref} will generate a
23981 @code{tags} file that can be used by vi. For examples how to use this
23982 feature, see @ref{1dd,,Examples of gnatxref Usage}. The tags file is output
23983 to the standard output, thus you will have to redirect it to a file.
23984 @end table
23986 All these switches may be in any order on the command line, and may even
23987 appear after the file names. They need not be separated by spaces, thus
23988 you can say @code{gnatxref -ag} instead of @code{gnatxref -a -g}.
23990 @node gnatfind Switches,Project Files for gnatxref and gnatfind,gnatxref Switches,The Cross-Referencing Tools gnatxref and gnatfind
23991 @anchor{gnat_ugn/gnat_utility_programs id11}@anchor{1de}@anchor{gnat_ugn/gnat_utility_programs gnatfind-switches}@anchor{1df}
23992 @subsection @cite{gnatfind} Switches
23995 The command invocation for @cite{gnatfind} is:
23997 @quotation
23999 @example
24000 $ gnatfind [`switches`] `pattern`[:`sourcefile`[:`line`[:`column`]]]
24001       [`file1` `file2` ...]
24002 @end example
24003 @end quotation
24005 with the following iterpretation of the command arguments:
24008 @table @asis
24010 @item @emph{pattern}
24012 An entity will be output only if it matches the regular expression found
24013 in @cite{pattern}, see @ref{1e0,,Regular Expressions in gnatfind and gnatxref}.
24015 Omitting the pattern is equivalent to specifying @code{*}, which
24016 will match any entity. Note that if you do not provide a pattern, you
24017 have to provide both a sourcefile and a line.
24019 Entity names are given in Latin-1, with uppercase/lowercase equivalence
24020 for matching purposes. At the current time there is no support for
24021 8-bit codes other than Latin-1, or for wide characters in identifiers.
24023 @item @emph{sourcefile}
24025 @cite{gnatfind} will look for references, bodies or declarations
24026 of symbols referenced in @code{sourcefile}, at line @cite{line}
24027 and column @cite{column}. See @ref{1e1,,Examples of gnatfind Usage}
24028 for syntax examples.
24030 @item @emph{line}
24032 A decimal integer identifying the line number containing
24033 the reference to the entity (or entities) to be located.
24035 @item @emph{column}
24037 A decimal integer identifying the exact location on the
24038 line of the first character of the identifier for the
24039 entity reference. Columns are numbered from 1.
24041 @item @emph{file1 file2 ...}
24043 The search will be restricted to these source files. If none are given, then
24044 the search will be conducted for every library file in the search path.
24045 These files must appear only after the pattern or sourcefile.
24047 These file names are considered to be regular expressions, so for instance
24048 specifying @code{source*.adb} is the same as giving every file in the current
24049 directory whose name starts with @code{source} and whose extension is
24050 @code{adb}.
24052 The location of the spec of the entity will always be displayed, even if it
24053 isn't in one of @code{file1}, @code{file2}, ... The
24054 occurrences of the entity in the separate units of the ones given on the
24055 command line will also be displayed.
24057 Note that if you specify at least one file in this part, @cite{gnatfind} may
24058 sometimes not be able to find the body of the subprograms.
24059 @end table
24061 At least one of 'sourcefile' or 'pattern' has to be present on
24062 the command line.
24064 The following switches are available:
24066 @geindex --version (gnatfind)
24069 @table @asis
24071 @item @code{--version}
24073 Display Copyright and version, then exit disregarding all other options.
24074 @end table
24076 @geindex --help (gnatfind)
24079 @table @asis
24081 @item @code{-help}
24083 If @emph{--version} was not used, display usage, then exit disregarding
24084 all other options.
24085 @end table
24087 @geindex -a (gnatfind)
24090 @table @asis
24092 @item @code{a}
24094 If this switch is present, @cite{gnatfind} and @cite{gnatxref} will parse
24095 the read-only files found in the library search path. Otherwise, these files
24096 will be ignored. This option can be used to protect Gnat sources or your own
24097 libraries from being parsed, thus making @cite{gnatfind} and @cite{gnatxref}
24098 much faster, and their output much smaller. Read-only here refers to access
24099 or permission status in the file system for the current user.
24100 @end table
24102 @geindex -aIDIR (gnatfind)
24105 @table @asis
24107 @item @code{aI@emph{DIR}}
24109 When looking for source files also look in directory DIR. The order in which
24110 source file search is undertaken is the same as for @emph{gnatmake}.
24111 @end table
24113 @geindex -aODIR (gnatfind)
24116 @table @asis
24118 @item @code{aO@emph{DIR}}
24120 When searching for library and object files, look in directory
24121 DIR. The order in which library files are searched is the same as for
24122 @emph{gnatmake}.
24123 @end table
24125 @geindex -nostdinc (gnatfind)
24128 @table @asis
24130 @item @code{nostdinc}
24132 Do not look for sources in the system default directory.
24133 @end table
24135 @geindex -nostdlib (gnatfind)
24138 @table @asis
24140 @item @code{nostdlib}
24142 Do not look for library files in the system default directory.
24143 @end table
24145 @geindex --ext (gnatfind)
24148 @table @asis
24150 @item @code{-ext=@emph{extension}}
24152 Specify an alternate ali file extension. The default is @cite{ali} and other
24153 extensions (e.g. @cite{gli} for C/C++ sources when using @emph{-fdump-xref})
24154 may be specified via this switch. Note that if this switch overrides the
24155 default, which means that only the new extension will be considered.
24156 @end table
24158 @geindex --RTS (gnatfind)
24161 @table @asis
24163 @item @code{-RTS=@emph{rts-path}}
24165 Specifies the default location of the runtime library. Same meaning as the
24166 equivalent @emph{gnatmake} flag (@ref{df,,Switches for gnatmake}).
24167 @end table
24169 @geindex -d (gnatfind)
24172 @table @asis
24174 @item @code{d}
24176 If this switch is set, then @cite{gnatfind} will output the parent type
24177 reference for each matching derived types.
24178 @end table
24180 @geindex -e (gnatfind)
24183 @table @asis
24185 @item @code{e}
24187 By default, @cite{gnatfind} accept the simple regular expression set for
24188 @cite{pattern}. If this switch is set, then the pattern will be
24189 considered as full Unix-style regular expression.
24190 @end table
24192 @geindex -f (gnatfind)
24195 @table @asis
24197 @item @code{f}
24199 If this switch is set, the output file names will be preceded by their
24200 directory (if the file was found in the search path). If this switch is
24201 not set, the directory will not be printed.
24202 @end table
24204 @geindex -g (gnatfind)
24207 @table @asis
24209 @item @code{g}
24211 If this switch is set, information is output only for library-level
24212 entities, ignoring local entities. The use of this switch may accelerate
24213 @cite{gnatfind} and @cite{gnatxref}.
24214 @end table
24216 @geindex -IDIR (gnatfind)
24219 @table @asis
24221 @item @code{I@emph{DIR}}
24223 Equivalent to @code{-aODIR -aIDIR}.
24224 @end table
24226 @geindex -pFILE (gnatfind)
24229 @table @asis
24231 @item @code{p@emph{FILE}}
24233 Specify a project file (@ref{b,,GNAT Project Manager}) to use.
24234 By default, @cite{gnatxref} and @cite{gnatfind} will try to locate a
24235 project file in the current directory.
24237 If a project file is either specified or found by the tools, then the content
24238 of the source directory and object directory lines are added as if they
24239 had been specified respectively by @code{-aI} and
24240 @code{-aO}.
24241 @end table
24243 @geindex -r (gnatfind)
24246 @table @asis
24248 @item @code{r}
24250 By default, @cite{gnatfind} will output only the information about the
24251 declaration, body or type completion of the entities. If this switch is
24252 set, the @cite{gnatfind} will locate every reference to the entities in
24253 the files specified on the command line (or in every file in the search
24254 path if no file is given on the command line).
24255 @end table
24257 @geindex -s (gnatfind)
24260 @table @asis
24262 @item @code{s}
24264 If this switch is set, then @cite{gnatfind} will output the content
24265 of the Ada source file lines were the entity was found.
24266 @end table
24268 @geindex -t (gnatfind)
24271 @table @asis
24273 @item @code{t}
24275 If this switch is set, then @cite{gnatfind} will output the type hierarchy for
24276 the specified type. It act like -d option but recursively from parent
24277 type to parent type. When this switch is set it is not possible to
24278 specify more than one file.
24279 @end table
24281 All these switches may be in any order on the command line, and may even
24282 appear after the file names. They need not be separated by spaces, thus
24283 you can say @code{gnatxref -ag} instead of
24284 @code{gnatxref -a -g}.
24286 As stated previously, gnatfind will search in every directory in the
24287 search path. You can force it to look only in the current directory if
24288 you specify @cite{*} at the end of the command line.
24290 @node Project Files for gnatxref and gnatfind,Regular Expressions in gnatfind and gnatxref,gnatfind Switches,The Cross-Referencing Tools gnatxref and gnatfind
24291 @anchor{gnat_ugn/gnat_utility_programs project-files-for-gnatxref-and-gnatfind}@anchor{1e2}@anchor{gnat_ugn/gnat_utility_programs id12}@anchor{1e3}
24292 @subsection Project Files for @emph{gnatxref} and @emph{gnatfind}
24295 Project files allow a programmer to specify how to compile its
24296 application, where to find sources, etc.  These files are used
24297 primarily by GPS, but they can also be used
24298 by the two tools @cite{gnatxref} and @cite{gnatfind}.
24300 A project file name must end with @code{.gpr}. If a single one is
24301 present in the current directory, then @cite{gnatxref} and @cite{gnatfind} will
24302 extract the information from it. If multiple project files are found, none of
24303 them is read, and you have to use the @code{-p} switch to specify the one
24304 you want to use.
24306 The following lines can be included, even though most of them have default
24307 values which can be used in most cases.
24308 The lines can be entered in any order in the file.
24309 Except for @code{src_dir} and @code{obj_dir}, you can only have one instance of
24310 each line. If you have multiple instances, only the last one is taken into
24311 account.
24314 @itemize *
24316 @item 
24318 @table @asis
24320 @item @emph{src_dir=DIR}
24322 [default: @cite{"./"}].
24323 Specifies a directory where to look for source files. Multiple @cite{src_dir}
24324 lines can be specified and they will be searched in the order they
24325 are specified.
24326 @end table
24328 @item 
24330 @table @asis
24332 @item @emph{obj_dir=DIR}
24334 [default: @cite{"./"}].
24335 Specifies a directory where to look for object and library files. Multiple
24336 @cite{obj_dir} lines can be specified, and they will be searched in the order
24337 they are specified
24338 @end table
24340 @item 
24342 @table @asis
24344 @item @emph{comp_opt=SWITCHES}
24346 [default: @cite{""}].
24347 Creates a variable which can be referred to subsequently by using
24348 the @cite{$@{comp_opt@}} notation. This is intended to store the default
24349 switches given to @emph{gnatmake} and @emph{gcc}.
24350 @end table
24352 @item 
24354 @table @asis
24356 @item @emph{bind_opt=SWITCHES}
24358 [default: @cite{""}].
24359 Creates a variable which can be referred to subsequently by using
24360 the @code{$@emph{bind_opt}} notation. This is intended to store the default
24361 switches given to @emph{gnatbind}.
24362 @end table
24364 @item 
24366 @table @asis
24368 @item @emph{link_opt=SWITCHES}
24370 [default: @cite{""}].
24371 Creates a variable which can be referred to subsequently by using
24372 the @code{$@emph{link_opt}} notation. This is intended to store the default
24373 switches given to @emph{gnatlink}.
24374 @end table
24376 @item 
24378 @table @asis
24380 @item @emph{main=EXECUTABLE}
24382 [default: @cite{""}].
24383 Specifies the name of the executable for the application. This variable can
24384 be referred to in the following lines by using the @code{@emph{$@{main}} notation.
24385 @end table
24387 @item 
24389 @table @asis
24391 @item @emph{comp_cmd=COMMAND}
24393 [default: @cite{"gcc -c -I$@{src_dir@} -g -gnatq"}].
24394 Specifies the command used to compile a single file in the application.
24395 @end table
24397 @item 
24399 @table @asis
24401 @item @emph{make_cmd=COMMAND}
24403 [default: @cite{"gnatmake $@{main@} -aI$@{src_dir@} -aO$@{obj_dir@} -g -gnatq -cargs $@{comp_opt@} -bargs $@{bind_opt@} -largs $@{link_opt@}"}].
24404 Specifies the command used to recompile the whole application.
24405 @end table
24407 @item 
24409 @table @asis
24411 @item @emph{run_cmd=COMMAND}
24413 [default: @cite{"$@{main@}"}].
24414 Specifies the command used to run the application.
24415 @end table
24417 @item 
24419 @table @asis
24421 @item @emph{debug_cmd=COMMAND}
24423 [default: @cite{"gdb $@{main@}"}].
24424 Specifies the command used to debug the application
24425 @end table
24426 @end itemize
24428 @emph{gnatxref} and @emph{gnatfind} only take into account the
24429 @cite{src_dir} and @cite{obj_dir} lines, and ignore the others.
24431 @node Regular Expressions in gnatfind and gnatxref,Examples of gnatxref Usage,Project Files for gnatxref and gnatfind,The Cross-Referencing Tools gnatxref and gnatfind
24432 @anchor{gnat_ugn/gnat_utility_programs id13}@anchor{1e4}@anchor{gnat_ugn/gnat_utility_programs regular-expressions-in-gnatfind-and-gnatxref}@anchor{1e0}
24433 @subsection Regular Expressions in @cite{gnatfind} and @cite{gnatxref}
24436 As specified in the section about @emph{gnatfind}, the pattern can be a
24437 regular expression. Two kinds of regular expressions
24438 are recognized:
24441 @itemize *
24443 @item 
24445 @table @asis
24447 @item @emph{Globbing pattern}
24449 These are the most common regular expression. They are the same as are
24450 generally used in a Unix shell command line, or in a DOS session.
24452 Here is a more formal grammar:
24454 @example
24455 regexp ::= term
24456 term   ::= elmt            -- matches elmt
24457 term   ::= elmt elmt       -- concatenation (elmt then elmt)
24458 term   ::= *               -- any string of 0 or more characters
24459 term   ::= ?               -- matches any character
24460 term   ::= [char @{char@}]   -- matches any character listed
24461 term   ::= [char - char]   -- matches any character in range
24462 @end example
24463 @end table
24465 @item 
24467 @table @asis
24469 @item @emph{Full regular expression}
24471 The second set of regular expressions is much more powerful. This is the
24472 type of regular expressions recognized by utilities such as @code{grep}.
24474 The following is the form of a regular expression, expressed in same BNF
24475 style as is found in the Ada Reference Manual:
24477 @example
24478 regexp ::= term @{| term@}   -- alternation (term or term ...)
24480 term ::= item @{item@}       -- concatenation (item then item)
24482 item ::= elmt              -- match elmt
24483 item ::= elmt *            -- zero or more elmt's
24484 item ::= elmt +            -- one or more elmt's
24485 item ::= elmt ?            -- matches elmt or nothing
24487 elmt ::= nschar            -- matches given character
24488 elmt ::= [nschar @{nschar@}]   -- matches any character listed
24489 elmt ::= [^ nschar @{nschar@}] -- matches any character not listed
24490 elmt ::= [char - char]     -- matches chars in given range
24491 elmt ::= \\ char            -- matches given character
24492 elmt ::= .                 -- matches any single character
24493 elmt ::= ( regexp )        -- parens used for grouping
24495 char ::= any character, including special characters
24496 nschar ::= any character except ()[].*+?^
24497 @end example
24499 Here are a few examples:
24501 @quotation
24504 @table @asis
24506 @item @code{abcde|fghi}
24508 will match any of the two strings @code{abcde} and @code{fghi},
24510 @item @code{abc*d}
24512 will match any string like @code{abd}, @code{abcd}, @code{abccd},
24513 @code{abcccd}, and so on,
24515 @item @code{[a-z]+}
24517 will match any string which has only lowercase characters in it (and at
24518 least one character.
24519 @end table
24520 @end quotation
24521 @end table
24522 @end itemize
24524 @node Examples of gnatxref Usage,Examples of gnatfind Usage,Regular Expressions in gnatfind and gnatxref,The Cross-Referencing Tools gnatxref and gnatfind
24525 @anchor{gnat_ugn/gnat_utility_programs examples-of-gnatxref-usage}@anchor{1dd}@anchor{gnat_ugn/gnat_utility_programs id14}@anchor{1e5}
24526 @subsection Examples of @cite{gnatxref} Usage
24529 @menu
24530 * General Usage:: 
24531 * Using gnatxref with vi:: 
24533 @end menu
24535 @node General Usage,Using gnatxref with vi,,Examples of gnatxref Usage
24536 @anchor{gnat_ugn/gnat_utility_programs general-usage}@anchor{1e6}
24537 @subsubsection General Usage
24540 For the following examples, we will consider the following units:
24542 @quotation
24544 @example
24545 main.ads:
24546 1: with Bar;
24547 2: package Main is
24548 3:     procedure Foo (B : in Integer);
24549 4:     C : Integer;
24550 5: private
24551 6:     D : Integer;
24552 7: end Main;
24554 main.adb:
24555 1: package body Main is
24556 2:     procedure Foo (B : in Integer) is
24557 3:     begin
24558 4:        C := B;
24559 5:        D := B;
24560 6:        Bar.Print (B);
24561 7:        Bar.Print (C);
24562 8:     end Foo;
24563 9: end Main;
24565 bar.ads:
24566 1: package Bar is
24567 2:     procedure Print (B : Integer);
24568 3: end bar;
24569 @end example
24570 @end quotation
24572 The first thing to do is to recompile your application (for instance, in
24573 that case just by doing a @code{gnatmake main}, so that GNAT generates
24574 the cross-referencing information.
24575 You can then issue any of the following commands:
24577 @quotation
24580 @itemize *
24582 @item 
24583 @code{gnatxref main.adb}
24584 @cite{gnatxref} generates cross-reference information for main.adb
24585 and every unit 'with'ed by main.adb.
24587 The output would be:
24589 @quotation
24591 @example
24592 B                                                      Type: Integer
24593   Decl: bar.ads           2:22
24594 B                                                      Type: Integer
24595   Decl: main.ads          3:20
24596   Body: main.adb          2:20
24597   Ref:  main.adb          4:13     5:13     6:19
24598 Bar                                                    Type: Unit
24599   Decl: bar.ads           1:9
24600   Ref:  main.adb          6:8      7:8
24601        main.ads           1:6
24602 C                                                      Type: Integer
24603   Decl: main.ads          4:5
24604   Modi: main.adb          4:8
24605   Ref:  main.adb          7:19
24606 D                                                      Type: Integer
24607   Decl: main.ads          6:5
24608   Modi: main.adb          5:8
24609 Foo                                                    Type: Unit
24610   Decl: main.ads          3:15
24611   Body: main.adb          2:15
24612 Main                                                    Type: Unit
24613   Decl: main.ads          2:9
24614   Body: main.adb          1:14
24615 Print                                                   Type: Unit
24616   Decl: bar.ads           2:15
24617   Ref:  main.adb          6:12     7:12
24618 @end example
24619 @end quotation
24621 This shows that the entity @cite{Main} is declared in main.ads, line 2, column 9,
24622 its body is in main.adb, line 1, column 14 and is not referenced any where.
24624 The entity @cite{Print} is declared in bar.ads, line 2, column 15 and it
24625 is referenced in main.adb, line 6 column 12 and line 7 column 12.
24627 @item 
24628 @code{gnatxref package1.adb package2.ads}
24629 @cite{gnatxref} will generates cross-reference information for
24630 package1.adb, package2.ads and any other package 'with'ed by any
24631 of these.
24632 @end itemize
24633 @end quotation
24635 @node Using gnatxref with vi,,General Usage,Examples of gnatxref Usage
24636 @anchor{gnat_ugn/gnat_utility_programs using-gnatxref-with-vi}@anchor{1e7}
24637 @subsubsection Using gnatxref with vi
24640 @cite{gnatxref} can generate a tags file output, which can be used
24641 directly from @emph{vi}. Note that the standard version of @emph{vi}
24642 will not work properly with overloaded symbols. Consider using another
24643 free implementation of @emph{vi}, such as @emph{vim}.
24645 @quotation
24647 @example
24648 $ gnatxref -v gnatfind.adb > tags
24649 @end example
24650 @end quotation
24652 The following command will generate the tags file for @cite{gnatfind} itself
24653 (if the sources are in the search path!):
24655 @quotation
24657 @example
24658 $ gnatxref -v gnatfind.adb > tags
24659 @end example
24660 @end quotation
24662 From @emph{vi}, you can then use the command @code{:tag @emph{entity}}
24663 (replacing @cite{entity} by whatever you are looking for), and vi will
24664 display a new file with the corresponding declaration of entity.
24666 @node Examples of gnatfind Usage,,Examples of gnatxref Usage,The Cross-Referencing Tools gnatxref and gnatfind
24667 @anchor{gnat_ugn/gnat_utility_programs id15}@anchor{1e8}@anchor{gnat_ugn/gnat_utility_programs examples-of-gnatfind-usage}@anchor{1e1}
24668 @subsection Examples of @cite{gnatfind} Usage
24672 @itemize *
24674 @item 
24675 @code{gnatfind -f xyz:main.adb}
24676 Find declarations for all entities xyz referenced at least once in
24677 main.adb. The references are search in every library file in the search
24678 path.
24680 The directories will be printed as well (as the @code{-f}
24681 switch is set)
24683 The output will look like:
24685 @quotation
24687 @example
24688 directory/main.ads:106:14: xyz <= declaration
24689 directory/main.adb:24:10: xyz <= body
24690 directory/foo.ads:45:23: xyz <= declaration
24691 @end example
24692 @end quotation
24694 I.e., one of the entities xyz found in main.adb is declared at
24695 line 12 of main.ads (and its body is in main.adb), and another one is
24696 declared at line 45 of foo.ads
24698 @item 
24699 @code{gnatfind -fs xyz:main.adb}
24700 This is the same command as the previous one, but @cite{gnatfind} will
24701 display the content of the Ada source file lines.
24703 The output will look like:
24705 @example
24706 directory/main.ads:106:14: xyz <= declaration
24707    procedure xyz;
24708 directory/main.adb:24:10: xyz <= body
24709    procedure xyz is
24710 directory/foo.ads:45:23: xyz <= declaration
24711    xyz : Integer;
24712 @end example
24714 This can make it easier to find exactly the location your are looking
24715 for.
24717 @item 
24718 @code{gnatfind -r "*x*":main.ads:123 foo.adb}
24719 Find references to all entities containing an x that are
24720 referenced on line 123 of main.ads.
24721 The references will be searched only in main.ads and foo.adb.
24723 @item 
24724 @code{gnatfind main.ads:123}
24725 Find declarations and bodies for all entities that are referenced on
24726 line 123 of main.ads.
24728 This is the same as @code{gnatfind "*":main.adb:123`}
24730 @item 
24731 @code{gnatfind mydir/main.adb:123:45}
24732 Find the declaration for the entity referenced at column 45 in
24733 line 123 of file main.adb in directory mydir. Note that it
24734 is usual to omit the identifier name when the column is given,
24735 since the column position identifies a unique reference.
24737 The column has to be the beginning of the identifier, and should not
24738 point to any character in the middle of the identifier.
24739 @end itemize
24741 @node The Ada to HTML Converter gnathtml,,The Cross-Referencing Tools gnatxref and gnatfind,GNAT Utility Programs
24742 @anchor{gnat_ugn/gnat_utility_programs the-ada-to-html-converter-gnathtml}@anchor{25}@anchor{gnat_ugn/gnat_utility_programs id16}@anchor{1e9}
24743 @section The Ada to HTML Converter @cite{gnathtml}
24746 @geindex gnathtml
24748 @emph{gnathtml} is a Perl script that allows Ada source files to be browsed using
24749 standard Web browsers. For installation information, see @ref{1ea,,Installing gnathtml}.
24751 Ada reserved keywords are highlighted in a bold font and Ada comments in
24752 a blue font. Unless your program was compiled with the gcc @emph{-gnatx}
24753 switch to suppress the generation of cross-referencing information, user
24754 defined variables and types will appear in a different color; you will
24755 be able to click on any identifier and go to its declaration.
24757 @menu
24758 * Invoking gnathtml:: 
24759 * Installing gnathtml:: 
24761 @end menu
24763 @node Invoking gnathtml,Installing gnathtml,,The Ada to HTML Converter gnathtml
24764 @anchor{gnat_ugn/gnat_utility_programs invoking-gnathtml}@anchor{1eb}@anchor{gnat_ugn/gnat_utility_programs id17}@anchor{1ec}
24765 @subsection Invoking @emph{gnathtml}
24768 The command line is as follows:
24770 @quotation
24772 @example
24773 $ perl gnathtml.pl [`switches`] `ada-files`
24774 @end example
24775 @end quotation
24777 You can specify as many Ada files as you want. @cite{gnathtml} will generate
24778 an html file for every ada file, and a global file called @code{index.htm}.
24779 This file is an index of every identifier defined in the files.
24781 The following switches are available:
24783 @geindex -83 (gnathtml)
24786 @table @asis
24788 @item @code{83}
24790 Only the Ada 83 subset of keywords will be highlighted.
24791 @end table
24793 @geindex -cc (gnathtml)
24796 @table @asis
24798 @item @code{cc @emph{color}}
24800 This option allows you to change the color used for comments. The default
24801 value is green. The color argument can be any name accepted by html.
24802 @end table
24804 @geindex -d (gnathtml)
24807 @table @asis
24809 @item @code{d}
24811 If the Ada files depend on some other files (for instance through
24812 @cite{with} clauses, the latter files will also be converted to html.
24813 Only the files in the user project will be converted to html, not the files
24814 in the run-time library itself.
24815 @end table
24817 @geindex -D (gnathtml)
24820 @table @asis
24822 @item @code{D}
24824 This command is the same as @emph{-d} above, but @emph{gnathtml} will
24825 also look for files in the run-time library, and generate html files for them.
24826 @end table
24828 @geindex -ext (gnathtml)
24831 @table @asis
24833 @item @code{ext @emph{extension}}
24835 This option allows you to change the extension of the generated HTML files.
24836 If you do not specify an extension, it will default to @code{htm}.
24837 @end table
24839 @geindex -f (gnathtml)
24842 @table @asis
24844 @item @code{f}
24846 By default, gnathtml will generate html links only for global entities
24847 ('with'ed units, global variables and types,...).  If you specify
24848 @emph{-f} on the command line, then links will be generated for local
24849 entities too.
24850 @end table
24852 @geindex -l (gnathtml)
24855 @table @asis
24857 @item @code{l @emph{number}}
24859 If this switch is provided and @cite{number} is not 0, then
24860 @cite{gnathtml} will number the html files every @cite{number} line.
24861 @end table
24863 @geindex -I (gnathtml)
24866 @table @asis
24868 @item @code{I @emph{dir}}
24870 Specify a directory to search for library files (@code{.ALI} files) and
24871 source files. You can provide several -I switches on the command line,
24872 and the directories will be parsed in the order of the command line.
24873 @end table
24875 @geindex -o (gnathtml)
24878 @table @asis
24880 @item @code{o @emph{dir}}
24882 Specify the output directory for html files. By default, gnathtml will
24883 saved the generated html files in a subdirectory named @code{html/}.
24884 @end table
24886 @geindex -p (gnathtml)
24889 @table @asis
24891 @item @code{p @emph{file}}
24893 If you are using Emacs and the most recent Emacs Ada mode, which provides
24894 a full Integrated Development Environment for compiling, checking,
24895 running and debugging applications, you may use @code{.gpr} files
24896 to give the directories where Emacs can find sources and object files.
24898 Using this switch, you can tell gnathtml to use these files.
24899 This allows you to get an html version of your application, even if it
24900 is spread over multiple directories.
24901 @end table
24903 @geindex -sc (gnathtml)
24906 @table @asis
24908 @item @code{sc @emph{color}}
24910 This switch allows you to change the color used for symbol
24911 definitions.
24912 The default value is red. The color argument can be any name accepted by html.
24913 @end table
24915 @geindex -t (gnathtml)
24918 @table @asis
24920 @item @code{t @emph{file}}
24922 This switch provides the name of a file. This file contains a list of
24923 file names to be converted, and the effect is exactly as though they had
24924 appeared explicitly on the command line. This
24925 is the recommended way to work around the command line length limit on some
24926 systems.
24927 @end table
24929 @node Installing gnathtml,,Invoking gnathtml,The Ada to HTML Converter gnathtml
24930 @anchor{gnat_ugn/gnat_utility_programs installing-gnathtml}@anchor{1ea}@anchor{gnat_ugn/gnat_utility_programs id18}@anchor{1ed}
24931 @subsection Installing @cite{gnathtml}
24934 @cite{Perl} needs to be installed on your machine to run this script.
24935 @cite{Perl} is freely available for almost every architecture and
24936 operating system via the Internet.
24938 On Unix systems, you  may want to modify  the  first line of  the script
24939 @cite{gnathtml},  to explicitly  specify  where Perl
24940 is located. The syntax of this line is:
24942 @quotation
24944 @example
24945 #!full_path_name_to_perl
24946 @end example
24947 @end quotation
24949 Alternatively, you may run the script using the following command line:
24951 @quotation
24953 @example
24954 $ perl gnathtml.pl [`switches`] `files`
24955 @end example
24956 @end quotation
24958 @c -- +---------------------------------------------------------------------+
24960 @c -- | The following sections are present only in the PRO and GPL editions |
24962 @c -- +---------------------------------------------------------------------+
24970 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
24972 @node GNAT and Program Execution,Platform-Specific Information,GNAT Utility Programs,Top
24973 @anchor{gnat_ugn/gnat_and_program_execution gnat-and-program-execution}@anchor{e}@anchor{gnat_ugn/gnat_and_program_execution doc}@anchor{1ee}@anchor{gnat_ugn/gnat_and_program_execution id1}@anchor{1ef}
24974 @chapter GNAT and Program Execution
24977 This chapter covers several topics:
24980 @itemize *
24982 @item 
24983 @ref{1f0,,Running and Debugging Ada Programs}
24985 @item 
24986 @ref{1f1,,Code Coverage and Profiling}
24988 @item 
24989 @ref{1f2,,Improving Performance}
24991 @item 
24992 @ref{1f3,,Overflow Check Handling in GNAT}
24994 @item 
24995 @ref{1f4,,Performing Dimensionality Analysis in GNAT}
24997 @item 
24998 @ref{1f5,,Stack Related Facilities}
25000 @item 
25001 @ref{1f6,,Memory Management Issues}
25002 @end itemize
25004 @menu
25005 * Running and Debugging Ada Programs:: 
25006 * Code Coverage and Profiling:: 
25007 * Improving Performance:: 
25008 * Overflow Check Handling in GNAT:: 
25009 * Performing Dimensionality Analysis in GNAT:: 
25010 * Stack Related Facilities:: 
25011 * Memory Management Issues:: 
25013 @end menu
25015 @node Running and Debugging Ada Programs,Code Coverage and Profiling,,GNAT and Program Execution
25016 @anchor{gnat_ugn/gnat_and_program_execution id2}@anchor{1f0}@anchor{gnat_ugn/gnat_and_program_execution running-and-debugging-ada-programs}@anchor{26}
25017 @section Running and Debugging Ada Programs
25020 @geindex Debugging
25022 This section discusses how to debug Ada programs.
25024 An incorrect Ada program may be handled in three ways by the GNAT compiler:
25027 @itemize *
25029 @item 
25030 The illegality may be a violation of the static semantics of Ada. In
25031 that case GNAT diagnoses the constructs in the program that are illegal.
25032 It is then a straightforward matter for the user to modify those parts of
25033 the program.
25035 @item 
25036 The illegality may be a violation of the dynamic semantics of Ada. In
25037 that case the program compiles and executes, but may generate incorrect
25038 results, or may terminate abnormally with some exception.
25040 @item 
25041 When presented with a program that contains convoluted errors, GNAT
25042 itself may terminate abnormally without providing full diagnostics on
25043 the incorrect user program.
25044 @end itemize
25046 @geindex Debugger
25048 @geindex gdb
25050 @menu
25051 * The GNAT Debugger GDB:: 
25052 * Running GDB:: 
25053 * Introduction to GDB Commands:: 
25054 * Using Ada Expressions:: 
25055 * Calling User-Defined Subprograms:: 
25056 * Using the next Command in a Function:: 
25057 * Stopping When Ada Exceptions Are Raised:: 
25058 * Ada Tasks:: 
25059 * Debugging Generic Units:: 
25060 * Remote Debugging with gdbserver:: 
25061 * GNAT Abnormal Termination or Failure to Terminate:: 
25062 * Naming Conventions for GNAT Source Files:: 
25063 * Getting Internal Debugging Information:: 
25064 * Stack Traceback:: 
25066 @end menu
25068 @node The GNAT Debugger GDB,Running GDB,,Running and Debugging Ada Programs
25069 @anchor{gnat_ugn/gnat_and_program_execution the-gnat-debugger-gdb}@anchor{1f7}@anchor{gnat_ugn/gnat_and_program_execution id3}@anchor{1f8}
25070 @subsection The GNAT Debugger GDB
25073 @cite{GDB} is a general purpose, platform-independent debugger that
25074 can be used to debug mixed-language programs compiled with @emph{gcc},
25075 and in particular is capable of debugging Ada programs compiled with
25076 GNAT. The latest versions of @cite{GDB} are Ada-aware and can handle
25077 complex Ada data structures.
25079 See @cite{Debugging with GDB},
25080 for full details on the usage of @cite{GDB}, including a section on
25081 its usage on programs. This manual should be consulted for full
25082 details. The section that follows is a brief introduction to the
25083 philosophy and use of @cite{GDB}.
25085 When GNAT programs are compiled, the compiler optionally writes debugging
25086 information into the generated object file, including information on
25087 line numbers, and on declared types and variables. This information is
25088 separate from the generated code. It makes the object files considerably
25089 larger, but it does not add to the size of the actual executable that
25090 will be loaded into memory, and has no impact on run-time performance. The
25091 generation of debug information is triggered by the use of the
25092 -g switch in the @emph{gcc} or @emph{gnatmake} command
25093 used to carry out the compilations. It is important to emphasize that
25094 the use of these options does not change the generated code.
25096 The debugging information is written in standard system formats that
25097 are used by many tools, including debuggers and profilers. The format
25098 of the information is typically designed to describe C types and
25099 semantics, but GNAT implements a translation scheme which allows full
25100 details about Ada types and variables to be encoded into these
25101 standard C formats. Details of this encoding scheme may be found in
25102 the file exp_dbug.ads in the GNAT source distribution. However, the
25103 details of this encoding are, in general, of no interest to a user,
25104 since @cite{GDB} automatically performs the necessary decoding.
25106 When a program is bound and linked, the debugging information is
25107 collected from the object files, and stored in the executable image of
25108 the program. Again, this process significantly increases the size of
25109 the generated executable file, but it does not increase the size of
25110 the executable program itself. Furthermore, if this program is run in
25111 the normal manner, it runs exactly as if the debug information were
25112 not present, and takes no more actual memory.
25114 However, if the program is run under control of @cite{GDB}, the
25115 debugger is activated.  The image of the program is loaded, at which
25116 point it is ready to run.  If a run command is given, then the program
25117 will run exactly as it would have if @cite{GDB} were not present. This
25118 is a crucial part of the @cite{GDB} design philosophy.  @cite{GDB} is
25119 entirely non-intrusive until a breakpoint is encountered.  If no
25120 breakpoint is ever hit, the program will run exactly as it would if no
25121 debugger were present. When a breakpoint is hit, @cite{GDB} accesses
25122 the debugging information and can respond to user commands to inspect
25123 variables, and more generally to report on the state of execution.
25125 @node Running GDB,Introduction to GDB Commands,The GNAT Debugger GDB,Running and Debugging Ada Programs
25126 @anchor{gnat_ugn/gnat_and_program_execution id4}@anchor{1f9}@anchor{gnat_ugn/gnat_and_program_execution running-gdb}@anchor{1fa}
25127 @subsection Running GDB
25130 This section describes how to initiate the debugger.
25132 The debugger can be launched from a @cite{GPS} menu or
25133 directly from the command line. The description below covers the latter use.
25134 All the commands shown can be used in the @cite{GPS} debug console window,
25135 but there are usually more GUI-based ways to achieve the same effect.
25137 The command to run @cite{GDB} is
25139 @quotation
25141 @example
25142 $ gdb program
25143 @end example
25144 @end quotation
25146 where @cite{program} is the name of the executable file. This
25147 activates the debugger and results in a prompt for debugger commands.
25148 The simplest command is simply @cite{run}, which causes the program to run
25149 exactly as if the debugger were not present. The following section
25150 describes some of the additional commands that can be given to @cite{GDB}.
25152 @node Introduction to GDB Commands,Using Ada Expressions,Running GDB,Running and Debugging Ada Programs
25153 @anchor{gnat_ugn/gnat_and_program_execution introduction-to-gdb-commands}@anchor{1fb}@anchor{gnat_ugn/gnat_and_program_execution id5}@anchor{1fc}
25154 @subsection Introduction to GDB Commands
25157 @cite{GDB} contains a large repertoire of commands.
25158 See @cite{Debugging with GDB} for extensive documentation on the use
25159 of these commands, together with examples of their use. Furthermore,
25160 the command @emph{help} invoked from within GDB activates a simple help
25161 facility which summarizes the available commands and their options.
25162 In this section we summarize a few of the most commonly
25163 used commands to give an idea of what @cite{GDB} is about. You should create
25164 a simple program with debugging information and experiment with the use of
25165 these @cite{GDB} commands on the program as you read through the
25166 following section.
25169 @itemize *
25171 @item 
25173 @table @asis
25175 @item @emph{set args `arguments`}
25177 The @cite{arguments} list above is a list of arguments to be passed to
25178 the program on a subsequent run command, just as though the arguments
25179 had been entered on a normal invocation of the program. The @cite{set args}
25180 command is not needed if the program does not require arguments.
25181 @end table
25183 @item 
25185 @table @asis
25187 @item @emph{run}
25189 The @cite{run} command causes execution of the program to start from
25190 the beginning. If the program is already running, that is to say if
25191 you are currently positioned at a breakpoint, then a prompt will ask
25192 for confirmation that you want to abandon the current execution and
25193 restart.
25194 @end table
25196 @item 
25198 @table @asis
25200 @item @emph{breakpoint `location`}
25202 The breakpoint command sets a breakpoint, that is to say a point at which
25203 execution will halt and @cite{GDB} will await further
25204 commands. @cite{location} is
25205 either a line number within a file, given in the format @cite{file:linenumber},
25206 or it is the name of a subprogram. If you request that a breakpoint be set on
25207 a subprogram that is overloaded, a prompt will ask you to specify on which of
25208 those subprograms you want to breakpoint. You can also
25209 specify that all of them should be breakpointed. If the program is run
25210 and execution encounters the breakpoint, then the program
25211 stops and @cite{GDB} signals that the breakpoint was encountered by
25212 printing the line of code before which the program is halted.
25213 @end table
25215 @item 
25217 @table @asis
25219 @item @emph{catch exception `name`}
25221 This command causes the program execution to stop whenever exception
25222 @cite{name} is raised.  If @cite{name} is omitted, then the execution is
25223 suspended when any exception is raised.
25224 @end table
25226 @item 
25228 @table @asis
25230 @item @emph{print `expression`}
25232 This will print the value of the given expression. Most simple
25233 Ada expression formats are properly handled by @cite{GDB}, so the expression
25234 can contain function calls, variables, operators, and attribute references.
25235 @end table
25237 @item 
25239 @table @asis
25241 @item @emph{continue}
25243 Continues execution following a breakpoint, until the next breakpoint or the
25244 termination of the program.
25245 @end table
25247 @item 
25249 @table @asis
25251 @item @emph{step}
25253 Executes a single line after a breakpoint. If the next statement
25254 is a subprogram call, execution continues into (the first statement of)
25255 the called subprogram.
25256 @end table
25258 @item 
25260 @table @asis
25262 @item @emph{next}
25264 Executes a single line. If this line is a subprogram call, executes and
25265 returns from the call.
25266 @end table
25268 @item 
25270 @table @asis
25272 @item @emph{list}
25274 Lists a few lines around the current source location. In practice, it
25275 is usually more convenient to have a separate edit window open with the
25276 relevant source file displayed. Successive applications of this command
25277 print subsequent lines. The command can be given an argument which is a
25278 line number, in which case it displays a few lines around the specified one.
25279 @end table
25281 @item 
25283 @table @asis
25285 @item @emph{backtrace}
25287 Displays a backtrace of the call chain. This command is typically
25288 used after a breakpoint has occurred, to examine the sequence of calls that
25289 leads to the current breakpoint. The display includes one line for each
25290 activation record (frame) corresponding to an active subprogram.
25291 @end table
25293 @item 
25295 @table @asis
25297 @item @emph{up}
25299 At a breakpoint, @cite{GDB} can display the values of variables local
25300 to the current frame. The command @cite{up} can be used to
25301 examine the contents of other active frames, by moving the focus up
25302 the stack, that is to say from callee to caller, one frame at a time.
25303 @end table
25305 @item 
25307 @table @asis
25309 @item @emph{down}
25311 Moves the focus of @cite{GDB} down from the frame currently being
25312 examined to the frame of its callee (the reverse of the previous command),
25313 @end table
25315 @item 
25317 @table @asis
25319 @item @emph{frame `n`}
25321 Inspect the frame with the given number. The value 0 denotes the frame
25322 of the current breakpoint, that is to say the top of the call stack.
25323 @end table
25325 @item 
25327 @table @asis
25329 @item @emph{kill}
25331 Kills the child process in which the program is running under GDB.
25332 This may be useful for several purposes:
25335 @itemize *
25337 @item 
25338 It allows you to recompile and relink your program, since on many systems
25339 you cannot regenerate an executable file while it is running in a process.
25341 @item 
25342 You can run your program outside the debugger, on systems that do not
25343 permit executing a program outside GDB while breakpoints are set
25344 within GDB.
25346 @item 
25347 It allows you to debug a core dump rather than a running process.
25348 @end itemize
25349 @end table
25350 @end itemize
25352 The above list is a very short introduction to the commands that
25353 @cite{GDB} provides. Important additional capabilities, including conditional
25354 breakpoints, the ability to execute command sequences on a breakpoint,
25355 the ability to debug at the machine instruction level and many other
25356 features are described in detail in @cite{Debugging with GDB}.
25357 Note that most commands can be abbreviated
25358 (for example, c for continue, bt for backtrace).
25360 @node Using Ada Expressions,Calling User-Defined Subprograms,Introduction to GDB Commands,Running and Debugging Ada Programs
25361 @anchor{gnat_ugn/gnat_and_program_execution id6}@anchor{1fd}@anchor{gnat_ugn/gnat_and_program_execution using-ada-expressions}@anchor{1fe}
25362 @subsection Using Ada Expressions
25365 @geindex Ada expressions (in gdb)
25367 @cite{GDB} supports a fairly large subset of Ada expression syntax, with some
25368 extensions. The philosophy behind the design of this subset is
25370 @quotation
25373 @itemize *
25375 @item 
25376 That @cite{GDB} should provide basic literals and access to operations for
25377 arithmetic, dereferencing, field selection, indexing, and subprogram calls,
25378 leaving more sophisticated computations to subprograms written into the
25379 program (which therefore may be called from @cite{GDB}).
25381 @item 
25382 That type safety and strict adherence to Ada language restrictions
25383 are not particularly relevant in a debugging context.
25385 @item 
25386 That brevity is important to the @cite{GDB} user.
25387 @end itemize
25388 @end quotation
25390 Thus, for brevity, the debugger acts as if there were
25391 implicit @cite{with} and @cite{use} clauses in effect for all user-written
25392 packages, thus making it unnecessary to fully qualify most names with
25393 their packages, regardless of context. Where this causes ambiguity,
25394 @cite{GDB} asks the user's intent.
25396 For details on the supported Ada syntax, see @cite{Debugging with GDB}.
25398 @node Calling User-Defined Subprograms,Using the next Command in a Function,Using Ada Expressions,Running and Debugging Ada Programs
25399 @anchor{gnat_ugn/gnat_and_program_execution id7}@anchor{1ff}@anchor{gnat_ugn/gnat_and_program_execution calling-user-defined-subprograms}@anchor{200}
25400 @subsection Calling User-Defined Subprograms
25403 An important capability of @cite{GDB} is the ability to call user-defined
25404 subprograms while debugging. This is achieved simply by entering
25405 a subprogram call statement in the form:
25407 @quotation
25409 @example
25410 call subprogram-name (parameters)
25411 @end example
25412 @end quotation
25414 The keyword @cite{call} can be omitted in the normal case where the
25415 @cite{subprogram-name} does not coincide with any of the predefined
25416 @cite{GDB} commands.
25418 The effect is to invoke the given subprogram, passing it the
25419 list of parameters that is supplied. The parameters can be expressions and
25420 can include variables from the program being debugged. The
25421 subprogram must be defined
25422 at the library level within your program, and @cite{GDB} will call the
25423 subprogram within the environment of your program execution (which
25424 means that the subprogram is free to access or even modify variables
25425 within your program).
25427 The most important use of this facility is in allowing the inclusion of
25428 debugging routines that are tailored to particular data structures
25429 in your program. Such debugging routines can be written to provide a suitably
25430 high-level description of an abstract type, rather than a low-level dump
25431 of its physical layout. After all, the standard
25432 @cite{GDB print} command only knows the physical layout of your
25433 types, not their abstract meaning. Debugging routines can provide information
25434 at the desired semantic level and are thus enormously useful.
25436 For example, when debugging GNAT itself, it is crucial to have access to
25437 the contents of the tree nodes used to represent the program internally.
25438 But tree nodes are represented simply by an integer value (which in turn
25439 is an index into a table of nodes).
25440 Using the @cite{print} command on a tree node would simply print this integer
25441 value, which is not very useful. But the PN routine (defined in file
25442 treepr.adb in the GNAT sources) takes a tree node as input, and displays
25443 a useful high level representation of the tree node, which includes the
25444 syntactic category of the node, its position in the source, the integers
25445 that denote descendant nodes and parent node, as well as varied
25446 semantic information. To study this example in more detail, you might want to
25447 look at the body of the PN procedure in the stated file.
25449 Another useful application of this capability is to deal with situations of
25450 complex data which are not handled suitably by GDB. For example, if you specify
25451 Convention Fortran for a multi-dimensional array, GDB does not know that
25452 the ordering of array elements has been switched and will not properly
25453 address the array elements. In such a case, instead of trying to print the
25454 elements directly from GDB, you can write a callable procedure that prints
25455 the elements in the desired format.
25457 @node Using the next Command in a Function,Stopping When Ada Exceptions Are Raised,Calling User-Defined Subprograms,Running and Debugging Ada Programs
25458 @anchor{gnat_ugn/gnat_and_program_execution using-the-next-command-in-a-function}@anchor{201}@anchor{gnat_ugn/gnat_and_program_execution id8}@anchor{202}
25459 @subsection Using the @emph{next} Command in a Function
25462 When you use the @cite{next} command in a function, the current source
25463 location will advance to the next statement as usual. A special case
25464 arises in the case of a @cite{return} statement.
25466 Part of the code for a return statement is the 'epilogue' of the function.
25467 This is the code that returns to the caller. There is only one copy of
25468 this epilogue code, and it is typically associated with the last return
25469 statement in the function if there is more than one return. In some
25470 implementations, this epilogue is associated with the first statement
25471 of the function.
25473 The result is that if you use the @cite{next} command from a return
25474 statement that is not the last return statement of the function you
25475 may see a strange apparent jump to the last return statement or to
25476 the start of the function. You should simply ignore this odd jump.
25477 The value returned is always that from the first return statement
25478 that was stepped through.
25480 @node Stopping When Ada Exceptions Are Raised,Ada Tasks,Using the next Command in a Function,Running and Debugging Ada Programs
25481 @anchor{gnat_ugn/gnat_and_program_execution stopping-when-ada-exceptions-are-raised}@anchor{203}@anchor{gnat_ugn/gnat_and_program_execution id9}@anchor{204}
25482 @subsection Stopping When Ada Exceptions Are Raised
25485 @geindex Exceptions (in gdb)
25487 You can set catchpoints that stop the program execution when your program
25488 raises selected exceptions.
25491 @itemize *
25493 @item 
25495 @table @asis
25497 @item @emph{catch exception}
25499 Set a catchpoint that stops execution whenever (any task in the) program
25500 raises any exception.
25501 @end table
25503 @item 
25505 @table @asis
25507 @item @emph{catch exception `name`}
25509 Set a catchpoint that stops execution whenever (any task in the) program
25510 raises the exception @cite{name}.
25511 @end table
25513 @item 
25515 @table @asis
25517 @item @emph{catch exception unhandled}
25519 Set a catchpoint that stops executing whenever (any task in the) program
25520 raises an exception for which there is no handler.
25521 @end table
25523 @item 
25525 @table @asis
25527 @item @emph{info exceptions}, @emph{info exceptions `regexp`}
25529 The @cite{info exceptions} command permits the user to examine all defined
25530 exceptions within Ada programs. With a regular expression, @cite{regexp}, as
25531 argument, prints out only those exceptions whose name matches @cite{regexp}.
25532 @end table
25533 @end itemize
25535 @geindex Tasks (in gdb)
25537 @node Ada Tasks,Debugging Generic Units,Stopping When Ada Exceptions Are Raised,Running and Debugging Ada Programs
25538 @anchor{gnat_ugn/gnat_and_program_execution ada-tasks}@anchor{205}@anchor{gnat_ugn/gnat_and_program_execution id10}@anchor{206}
25539 @subsection Ada Tasks
25542 @cite{GDB} allows the following task-related commands:
25545 @itemize *
25547 @item 
25549 @table @asis
25551 @item @emph{info tasks}
25553 This command shows a list of current Ada tasks, as in the following example:
25555 @example
25556 (gdb) info tasks
25557   ID       TID P-ID   Thread Pri State                 Name
25558    1   8088000   0   807e000  15 Child Activation Wait main_task
25559    2   80a4000   1   80ae000  15 Accept/Select Wait    b
25560    3   809a800   1   80a4800  15 Child Activation Wait a
25561 *  4   80ae800   3   80b8000  15 Running               c
25562 @end example
25564 In this listing, the asterisk before the first task indicates it to be the
25565 currently running task. The first column lists the task ID that is used
25566 to refer to tasks in the following commands.
25567 @end table
25568 @end itemize
25570 @geindex Breakpoints and tasks
25573 @itemize *
25575 @item 
25576 @emph{break `linespec` task `taskid`}, @emph{break `linespec` task `taskid` if ...}
25578 @quotation
25580 These commands are like the @cite{break ... thread ...}.
25581 @cite{linespec} specifies source lines.
25583 Use the qualifier @code{task @emph{taskid}} with a breakpoint command
25584 to specify that you only want @cite{GDB} to stop the program when a
25585 particular Ada task reaches this breakpoint. @cite{taskid} is one of the
25586 numeric task identifiers assigned by @cite{GDB}, shown in the first
25587 column of the @code{info tasks} display.
25589 If you do not specify @code{task @emph{taskid}} when you set a
25590 breakpoint, the breakpoint applies to @emph{all} tasks of your
25591 program.
25593 You can use the @cite{task} qualifier on conditional breakpoints as
25594 well; in this case, place @code{task @emph{taskid}} before the
25595 breakpoint condition (before the @cite{if}).
25596 @end quotation
25597 @end itemize
25599 @geindex Task switching (in gdb)
25602 @itemize *
25604 @item 
25605 @emph{task `taskno`}
25607 @quotation
25609 This command allows switching to the task referred by @cite{taskno}. In
25610 particular, this allows browsing of the backtrace of the specified
25611 task. It is advisable to switch back to the original task before
25612 continuing execution otherwise the scheduling of the program may be
25613 perturbed.
25614 @end quotation
25615 @end itemize
25617 For more detailed information on the tasking support,
25618 see @cite{Debugging with GDB}.
25620 @geindex Debugging Generic Units
25622 @geindex Generics
25624 @node Debugging Generic Units,Remote Debugging with gdbserver,Ada Tasks,Running and Debugging Ada Programs
25625 @anchor{gnat_ugn/gnat_and_program_execution debugging-generic-units}@anchor{207}@anchor{gnat_ugn/gnat_and_program_execution id11}@anchor{208}
25626 @subsection Debugging Generic Units
25629 GNAT always uses code expansion for generic instantiation. This means that
25630 each time an instantiation occurs, a complete copy of the original code is
25631 made, with appropriate substitutions of formals by actuals.
25633 It is not possible to refer to the original generic entities in
25634 @cite{GDB}, but it is always possible to debug a particular instance of
25635 a generic, by using the appropriate expanded names. For example, if we have
25637 @quotation
25639 @example
25640 procedure g is
25642    generic package k is
25643       procedure kp (v1 : in out integer);
25644    end k;
25646    package body k is
25647       procedure kp (v1 : in out integer) is
25648       begin
25649          v1 := v1 + 1;
25650       end kp;
25651    end k;
25653    package k1 is new k;
25654    package k2 is new k;
25656    var : integer := 1;
25658 begin
25659    k1.kp (var);
25660    k2.kp (var);
25661    k1.kp (var);
25662    k2.kp (var);
25663 end;
25664 @end example
25665 @end quotation
25667 Then to break on a call to procedure kp in the k2 instance, simply
25668 use the command:
25670 @quotation
25672 @example
25673 (gdb) break g.k2.kp
25674 @end example
25675 @end quotation
25677 When the breakpoint occurs, you can step through the code of the
25678 instance in the normal manner and examine the values of local variables, as for
25679 other units.
25681 @geindex Remote Debugging with gdbserver
25683 @node Remote Debugging with gdbserver,GNAT Abnormal Termination or Failure to Terminate,Debugging Generic Units,Running and Debugging Ada Programs
25684 @anchor{gnat_ugn/gnat_and_program_execution remote-debugging-with-gdbserver}@anchor{209}@anchor{gnat_ugn/gnat_and_program_execution id12}@anchor{20a}
25685 @subsection Remote Debugging with gdbserver
25688 On platforms where gdbserver is supported, it is possible to use this tool
25689 to debug your application remotely.  This can be useful in situations
25690 where the program needs to be run on a target host that is different
25691 from the host used for development, particularly when the target has
25692 a limited amount of resources (either CPU and/or memory).
25694 To do so, start your program using gdbserver on the target machine.
25695 gdbserver then automatically suspends the execution of your program
25696 at its entry point, waiting for a debugger to connect to it.  The
25697 following commands starts an application and tells gdbserver to
25698 wait for a connection with the debugger on localhost port 4444.
25700 @quotation
25702 @example
25703 $ gdbserver localhost:4444 program
25704 Process program created; pid = 5685
25705 Listening on port 4444
25706 @end example
25707 @end quotation
25709 Once gdbserver has started listening, we can tell the debugger to establish
25710 a connection with this gdbserver, and then start the same debugging session
25711 as if the program was being debugged on the same host, directly under
25712 the control of GDB.
25714 @quotation
25716 @example
25717 $ gdb program
25718 (gdb) target remote targethost:4444
25719 Remote debugging using targethost:4444
25720 0x00007f29936d0af0 in ?? () from /lib64/ld-linux-x86-64.so.
25721 (gdb) b foo.adb:3
25722 Breakpoint 1 at 0x401f0c: file foo.adb, line 3.
25723 (gdb) continue
25724 Continuing.
25726 Breakpoint 1, foo () at foo.adb:4
25727 4       end foo;
25728 @end example
25729 @end quotation
25731 It is also possible to use gdbserver to attach to an already running
25732 program, in which case the execution of that program is simply suspended
25733 until the connection between the debugger and gdbserver is established.
25735 For more information on how to use gdbserver, see the @emph{Using the gdbserver Program}
25736 section in @cite{Debugging with GDB}.
25737 GNAT provides support for gdbserver on x86-linux, x86-windows and x86_64-linux.
25739 @geindex Abnormal Termination or Failure to Terminate
25741 @node GNAT Abnormal Termination or Failure to Terminate,Naming Conventions for GNAT Source Files,Remote Debugging with gdbserver,Running and Debugging Ada Programs
25742 @anchor{gnat_ugn/gnat_and_program_execution gnat-abnormal-termination-or-failure-to-terminate}@anchor{20b}@anchor{gnat_ugn/gnat_and_program_execution id13}@anchor{20c}
25743 @subsection GNAT Abnormal Termination or Failure to Terminate
25746 When presented with programs that contain serious errors in syntax
25747 or semantics,
25748 GNAT may on rare occasions  experience problems in operation, such
25749 as aborting with a
25750 segmentation fault or illegal memory access, raising an internal
25751 exception, terminating abnormally, or failing to terminate at all.
25752 In such cases, you can activate
25753 various features of GNAT that can help you pinpoint the construct in your
25754 program that is the likely source of the problem.
25756 The following strategies are presented in increasing order of
25757 difficulty, corresponding to your experience in using GNAT and your
25758 familiarity with compiler internals.
25761 @itemize *
25763 @item 
25764 Run @emph{gcc} with the @emph{-gnatf}. This first
25765 switch causes all errors on a given line to be reported. In its absence,
25766 only the first error on a line is displayed.
25768 The @emph{-gnatdO} switch causes errors to be displayed as soon as they
25769 are encountered, rather than after compilation is terminated. If GNAT
25770 terminates prematurely or goes into an infinite loop, the last error
25771 message displayed may help to pinpoint the culprit.
25773 @item 
25774 Run @emph{gcc} with the @emph{-v (verbose)} switch. In this
25775 mode, @emph{gcc} produces ongoing information about the progress of the
25776 compilation and provides the name of each procedure as code is
25777 generated. This switch allows you to find which Ada procedure was being
25778 compiled when it encountered a code generation problem.
25779 @end itemize
25781 @geindex -gnatdc switch
25784 @itemize *
25786 @item 
25787 Run @emph{gcc} with the @emph{-gnatdc} switch. This is a GNAT specific
25788 switch that does for the front-end what @emph{-v} does
25789 for the back end. The system prints the name of each unit,
25790 either a compilation unit or nested unit, as it is being analyzed.
25792 @item 
25793 Finally, you can start
25794 @cite{gdb} directly on the @cite{gnat1} executable. @cite{gnat1} is the
25795 front-end of GNAT, and can be run independently (normally it is just
25796 called from @emph{gcc}). You can use @cite{gdb} on @cite{gnat1} as you
25797 would on a C program (but @ref{1f7,,The GNAT Debugger GDB} for caveats). The
25798 @cite{where} command is the first line of attack; the variable
25799 @cite{lineno} (seen by @cite{print lineno}), used by the second phase of
25800 @cite{gnat1} and by the @emph{gcc} backend, indicates the source line at
25801 which the execution stopped, and @cite{input_file name} indicates the name of
25802 the source file.
25803 @end itemize
25805 @node Naming Conventions for GNAT Source Files,Getting Internal Debugging Information,GNAT Abnormal Termination or Failure to Terminate,Running and Debugging Ada Programs
25806 @anchor{gnat_ugn/gnat_and_program_execution naming-conventions-for-gnat-source-files}@anchor{20d}@anchor{gnat_ugn/gnat_and_program_execution id14}@anchor{20e}
25807 @subsection Naming Conventions for GNAT Source Files
25810 In order to examine the workings of the GNAT system, the following
25811 brief description of its organization may be helpful:
25814 @itemize *
25816 @item 
25817 Files with prefix @code{sc} contain the lexical scanner.
25819 @item 
25820 All files prefixed with @code{par} are components of the parser. The
25821 numbers correspond to chapters of the Ada Reference Manual. For example,
25822 parsing of select statements can be found in @code{par-ch9.adb}.
25824 @item 
25825 All files prefixed with @code{sem} perform semantic analysis. The
25826 numbers correspond to chapters of the Ada standard. For example, all
25827 issues involving context clauses can be found in @code{sem_ch10.adb}. In
25828 addition, some features of the language require sufficient special processing
25829 to justify their own semantic files: sem_aggr for aggregates, sem_disp for
25830 dynamic dispatching, etc.
25832 @item 
25833 All files prefixed with @code{exp} perform normalization and
25834 expansion of the intermediate representation (abstract syntax tree, or AST).
25835 these files use the same numbering scheme as the parser and semantics files.
25836 For example, the construction of record initialization procedures is done in
25837 @code{exp_ch3.adb}.
25839 @item 
25840 The files prefixed with @code{bind} implement the binder, which
25841 verifies the consistency of the compilation, determines an order of
25842 elaboration, and generates the bind file.
25844 @item 
25845 The files @code{atree.ads} and @code{atree.adb} detail the low-level
25846 data structures used by the front-end.
25848 @item 
25849 The files @code{sinfo.ads} and @code{sinfo.adb} detail the structure of
25850 the abstract syntax tree as produced by the parser.
25852 @item 
25853 The files @code{einfo.ads} and @code{einfo.adb} detail the attributes of
25854 all entities, computed during semantic analysis.
25856 @item 
25857 Library management issues are dealt with in files with prefix
25858 @code{lib}.
25860 @geindex Annex A (in Ada Reference Manual)
25862 @item 
25863 Ada files with the prefix @code{a-} are children of @cite{Ada}, as
25864 defined in Annex A.
25866 @geindex Annex B (in Ada reference Manual)
25868 @item 
25869 Files with prefix @code{i-} are children of @cite{Interfaces}, as
25870 defined in Annex B.
25872 @geindex System (package in Ada Reference Manual)
25874 @item 
25875 Files with prefix @code{s-} are children of @cite{System}. This includes
25876 both language-defined children and GNAT run-time routines.
25878 @geindex GNAT (package)
25880 @item 
25881 Files with prefix @code{g-} are children of @cite{GNAT}. These are useful
25882 general-purpose packages, fully documented in their specs. All
25883 the other @code{.c} files are modifications of common @emph{gcc} files.
25884 @end itemize
25886 @node Getting Internal Debugging Information,Stack Traceback,Naming Conventions for GNAT Source Files,Running and Debugging Ada Programs
25887 @anchor{gnat_ugn/gnat_and_program_execution id15}@anchor{20f}@anchor{gnat_ugn/gnat_and_program_execution getting-internal-debugging-information}@anchor{210}
25888 @subsection Getting Internal Debugging Information
25891 Most compilers have internal debugging switches and modes. GNAT
25892 does also, except GNAT internal debugging switches and modes are not
25893 secret. A summary and full description of all the compiler and binder
25894 debug flags are in the file @code{debug.adb}. You must obtain the
25895 sources of the compiler to see the full detailed effects of these flags.
25897 The switches that print the source of the program (reconstructed from
25898 the internal tree) are of general interest for user programs, as are the
25899 options to print
25900 the full internal tree, and the entity table (the symbol table
25901 information). The reconstructed source provides a readable version of the
25902 program after the front-end has completed analysis and  expansion,
25903 and is useful when studying the performance of specific constructs.
25904 For example, constraint checks are indicated, complex aggregates
25905 are replaced with loops and assignments, and tasking primitives
25906 are replaced with run-time calls.
25908 @geindex traceback
25910 @geindex stack traceback
25912 @geindex stack unwinding
25914 @node Stack Traceback,,Getting Internal Debugging Information,Running and Debugging Ada Programs
25915 @anchor{gnat_ugn/gnat_and_program_execution stack-traceback}@anchor{211}@anchor{gnat_ugn/gnat_and_program_execution id16}@anchor{212}
25916 @subsection Stack Traceback
25919 Traceback is a mechanism to display the sequence of subprogram calls that
25920 leads to a specified execution point in a program. Often (but not always)
25921 the execution point is an instruction at which an exception has been raised.
25922 This mechanism is also known as @emph{stack unwinding} because it obtains
25923 its information by scanning the run-time stack and recovering the activation
25924 records of all active subprograms. Stack unwinding is one of the most
25925 important tools for program debugging.
25927 The first entry stored in traceback corresponds to the deepest calling level,
25928 that is to say the subprogram currently executing the instruction
25929 from which we want to obtain the traceback.
25931 Note that there is no runtime performance penalty when stack traceback
25932 is enabled, and no exception is raised during program execution.
25934 @geindex traceback
25935 @geindex non-symbolic
25937 @menu
25938 * Non-Symbolic Traceback:: 
25939 * Symbolic Traceback:: 
25941 @end menu
25943 @node Non-Symbolic Traceback,Symbolic Traceback,,Stack Traceback
25944 @anchor{gnat_ugn/gnat_and_program_execution non-symbolic-traceback}@anchor{213}@anchor{gnat_ugn/gnat_and_program_execution id17}@anchor{214}
25945 @subsubsection Non-Symbolic Traceback
25948 Note: this feature is not supported on all platforms. See
25949 @code{GNAT.Traceback} spec in @code{g-traceb.ads}
25950 for a complete list of supported platforms.
25952 @subsubheading Tracebacks From an Unhandled Exception
25955 A runtime non-symbolic traceback is a list of addresses of call instructions.
25956 To enable this feature you must use the @emph{-E}
25957 @cite{gnatbind}'s option. With this option a stack traceback is stored as part
25958 of exception information. You can retrieve this information using the
25959 @cite{addr2line} tool.
25961 Here is a simple example:
25963 @quotation
25965 @example
25966 procedure STB is
25968    procedure P1 is
25969    begin
25970       raise Constraint_Error;
25971    end P1;
25973    procedure P2 is
25974    begin
25975       P1;
25976    end P2;
25978 begin
25979    P2;
25980 end STB;
25981 @end example
25983 @example
25984 $ gnatmake stb -bargs -E
25985 $ stb
25987 Execution terminated by unhandled exception
25988 Exception name: CONSTRAINT_ERROR
25989 Message: stb.adb:5
25990 Call stack traceback locations:
25991 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
25992 @end example
25993 @end quotation
25995 As we see the traceback lists a sequence of addresses for the unhandled
25996 exception @cite{CONSTRAINT_ERROR} raised in procedure P1. It is easy to
25997 guess that this exception come from procedure P1. To translate these
25998 addresses into the source lines where the calls appear, the
25999 @cite{addr2line} tool, described below, is invaluable. The use of this tool
26000 requires the program to be compiled with debug information.
26002 @quotation
26004 @example
26005 $ gnatmake -g stb -bargs -E
26006 $ stb
26008 Execution terminated by unhandled exception
26009 Exception name: CONSTRAINT_ERROR
26010 Message: stb.adb:5
26011 Call stack traceback locations:
26012 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
26014 $ addr2line --exe=stb 0x401373 0x40138b 0x40139c 0x401335 0x4011c4
26015    0x4011f1 0x77e892a4
26017 00401373 at d:/stb/stb.adb:5
26018 0040138B at d:/stb/stb.adb:10
26019 0040139C at d:/stb/stb.adb:14
26020 00401335 at d:/stb/b~stb.adb:104
26021 004011C4 at /build/.../crt1.c:200
26022 004011F1 at /build/.../crt1.c:222
26023 77E892A4 in ?? at ??:0
26024 @end example
26025 @end quotation
26027 The @cite{addr2line} tool has several other useful options:
26029 @quotation
26032 @multitable {xxxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} 
26033 @item
26035 @code{--functions}
26037 @tab
26039 to get the function name corresponding to any location
26041 @item
26043 @code{--demangle=gnat}
26045 @tab
26047 to use the gnat decoding mode for the function names.
26048 Note that for binutils version 2.9.x the option is
26049 simply @code{--demangle}.
26051 @end multitable
26054 @example
26055 $ addr2line --exe=stb --functions --demangle=gnat 0x401373 0x40138b
26056    0x40139c 0x401335 0x4011c4 0x4011f1
26058 00401373 in stb.p1 at d:/stb/stb.adb:5
26059 0040138B in stb.p2 at d:/stb/stb.adb:10
26060 0040139C in stb at d:/stb/stb.adb:14
26061 00401335 in main at d:/stb/b~stb.adb:104
26062 004011C4 in <__mingw_CRTStartup> at /build/.../crt1.c:200
26063 004011F1 in <mainCRTStartup> at /build/.../crt1.c:222
26064 @end example
26065 @end quotation
26067 From this traceback we can see that the exception was raised in
26068 @code{stb.adb} at line 5, which was reached from a procedure call in
26069 @code{stb.adb} at line 10, and so on. The @code{b~std.adb} is the binder file,
26070 which contains the call to the main program.
26071 @ref{120,,Running gnatbind}. The remaining entries are assorted runtime routines,
26072 and the output will vary from platform to platform.
26074 It is also possible to use @cite{GDB} with these traceback addresses to debug
26075 the program. For example, we can break at a given code location, as reported
26076 in the stack traceback:
26078 @quotation
26080 @example
26081 $ gdb -nw stb
26082 @end example
26083 @end quotation
26085 Furthermore, this feature is not implemented inside Windows DLL. Only
26086 the non-symbolic traceback is reported in this case.
26088 @quotation
26090 @example
26091 (gdb) break *0x401373
26092 Breakpoint 1 at 0x401373: file stb.adb, line 5.
26093 @end example
26094 @end quotation
26096 It is important to note that the stack traceback addresses
26097 do not change when debug information is included. This is particularly useful
26098 because it makes it possible to release software without debug information (to
26099 minimize object size), get a field report that includes a stack traceback
26100 whenever an internal bug occurs, and then be able to retrieve the sequence
26101 of calls with the same program compiled with debug information.
26103 @subsubheading Tracebacks From Exception Occurrences
26106 Non-symbolic tracebacks are obtained by using the @emph{-E} binder argument.
26107 The stack traceback is attached to the exception information string, and can
26108 be retrieved in an exception handler within the Ada program, by means of the
26109 Ada facilities defined in @cite{Ada.Exceptions}. Here is a simple example:
26111 @quotation
26113 @example
26114 with Ada.Text_IO;
26115 with Ada.Exceptions;
26117 procedure STB is
26119    use Ada;
26120    use Ada.Exceptions;
26122    procedure P1 is
26123       K : Positive := 1;
26124    begin
26125       K := K - 1;
26126    exception
26127       when E : others =>
26128          Text_IO.Put_Line (Exception_Information (E));
26129    end P1;
26131    procedure P2 is
26132    begin
26133       P1;
26134    end P2;
26136 begin
26137    P2;
26138 end STB;
26139 @end example
26140 @end quotation
26142 This program will output:
26144 @quotation
26146 @example
26147 $ stb
26149 Exception name: CONSTRAINT_ERROR
26150 Message: stb.adb:12
26151 Call stack traceback locations:
26152 0x4015e4 0x401633 0x401644 0x401461 0x4011c4 0x4011f1 0x77e892a4
26153 @end example
26154 @end quotation
26156 @subsubheading Tracebacks From Anywhere in a Program
26159 It is also possible to retrieve a stack traceback from anywhere in a
26160 program. For this you need to
26161 use the @cite{GNAT.Traceback} API. This package includes a procedure called
26162 @cite{Call_Chain} that computes a complete stack traceback, as well as useful
26163 display procedures described below. It is not necessary to use the
26164 @emph{-E gnatbind} option in this case, because the stack traceback mechanism
26165 is invoked explicitly.
26167 In the following example we compute a traceback at a specific location in
26168 the program, and we display it using @cite{GNAT.Debug_Utilities.Image} to
26169 convert addresses to strings:
26171 @quotation
26173 @example
26174 with Ada.Text_IO;
26175 with GNAT.Traceback;
26176 with GNAT.Debug_Utilities;
26178 procedure STB is
26180    use Ada;
26181    use GNAT;
26182    use GNAT.Traceback;
26184    procedure P1 is
26185       TB  : Tracebacks_Array (1 .. 10);
26186       --  We are asking for a maximum of 10 stack frames.
26187       Len : Natural;
26188       --  Len will receive the actual number of stack frames returned.
26189    begin
26190       Call_Chain (TB, Len);
26192       Text_IO.Put ("In STB.P1 : ");
26194       for K in 1 .. Len loop
26195          Text_IO.Put (Debug_Utilities.Image (TB (K)));
26196          Text_IO.Put (' ');
26197       end loop;
26199       Text_IO.New_Line;
26200    end P1;
26202    procedure P2 is
26203    begin
26204       P1;
26205    end P2;
26207 begin
26208    P2;
26209 end STB;
26210 @end example
26212 @example
26213 $ gnatmake -g stb
26214 $ stb
26216 In STB.P1 : 16#0040_F1E4# 16#0040_14F2# 16#0040_170B# 16#0040_171C#
26217 16#0040_1461# 16#0040_11C4# 16#0040_11F1# 16#77E8_92A4#
26218 @end example
26219 @end quotation
26221 You can then get further information by invoking the @cite{addr2line}
26222 tool as described earlier (note that the hexadecimal addresses
26223 need to be specified in C format, with a leading '0x').
26225 @geindex traceback
26226 @geindex symbolic
26228 @node Symbolic Traceback,,Non-Symbolic Traceback,Stack Traceback
26229 @anchor{gnat_ugn/gnat_and_program_execution id18}@anchor{215}@anchor{gnat_ugn/gnat_and_program_execution symbolic-traceback}@anchor{216}
26230 @subsubsection Symbolic Traceback
26233 A symbolic traceback is a stack traceback in which procedure names are
26234 associated with each code location.
26236 Note that this feature is not supported on all platforms. See
26237 @code{GNAT.Traceback.Symbolic} spec in @code{g-trasym.ads} for a complete
26238 list of currently supported platforms.
26240 Note that the symbolic traceback requires that the program be compiled
26241 with debug information. If it is not compiled with debug information
26242 only the non-symbolic information will be valid.
26244 @subsubheading Tracebacks From Exception Occurrences
26247 Here is an example:
26249 @quotation
26251 @example
26252 with Ada.Text_IO;
26253 with GNAT.Traceback.Symbolic;
26255 procedure STB is
26257    procedure P1 is
26258    begin
26259       raise Constraint_Error;
26260    end P1;
26262    procedure P2 is
26263    begin
26264       P1;
26265    end P2;
26267    procedure P3 is
26268    begin
26269       P2;
26270    end P3;
26272 begin
26273    P3;
26274 exception
26275    when E : others =>
26276       Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
26277 end STB;
26278 @end example
26280 @example
26281 $ gnatmake -g .\stb -bargs -E
26282 $ stb
26284 0040149F in stb.p1 at stb.adb:8
26285 004014B7 in stb.p2 at stb.adb:13
26286 004014CF in stb.p3 at stb.adb:18
26287 004015DD in ada.stb at stb.adb:22
26288 00401461 in main at b~stb.adb:168
26289 004011C4 in __mingw_CRTStartup at crt1.c:200
26290 004011F1 in mainCRTStartup at crt1.c:222
26291 77E892A4 in ?? at ??:0
26292 @end example
26293 @end quotation
26295 In the above example the @code{.\} syntax in the @emph{gnatmake} command
26296 is currently required by @emph{addr2line} for files that are in
26297 the current working directory.
26298 Moreover, the exact sequence of linker options may vary from platform
26299 to platform.
26300 The above @emph{-largs} section is for Windows platforms. By contrast,
26301 under Unix there is no need for the @emph{-largs} section.
26302 Differences across platforms are due to details of linker implementation.
26304 @subsubheading Tracebacks From Anywhere in a Program
26307 It is possible to get a symbolic stack traceback
26308 from anywhere in a program, just as for non-symbolic tracebacks.
26309 The first step is to obtain a non-symbolic
26310 traceback, and then call @cite{Symbolic_Traceback} to compute the symbolic
26311 information. Here is an example:
26313 @quotation
26315 @example
26316 with Ada.Text_IO;
26317 with GNAT.Traceback;
26318 with GNAT.Traceback.Symbolic;
26320 procedure STB is
26322    use Ada;
26323    use GNAT.Traceback;
26324    use GNAT.Traceback.Symbolic;
26326    procedure P1 is
26327       TB  : Tracebacks_Array (1 .. 10);
26328       --  We are asking for a maximum of 10 stack frames.
26329       Len : Natural;
26330       --  Len will receive the actual number of stack frames returned.
26331    begin
26332       Call_Chain (TB, Len);
26333       Text_IO.Put_Line (Symbolic_Traceback (TB (1 .. Len)));
26334    end P1;
26336    procedure P2 is
26337    begin
26338       P1;
26339    end P2;
26341 begin
26342    P2;
26343 end STB;
26344 @end example
26345 @end quotation
26347 @geindex Code Coverage
26349 @geindex Profiling
26351 @node Code Coverage and Profiling,Improving Performance,Running and Debugging Ada Programs,GNAT and Program Execution
26352 @anchor{gnat_ugn/gnat_and_program_execution id19}@anchor{1f1}@anchor{gnat_ugn/gnat_and_program_execution code-coverage-and-profiling}@anchor{27}
26353 @section Code Coverage and Profiling
26356 This section describes how to use the @cite{gcov} coverage testing tool and
26357 the @cite{gprof} profiler tool on Ada programs.
26359 @geindex gcov
26361 @menu
26362 * Code Coverage of Ada Programs with gcov:: 
26363 * Profiling an Ada Program with gprof:: 
26365 @end menu
26367 @node Code Coverage of Ada Programs with gcov,Profiling an Ada Program with gprof,,Code Coverage and Profiling
26368 @anchor{gnat_ugn/gnat_and_program_execution id20}@anchor{217}@anchor{gnat_ugn/gnat_and_program_execution code-coverage-of-ada-programs-with-gcov}@anchor{218}
26369 @subsection Code Coverage of Ada Programs with gcov
26372 @cite{gcov} is a test coverage program: it analyzes the execution of a given
26373 program on selected tests, to help you determine the portions of the program
26374 that are still untested.
26376 @cite{gcov} is part of the GCC suite, and is described in detail in the GCC
26377 User's Guide. You can refer to this documentation for a more complete
26378 description.
26380 This chapter provides a quick startup guide, and
26381 details some GNAT-specific features.
26383 @menu
26384 * Quick startup guide:: 
26385 * GNAT specifics:: 
26387 @end menu
26389 @node Quick startup guide,GNAT specifics,,Code Coverage of Ada Programs with gcov
26390 @anchor{gnat_ugn/gnat_and_program_execution id21}@anchor{219}@anchor{gnat_ugn/gnat_and_program_execution quick-startup-guide}@anchor{21a}
26391 @subsubsection Quick startup guide
26394 In order to perform coverage analysis of a program using @cite{gcov}, several
26395 steps are needed:
26398 @enumerate 
26400 @item 
26401 Instrument the code during the compilation process,
26403 @item 
26404 Execute the instrumented program, and
26406 @item 
26407 Invoke the @cite{gcov} tool to generate the coverage results.
26408 @end enumerate
26410 @geindex -fprofile-arcs (gcc)
26412 @geindex -ftest-coverage (gcc
26414 @geindex -fprofile-arcs (gnatbind)
26416 The code instrumentation needed by gcov is created at the object level.
26417 The source code is not modified in any way, because the instrumentation code is
26418 inserted by gcc during the compilation process. To compile your code with code
26419 coverage activated, you need to recompile your whole project using the
26420 switches
26421 @cite{-fprofile-arcs} and @cite{-ftest-coverage}, and link it using
26422 @cite{-fprofile-arcs}.
26424 @quotation
26426 @example
26427 $ gnatmake -P my_project.gpr -f -cargs -fprofile-arcs -ftest-coverage \\
26428    -largs -fprofile-arcs
26429 @end example
26430 @end quotation
26432 This compilation process will create @code{.gcno} files together with
26433 the usual object files.
26435 Once the program is compiled with coverage instrumentation, you can
26436 run it as many times as needed -- on portions of a test suite for
26437 example. The first execution will produce @code{.gcda} files at the
26438 same location as the @code{.gcno} files.  Subsequent executions
26439 will update those files, so that a cumulative result of the covered
26440 portions of the program is generated.
26442 Finally, you need to call the @cite{gcov} tool. The different options of
26443 @cite{gcov} are described in the GCC User's Guide, section 'Invoking gcov'.
26445 This will create annotated source files with a @code{.gcov} extension:
26446 @code{my_main.adb} file will be analyzed in @code{my_main.adb.gcov}.
26448 @node GNAT specifics,,Quick startup guide,Code Coverage of Ada Programs with gcov
26449 @anchor{gnat_ugn/gnat_and_program_execution gnat-specifics}@anchor{21b}@anchor{gnat_ugn/gnat_and_program_execution id22}@anchor{21c}
26450 @subsubsection GNAT specifics
26453 Because of Ada semantics, portions of the source code may be shared among
26454 several object files. This is the case for example when generics are
26455 involved, when inlining is active  or when declarations generate  initialisation
26456 calls. In order to take
26457 into account this shared code, you need to call @cite{gcov} on all
26458 source files of the tested program at once.
26460 The list of source files might exceed the system's maximum command line
26461 length. In order to bypass this limitation, a new mechanism has been
26462 implemented in @cite{gcov}: you can now list all your project's files into a
26463 text file, and provide this file to gcov as a parameter,  preceded by a @code{@@}
26464 (e.g. @code{gcov @@mysrclist.txt}).
26466 Note that on AIX compiling a static library with @cite{-fprofile-arcs} is
26467 not supported as there can be unresolved symbols during the final link.
26469 @geindex gprof
26471 @geindex Profiling
26473 @node Profiling an Ada Program with gprof,,Code Coverage of Ada Programs with gcov,Code Coverage and Profiling
26474 @anchor{gnat_ugn/gnat_and_program_execution profiling-an-ada-program-with-gprof}@anchor{21d}@anchor{gnat_ugn/gnat_and_program_execution id23}@anchor{21e}
26475 @subsection Profiling an Ada Program with gprof
26478 This section is not meant to be an exhaustive documentation of @cite{gprof}.
26479 Full documentation for it can be found in the @cite{GNU Profiler User's Guide}
26480 documentation that is part of this GNAT distribution.
26482 Profiling a program helps determine the parts of a program that are executed
26483 most often, and are therefore the most time-consuming.
26485 @cite{gprof} is the standard GNU profiling tool; it has been enhanced to
26486 better handle Ada programs and multitasking.
26487 It is currently supported on the following platforms
26490 @itemize *
26492 @item 
26493 linux x86/x86_64
26495 @item 
26496 solaris sparc/sparc64/x86
26498 @item 
26499 windows x86
26500 @end itemize
26502 In order to profile a program using @cite{gprof}, several steps are needed:
26505 @enumerate 
26507 @item 
26508 Instrument the code, which requires a full recompilation of the project with the
26509 proper switches.
26511 @item 
26512 Execute the program under the analysis conditions, i.e. with the desired
26513 input.
26515 @item 
26516 Analyze the results using the @cite{gprof} tool.
26517 @end enumerate
26519 The following sections detail the different steps, and indicate how
26520 to interpret the results.
26522 @menu
26523 * Compilation for profiling:: 
26524 * Program execution:: 
26525 * Running gprof:: 
26526 * Interpretation of profiling results:: 
26528 @end menu
26530 @node Compilation for profiling,Program execution,,Profiling an Ada Program with gprof
26531 @anchor{gnat_ugn/gnat_and_program_execution id24}@anchor{21f}@anchor{gnat_ugn/gnat_and_program_execution compilation-for-profiling}@anchor{220}
26532 @subsubsection Compilation for profiling
26535 @geindex -pg (gcc)
26536 @geindex for profiling
26538 @geindex -pg (gnatlink)
26539 @geindex for profiling
26541 In order to profile a program the first step is to tell the compiler
26542 to generate the necessary profiling information. The compiler switch to be used
26543 is @code{-pg}, which must be added to other compilation switches. This
26544 switch needs to be specified both during compilation and link stages, and can
26545 be specified once when using gnatmake:
26547 @quotation
26549 @example
26550 $ gnatmake -f -pg -P my_project
26551 @end example
26552 @end quotation
26554 Note that only the objects that were compiled with the @code{-pg} switch will
26555 be profiled; if you need to profile your whole project, use the @code{-f}
26556 gnatmake switch to force full recompilation.
26558 @node Program execution,Running gprof,Compilation for profiling,Profiling an Ada Program with gprof
26559 @anchor{gnat_ugn/gnat_and_program_execution program-execution}@anchor{221}@anchor{gnat_ugn/gnat_and_program_execution id25}@anchor{222}
26560 @subsubsection Program execution
26563 Once the program has been compiled for profiling, you can run it as usual.
26565 The only constraint imposed by profiling is that the program must terminate
26566 normally. An interrupted program (via a Ctrl-C, kill, etc.) will not be
26567 properly analyzed.
26569 Once the program completes execution, a data file called @code{gmon.out} is
26570 generated in the directory where the program was launched from. If this file
26571 already exists, it will be overwritten.
26573 @node Running gprof,Interpretation of profiling results,Program execution,Profiling an Ada Program with gprof
26574 @anchor{gnat_ugn/gnat_and_program_execution running-gprof}@anchor{223}@anchor{gnat_ugn/gnat_and_program_execution id26}@anchor{224}
26575 @subsubsection Running gprof
26578 The @cite{gprof} tool is called as follow:
26580 @quotation
26582 @example
26583 $ gprof my_prog gmon.out
26584 @end example
26585 @end quotation
26587 or simply:
26589 @quotation
26591 @example
26592 $  gprof my_prog
26593 @end example
26594 @end quotation
26596 The complete form of the gprof command line is the following:
26598 @quotation
26600 @example
26601 $ gprof [switches] [executable [data-file]]
26602 @end example
26603 @end quotation
26605 @cite{gprof} supports numerous switches. The order of these
26606 switch does not matter. The full list of options can be found in
26607 the GNU Profiler User's Guide documentation that comes with this documentation.
26609 The following is the subset of those switches that is most relevant:
26611 @geindex --demangle (gprof)
26614 @table @asis
26616 @item @code{--demangle[=@emph{style}]}, @code{--no-demangle}
26618 These options control whether symbol names should be demangled when
26619 printing output.  The default is to demangle C++ symbols.  The
26620 @code{--no-demangle} option may be used to turn off demangling. Different
26621 compilers have different mangling styles.  The optional demangling style
26622 argument can be used to choose an appropriate demangling style for your
26623 compiler, in particular Ada symbols generated by GNAT can be demangled using
26624 @code{--demangle=gnat}.
26625 @end table
26627 @geindex -e (gprof)
26630 @table @asis
26632 @item @code{-e @emph{function_name}}
26634 The @code{-e @emph{function}} option tells @cite{gprof} not to print
26635 information about the function @cite{function_name} (and its
26636 children...) in the call graph.  The function will still be listed
26637 as a child of any functions that call it, but its index number will be
26638 shown as @code{[not printed]}.  More than one @code{-e} option may be
26639 given; only one @cite{function_name} may be indicated with each @code{-e}
26640 option.
26641 @end table
26643 @geindex -E (gprof)
26646 @table @asis
26648 @item @code{-E @emph{function_name}}
26650 The @code{-E @emph{function}} option works like the @code{-e} option, but
26651 execution time spent in the function (and children who were not called from
26652 anywhere else), will not be used to compute the percentages-of-time for
26653 the call graph.  More than one @code{-E} option may be given; only one
26654 @cite{function_name} may be indicated with each @code{-E} option.
26655 @end table
26657 @geindex -f (gprof)
26660 @table @asis
26662 @item @code{-f @emph{function_name}}
26664 The @code{-f @emph{function}} option causes @cite{gprof} to limit the
26665 call graph to the function @cite{function_name} and its children (and
26666 their children...).  More than one @code{-f} option may be given;
26667 only one @cite{function_name} may be indicated with each @code{-f}
26668 option.
26669 @end table
26671 @geindex -F (gprof)
26674 @table @asis
26676 @item @code{-F @emph{function_name}}
26678 The @code{-F @emph{function}} option works like the @code{-f} option, but
26679 only time spent in the function and its children (and their
26680 children...) will be used to determine total-time and
26681 percentages-of-time for the call graph.  More than one @code{-F} option
26682 may be given; only one @cite{function_name} may be indicated with each
26683 @code{-F} option.  The @code{-F} option overrides the @code{-E} option.
26684 @end table
26686 @node Interpretation of profiling results,,Running gprof,Profiling an Ada Program with gprof
26687 @anchor{gnat_ugn/gnat_and_program_execution id27}@anchor{225}@anchor{gnat_ugn/gnat_and_program_execution interpretation-of-profiling-results}@anchor{226}
26688 @subsubsection Interpretation of profiling results
26691 The results of the profiling analysis are represented by two arrays: the
26692 'flat profile' and the 'call graph'. Full documentation of those outputs
26693 can be found in the GNU Profiler User's Guide.
26695 The flat profile shows the time spent in each function of the program, and how
26696 many time it has been called. This allows you to locate easily the most
26697 time-consuming functions.
26699 The call graph shows, for each subprogram, the subprograms that call it,
26700 and the subprograms that it calls. It also provides an estimate of the time
26701 spent in each of those callers/called subprograms.
26703 @node Improving Performance,Overflow Check Handling in GNAT,Code Coverage and Profiling,GNAT and Program Execution
26704 @anchor{gnat_ugn/gnat_and_program_execution improving-performance}@anchor{28}@anchor{gnat_ugn/gnat_and_program_execution id28}@anchor{1f2}
26705 @section Improving Performance
26708 @geindex Improving performance
26710 This section presents several topics related to program performance.
26711 It first describes some of the tradeoffs that need to be considered
26712 and some of the techniques for making your program run faster.
26715 It then documents the unused subprogram/data elimination feature,
26716 which can reduce the size of program executables.
26718 @menu
26719 * Performance Considerations:: 
26720 * Text_IO Suggestions:: 
26721 * Reducing Size of Executables with Unused Subprogram/Data Elimination:: 
26723 @end menu
26725 @node Performance Considerations,Text_IO Suggestions,,Improving Performance
26726 @anchor{gnat_ugn/gnat_and_program_execution id29}@anchor{227}@anchor{gnat_ugn/gnat_and_program_execution performance-considerations}@anchor{228}
26727 @subsection Performance Considerations
26730 The GNAT system provides a number of options that allow a trade-off
26731 between
26734 @itemize *
26736 @item 
26737 performance of the generated code
26739 @item 
26740 speed of compilation
26742 @item 
26743 minimization of dependences and recompilation
26745 @item 
26746 the degree of run-time checking.
26747 @end itemize
26749 The defaults (if no options are selected) aim at improving the speed
26750 of compilation and minimizing dependences, at the expense of performance
26751 of the generated code:
26754 @itemize *
26756 @item 
26757 no optimization
26759 @item 
26760 no inlining of subprogram calls
26762 @item 
26763 all run-time checks enabled except overflow and elaboration checks
26764 @end itemize
26766 These options are suitable for most program development purposes. This
26767 section describes how you can modify these choices, and also provides
26768 some guidelines on debugging optimized code.
26770 @menu
26771 * Controlling Run-Time Checks:: 
26772 * Use of Restrictions:: 
26773 * Optimization Levels:: 
26774 * Debugging Optimized Code:: 
26775 * Inlining of Subprograms:: 
26776 * Floating_Point_Operations:: 
26777 * Vectorization of loops:: 
26778 * Other Optimization Switches:: 
26779 * Optimization and Strict Aliasing:: 
26780 * Aliased Variables and Optimization:: 
26781 * Atomic Variables and Optimization:: 
26782 * Passive Task Optimization:: 
26784 @end menu
26786 @node Controlling Run-Time Checks,Use of Restrictions,,Performance Considerations
26787 @anchor{gnat_ugn/gnat_and_program_execution controlling-run-time-checks}@anchor{229}@anchor{gnat_ugn/gnat_and_program_execution id30}@anchor{22a}
26788 @subsubsection Controlling Run-Time Checks
26791 By default, GNAT generates all run-time checks, except integer overflow
26792 checks, stack overflow checks, and checks for access before elaboration on
26793 subprogram calls. The latter are not required in default mode, because all
26794 necessary checking is done at compile time.
26796 @geindex -gnatp (gcc)
26798 @geindex -gnato (gcc)
26800 Two gnat switches, @emph{-gnatp} and @emph{-gnato} allow this default to
26801 be modified. See @ref{fe,,Run-Time Checks}.
26803 Our experience is that the default is suitable for most development
26804 purposes.
26806 We treat integer overflow specially because these
26807 are quite expensive and in our experience are not as important as other
26808 run-time checks in the development process. Note that division by zero
26809 is not considered an overflow check, and divide by zero checks are
26810 generated where required by default.
26812 Elaboration checks are off by default, and also not needed by default, since
26813 GNAT uses a static elaboration analysis approach that avoids the need for
26814 run-time checking. This manual contains a full chapter discussing the issue
26815 of elaboration checks, and if the default is not satisfactory for your use,
26816 you should read this chapter.
26818 For validity checks, the minimal checks required by the Ada Reference
26819 Manual (for case statements and assignments to array elements) are on
26820 by default. These can be suppressed by use of the @emph{-gnatVn} switch.
26821 Note that in Ada 83, there were no validity checks, so if the Ada 83 mode
26822 is acceptable (or when comparing GNAT performance with an Ada 83 compiler),
26823 it may be reasonable to routinely use @emph{-gnatVn}. Validity checks
26824 are also suppressed entirely if @emph{-gnatp} is used.
26826 @geindex Overflow checks
26828 @geindex Checks
26829 @geindex overflow
26831 @geindex Suppress
26833 @geindex Unsuppress
26835 @geindex pragma Suppress
26837 @geindex pragma Unsuppress
26839 Note that the setting of the switches controls the default setting of
26840 the checks. They may be modified using either @cite{pragma Suppress} (to
26841 remove checks) or @cite{pragma Unsuppress} (to add back suppressed
26842 checks) in the program source.
26844 @node Use of Restrictions,Optimization Levels,Controlling Run-Time Checks,Performance Considerations
26845 @anchor{gnat_ugn/gnat_and_program_execution use-of-restrictions}@anchor{22b}@anchor{gnat_ugn/gnat_and_program_execution id31}@anchor{22c}
26846 @subsubsection Use of Restrictions
26849 The use of pragma Restrictions allows you to control which features are
26850 permitted in your program. Apart from the obvious point that if you avoid
26851 relatively expensive features like finalization (enforceable by the use
26852 of pragma Restrictions (No_Finalization), the use of this pragma does not
26853 affect the generated code in most cases.
26855 One notable exception to this rule is that the possibility of task abort
26856 results in some distributed overhead, particularly if finalization or
26857 exception handlers are used. The reason is that certain sections of code
26858 have to be marked as non-abortable.
26860 If you use neither the @cite{abort} statement, nor asynchronous transfer
26861 of control (@cite{select ... then abort}), then this distributed overhead
26862 is removed, which may have a general positive effect in improving
26863 overall performance.  Especially code involving frequent use of tasking
26864 constructs and controlled types will show much improved performance.
26865 The relevant restrictions pragmas are
26867 @quotation
26869 @example
26870 pragma Restrictions (No_Abort_Statements);
26871 pragma Restrictions (Max_Asynchronous_Select_Nesting => 0);
26872 @end example
26873 @end quotation
26875 It is recommended that these restriction pragmas be used if possible. Note
26876 that this also means that you can write code without worrying about the
26877 possibility of an immediate abort at any point.
26879 @node Optimization Levels,Debugging Optimized Code,Use of Restrictions,Performance Considerations
26880 @anchor{gnat_ugn/gnat_and_program_execution id32}@anchor{22d}@anchor{gnat_ugn/gnat_and_program_execution optimization-levels}@anchor{101}
26881 @subsubsection Optimization Levels
26884 @geindex -O (gcc)
26886 Without any optimization option,
26887 the compiler's goal is to reduce the cost of
26888 compilation and to make debugging produce the expected results.
26889 Statements are independent: if you stop the program with a breakpoint between
26890 statements, you can then assign a new value to any variable or change
26891 the program counter to any other statement in the subprogram and get exactly
26892 the results you would expect from the source code.
26894 Turning on optimization makes the compiler attempt to improve the
26895 performance and/or code size at the expense of compilation time and
26896 possibly the ability to debug the program.
26898 If you use multiple
26899 -O options, with or without level numbers,
26900 the last such option is the one that is effective.
26902 The default is optimization off. This results in the fastest compile
26903 times, but GNAT makes absolutely no attempt to optimize, and the
26904 generated programs are considerably larger and slower than when
26905 optimization is enabled. You can use the
26906 @emph{-O} switch (the permitted forms are @emph{-O0}, @emph{-O1}
26907 @emph{-O2}, @emph{-O3}, and @emph{-Os})
26908 to @emph{gcc} to control the optimization level:
26911 @itemize *
26913 @item 
26915 @table @asis
26917 @item @emph{-O0}
26919 No optimization (the default);
26920 generates unoptimized code but has
26921 the fastest compilation time.
26923 Note that many other compilers do fairly extensive optimization
26924 even if 'no optimization' is specified. With gcc, it is
26925 very unusual to use -O0 for production if
26926 execution time is of any concern, since -O0
26927 really does mean no optimization at all. This difference between
26928 gcc and other compilers should be kept in mind when doing
26929 performance comparisons.
26930 @end table
26932 @item 
26934 @table @asis
26936 @item @emph{-O1}
26938 Moderate optimization;
26939 optimizes reasonably well but does not
26940 degrade compilation time significantly.
26941 @end table
26943 @item 
26945 @table @asis
26947 @item @emph{-O2}
26949 Full optimization;
26950 generates highly optimized code and has
26951 the slowest compilation time.
26952 @end table
26954 @item 
26956 @table @asis
26958 @item @emph{-O3}
26960 Full optimization as in @emph{-O2};
26961 also uses more aggressive automatic inlining of subprograms within a unit
26962 (@ref{114,,Inlining of Subprograms}) and attempts to vectorize loops.
26963 @end table
26965 @item 
26967 @table @asis
26969 @item @emph{-Os}
26971 Optimize space usage (code and data) of resulting program.
26972 @end table
26973 @end itemize
26975 Higher optimization levels perform more global transformations on the
26976 program and apply more expensive analysis algorithms in order to generate
26977 faster and more compact code. The price in compilation time, and the
26978 resulting improvement in execution time,
26979 both depend on the particular application and the hardware environment.
26980 You should experiment to find the best level for your application.
26982 Since the precise set of optimizations done at each level will vary from
26983 release to release (and sometime from target to target), it is best to think
26984 of the optimization settings in general terms.
26985 See the @emph{Options That Control Optimization} section in
26986 @cite{Using the GNU Compiler Collection (GCC)}
26987 for details about
26988 the @emph{-O} settings and a number of @emph{-f} options that
26989 individually enable or disable specific optimizations.
26991 Unlike some other compilation systems, @emph{gcc} has
26992 been tested extensively at all optimization levels. There are some bugs
26993 which appear only with optimization turned on, but there have also been
26994 bugs which show up only in @emph{unoptimized} code. Selecting a lower
26995 level of optimization does not improve the reliability of the code
26996 generator, which in practice is highly reliable at all optimization
26997 levels.
26999 Note regarding the use of @emph{-O3}: The use of this optimization level
27000 is generally discouraged with GNAT, since it often results in larger
27001 executables which may run more slowly. See further discussion of this point
27002 in @ref{114,,Inlining of Subprograms}.
27004 @node Debugging Optimized Code,Inlining of Subprograms,Optimization Levels,Performance Considerations
27005 @anchor{gnat_ugn/gnat_and_program_execution id33}@anchor{22e}@anchor{gnat_ugn/gnat_and_program_execution debugging-optimized-code}@anchor{22f}
27006 @subsubsection Debugging Optimized Code
27009 @geindex Debugging optimized code
27011 @geindex Optimization and debugging
27013 Although it is possible to do a reasonable amount of debugging at
27014 nonzero optimization levels,
27015 the higher the level the more likely that
27016 source-level constructs will have been eliminated by optimization.
27017 For example, if a loop is strength-reduced, the loop
27018 control variable may be completely eliminated and thus cannot be
27019 displayed in the debugger.
27020 This can only happen at @emph{-O2} or @emph{-O3}.
27021 Explicit temporary variables that you code might be eliminated at
27022 level @emph{-O1} or higher.
27024 @geindex -g (gcc)
27026 The use of the @emph{-g} switch,
27027 which is needed for source-level debugging,
27028 affects the size of the program executable on disk,
27029 and indeed the debugging information can be quite large.
27030 However, it has no effect on the generated code (and thus does not
27031 degrade performance)
27033 Since the compiler generates debugging tables for a compilation unit before
27034 it performs optimizations, the optimizing transformations may invalidate some
27035 of the debugging data.  You therefore need to anticipate certain
27036 anomalous situations that may arise while debugging optimized code.
27037 These are the most common cases:
27040 @itemize *
27042 @item 
27043 @emph{The 'hopping Program Counter':}  Repeated @cite{step} or @cite{next}
27044 commands show
27045 the PC bouncing back and forth in the code.  This may result from any of
27046 the following optimizations:
27049 @itemize -
27051 @item 
27052 @emph{Common subexpression elimination:} using a single instance of code for a
27053 quantity that the source computes several times.  As a result you
27054 may not be able to stop on what looks like a statement.
27056 @item 
27057 @emph{Invariant code motion:} moving an expression that does not change within a
27058 loop, to the beginning of the loop.
27060 @item 
27061 @emph{Instruction scheduling:} moving instructions so as to
27062 overlap loads and stores (typically) with other code, or in
27063 general to move computations of values closer to their uses. Often
27064 this causes you to pass an assignment statement without the assignment
27065 happening and then later bounce back to the statement when the
27066 value is actually needed.  Placing a breakpoint on a line of code
27067 and then stepping over it may, therefore, not always cause all the
27068 expected side-effects.
27069 @end itemize
27071 @item 
27072 @emph{The 'big leap':} More commonly known as @emph{cross-jumping}, in which
27073 two identical pieces of code are merged and the program counter suddenly
27074 jumps to a statement that is not supposed to be executed, simply because
27075 it (and the code following) translates to the same thing as the code
27076 that @emph{was} supposed to be executed.  This effect is typically seen in
27077 sequences that end in a jump, such as a @cite{goto}, a @cite{return}, or
27078 a @cite{break} in a C @cite{switch} statement.
27080 @item 
27081 @emph{The 'roving variable':} The symptom is an unexpected value in a variable.
27082 There are various reasons for this effect:
27085 @itemize -
27087 @item 
27088 In a subprogram prologue, a parameter may not yet have been moved to its
27089 'home'.
27091 @item 
27092 A variable may be dead, and its register re-used.  This is
27093 probably the most common cause.
27095 @item 
27096 As mentioned above, the assignment of a value to a variable may
27097 have been moved.
27099 @item 
27100 A variable may be eliminated entirely by value propagation or
27101 other means.  In this case, GCC may incorrectly generate debugging
27102 information for the variable
27103 @end itemize
27105 In general, when an unexpected value appears for a local variable or parameter
27106 you should first ascertain if that value was actually computed by
27107 your program, as opposed to being incorrectly reported by the debugger.
27108 Record fields or
27109 array elements in an object designated by an access value
27110 are generally less of a problem, once you have ascertained that the access
27111 value is sensible.
27112 Typically, this means checking variables in the preceding code and in the
27113 calling subprogram to verify that the value observed is explainable from other
27114 values (one must apply the procedure recursively to those
27115 other values); or re-running the code and stopping a little earlier
27116 (perhaps before the call) and stepping to better see how the variable obtained
27117 the value in question; or continuing to step @emph{from} the point of the
27118 strange value to see if code motion had simply moved the variable's
27119 assignments later.
27120 @end itemize
27122 In light of such anomalies, a recommended technique is to use @emph{-O0}
27123 early in the software development cycle, when extensive debugging capabilities
27124 are most needed, and then move to @emph{-O1} and later @emph{-O2} as
27125 the debugger becomes less critical.
27126 Whether to use the @emph{-g} switch in the release version is
27127 a release management issue.
27128 Note that if you use @emph{-g} you can then use the @emph{strip} program
27129 on the resulting executable,
27130 which removes both debugging information and global symbols.
27132 @node Inlining of Subprograms,Floating_Point_Operations,Debugging Optimized Code,Performance Considerations
27133 @anchor{gnat_ugn/gnat_and_program_execution id34}@anchor{230}@anchor{gnat_ugn/gnat_and_program_execution inlining-of-subprograms}@anchor{114}
27134 @subsubsection Inlining of Subprograms
27137 A call to a subprogram in the current unit is inlined if all the
27138 following conditions are met:
27141 @itemize *
27143 @item 
27144 The optimization level is at least @emph{-O1}.
27146 @item 
27147 The called subprogram is suitable for inlining: It must be small enough
27148 and not contain something that @emph{gcc} cannot support in inlined
27149 subprograms.
27151 @geindex pragma Inline
27153 @geindex Inline
27155 @item 
27156 Any one of the following applies: @cite{pragma Inline} is applied to the
27157 subprogram and the @emph{-gnatn} switch is specified; the
27158 subprogram is local to the unit and called once from within it; the
27159 subprogram is small and optimization level @emph{-O2} is specified;
27160 optimization level @emph{-O3} is specified.
27161 @end itemize
27163 Calls to subprograms in @emph{with}ed units are normally not inlined.
27164 To achieve actual inlining (that is, replacement of the call by the code
27165 in the body of the subprogram), the following conditions must all be true:
27168 @itemize *
27170 @item 
27171 The optimization level is at least @emph{-O1}.
27173 @item 
27174 The called subprogram is suitable for inlining: It must be small enough
27175 and not contain something that @emph{gcc} cannot support in inlined
27176 subprograms.
27178 @item 
27179 The call appears in a body (not in a package spec).
27181 @item 
27182 There is a @cite{pragma Inline} for the subprogram.
27184 @item 
27185 The @emph{-gnatn} switch is used on the command line.
27186 @end itemize
27188 Even if all these conditions are met, it may not be possible for
27189 the compiler to inline the call, due to the length of the body,
27190 or features in the body that make it impossible for the compiler
27191 to do the inlining.
27193 Note that specifying the @emph{-gnatn} switch causes additional
27194 compilation dependencies. Consider the following:
27196 @quotation
27198 @example
27199 package R is
27200    procedure Q;
27201    pragma Inline (Q);
27202 end R;
27203 package body R is
27204    ...
27205 end R;
27207 with R;
27208 procedure Main is
27209 begin
27210    ...
27211    R.Q;
27212 end Main;
27213 @end example
27214 @end quotation
27216 With the default behavior (no @emph{-gnatn} switch specified), the
27217 compilation of the @cite{Main} procedure depends only on its own source,
27218 @code{main.adb}, and the spec of the package in file @code{r.ads}. This
27219 means that editing the body of @cite{R} does not require recompiling
27220 @cite{Main}.
27222 On the other hand, the call @cite{R.Q} is not inlined under these
27223 circumstances. If the @emph{-gnatn} switch is present when @cite{Main}
27224 is compiled, the call will be inlined if the body of @cite{Q} is small
27225 enough, but now @cite{Main} depends on the body of @cite{R} in
27226 @code{r.adb} as well as on the spec. This means that if this body is edited,
27227 the main program must be recompiled. Note that this extra dependency
27228 occurs whether or not the call is in fact inlined by @emph{gcc}.
27230 The use of front end inlining with @emph{-gnatN} generates similar
27231 additional dependencies.
27233 @geindex -fno-inline (gcc)
27235 Note: The @emph{-fno-inline} switch overrides all other conditions and ensures that
27236 no inlining occurs, unless requested with pragma Inline_Always for gcc
27237 back-ends. The extra dependences resulting from @emph{-gnatn} will still be active,
27238 even if this switch is used to suppress the resulting inlining actions.
27240 @geindex -fno-inline-functions (gcc)
27242 Note: The @emph{-fno-inline-functions} switch can be used to prevent
27243 automatic inlining of subprograms if @emph{-O3} is used.
27245 @geindex -fno-inline-small-functions (gcc)
27247 Note: The @emph{-fno-inline-small-functions} switch can be used to prevent
27248 automatic inlining of small subprograms if @emph{-O2} is used.
27250 @geindex -fno-inline-functions-called-once (gcc)
27252 Note: The @emph{-fno-inline-functions-called-once} switch
27253 can be used to prevent inlining of subprograms local to the unit
27254 and called once from within it if @emph{-O1} is used.
27256 Note regarding the use of @emph{-O3}: @emph{-gnatn} is made up of two
27257 sub-switches @emph{-gnatn1} and @emph{-gnatn2} that can be directly
27258 specified in lieu of it, @emph{-gnatn} being translated into one of them
27259 based on the optimization level. With @emph{-O2} or below, @emph{-gnatn}
27260 is equivalent to @emph{-gnatn1} which activates pragma @cite{Inline} with
27261 moderate inlining across modules. With @emph{-O3}, @emph{-gnatn} is
27262 equivalent to @emph{-gnatn2} which activates pragma @cite{Inline} with
27263 full inlining across modules. If you have used pragma @cite{Inline} in
27264 appropriate cases, then it is usually much better to use @emph{-O2}
27265 and @emph{-gnatn} and avoid the use of @emph{-O3} which has the additional
27266 effect of inlining subprograms you did not think should be inlined. We have
27267 found that the use of @emph{-O3} may slow down the compilation and increase
27268 the code size by performing excessive inlining, leading to increased
27269 instruction cache pressure from the increased code size and thus minor
27270 performance improvements. So the bottom line here is that you should not
27271 automatically assume that @emph{-O3} is better than @emph{-O2}, and
27272 indeed you should use @emph{-O3} only if tests show that it actually
27273 improves performance for your program.
27275 @node Floating_Point_Operations,Vectorization of loops,Inlining of Subprograms,Performance Considerations
27276 @anchor{gnat_ugn/gnat_and_program_execution floating-point-operations}@anchor{231}@anchor{gnat_ugn/gnat_and_program_execution id35}@anchor{232}
27277 @subsubsection Floating_Point_Operations
27280 @geindex Floating-Point Operations
27282 On almost all targets, GNAT maps Float and Long_Float to the 32-bit and
27283 64-bit standard IEEE floating-point representations, and operations will
27284 use standard IEEE arithmetic as provided by the processor. On most, but
27285 not all, architectures, the attribute Machine_Overflows is False for these
27286 types, meaning that the semantics of overflow is implementation-defined.
27287 In the case of GNAT, these semantics correspond to the normal IEEE
27288 treatment of infinities and NaN (not a number) values. For example,
27289 1.0 / 0.0 yields plus infinitiy and 0.0 / 0.0 yields a NaN. By
27290 avoiding explicit overflow checks, the performance is greatly improved
27291 on many targets. However, if required, floating-point overflow can be
27292 enabled by the use of the pragma Check_Float_Overflow.
27294 Another consideration that applies specifically to x86 32-bit
27295 architectures is which form of floating-point arithmetic is used.
27296 By default the operations use the old style x86 floating-point,
27297 which implements an 80-bit extended precision form (on these
27298 architectures the type Long_Long_Float corresponds to that form).
27299 In addition, generation of efficient code in this mode means that
27300 the extended precision form will be used for intermediate results.
27301 This may be helpful in improving the final precision of a complex
27302 expression. However it means that the results obtained on the x86
27303 will be different from those on other architectures, and for some
27304 algorithms, the extra intermediate precision can be detrimental.
27306 In addition to this old-style floating-point, all modern x86 chips
27307 implement an alternative floating-point operation model referred
27308 to as SSE2. In this model there is no extended form, and furthermore
27309 execution performance is significantly enhanced. To force GNAT to use
27310 this more modern form, use both of the switches:
27312 @quotation
27314 -msse2 -mfpmath=sse
27315 @end quotation
27317 A unit compiled with these switches will automatically use the more
27318 efficient SSE2 instruction set for Float and Long_Float operations.
27319 Note that the ABI has the same form for both floating-point models,
27320 so it is permissible to mix units compiled with and without these
27321 switches.
27323 @node Vectorization of loops,Other Optimization Switches,Floating_Point_Operations,Performance Considerations
27324 @anchor{gnat_ugn/gnat_and_program_execution id36}@anchor{233}@anchor{gnat_ugn/gnat_and_program_execution vectorization-of-loops}@anchor{234}
27325 @subsubsection Vectorization of loops
27328 @geindex Optimization Switches
27330 You can take advantage of the auto-vectorizer present in the @emph{gcc}
27331 back end to vectorize loops with GNAT.  The corresponding command line switch
27332 is @emph{-ftree-vectorize} but, as it is enabled by default at @emph{-O3}
27333 and other aggressive optimizations helpful for vectorization also are enabled
27334 by default at this level, using @emph{-O3} directly is recommended.
27336 You also need to make sure that the target architecture features a supported
27337 SIMD instruction set.  For example, for the x86 architecture, you should at
27338 least specify @emph{-msse2} to get significant vectorization (but you don't
27339 need to specify it for x86-64 as it is part of the base 64-bit architecture).
27340 Similarly, for the PowerPC architecture, you should specify @emph{-maltivec}.
27342 The preferred loop form for vectorization is the @cite{for} iteration scheme.
27343 Loops with a @cite{while} iteration scheme can also be vectorized if they are
27344 very simple, but the vectorizer will quickly give up otherwise.  With either
27345 iteration scheme, the flow of control must be straight, in particular no
27346 @cite{exit} statement may appear in the loop body.  The loop may however
27347 contain a single nested loop, if it can be vectorized when considered alone:
27349 @quotation
27351 @example
27352 A : array (1..4, 1..4) of Long_Float;
27353 S : array (1..4) of Long_Float;
27355 procedure Sum is
27356 begin
27357    for I in A'Range(1) loop
27358       for J in A'Range(2) loop
27359          S (I) := S (I) + A (I, J);
27360       end loop;
27361    end loop;
27362 end Sum;
27363 @end example
27364 @end quotation
27366 The vectorizable operations depend on the targeted SIMD instruction set, but
27367 the adding and some of the multiplying operators are generally supported, as
27368 well as the logical operators for modular types.  Note that, in the former
27369 case, enabling overflow checks, for example with @emph{-gnato}, totally
27370 disables vectorization.  The other checks are not supposed to have the same
27371 definitive effect, although compiling with @emph{-gnatp} might well reveal
27372 cases where some checks do thwart vectorization.
27374 Type conversions may also prevent vectorization if they involve semantics that
27375 are not directly supported by the code generator or the SIMD instruction set.
27376 A typical example is direct conversion from floating-point to integer types.
27377 The solution in this case is to use the following idiom:
27379 @quotation
27381 @example
27382 Integer (S'Truncation (F))
27383 @end example
27384 @end quotation
27386 if @cite{S} is the subtype of floating-point object @cite{F}.
27388 In most cases, the vectorizable loops are loops that iterate over arrays.
27389 All kinds of array types are supported, i.e. constrained array types with
27390 static bounds:
27392 @quotation
27394 @example
27395 type Array_Type is array (1 .. 4) of Long_Float;
27396 @end example
27397 @end quotation
27399 constrained array types with dynamic bounds:
27401 @quotation
27403 @example
27404 type Array_Type is array (1 .. Q.N) of Long_Float;
27406 type Array_Type is array (Q.K .. 4) of Long_Float;
27408 type Array_Type is array (Q.K .. Q.N) of Long_Float;
27409 @end example
27410 @end quotation
27412 or unconstrained array types:
27414 @quotation
27416 @example
27417 type Array_Type is array (Positive range <>) of Long_Float;
27418 @end example
27419 @end quotation
27421 The quality of the generated code decreases when the dynamic aspect of the
27422 array type increases, the worst code being generated for unconstrained array
27423 types.  This is so because, the less information the compiler has about the
27424 bounds of the array, the more fallback code it needs to generate in order to
27425 fix things up at run time.
27427 It is possible to specify that a given loop should be subject to vectorization
27428 preferably to other optimizations by means of pragma @cite{Loop_Optimize}:
27430 @quotation
27432 @example
27433 pragma Loop_Optimize (Vector);
27434 @end example
27435 @end quotation
27437 placed immediately within the loop will convey the appropriate hint to the
27438 compiler for this loop.
27440 It is also possible to help the compiler generate better vectorized code
27441 for a given loop by asserting that there are no loop-carried dependencies
27442 in the loop.  Consider for example the procedure:
27444 @quotation
27446 @example
27447 type Arr is array (1 .. 4) of Long_Float;
27449 procedure Add (X, Y : not null access Arr; R : not null access Arr) is
27450 begin
27451   for I in Arr'Range loop
27452     R(I) := X(I) + Y(I);
27453   end loop;
27454 end;
27455 @end example
27456 @end quotation
27458 By default, the compiler cannot unconditionally vectorize the loop because
27459 assigning to a component of the array designated by R in one iteration could
27460 change the value read from the components of the array designated by X or Y
27461 in a later iteration.  As a result, the compiler will generate two versions
27462 of the loop in the object code, one vectorized and the other not vectorized,
27463 as well as a test to select the appropriate version at run time.  This can
27464 be overcome by another hint:
27466 @quotation
27468 @example
27469 pragma Loop_Optimize (Ivdep);
27470 @end example
27471 @end quotation
27473 placed immediately within the loop will tell the compiler that it can safely
27474 omit the non-vectorized version of the loop as well as the run-time test.
27476 @node Other Optimization Switches,Optimization and Strict Aliasing,Vectorization of loops,Performance Considerations
27477 @anchor{gnat_ugn/gnat_and_program_execution id37}@anchor{235}@anchor{gnat_ugn/gnat_and_program_execution other-optimization-switches}@anchor{236}
27478 @subsubsection Other Optimization Switches
27481 @geindex Optimization Switches
27483 Since @cite{GNAT} uses the @emph{gcc} back end, all the specialized
27484 @emph{gcc} optimization switches are potentially usable. These switches
27485 have not been extensively tested with GNAT but can generally be expected
27486 to work. Examples of switches in this category are @emph{-funroll-loops}
27487 and the various target-specific @emph{-m} options (in particular, it has
27488 been observed that @emph{-march=xxx} can significantly improve performance
27489 on appropriate machines). For full details of these switches, see
27490 the @cite{Submodel Options} section in the @cite{Hardware Models and Configurations}
27491 chapter of @cite{Using the GNU Compiler Collection (GCC)}.
27493 @node Optimization and Strict Aliasing,Aliased Variables and Optimization,Other Optimization Switches,Performance Considerations
27494 @anchor{gnat_ugn/gnat_and_program_execution optimization-and-strict-aliasing}@anchor{f8}@anchor{gnat_ugn/gnat_and_program_execution id38}@anchor{237}
27495 @subsubsection Optimization and Strict Aliasing
27498 @geindex Aliasing
27500 @geindex Strict Aliasing
27502 @geindex No_Strict_Aliasing
27504 The strong typing capabilities of Ada allow an optimizer to generate
27505 efficient code in situations where other languages would be forced to
27506 make worst case assumptions preventing such optimizations. Consider
27507 the following example:
27509 @quotation
27511 @example
27512 procedure R is
27513    type Int1 is new Integer;
27514    type Int2 is new Integer;
27515    type Int1A is access Int1;
27516    type Int2A is access Int2;
27517    Int1V : Int1A;
27518    Int2V : Int2A;
27519    ...
27521 begin
27522    ...
27523    for J in Data'Range loop
27524       if Data (J) = Int1V.all then
27525          Int2V.all := Int2V.all + 1;
27526       end if;
27527    end loop;
27528    ...
27529 end R;
27530 @end example
27531 @end quotation
27533 In this example, since the variable @cite{Int1V} can only access objects
27534 of type @cite{Int1}, and @cite{Int2V} can only access objects of type
27535 @cite{Int2}, there is no possibility that the assignment to
27536 @cite{Int2V.all} affects the value of @cite{Int1V.all}. This means that
27537 the compiler optimizer can "know" that the value @cite{Int1V.all} is constant
27538 for all iterations of the loop and avoid the extra memory reference
27539 required to dereference it each time through the loop.
27541 This kind of optimization, called strict aliasing analysis, is
27542 triggered by specifying an optimization level of @emph{-O2} or
27543 higher or @emph{-Os} and allows @cite{GNAT} to generate more efficient code
27544 when access values are involved.
27546 However, although this optimization is always correct in terms of
27547 the formal semantics of the Ada Reference Manual, difficulties can
27548 arise if features like @cite{Unchecked_Conversion} are used to break
27549 the typing system. Consider the following complete program example:
27551 @quotation
27553 @example
27554 package p1 is
27555    type int1 is new integer;
27556    type int2 is new integer;
27557    type a1 is access int1;
27558    type a2 is access int2;
27559 end p1;
27561 with p1; use p1;
27562 package p2 is
27563    function to_a2 (Input : a1) return a2;
27564 end p2;
27566 with Unchecked_Conversion;
27567 package body p2 is
27568    function to_a2 (Input : a1) return a2 is
27569       function to_a2u is
27570         new Unchecked_Conversion (a1, a2);
27571    begin
27572       return to_a2u (Input);
27573    end to_a2;
27574 end p2;
27576 with p2; use p2;
27577 with p1; use p1;
27578 with Text_IO; use Text_IO;
27579 procedure m is
27580    v1 : a1 := new int1;
27581    v2 : a2 := to_a2 (v1);
27582 begin
27583    v1.all := 1;
27584    v2.all := 0;
27585    put_line (int1'image (v1.all));
27586 end;
27587 @end example
27588 @end quotation
27590 This program prints out 0 in @emph{-O0} or @emph{-O1}
27591 mode, but it prints out 1 in @emph{-O2} mode. That's
27592 because in strict aliasing mode, the compiler can and
27593 does assume that the assignment to @cite{v2.all} could not
27594 affect the value of @cite{v1.all}, since different types
27595 are involved.
27597 This behavior is not a case of non-conformance with the standard, since
27598 the Ada RM specifies that an unchecked conversion where the resulting
27599 bit pattern is not a correct value of the target type can result in an
27600 abnormal value and attempting to reference an abnormal value makes the
27601 execution of a program erroneous.  That's the case here since the result
27602 does not point to an object of type @cite{int2}.  This means that the
27603 effect is entirely unpredictable.
27605 However, although that explanation may satisfy a language
27606 lawyer, in practice an applications programmer expects an
27607 unchecked conversion involving pointers to create true
27608 aliases and the behavior of printing 1 seems plain wrong.
27609 In this case, the strict aliasing optimization is unwelcome.
27611 Indeed the compiler recognizes this possibility, and the
27612 unchecked conversion generates a warning:
27614 @quotation
27616 @example
27617 p2.adb:5:07: warning: possible aliasing problem with type "a2"
27618 p2.adb:5:07: warning: use -fno-strict-aliasing switch for references
27619 p2.adb:5:07: warning:  or use "pragma No_Strict_Aliasing (a2);"
27620 @end example
27621 @end quotation
27623 Unfortunately the problem is recognized when compiling the body of
27624 package @cite{p2}, but the actual "bad" code is generated while
27625 compiling the body of @cite{m} and this latter compilation does not see
27626 the suspicious @cite{Unchecked_Conversion}.
27628 As implied by the warning message, there are approaches you can use to
27629 avoid the unwanted strict aliasing optimization in a case like this.
27631 One possibility is to simply avoid the use of @emph{-O2}, but
27632 that is a bit drastic, since it throws away a number of useful
27633 optimizations that do not involve strict aliasing assumptions.
27635 A less drastic approach is to compile the program using the
27636 option @emph{-fno-strict-aliasing}. Actually it is only the
27637 unit containing the dereferencing of the suspicious pointer
27638 that needs to be compiled. So in this case, if we compile
27639 unit @cite{m} with this switch, then we get the expected
27640 value of zero printed. Analyzing which units might need
27641 the switch can be painful, so a more reasonable approach
27642 is to compile the entire program with options @emph{-O2}
27643 and @emph{-fno-strict-aliasing}. If the performance is
27644 satisfactory with this combination of options, then the
27645 advantage is that the entire issue of possible "wrong"
27646 optimization due to strict aliasing is avoided.
27648 To avoid the use of compiler switches, the configuration
27649 pragma @cite{No_Strict_Aliasing} with no parameters may be
27650 used to specify that for all access types, the strict
27651 aliasing optimization should be suppressed.
27653 However, these approaches are still overkill, in that they causes
27654 all manipulations of all access values to be deoptimized. A more
27655 refined approach is to concentrate attention on the specific
27656 access type identified as problematic.
27658 First, if a careful analysis of uses of the pointer shows
27659 that there are no possible problematic references, then
27660 the warning can be suppressed by bracketing the
27661 instantiation of @cite{Unchecked_Conversion} to turn
27662 the warning off:
27664 @quotation
27666 @example
27667 pragma Warnings (Off);
27668 function to_a2u is
27669   new Unchecked_Conversion (a1, a2);
27670 pragma Warnings (On);
27671 @end example
27672 @end quotation
27674 Of course that approach is not appropriate for this particular
27675 example, since indeed there is a problematic reference. In this
27676 case we can take one of two other approaches.
27678 The first possibility is to move the instantiation of unchecked
27679 conversion to the unit in which the type is declared. In
27680 this example, we would move the instantiation of
27681 @cite{Unchecked_Conversion} from the body of package
27682 @cite{p2} to the spec of package @cite{p1}. Now the
27683 warning disappears. That's because any use of the
27684 access type knows there is a suspicious unchecked
27685 conversion, and the strict aliasing optimization
27686 is automatically suppressed for the type.
27688 If it is not practical to move the unchecked conversion to the same unit
27689 in which the destination access type is declared (perhaps because the
27690 source type is not visible in that unit), you may use pragma
27691 @cite{No_Strict_Aliasing} for the type. This pragma must occur in the
27692 same declarative sequence as the declaration of the access type:
27694 @quotation
27696 @example
27697 type a2 is access int2;
27698 pragma No_Strict_Aliasing (a2);
27699 @end example
27700 @end quotation
27702 Here again, the compiler now knows that the strict aliasing optimization
27703 should be suppressed for any reference to type @cite{a2} and the
27704 expected behavior is obtained.
27706 Finally, note that although the compiler can generate warnings for
27707 simple cases of unchecked conversions, there are tricker and more
27708 indirect ways of creating type incorrect aliases which the compiler
27709 cannot detect. Examples are the use of address overlays and unchecked
27710 conversions involving composite types containing access types as
27711 components. In such cases, no warnings are generated, but there can
27712 still be aliasing problems. One safe coding practice is to forbid the
27713 use of address clauses for type overlaying, and to allow unchecked
27714 conversion only for primitive types. This is not really a significant
27715 restriction since any possible desired effect can be achieved by
27716 unchecked conversion of access values.
27718 The aliasing analysis done in strict aliasing mode can certainly
27719 have significant benefits. We have seen cases of large scale
27720 application code where the time is increased by up to 5% by turning
27721 this optimization off. If you have code that includes significant
27722 usage of unchecked conversion, you might want to just stick with
27723 @emph{-O1} and avoid the entire issue. If you get adequate
27724 performance at this level of optimization level, that's probably
27725 the safest approach. If tests show that you really need higher
27726 levels of optimization, then you can experiment with @emph{-O2}
27727 and @emph{-O2 -fno-strict-aliasing} to see how much effect this
27728 has on size and speed of the code. If you really need to use
27729 @emph{-O2} with strict aliasing in effect, then you should
27730 review any uses of unchecked conversion of access types,
27731 particularly if you are getting the warnings described above.
27733 @node Aliased Variables and Optimization,Atomic Variables and Optimization,Optimization and Strict Aliasing,Performance Considerations
27734 @anchor{gnat_ugn/gnat_and_program_execution aliased-variables-and-optimization}@anchor{238}@anchor{gnat_ugn/gnat_and_program_execution id39}@anchor{239}
27735 @subsubsection Aliased Variables and Optimization
27738 @geindex Aliasing
27740 There are scenarios in which programs may
27741 use low level techniques to modify variables
27742 that otherwise might be considered to be unassigned. For example,
27743 a variable can be passed to a procedure by reference, which takes
27744 the address of the parameter and uses the address to modify the
27745 variable's value, even though it is passed as an IN parameter.
27746 Consider the following example:
27748 @quotation
27750 @example
27751 procedure P is
27752    Max_Length : constant Natural := 16;
27753    type Char_Ptr is access all Character;
27755    procedure Get_String(Buffer: Char_Ptr; Size : Integer);
27756    pragma Import (C, Get_String, "get_string");
27758    Name : aliased String (1 .. Max_Length) := (others => ' ');
27759    Temp : Char_Ptr;
27761    function Addr (S : String) return Char_Ptr is
27762       function To_Char_Ptr is
27763         new Ada.Unchecked_Conversion (System.Address, Char_Ptr);
27764    begin
27765       return To_Char_Ptr (S (S'First)'Address);
27766    end;
27768 begin
27769    Temp := Addr (Name);
27770    Get_String (Temp, Max_Length);
27771 end;
27772 @end example
27773 @end quotation
27775 where Get_String is a C function that uses the address in Temp to
27776 modify the variable @cite{Name}. This code is dubious, and arguably
27777 erroneous, and the compiler would be entitled to assume that
27778 @cite{Name} is never modified, and generate code accordingly.
27780 However, in practice, this would cause some existing code that
27781 seems to work with no optimization to start failing at high
27782 levels of optimzization.
27784 What the compiler does for such cases is to assume that marking
27785 a variable as aliased indicates that some "funny business" may
27786 be going on. The optimizer recognizes the aliased keyword and
27787 inhibits optimizations that assume the value cannot be assigned.
27788 This means that the above example will in fact "work" reliably,
27789 that is, it will produce the expected results.
27791 @node Atomic Variables and Optimization,Passive Task Optimization,Aliased Variables and Optimization,Performance Considerations
27792 @anchor{gnat_ugn/gnat_and_program_execution atomic-variables-and-optimization}@anchor{23a}@anchor{gnat_ugn/gnat_and_program_execution id40}@anchor{23b}
27793 @subsubsection Atomic Variables and Optimization
27796 @geindex Atomic
27798 There are two considerations with regard to performance when
27799 atomic variables are used.
27801 First, the RM only guarantees that access to atomic variables
27802 be atomic, it has nothing to say about how this is achieved,
27803 though there is a strong implication that this should not be
27804 achieved by explicit locking code. Indeed GNAT will never
27805 generate any locking code for atomic variable access (it will
27806 simply reject any attempt to make a variable or type atomic
27807 if the atomic access cannot be achieved without such locking code).
27809 That being said, it is important to understand that you cannot
27810 assume that the entire variable will always be accessed. Consider
27811 this example:
27813 @quotation
27815 @example
27816 type R is record
27817    A,B,C,D : Character;
27818 end record;
27819 for R'Size use 32;
27820 for R'Alignment use 4;
27822 RV : R;
27823 pragma Atomic (RV);
27824 X : Character;
27826 X := RV.B;
27827 @end example
27828 @end quotation
27830 You cannot assume that the reference to @cite{RV.B}
27831 will read the entire 32-bit
27832 variable with a single load instruction. It is perfectly legitimate if
27833 the hardware allows it to do a byte read of just the B field. This read
27834 is still atomic, which is all the RM requires. GNAT can and does take
27835 advantage of this, depending on the architecture and optimization level.
27836 Any assumption to the contrary is non-portable and risky. Even if you
27837 examine the assembly language and see a full 32-bit load, this might
27838 change in a future version of the compiler.
27840 If your application requires that all accesses to @cite{RV} in this
27841 example be full 32-bit loads, you need to make a copy for the access
27842 as in:
27844 @quotation
27846 @example
27847 declare
27848    RV_Copy : constant R := RV;
27849 begin
27850    X := RV_Copy.B;
27851 end;
27852 @end example
27853 @end quotation
27855 Now the reference to RV must read the whole variable.
27856 Actually one can imagine some compiler which figures
27857 out that the whole copy is not required (because only
27858 the B field is actually accessed), but GNAT
27859 certainly won't do that, and we don't know of any
27860 compiler that would not handle this right, and the
27861 above code will in practice work portably across
27862 all architectures (that permit the Atomic declaration).
27864 The second issue with atomic variables has to do with
27865 the possible requirement of generating synchronization
27866 code. For more details on this, consult the sections on
27867 the pragmas Enable/Disable_Atomic_Synchronization in the
27868 GNAT Reference Manual. If performance is critical, and
27869 such synchronization code is not required, it may be
27870 useful to disable it.
27872 @node Passive Task Optimization,,Atomic Variables and Optimization,Performance Considerations
27873 @anchor{gnat_ugn/gnat_and_program_execution id41}@anchor{23c}@anchor{gnat_ugn/gnat_and_program_execution passive-task-optimization}@anchor{23d}
27874 @subsubsection Passive Task Optimization
27877 @geindex Passive Task
27879 A passive task is one which is sufficiently simple that
27880 in theory a compiler could recognize it an implement it
27881 efficiently without creating a new thread. The original design
27882 of Ada 83 had in mind this kind of passive task optimization, but
27883 only a few Ada 83 compilers attempted it. The problem was that
27884 it was difficult to determine the exact conditions under which
27885 the optimization was possible. The result is a very fragile
27886 optimization where a very minor change in the program can
27887 suddenly silently make a task non-optimizable.
27889 With the revisiting of this issue in Ada 95, there was general
27890 agreement that this approach was fundamentally flawed, and the
27891 notion of protected types was introduced. When using protected
27892 types, the restrictions are well defined, and you KNOW that the
27893 operations will be optimized, and furthermore this optimized
27894 performance is fully portable.
27896 Although it would theoretically be possible for GNAT to attempt to
27897 do this optimization, but it really doesn't make sense in the
27898 context of Ada 95, and none of the Ada 95 compilers implement
27899 this optimization as far as we know. In particular GNAT never
27900 attempts to perform this optimization.
27902 In any new Ada 95 code that is written, you should always
27903 use protected types in place of tasks that might be able to
27904 be optimized in this manner.
27905 Of course this does not help if you have legacy Ada 83 code
27906 that depends on this optimization, but it is unusual to encounter
27907 a case where the performance gains from this optimization
27908 are significant.
27910 Your program should work correctly without this optimization. If
27911 you have performance problems, then the most practical
27912 approach is to figure out exactly where these performance problems
27913 arise, and update those particular tasks to be protected types. Note
27914 that typically clients of the tasks who call entries, will not have
27915 to be modified, only the task definition itself.
27917 @node Text_IO Suggestions,Reducing Size of Executables with Unused Subprogram/Data Elimination,Performance Considerations,Improving Performance
27918 @anchor{gnat_ugn/gnat_and_program_execution text-io-suggestions}@anchor{23e}@anchor{gnat_ugn/gnat_and_program_execution id42}@anchor{23f}
27919 @subsection @cite{Text_IO} Suggestions
27922 @geindex Text_IO and performance
27924 The @cite{Ada.Text_IO} package has fairly high overheads due in part to
27925 the requirement of maintaining page and line counts. If performance
27926 is critical, a recommendation is to use @cite{Stream_IO} instead of
27927 @cite{Text_IO} for volume output, since this package has less overhead.
27929 If @cite{Text_IO} must be used, note that by default output to the standard
27930 output and standard error files is unbuffered (this provides better
27931 behavior when output statements are used for debugging, or if the
27932 progress of a program is observed by tracking the output, e.g. by
27933 using the Unix @emph{tail -f} command to watch redirected output.
27935 If you are generating large volumes of output with @cite{Text_IO} and
27936 performance is an important factor, use a designated file instead
27937 of the standard output file, or change the standard output file to
27938 be buffered using @cite{Interfaces.C_Streams.setvbuf}.
27940 @node Reducing Size of Executables with Unused Subprogram/Data Elimination,,Text_IO Suggestions,Improving Performance
27941 @anchor{gnat_ugn/gnat_and_program_execution id43}@anchor{240}@anchor{gnat_ugn/gnat_and_program_execution reducing-size-of-executables-with-unused-subprogram-data-elimination}@anchor{241}
27942 @subsection Reducing Size of Executables with Unused Subprogram/Data Elimination
27945 @geindex Uunused subprogram/data elimination
27947 This section describes how you can eliminate unused subprograms and data from
27948 your executable just by setting options at compilation time.
27950 @menu
27951 * About unused subprogram/data elimination:: 
27952 * Compilation options:: 
27953 * Example of unused subprogram/data elimination:: 
27955 @end menu
27957 @node About unused subprogram/data elimination,Compilation options,,Reducing Size of Executables with Unused Subprogram/Data Elimination
27958 @anchor{gnat_ugn/gnat_and_program_execution id44}@anchor{242}@anchor{gnat_ugn/gnat_and_program_execution about-unused-subprogram-data-elimination}@anchor{243}
27959 @subsubsection About unused subprogram/data elimination
27962 By default, an executable contains all code and data of its composing objects
27963 (directly linked or coming from statically linked libraries), even data or code
27964 never used by this executable.
27966 This feature will allow you to eliminate such unused code from your
27967 executable, making it smaller (in disk and in memory).
27969 This functionality is available on all Linux platforms except for the IA-64
27970 architecture and on all cross platforms using the ELF binary file format.
27971 In both cases GNU binutils version 2.16 or later are required to enable it.
27973 @node Compilation options,Example of unused subprogram/data elimination,About unused subprogram/data elimination,Reducing Size of Executables with Unused Subprogram/Data Elimination
27974 @anchor{gnat_ugn/gnat_and_program_execution id45}@anchor{244}@anchor{gnat_ugn/gnat_and_program_execution compilation-options}@anchor{245}
27975 @subsubsection Compilation options
27978 The operation of eliminating the unused code and data from the final executable
27979 is directly performed by the linker.
27981 @geindex -ffunction-sections (gcc)
27983 @geindex -fdata-sections (gcc)
27985 In order to do this, it has to work with objects compiled with the
27986 following options:
27987 @emph{-ffunction-sections} @emph{-fdata-sections}.
27989 These options are usable with C and Ada files.
27990 They will place respectively each
27991 function or data in a separate section in the resulting object file.
27993 Once the objects and static libraries are created with these options, the
27994 linker can perform the dead code elimination. You can do this by setting
27995 the @emph{-Wl,--gc-sections} option to gcc command or in the
27996 @emph{-largs} section of @emph{gnatmake}. This will perform a
27997 garbage collection of code and data never referenced.
27999 If the linker performs a partial link (@emph{-r} linker option), then you
28000 will need to provide the entry point using the @emph{-e} / @emph{--entry}
28001 linker option.
28003 Note that objects compiled without the @emph{-ffunction-sections} and
28004 @emph{-fdata-sections} options can still be linked with the executable.
28005 However, no dead code elimination will be performed on those objects (they will
28006 be linked as is).
28008 The GNAT static library is now compiled with -ffunction-sections and
28009 -fdata-sections on some platforms. This allows you to eliminate the unused code
28010 and data of the GNAT library from your executable.
28012 @node Example of unused subprogram/data elimination,,Compilation options,Reducing Size of Executables with Unused Subprogram/Data Elimination
28013 @anchor{gnat_ugn/gnat_and_program_execution id46}@anchor{246}@anchor{gnat_ugn/gnat_and_program_execution example-of-unused-subprogram-data-elimination}@anchor{247}
28014 @subsubsection Example of unused subprogram/data elimination
28017 Here is a simple example:
28019 @quotation
28021 @example
28022 with Aux;
28024 procedure Test is
28025 begin
28026    Aux.Used (10);
28027 end Test;
28029 package Aux is
28030    Used_Data   : Integer;
28031    Unused_Data : Integer;
28033    procedure Used   (Data : Integer);
28034    procedure Unused (Data : Integer);
28035 end Aux;
28037 package body Aux is
28038    procedure Used (Data : Integer) is
28039    begin
28040       Used_Data := Data;
28041    end Used;
28043    procedure Unused (Data : Integer) is
28044    begin
28045       Unused_Data := Data;
28046    end Unused;
28047 end Aux;
28048 @end example
28049 @end quotation
28051 @cite{Unused} and @cite{Unused_Data} are never referenced in this code
28052 excerpt, and hence they may be safely removed from the final executable.
28054 @quotation
28056 @example
28057 $ gnatmake test
28059 $ nm test | grep used
28060 020015f0 T aux__unused
28061 02005d88 B aux__unused_data
28062 020015cc T aux__used
28063 02005d84 B aux__used_data
28065 $ gnatmake test -cargs -fdata-sections -ffunction-sections \\
28066      -largs -Wl,--gc-sections
28068 $ nm test | grep used
28069 02005350 T aux__used
28070 0201ffe0 B aux__used_data
28071 @end example
28072 @end quotation
28074 It can be observed that the procedure @cite{Unused} and the object
28075 @cite{Unused_Data} are removed by the linker when using the
28076 appropriate options.
28078 @geindex Overflow checks
28080 @geindex Checks (overflow)
28083 @node Overflow Check Handling in GNAT,Performing Dimensionality Analysis in GNAT,Improving Performance,GNAT and Program Execution
28084 @anchor{gnat_ugn/gnat_and_program_execution id54}@anchor{1f3}@anchor{gnat_ugn/gnat_and_program_execution overflow-check-handling-in-gnat}@anchor{29}
28085 @section Overflow Check Handling in GNAT
28088 This section explains how to control the handling of overflow checks.
28090 @menu
28091 * Background:: 
28092 * Overflow Checking Modes in GNAT:: 
28093 * Specifying the Desired Mode:: 
28094 * Default Settings:: 
28095 * Implementation Notes:: 
28097 @end menu
28099 @node Background,Overflow Checking Modes in GNAT,,Overflow Check Handling in GNAT
28100 @anchor{gnat_ugn/gnat_and_program_execution id55}@anchor{248}@anchor{gnat_ugn/gnat_and_program_execution background}@anchor{249}
28101 @subsection Background
28104 Overflow checks are checks that the compiler may make to ensure
28105 that intermediate results are not out of range. For example:
28107 @quotation
28109 @example
28110 A : Integer;
28112 A := A + 1;
28113 @end example
28114 @end quotation
28116 If @cite{A} has the value @cite{Integer'Last}, then the addition may cause
28117 overflow since the result is out of range of the type @cite{Integer}.
28118 In this case @cite{Constraint_Error} will be raised if checks are
28119 enabled.
28121 A trickier situation arises in examples like the following:
28123 @quotation
28125 @example
28126 A, C : Integer;
28128 A := (A + 1) + C;
28129 @end example
28130 @end quotation
28132 where @cite{A} is @cite{Integer'Last} and @cite{C} is @cite{-1}.
28133 Now the final result of the expression on the right hand side is
28134 @cite{Integer'Last} which is in range, but the question arises whether the
28135 intermediate addition of @cite{(A + 1)} raises an overflow error.
28137 The (perhaps surprising) answer is that the Ada language
28138 definition does not answer this question. Instead it leaves
28139 it up to the implementation to do one of two things if overflow
28140 checks are enabled.
28143 @itemize *
28145 @item 
28146 raise an exception (@cite{Constraint_Error}), or
28148 @item 
28149 yield the correct mathematical result which is then used in
28150 subsequent operations.
28151 @end itemize
28153 If the compiler chooses the first approach, then the assignment of this
28154 example will indeed raise @cite{Constraint_Error} if overflow checking is
28155 enabled, or result in erroneous execution if overflow checks are suppressed.
28157 But if the compiler
28158 chooses the second approach, then it can perform both additions yielding
28159 the correct mathematical result, which is in range, so no exception
28160 will be raised, and the right result is obtained, regardless of whether
28161 overflow checks are suppressed.
28163 Note that in the first example an
28164 exception will be raised in either case, since if the compiler
28165 gives the correct mathematical result for the addition, it will
28166 be out of range of the target type of the assignment, and thus
28167 fails the range check.
28169 This lack of specified behavior in the handling of overflow for
28170 intermediate results is a source of non-portability, and can thus
28171 be problematic when programs are ported. Most typically this arises
28172 in a situation where the original compiler did not raise an exception,
28173 and then the application is moved to a compiler where the check is
28174 performed on the intermediate result and an unexpected exception is
28175 raised.
28177 Furthermore, when using Ada 2012's preconditions and other
28178 assertion forms, another issue arises. Consider:
28180 @quotation
28182 @example
28183 procedure P (A, B : Integer) with
28184   Pre => A + B <= Integer'Last;
28185 @end example
28186 @end quotation
28188 One often wants to regard arithmetic in a context like this from
28189 a mathematical point of view. So for example, if the two actual parameters
28190 for a call to @cite{P} are both @cite{Integer'Last}, then
28191 the precondition should be regarded as False. If we are executing
28192 in a mode with run-time checks enabled for preconditions, then we would
28193 like this precondition to fail, rather than raising an exception
28194 because of the intermediate overflow.
28196 However, the language definition leaves the specification of
28197 whether the above condition fails (raising @cite{Assert_Error}) or
28198 causes an intermediate overflow (raising @cite{Constraint_Error})
28199 up to the implementation.
28201 The situation is worse in a case such as the following:
28203 @quotation
28205 @example
28206 procedure Q (A, B, C : Integer) with
28207   Pre => A + B + C <= Integer'Last;
28208 @end example
28209 @end quotation
28211 Consider the call
28213 @quotation
28215 @example
28216 Q (A => Integer'Last, B => 1, C => -1);
28217 @end example
28218 @end quotation
28220 From a mathematical point of view the precondition
28221 is True, but at run time we may (but are not guaranteed to) get an
28222 exception raised because of the intermediate overflow (and we really
28223 would prefer this precondition to be considered True at run time).
28225 @node Overflow Checking Modes in GNAT,Specifying the Desired Mode,Background,Overflow Check Handling in GNAT
28226 @anchor{gnat_ugn/gnat_and_program_execution id56}@anchor{24a}@anchor{gnat_ugn/gnat_and_program_execution overflow-checking-modes-in-gnat}@anchor{24b}
28227 @subsection Overflow Checking Modes in GNAT
28230 To deal with the portability issue, and with the problem of
28231 mathematical versus run-time interpretation of the expressions in
28232 assertions, GNAT provides comprehensive control over the handling
28233 of intermediate overflow. GNAT can operate in three modes, and
28234 furthemore, permits separate selection of operating modes for
28235 the expressions within assertions (here the term 'assertions'
28236 is used in the technical sense, which includes preconditions and so forth)
28237 and for expressions appearing outside assertions.
28239 The three modes are:
28242 @itemize *
28244 @item 
28245 @emph{Use base type for intermediate operations} (@cite{STRICT})
28247 In this mode, all intermediate results for predefined arithmetic
28248 operators are computed using the base type, and the result must
28249 be in range of the base type. If this is not the
28250 case then either an exception is raised (if overflow checks are
28251 enabled) or the execution is erroneous (if overflow checks are suppressed).
28252 This is the normal default mode.
28254 @item 
28255 @emph{Most intermediate overflows avoided} (@cite{MINIMIZED})
28257 In this mode, the compiler attempts to avoid intermediate overflows by
28258 using a larger integer type, typically @cite{Long_Long_Integer},
28259 as the type in which arithmetic is
28260 performed for predefined arithmetic operators. This may be slightly more
28261 expensive at
28262 run time (compared to suppressing intermediate overflow checks), though
28263 the cost is negligible on modern 64-bit machines. For the examples given
28264 earlier, no intermediate overflows would have resulted in exceptions,
28265 since the intermediate results are all in the range of
28266 @cite{Long_Long_Integer} (typically 64-bits on nearly all implementations
28267 of GNAT). In addition, if checks are enabled, this reduces the number of
28268 checks that must be made, so this choice may actually result in an
28269 improvement in space and time behavior.
28271 However, there are cases where @cite{Long_Long_Integer} is not large
28272 enough, consider the following example:
28274 @quotation
28276 @example
28277 procedure R (A, B, C, D : Integer) with
28278   Pre => (A**2 * B**2) / (C**2 * D**2) <= 10;
28279 @end example
28280 @end quotation
28282 where @cite{A} = @cite{B} = @cite{C} = @cite{D} = @cite{Integer'Last}.
28283 Now the intermediate results are
28284 out of the range of @cite{Long_Long_Integer} even though the final result
28285 is in range and the precondition is True (from a mathematical point
28286 of view). In such a case, operating in this mode, an overflow occurs
28287 for the intermediate computation (which is why this mode
28288 says @emph{most} intermediate overflows are avoided). In this case,
28289 an exception is raised if overflow checks are enabled, and the
28290 execution is erroneous if overflow checks are suppressed.
28292 @item 
28293 @emph{All intermediate overflows avoided} (@cite{ELIMINATED})
28295 In this mode, the compiler  avoids all intermediate overflows
28296 by using arbitrary precision arithmetic as required. In this
28297 mode, the above example with @cite{A**2 * B**2} would
28298 not cause intermediate overflow, because the intermediate result
28299 would be evaluated using sufficient precision, and the result
28300 of evaluating the precondition would be True.
28302 This mode has the advantage of avoiding any intermediate
28303 overflows, but at the expense of significant run-time overhead,
28304 including the use of a library (included automatically in this
28305 mode) for multiple-precision arithmetic.
28307 This mode provides cleaner semantics for assertions, since now
28308 the run-time behavior emulates true arithmetic behavior for the
28309 predefined arithmetic operators, meaning that there is never a
28310 conflict between the mathematical view of the assertion, and its
28311 run-time behavior.
28313 Note that in this mode, the behavior is unaffected by whether or
28314 not overflow checks are suppressed, since overflow does not occur.
28315 It is possible for gigantic intermediate expressions to raise
28316 @cite{Storage_Error} as a result of attempting to compute the
28317 results of such expressions (e.g. @cite{Integer'Last ** Integer'Last})
28318 but overflow is impossible.
28319 @end itemize
28321 Note that these modes apply only to the evaluation of predefined
28322 arithmetic, membership, and comparison operators for signed integer
28323 aritmetic.
28325 For fixed-point arithmetic, checks can be suppressed. But if checks
28326 are enabled
28327 then fixed-point values are always checked for overflow against the
28328 base type for intermediate expressions (that is such checks always
28329 operate in the equivalent of @cite{STRICT} mode).
28331 For floating-point, on nearly all architectures, @cite{Machine_Overflows}
28332 is False, and IEEE infinities are generated, so overflow exceptions
28333 are never raised. If you want to avoid infinities, and check that
28334 final results of expressions are in range, then you can declare a
28335 constrained floating-point type, and range checks will be carried
28336 out in the normal manner (with infinite values always failing all
28337 range checks).
28339 @node Specifying the Desired Mode,Default Settings,Overflow Checking Modes in GNAT,Overflow Check Handling in GNAT
28340 @anchor{gnat_ugn/gnat_and_program_execution specifying-the-desired-mode}@anchor{fd}@anchor{gnat_ugn/gnat_and_program_execution id57}@anchor{24c}
28341 @subsection Specifying the Desired Mode
28344 @geindex pragma Overflow_Mode
28346 The desired mode of for handling intermediate overflow can be specified using
28347 either the @cite{Overflow_Mode} pragma or an equivalent compiler switch.
28348 The pragma has the form
28350 @quotation
28352 @example
28353 pragma Overflow_Mode ([General =>] MODE [, [Assertions =>] MODE]);
28354 @end example
28355 @end quotation
28357 where @cite{MODE} is one of
28360 @itemize *
28362 @item 
28363 @cite{STRICT}:  intermediate overflows checked (using base type)
28365 @item 
28366 @cite{MINIMIZED}: minimize intermediate overflows
28368 @item 
28369 @cite{ELIMINATED}: eliminate intermediate overflows
28370 @end itemize
28372 The case is ignored, so @cite{MINIMIZED}, @cite{Minimized} and
28373 @cite{minimized} all have the same effect.
28375 If only the @cite{General} parameter is present, then the given @cite{MODE}
28376 applies
28377 to expressions both within and outside assertions. If both arguments
28378 are present, then @cite{General} applies to expressions outside assertions,
28379 and @cite{Assertions} applies to expressions within assertions. For example:
28381 @quotation
28383 @example
28384 pragma Overflow_Mode
28385   (General => Minimized, Assertions => Eliminated);
28386 @end example
28387 @end quotation
28389 specifies that general expressions outside assertions be evaluated
28390 in 'minimize intermediate overflows' mode, and expressions within
28391 assertions be evaluated in 'eliminate intermediate overflows' mode.
28392 This is often a reasonable choice, avoiding excessive overhead
28393 outside assertions, but assuring a high degree of portability
28394 when importing code from another compiler, while incurring
28395 the extra overhead for assertion expressions to ensure that
28396 the behavior at run time matches the expected mathematical
28397 behavior.
28399 The @cite{Overflow_Mode} pragma has the same scoping and placement
28400 rules as pragma @cite{Suppress}, so it can occur either as a
28401 configuration pragma, specifying a default for the whole
28402 program, or in a declarative scope, where it applies to the
28403 remaining declarations and statements in that scope.
28405 Note that pragma @cite{Overflow_Mode} does not affect whether
28406 overflow checks are enabled or suppressed. It only controls the
28407 method used to compute intermediate values. To control whether
28408 overflow checking is enabled or suppressed, use pragma @cite{Suppress}
28409 or @cite{Unsuppress} in the usual manner
28411 @geindex -gnato? (gcc)
28413 @geindex -gnato?? (gcc)
28415 Additionally, a compiler switch @emph{-gnato?} or @emph{-gnato??}
28416 can be used to control the checking mode default (which can be subsequently
28417 overridden using pragmas).
28419 Here @code{?} is one of the digits @code{1} through @code{3}:
28421 @quotation
28424 @multitable {xxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} 
28425 @item
28427 @code{1}
28429 @tab
28431 use base type for intermediate operations (@cite{STRICT})
28433 @item
28435 @code{2}
28437 @tab
28439 minimize intermediate overflows (@cite{MINIMIZED})
28441 @item
28443 @code{3}
28445 @tab
28447 eliminate intermediate overflows (@cite{ELIMINATED})
28449 @end multitable
28451 @end quotation
28453 As with the pragma, if only one digit appears then it applies to all
28454 cases; if two digits are given, then the first applies outside
28455 assertions, and the second within assertions. Thus the equivalent
28456 of the example pragma above would be
28457 @emph{-gnato23}.
28459 If no digits follow the @emph{-gnato}, then it is equivalent to
28460 @emph{-gnato11},
28461 causing all intermediate operations to be computed using the base
28462 type (@cite{STRICT} mode).
28464 In addition to setting the mode used for computation of intermediate
28465 results, the @cite{-gnato} switch also enables overflow checking (which
28466 is suppressed by default). It thus combines the effect of using
28467 a pragma @cite{Overflow_Mode} and pragma @cite{Unsuppress}.
28469 @node Default Settings,Implementation Notes,Specifying the Desired Mode,Overflow Check Handling in GNAT
28470 @anchor{gnat_ugn/gnat_and_program_execution id58}@anchor{24d}@anchor{gnat_ugn/gnat_and_program_execution default-settings}@anchor{24e}
28471 @subsection Default Settings
28474 The default mode for overflow checks is
28476 @quotation
28478 @example
28479 General => Strict
28480 @end example
28481 @end quotation
28483 which causes all computations both inside and outside assertions to use
28484 the base type. In addition overflow checks are suppressed.
28486 This retains compatibility with previous versions of
28487 GNAT which suppressed overflow checks by default and always
28488 used the base type for computation of intermediate results.
28490 The switch @emph{-gnato} (with no digits following) is equivalent to
28491 .. index:: -gnato (gcc)
28493 @quotation
28495 @example
28496 General => Strict
28497 @end example
28498 @end quotation
28500 which causes overflow checking of all intermediate overflows
28501 both inside and outside assertions against the base type.
28502 This provides compatibility
28503 with this switch as implemented in previous versions of GNAT.
28505 The pragma @cite{Suppress (Overflow_Check)} disables overflow
28506 checking, but it has no effect on the method used for computing
28507 intermediate results.
28509 The pragma @cite{Unsuppress (Overflow_Check)} enables overflow
28510 checking, but it has no effect on the method used for computing
28511 intermediate results.
28513 @node Implementation Notes,,Default Settings,Overflow Check Handling in GNAT
28514 @anchor{gnat_ugn/gnat_and_program_execution implementation-notes}@anchor{24f}@anchor{gnat_ugn/gnat_and_program_execution id59}@anchor{250}
28515 @subsection Implementation Notes
28518 In practice on typical 64-bit machines, the @cite{MINIMIZED} mode is
28519 reasonably efficient, and can be generally used. It also helps
28520 to ensure compatibility with code imported from some other
28521 compiler to GNAT.
28523 Setting all intermediate overflows checking (@cite{CHECKED} mode)
28524 makes sense if you want to
28525 make sure that your code is compatible with any other possible
28526 Ada implementation. This may be useful in ensuring portability
28527 for code that is to be exported to some other compiler than GNAT.
28529 The Ada standard allows the reassociation of expressions at
28530 the same precedence level if no parentheses are present. For
28531 example, @cite{A+B+C} parses as though it were @cite{(A+B)+C}, but
28532 the compiler can reintepret this as @cite{A+(B+C)}, possibly
28533 introducing or eliminating an overflow exception. The GNAT
28534 compiler never takes advantage of this freedom, and the
28535 expression @cite{A+B+C} will be evaluated as @cite{(A+B)+C}.
28536 If you need the other order, you can write the parentheses
28537 explicitly @cite{A+(B+C)} and GNAT will respect this order.
28539 The use of @cite{ELIMINATED} mode will cause the compiler to
28540 automatically include an appropriate arbitrary precision
28541 integer arithmetic package. The compiler will make calls
28542 to this package, though only in cases where it cannot be
28543 sure that @cite{Long_Long_Integer} is sufficient to guard against
28544 intermediate overflows. This package does not use dynamic
28545 alllocation, but it does use the secondary stack, so an
28546 appropriate secondary stack package must be present (this
28547 is always true for standard full Ada, but may require
28548 specific steps for restricted run times such as ZFP).
28550 Although @cite{ELIMINATED} mode causes expressions to use arbitrary
28551 precision arithmetic, avoiding overflow, the final result
28552 must be in an appropriate range. This is true even if the
28553 final result is of type @cite{[Long_[Long_]]Integer'Base}, which
28554 still has the same bounds as its associated constrained
28555 type at run-time.
28557 Currently, the @cite{ELIMINATED} mode is only available on target
28558 platforms for which @cite{Long_Long_Integer} is 64-bits (nearly all GNAT
28559 platforms).
28561 @node Performing Dimensionality Analysis in GNAT,Stack Related Facilities,Overflow Check Handling in GNAT,GNAT and Program Execution
28562 @anchor{gnat_ugn/gnat_and_program_execution performing-dimensionality-analysis-in-gnat}@anchor{2a}@anchor{gnat_ugn/gnat_and_program_execution id60}@anchor{1f4}
28563 @section Performing Dimensionality Analysis in GNAT
28566 @geindex Dimensionality analysis
28568 The GNAT compiler supports dimensionality checking. The user can
28569 specify physical units for objects, and the compiler will verify that uses
28570 of these objects are compatible with their dimensions, in a fashion that is
28571 familiar to engineering practice. The dimensions of algebraic expressions
28572 (including powers with static exponents) are computed from their constituents.
28574 @geindex Dimension_System aspect
28576 @geindex Dimension aspect
28578 This feature depends on Ada 2012 aspect specifications, and is available from
28579 version 7.0.1 of GNAT onwards.
28580 The GNAT-specific aspect @cite{Dimension_System}
28581 allows you to define a system of units; the aspect @cite{Dimension}
28582 then allows the user to declare dimensioned quantities within a given system.
28583 (These aspects are described in the @emph{Implementation Defined Aspects}
28584 chapter of the @emph{GNAT Reference Manual}).
28586 The major advantage of this model is that it does not require the declaration of
28587 multiple operators for all possible combinations of types: it is only necessary
28588 to use the proper subtypes in object declarations.
28590 @geindex System.Dim.Mks package (GNAT library)
28592 @geindex MKS_Type type
28594 The simplest way to impose dimensionality checking on a computation is to make
28595 use of the package @cite{System.Dim.Mks},
28596 which is part of the GNAT library. This
28597 package defines a floating-point type @cite{MKS_Type},
28598 for which a sequence of
28599 dimension names are specified, together with their conventional abbreviations.
28600 The following should be read together with the full specification of the
28601 package, in file @code{s-dimmks.ads}.
28603 @quotation
28605 @geindex s-dimmks.ads file
28607 @example
28608 type Mks_Type is new Long_Long_Float
28609   with
28610    Dimension_System => (
28611      (Unit_Name => Meter,    Unit_Symbol => 'm',   Dim_Symbol => 'L'),
28612      (Unit_Name => Kilogram, Unit_Symbol => "kg",  Dim_Symbol => 'M'),
28613      (Unit_Name => Second,   Unit_Symbol => 's',   Dim_Symbol => 'T'),
28614      (Unit_Name => Ampere,   Unit_Symbol => 'A',   Dim_Symbol => 'I'),
28615      (Unit_Name => Kelvin,   Unit_Symbol => 'K',   Dim_Symbol => "Theta"),
28616      (Unit_Name => Mole,     Unit_Symbol => "mol", Dim_Symbol => 'N'),
28617      (Unit_Name => Candela,  Unit_Symbol => "cd",  Dim_Symbol => 'J'));
28618 @end example
28619 @end quotation
28621 The package then defines a series of subtypes that correspond to these
28622 conventional units. For example:
28624 @quotation
28626 @example
28627 subtype Length is Mks_Type
28628   with
28629    Dimension => (Symbol => 'm', Meter  => 1, others => 0);
28630 @end example
28631 @end quotation
28633 and similarly for @cite{Mass}, @cite{Time}, @cite{Electric_Current},
28634 @cite{Thermodynamic_Temperature}, @cite{Amount_Of_Substance}, and
28635 @cite{Luminous_Intensity} (the standard set of units of the SI system).
28637 The package also defines conventional names for values of each unit, for
28638 example:
28640 @quotation
28642 @c code-block":: ada
28643 @c 
28644 @c m   : constant Length           := 1.0;
28645 @c kg  : constant Mass             := 1.0;
28646 @c s   : constant Time             := 1.0;
28647 @c A   : constant Electric_Current := 1.0;
28648 @end quotation
28650 as well as useful multiples of these units:
28652 @quotation
28654 @example
28655  cm  : constant Length := 1.0E-02;
28656  g   : constant Mass   := 1.0E-03;
28657  min : constant Time   := 60.0;
28658  day : constant Time   := 60.0 * 24.0 * min;
28660 @end example
28661 @end quotation
28663 Using this package, you can then define a derived unit by
28664 providing the aspect that
28665 specifies its dimensions within the MKS system, as well as the string to
28666 be used for output of a value of that unit:
28668 @quotation
28670 @example
28671 subtype Acceleration is Mks_Type
28672   with Dimension => ("m/sec^2",
28673                      Meter => 1,
28674                      Second => -2,
28675                      others => 0);
28676 @end example
28677 @end quotation
28679 Here is a complete example of use:
28681 @quotation
28683 @example
28684 with System.Dim.MKS; use System.Dim.Mks;
28685 with System.Dim.Mks_IO; use System.Dim.Mks_IO;
28686 with Text_IO; use Text_IO;
28687 procedure Free_Fall is
28688   subtype Acceleration is Mks_Type
28689     with Dimension => ("m/sec^2", 1, 0, -2, others => 0);
28690   G : constant acceleration := 9.81 * m / (s ** 2);
28691   T : Time := 10.0*s;
28692   Distance : Length;
28694 begin
28695   Put ("Gravitational constant: ");
28696   Put (G, Aft => 2, Exp => 0); Put_Line ("");
28697   Distance := 0.5 * G * T ** 2;
28698   Put ("distance travelled in 10 seconds of free fall ");
28699   Put (Distance, Aft => 2, Exp => 0);
28700   Put_Line ("");
28701 end Free_Fall;
28702 @end example
28703 @end quotation
28705 Execution of this program yields:
28707 @quotation
28709 @example
28710 Gravitational constant:  9.81 m/sec^2
28711 distance travelled in 10 seconds of free fall 490.50 m
28712 @end example
28713 @end quotation
28715 However, incorrect assignments such as:
28717 @quotation
28719 @example
28720 Distance := 5.0;
28721 Distance := 5.0 * kg:
28722 @end example
28723 @end quotation
28725 are rejected with the following diagnoses:
28727 @quotation
28729 @example
28730 Distance := 5.0;
28731    >>> dimensions mismatch in assignment
28732    >>> left-hand side has dimension [L]
28733    >>> right-hand side is dimensionless
28735 Distance := 5.0 * kg:
28736    >>> dimensions mismatch in assignment
28737    >>> left-hand side has dimension [L]
28738    >>> right-hand side has dimension [M]
28739 @end example
28740 @end quotation
28742 The dimensions of an expression are properly displayed, even if there is
28743 no explicit subtype for it. If we add to the program:
28745 @quotation
28747 @example
28748 Put ("Final velocity: ");
28749 Put (G * T, Aft =>2, Exp =>0);
28750 Put_Line ("");
28751 @end example
28752 @end quotation
28754 then the output includes:
28756 @quotation
28758 @example
28759 Final velocity: 98.10 m.s**(-1)
28760 @end example
28761 @end quotation
28763 @node Stack Related Facilities,Memory Management Issues,Performing Dimensionality Analysis in GNAT,GNAT and Program Execution
28764 @anchor{gnat_ugn/gnat_and_program_execution id61}@anchor{1f5}@anchor{gnat_ugn/gnat_and_program_execution stack-related-facilities}@anchor{2b}
28765 @section Stack Related Facilities
28768 This section describes some useful tools associated with stack
28769 checking and analysis. In
28770 particular, it deals with dynamic and static stack usage measurements.
28772 @menu
28773 * Stack Overflow Checking:: 
28774 * Static Stack Usage Analysis:: 
28775 * Dynamic Stack Usage Analysis:: 
28777 @end menu
28779 @node Stack Overflow Checking,Static Stack Usage Analysis,,Stack Related Facilities
28780 @anchor{gnat_ugn/gnat_and_program_execution id62}@anchor{251}@anchor{gnat_ugn/gnat_and_program_execution stack-overflow-checking}@anchor{f9}
28781 @subsection Stack Overflow Checking
28784 @geindex Stack Overflow Checking
28786 @geindex -fstack-check (gcc)
28788 For most operating systems, @emph{gcc} does not perform stack overflow
28789 checking by default. This means that if the main environment task or
28790 some other task exceeds the available stack space, then unpredictable
28791 behavior will occur. Most native systems offer some level of protection by
28792 adding a guard page at the end of each task stack. This mechanism is usually
28793 not enough for dealing properly with stack overflow situations because
28794 a large local variable could "jump" above the guard page.
28795 Furthermore, when the
28796 guard page is hit, there may not be any space left on the stack for executing
28797 the exception propagation code. Enabling stack checking avoids
28798 such situations.
28800 To activate stack checking, compile all units with the gcc option
28801 @cite{-fstack-check}. For example:
28803 @quotation
28805 @example
28806 $ gcc -c -fstack-check package1.adb
28807 @end example
28808 @end quotation
28810 Units compiled with this option will generate extra instructions to check
28811 that any use of the stack (for procedure calls or for declaring local
28812 variables in declare blocks) does not exceed the available stack space.
28813 If the space is exceeded, then a @cite{Storage_Error} exception is raised.
28815 For declared tasks, the stack size is controlled by the size
28816 given in an applicable @cite{Storage_Size} pragma or by the value specified
28817 at bind time with @code{-d} (@ref{123,,Switches for gnatbind}) or is set to
28818 the default size as defined in the GNAT runtime otherwise.
28820 @geindex GNAT_STACK_LIMIT
28822 For the environment task, the stack size depends on
28823 system defaults and is unknown to the compiler. Stack checking
28824 may still work correctly if a fixed
28825 size stack is allocated, but this cannot be guaranteed.
28826 To ensure that a clean exception is signalled for stack
28827 overflow, set the environment variable
28828 @geindex GNAT_STACK_LIMIT
28829 @geindex environment variable; GNAT_STACK_LIMIT
28830 @code{GNAT_STACK_LIMIT} to indicate the maximum
28831 stack area that can be used, as in:
28833 @quotation
28835 @example
28836 $ SET GNAT_STACK_LIMIT 1600
28837 @end example
28838 @end quotation
28840 The limit is given in kilobytes, so the above declaration would
28841 set the stack limit of the environment task to 1.6 megabytes.
28842 Note that the only purpose of this usage is to limit the amount
28843 of stack used by the environment task. If it is necessary to
28844 increase the amount of stack for the environment task, then this
28845 is an operating systems issue, and must be addressed with the
28846 appropriate operating systems commands.
28848 @node Static Stack Usage Analysis,Dynamic Stack Usage Analysis,Stack Overflow Checking,Stack Related Facilities
28849 @anchor{gnat_ugn/gnat_and_program_execution static-stack-usage-analysis}@anchor{fa}@anchor{gnat_ugn/gnat_and_program_execution id63}@anchor{252}
28850 @subsection Static Stack Usage Analysis
28853 @geindex Static Stack Usage Analysis
28855 @geindex -fstack-usage
28857 A unit compiled with @code{-fstack-usage} will generate an extra file
28858 that specifies
28859 the maximum amount of stack used, on a per-function basis.
28860 The file has the same
28861 basename as the target object file with a @code{.su} extension.
28862 Each line of this file is made up of three fields:
28865 @itemize *
28867 @item 
28868 The name of the function.
28870 @item 
28871 A number of bytes.
28873 @item 
28874 One or more qualifiers: @cite{static}, @cite{dynamic}, @cite{bounded}.
28875 @end itemize
28877 The second field corresponds to the size of the known part of the function
28878 frame.
28880 The qualifier @cite{static} means that the function frame size
28881 is purely static.
28882 It usually means that all local variables have a static size.
28883 In this case, the second field is a reliable measure of the function stack
28884 utilization.
28886 The qualifier @cite{dynamic} means that the function frame size is not static.
28887 It happens mainly when some local variables have a dynamic size. When this
28888 qualifier appears alone, the second field is not a reliable measure
28889 of the function stack analysis. When it is qualified with  @cite{bounded}, it
28890 means that the second field is a reliable maximum of the function stack
28891 utilization.
28893 A unit compiled with @code{-Wstack-usage} will issue a warning for each
28894 subprogram whose stack usage might be larger than the specified amount of
28895 bytes.  The wording is in keeping with the qualifier documented above.
28897 @node Dynamic Stack Usage Analysis,,Static Stack Usage Analysis,Stack Related Facilities
28898 @anchor{gnat_ugn/gnat_and_program_execution id64}@anchor{253}@anchor{gnat_ugn/gnat_and_program_execution dynamic-stack-usage-analysis}@anchor{125}
28899 @subsection Dynamic Stack Usage Analysis
28902 It is possible to measure the maximum amount of stack used by a task, by
28903 adding a switch to @emph{gnatbind}, as:
28905 @quotation
28907 @example
28908 $ gnatbind -u0 file
28909 @end example
28910 @end quotation
28912 With this option, at each task termination, its stack usage is  output on
28913 @code{stderr}.
28914 It is not always convenient to output the stack usage when the program
28915 is still running. Hence, it is possible to delay this output until program
28916 termination. for a given number of tasks specified as the argument of the
28917 @code{-u} option. For instance:
28919 @quotation
28921 @example
28922 $ gnatbind -u100 file
28923 @end example
28924 @end quotation
28926 will buffer the stack usage information of the first 100 tasks to terminate and
28927 output this info at program termination. Results are displayed in four
28928 columns:
28930 @quotation
28932 @example
28933 Index | Task Name | Stack Size | Stack Usage
28934 @end example
28935 @end quotation
28937 where:
28940 @itemize *
28942 @item 
28943 @emph{Index} is a number associated with each task.
28945 @item 
28946 @emph{Task Name} is the name of the task analyzed.
28948 @item 
28949 @emph{Stack Size} is the maximum size for the stack.
28951 @item 
28952 @emph{Stack Usage} is the measure done by the stack analyzer.
28953 In order to prevent overflow, the stack
28954 is not entirely analyzed, and it's not possible to know exactly how
28955 much has actually been used.
28956 @end itemize
28958 The environment task stack, e.g., the stack that contains the main unit, is
28959 only processed when the environment variable GNAT_STACK_LIMIT is set.
28961 The package @cite{GNAT.Task_Stack_Usage} provides facilities to get
28962 stack usage reports at run-time. See its body for the details.
28964 @node Memory Management Issues,,Stack Related Facilities,GNAT and Program Execution
28965 @anchor{gnat_ugn/gnat_and_program_execution id65}@anchor{1f6}@anchor{gnat_ugn/gnat_and_program_execution memory-management-issues}@anchor{2c}
28966 @section Memory Management Issues
28969 This section describes some useful memory pools provided in the GNAT library
28970 and in particular the GNAT Debug Pool facility, which can be used to detect
28971 incorrect uses of access values (including 'dangling references').
28974 @menu
28975 * Some Useful Memory Pools:: 
28976 * The GNAT Debug Pool Facility:: 
28978 @end menu
28980 @node Some Useful Memory Pools,The GNAT Debug Pool Facility,,Memory Management Issues
28981 @anchor{gnat_ugn/gnat_and_program_execution id66}@anchor{254}@anchor{gnat_ugn/gnat_and_program_execution some-useful-memory-pools}@anchor{255}
28982 @subsection Some Useful Memory Pools
28985 @geindex Memory Pool
28987 @geindex storage
28988 @geindex pool
28990 The @cite{System.Pool_Global} package offers the Unbounded_No_Reclaim_Pool
28991 storage pool. Allocations use the standard system call @cite{malloc} while
28992 deallocations use the standard system call @cite{free}. No reclamation is
28993 performed when the pool goes out of scope. For performance reasons, the
28994 standard default Ada allocators/deallocators do not use any explicit storage
28995 pools but if they did, they could use this storage pool without any change in
28996 behavior. That is why this storage pool is used  when the user
28997 manages to make the default implicit allocator explicit as in this example:
28999 @quotation
29001 @example
29002 type T1 is access Something;
29003  -- no Storage pool is defined for T2
29005 type T2 is access Something_Else;
29006 for T2'Storage_Pool use T1'Storage_Pool;
29007 -- the above is equivalent to
29008 for T2'Storage_Pool use System.Pool_Global.Global_Pool_Object;
29009 @end example
29010 @end quotation
29012 The @cite{System.Pool_Local} package offers the Unbounded_Reclaim_Pool storage
29013 pool. The allocation strategy is similar to @cite{Pool_Local}'s
29014 except that the all
29015 storage allocated with this pool is reclaimed when the pool object goes out of
29016 scope. This pool provides a explicit mechanism similar to the implicit one
29017 provided by several Ada 83 compilers for allocations performed through a local
29018 access type and whose purpose was to reclaim memory when exiting the
29019 scope of a given local access. As an example, the following program does not
29020 leak memory even though it does not perform explicit deallocation:
29022 @quotation
29024 @example
29025 with System.Pool_Local;
29026 procedure Pooloc1 is
29027    procedure Internal is
29028       type A is access Integer;
29029       X : System.Pool_Local.Unbounded_Reclaim_Pool;
29030       for A'Storage_Pool use X;
29031       v : A;
29032    begin
29033       for I in  1 .. 50 loop
29034          v := new Integer;
29035       end loop;
29036    end Internal;
29037 begin
29038    for I in  1 .. 100 loop
29039       Internal;
29040    end loop;
29041 end Pooloc1;
29042 @end example
29043 @end quotation
29045 The @cite{System.Pool_Size} package implements the Stack_Bounded_Pool used when
29046 @cite{Storage_Size} is specified for an access type.
29047 The whole storage for the pool is
29048 allocated at once, usually on the stack at the point where the access type is
29049 elaborated. It is automatically reclaimed when exiting the scope where the
29050 access type is defined. This package is not intended to be used directly by the
29051 user and it is implicitly used for each such declaration:
29053 @quotation
29055 @example
29056 type T1 is access Something;
29057 for T1'Storage_Size use 10_000;
29058 @end example
29059 @end quotation
29061 @node The GNAT Debug Pool Facility,,Some Useful Memory Pools,Memory Management Issues
29062 @anchor{gnat_ugn/gnat_and_program_execution id67}@anchor{256}@anchor{gnat_ugn/gnat_and_program_execution the-gnat-debug-pool-facility}@anchor{257}
29063 @subsection The GNAT Debug Pool Facility
29066 @geindex Debug Pool
29068 @geindex storage
29069 @geindex pool
29070 @geindex memory corruption
29072 The use of unchecked deallocation and unchecked conversion can easily
29073 lead to incorrect memory references. The problems generated by such
29074 references are usually difficult to tackle because the symptoms can be
29075 very remote from the origin of the problem. In such cases, it is
29076 very helpful to detect the problem as early as possible. This is the
29077 purpose of the Storage Pool provided by @cite{GNAT.Debug_Pools}.
29079 In order to use the GNAT specific debugging pool, the user must
29080 associate a debug pool object with each of the access types that may be
29081 related to suspected memory problems. See Ada Reference Manual 13.11.
29083 @quotation
29085 @example
29086 type Ptr is access Some_Type;
29087 Pool : GNAT.Debug_Pools.Debug_Pool;
29088 for Ptr'Storage_Pool use Pool;
29089 @end example
29090 @end quotation
29092 @cite{GNAT.Debug_Pools} is derived from a GNAT-specific kind of
29093 pool: the @cite{Checked_Pool}. Such pools, like standard Ada storage pools,
29094 allow the user to redefine allocation and deallocation strategies. They
29095 also provide a checkpoint for each dereference, through the use of
29096 the primitive operation @cite{Dereference} which is implicitly called at
29097 each dereference of an access value.
29099 Once an access type has been associated with a debug pool, operations on
29100 values of the type may raise four distinct exceptions,
29101 which correspond to four potential kinds of memory corruption:
29104 @itemize *
29106 @item 
29107 @cite{GNAT.Debug_Pools.Accessing_Not_Allocated_Storage}
29109 @item 
29110 @cite{GNAT.Debug_Pools.Accessing_Deallocated_Storage}
29112 @item 
29113 @cite{GNAT.Debug_Pools.Freeing_Not_Allocated_Storage}
29115 @item 
29116 @cite{GNAT.Debug_Pools.Freeing_Deallocated_Storage}
29117 @end itemize
29119 For types associated with a Debug_Pool, dynamic allocation is performed using
29120 the standard GNAT allocation routine. References to all allocated chunks of
29121 memory are kept in an internal dictionary. Several deallocation strategies are
29122 provided, whereupon the user can choose to release the memory to the system,
29123 keep it allocated for further invalid access checks, or fill it with an easily
29124 recognizable pattern for debug sessions. The memory pattern is the old IBM
29125 hexadecimal convention: @cite{16#DEADBEEF#}.
29127 See the documentation in the file g-debpoo.ads for more information on the
29128 various strategies.
29130 Upon each dereference, a check is made that the access value denotes a
29131 properly allocated memory location. Here is a complete example of use of
29132 @cite{Debug_Pools}, that includes typical instances of  memory corruption:
29134 @quotation
29136 @example
29137 with Gnat.Io; use Gnat.Io;
29138 with Unchecked_Deallocation;
29139 with Unchecked_Conversion;
29140 with GNAT.Debug_Pools;
29141 with System.Storage_Elements;
29142 with Ada.Exceptions; use Ada.Exceptions;
29143 procedure Debug_Pool_Test is
29145    type T is access Integer;
29146    type U is access all T;
29148    P : GNAT.Debug_Pools.Debug_Pool;
29149    for T'Storage_Pool use P;
29151    procedure Free is new Unchecked_Deallocation (Integer, T);
29152    function UC is new Unchecked_Conversion (U, T);
29153    A, B : aliased T;
29155    procedure Info is new GNAT.Debug_Pools.Print_Info(Put_Line);
29157 begin
29158    Info (P);
29159    A := new Integer;
29160    B := new Integer;
29161    B := A;
29162    Info (P);
29163    Free (A);
29164    begin
29165       Put_Line (Integer'Image(B.all));
29166    exception
29167       when E : others => Put_Line ("raised: " & Exception_Name (E));
29168    end;
29169    begin
29170       Free (B);
29171    exception
29172       when E : others => Put_Line ("raised: " & Exception_Name (E));
29173    end;
29174    B := UC(A'Access);
29175    begin
29176       Put_Line (Integer'Image(B.all));
29177    exception
29178       when E : others => Put_Line ("raised: " & Exception_Name (E));
29179    end;
29180    begin
29181       Free (B);
29182    exception
29183       when E : others => Put_Line ("raised: " & Exception_Name (E));
29184    end;
29185    Info (P);
29186 end Debug_Pool_Test;
29187 @end example
29188 @end quotation
29190 The debug pool mechanism provides the following precise diagnostics on the
29191 execution of this erroneous program:
29193 @quotation
29195 @example
29196 Debug Pool info:
29197   Total allocated bytes :  0
29198   Total deallocated bytes :  0
29199   Current Water Mark:  0
29200   High Water Mark:  0
29202 Debug Pool info:
29203   Total allocated bytes :  8
29204   Total deallocated bytes :  0
29205   Current Water Mark:  8
29206   High Water Mark:  8
29208 raised: GNAT.DEBUG_POOLS.ACCESSING_DEALLOCATED_STORAGE
29209 raised: GNAT.DEBUG_POOLS.FREEING_DEALLOCATED_STORAGE
29210 raised: GNAT.DEBUG_POOLS.ACCESSING_NOT_ALLOCATED_STORAGE
29211 raised: GNAT.DEBUG_POOLS.FREEING_NOT_ALLOCATED_STORAGE
29212 Debug Pool info:
29213   Total allocated bytes :  8
29214   Total deallocated bytes :  4
29215   Current Water Mark:  4
29216   High Water Mark:  8
29217 @end example
29218 @end quotation
29221 @c -- Non-breaking space in running text
29222 @c -- E.g. Ada |nbsp| 95
29224 @node Platform-Specific Information,Example of Binder Output File,GNAT and Program Execution,Top
29225 @anchor{gnat_ugn/platform_specific_information platform-specific-information}@anchor{f}@anchor{gnat_ugn/platform_specific_information doc}@anchor{258}@anchor{gnat_ugn/platform_specific_information id1}@anchor{259}
29226 @chapter Platform-Specific Information
29229 This appendix contains information relating to the implementation
29230 of run-time libraries on various platforms and also covers
29231 topics related to the GNAT implementation on Windows and Mac OS.
29233 @menu
29234 * Run-Time Libraries:: 
29235 * Specifying a Run-Time Library:: 
29236 * Microsoft Windows Topics:: 
29237 * Mac OS Topics:: 
29239 @end menu
29241 @node Run-Time Libraries,Specifying a Run-Time Library,,Platform-Specific Information
29242 @anchor{gnat_ugn/platform_specific_information id2}@anchor{25a}@anchor{gnat_ugn/platform_specific_information run-time-libraries}@anchor{2d}
29243 @section Run-Time Libraries
29246 @geindex Tasking and threads libraries
29248 @geindex Threads libraries and tasking
29250 @geindex Run-time libraries (platform-specific information)
29252 The GNAT run-time implementation may vary with respect to both the
29253 underlying threads library and the exception handling scheme.
29254 For threads support, one or more of the following are supplied:
29257 @itemize *
29259 @item 
29260 @strong{native threads library}, a binding to the thread package from
29261 the underlying operating system
29263 @item 
29264 @strong{pthreads library} (Sparc Solaris only), a binding to the Solaris
29265 POSIX thread package
29266 @end itemize
29268 For exception handling, either or both of two models are supplied:
29270 @quotation
29272 @geindex Zero-Cost Exceptions
29274 @geindex ZCX (Zero-Cost Exceptions)
29275 @end quotation
29278 @itemize *
29280 @item 
29281 @strong{Zero-Cost Exceptions} ("ZCX"),
29282 which uses binder-generated tables that
29283 are interrogated at run time to locate a handler.
29285 @geindex setjmp/longjmp Exception Model
29287 @geindex SJLJ (setjmp/longjmp Exception Model)
29289 @item 
29290 @strong{setjmp / longjmp} ('SJLJ'),
29291 which uses dynamically-set data to establish
29292 the set of handlers
29293 @end itemize
29295 Most programs should experience a substantial speed improvement by
29296 being compiled with a ZCX run-time.
29297 This is especially true for
29298 tasking applications or applications with many exception handlers.@}
29300 This section summarizes which combinations of threads and exception support
29301 are supplied on various GNAT platforms.
29302 It then shows how to select a particular library either
29303 permanently or temporarily,
29304 explains the properties of (and tradeoffs among) the various threads
29305 libraries, and provides some additional
29306 information about several specific platforms.
29308 @menu
29309 * Summary of Run-Time Configurations:: 
29311 @end menu
29313 @node Summary of Run-Time Configurations,,,Run-Time Libraries
29314 @anchor{gnat_ugn/platform_specific_information summary-of-run-time-configurations}@anchor{25b}@anchor{gnat_ugn/platform_specific_information id3}@anchor{25c}
29315 @subsection Summary of Run-Time Configurations
29319 @multitable {xxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxx} 
29320 @headitem
29322 Platform
29324 @tab
29326 Run-Time
29328 @tab
29330 Tasking
29332 @tab
29334 Exceptions
29336 @item
29338 ppc-aix
29340 @tab
29342 rts-native
29343 (default)
29345 @tab
29347 native AIX threads
29349 @tab
29353 @item
29355 rts-sjlj
29357 @tab
29359 native AIX threads
29361 @tab
29363 SJLJ
29365 @item
29367 sparc-solaris
29369 @tab
29371 rts-native
29372 (default)
29374 @tab
29376 native Solaris
29377 threads library
29379 @tab
29383 @item
29385 rts-pthread
29387 @tab
29389 pthread library
29391 @tab
29395 @item
29397 rts-sjlj
29399 @tab
29401 native Solaris
29402 threads library
29404 @tab
29406 SJLJ
29408 @item
29410 sparc64-solaris
29412 @tab
29414 rts-native
29415 (default)
29417 @tab
29419 native Solaris
29420 threads library
29422 @tab
29426 @item
29428 x86-linux
29430 @tab
29432 rts-native
29433 (default)
29435 @tab
29437 pthread library
29439 @tab
29443 @item
29445 rts-sjlj
29447 @tab
29449 pthread library
29451 @tab
29453 SJLJ
29455 @item
29457 x86-lynx
29459 @tab
29461 rts-native
29462 (default)
29464 @tab
29466 native LynxOS threads
29468 @tab
29470 SJLJ
29472 @item
29474 x86-solaris
29476 @tab
29478 rts-native
29479 (default)
29481 @tab
29483 native Solaris
29484 threads library
29486 @tab
29490 @item
29492 rts-sjlj
29494 @tab
29496 native Solaris
29497 threads library
29499 @tab
29501 SJLJ
29503 @item
29505 x86-windows
29507 @tab
29509 rts-native
29510 (default)
29512 @tab
29514 native Win32 threads
29516 @tab
29520 @item
29522 rts-sjlj
29524 @tab
29526 native Win32 threads
29528 @tab
29530 SJLJ
29532 @item
29534 x86_64-linux
29536 @tab
29538 rts-native
29539 (default)
29541 @tab
29543 pthread library
29545 @tab
29549 @item
29551 rts-sjlj
29553 @tab
29555 pthread library
29557 @tab
29559 SJLJ
29561 @end multitable
29564 @node Specifying a Run-Time Library,Microsoft Windows Topics,Run-Time Libraries,Platform-Specific Information
29565 @anchor{gnat_ugn/platform_specific_information specifying-a-run-time-library}@anchor{25d}@anchor{gnat_ugn/platform_specific_information id4}@anchor{25e}
29566 @section Specifying a Run-Time Library
29569 The @code{adainclude} subdirectory containing the sources of the GNAT
29570 run-time library, and the @code{adalib} subdirectory containing the
29571 @code{ALI} files and the static and/or shared GNAT library, are located
29572 in the gcc target-dependent area:
29574 @quotation
29576 @example
29577 target=$prefix/lib/gcc/gcc-*dumpmachine*/gcc-*dumpversion*/
29578 @end example
29579 @end quotation
29581 As indicated above, on some platforms several run-time libraries are supplied.
29582 These libraries are installed in the target dependent area and
29583 contain a complete source and binary subdirectory. The detailed description
29584 below explains the differences between the different libraries in terms of
29585 their thread support.
29587 The default run-time library (when GNAT is installed) is @emph{rts-native}.
29588 This default run time is selected by the means of soft links.
29589 For example on x86-linux:
29591 @example
29593 --  $(target-dir)
29594 --      |
29595 --      +--- adainclude----------+
29596 --      |                        |
29597 --      +--- adalib-----------+  |
29598 --      |                     |  |
29599 --      +--- rts-native       |  |
29600 --      |    |                |  |
29601 --      |    +--- adainclude <---+
29602 --      |    |                |
29603 --      |    +--- adalib <----+
29604 --      |
29605 --      +--- rts-sjlj
29606 --           |
29607 --           +--- adainclude
29608 --           |
29609 --           +--- adalib
29610 @end example
29612 If the @emph{rts-sjlj} library is to be selected on a permanent basis,
29613 these soft links can be modified with the following commands:
29615 @quotation
29617 @example
29618 $ cd $target
29619 $ rm -f adainclude adalib
29620 $ ln -s rts-sjlj/adainclude adainclude
29621 $ ln -s rts-sjlj/adalib adalib
29622 @end example
29623 @end quotation
29625 Alternatively, you can specify @code{rts-sjlj/adainclude} in the file
29626 @code{$target/ada_source_path} and @code{rts-sjlj/adalib} in
29627 @code{$target/ada_object_path}.
29629 @geindex --RTS option
29631 Selecting another run-time library temporarily can be
29632 achieved by using the @emph{--RTS} switch, e.g., @emph{--RTS=sjlj}
29633 @anchor{gnat_ugn/platform_specific_information choosing-the-scheduling-policy}@anchor{25f}
29634 @geindex SCHED_FIFO scheduling policy
29636 @geindex SCHED_RR scheduling policy
29638 @geindex SCHED_OTHER scheduling policy
29640 @menu
29641 * Choosing the Scheduling Policy:: 
29642 * Solaris-Specific Considerations:: 
29643 * Solaris Threads Issues:: 
29644 * AIX-Specific Considerations:: 
29646 @end menu
29648 @node Choosing the Scheduling Policy,Solaris-Specific Considerations,,Specifying a Run-Time Library
29649 @anchor{gnat_ugn/platform_specific_information id5}@anchor{260}
29650 @subsection Choosing the Scheduling Policy
29653 When using a POSIX threads implementation, you have a choice of several
29654 scheduling policies: @cite{SCHED_FIFO}, @cite{SCHED_RR} and @cite{SCHED_OTHER}.
29656 Typically, the default is @cite{SCHED_OTHER}, while using @cite{SCHED_FIFO}
29657 or @cite{SCHED_RR} requires special (e.g., root) privileges.
29659 @geindex pragma Time_Slice
29661 @geindex -T0 option
29663 @geindex pragma Task_Dispatching_Policy
29665 By default, GNAT uses the @cite{SCHED_OTHER} policy. To specify
29666 @cite{SCHED_FIFO},
29667 you can use one of the following:
29670 @itemize *
29672 @item 
29673 @cite{pragma Time_Slice (0.0)}
29675 @item 
29676 the corresponding binder option @emph{-T0}
29678 @item 
29679 @cite{pragma Task_Dispatching_Policy (FIFO_Within_Priorities)}
29680 @end itemize
29682 To specify @cite{SCHED_RR},
29683 you should use @cite{pragma Time_Slice} with a
29684 value greater than 0.0, or else use the corresponding @emph{-T}
29685 binder option.
29687 @geindex Solaris Sparc threads libraries
29689 @node Solaris-Specific Considerations,Solaris Threads Issues,Choosing the Scheduling Policy,Specifying a Run-Time Library
29690 @anchor{gnat_ugn/platform_specific_information id6}@anchor{261}@anchor{gnat_ugn/platform_specific_information solaris-specific-considerations}@anchor{262}
29691 @subsection Solaris-Specific Considerations
29694 This section addresses some topics related to the various threads libraries
29695 on Sparc Solaris.
29697 @geindex rts-pthread threads library
29699 @node Solaris Threads Issues,AIX-Specific Considerations,Solaris-Specific Considerations,Specifying a Run-Time Library
29700 @anchor{gnat_ugn/platform_specific_information id7}@anchor{263}@anchor{gnat_ugn/platform_specific_information solaris-threads-issues}@anchor{264}
29701 @subsection Solaris Threads Issues
29704 GNAT under Solaris/Sparc 32 bits comes with an alternate tasking run-time
29705 library based on POSIX threads --- @emph{rts-pthread}.
29707 @geindex PTHREAD_PRIO_INHERIT policy (under rts-pthread)
29709 @geindex PTHREAD_PRIO_PROTECT policy (under rts-pthread)
29711 @geindex pragma Locking_Policy (under rts-pthread)
29713 @geindex Inheritance_Locking (under rts-pthread)
29715 @geindex Ceiling_Locking (under rts-pthread)
29717 This run-time library has the advantage of being mostly shared across all
29718 POSIX-compliant thread implementations, and it also provides under
29719 Solaris 8 the @cite{PTHREAD_PRIO_INHERIT}
29720 and @cite{PTHREAD_PRIO_PROTECT}
29721 semantics that can be selected using the predefined pragma
29722 @cite{Locking_Policy}
29723 with respectively
29724 @cite{Inheritance_Locking} and @cite{Ceiling_Locking} as the policy.
29726 As explained above, the native run-time library is based on the Solaris thread
29727 library (@cite{libthread}) and is the default library.
29729 @geindex GNAT_PROCESSOR environment variable (on Sparc Solaris)
29731 When the Solaris threads library is used (this is the default), programs
29732 compiled with GNAT can automatically take advantage of
29733 and can thus execute on multiple processors.
29734 The user can alternatively specify a processor on which the program should run
29735 to emulate a single-processor system. The multiprocessor / uniprocessor choice
29736 is made by
29737 setting the environment variable 
29738 @geindex GNAT_PROCESSOR
29739 @geindex environment variable; GNAT_PROCESSOR
29740 @code{GNAT_PROCESSOR}
29741 to one of the following:
29743 @quotation
29746 @multitable {xxxxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} 
29747 @headitem
29749 @code{GNAT_PROCESSOR} Value
29751 @tab
29753 Effect
29755 @item
29757 @emph{-2}
29759 @tab
29761 Use the default configuration (run the program on all
29762 available processors) - this is the same as having @cite{GNAT_PROCESSOR}
29763 unset
29765 @item
29767 @emph{-1}
29769 @tab
29771 Let the run-time implementation choose one processor and run the
29772 program on that processor
29774 @item
29776 @emph{0 .. Last_Proc}
29778 @tab
29780 Run the program on the specified processor.
29781 @cite{Last_Proc} is equal to @cite{_SC_NPROCESSORS_CONF - 1}
29782 (where @cite{_SC_NPROCESSORS_CONF} is a system variable).
29784 @end multitable
29786 @end quotation
29788 @node AIX-Specific Considerations,,Solaris Threads Issues,Specifying a Run-Time Library
29789 @anchor{gnat_ugn/platform_specific_information aix-specific-considerations}@anchor{265}@anchor{gnat_ugn/platform_specific_information id8}@anchor{266}
29790 @subsection AIX-Specific Considerations
29793 @geindex AIX resolver library
29795 On AIX, the resolver library initializes some internal structure on
29796 the first call to @cite{get*by*} functions, which are used to implement
29797 @cite{GNAT.Sockets.Get_Host_By_Name} and
29798 @cite{GNAT.Sockets.Get_Host_By_Address}.
29799 If such initialization occurs within an Ada task, and the stack size for
29800 the task is the default size, a stack overflow may occur.
29802 To avoid this overflow, the user should either ensure that the first call
29803 to @cite{GNAT.Sockets.Get_Host_By_Name} or
29804 @cite{GNAT.Sockets.Get_Host_By_Addrss}
29805 occurs in the environment task, or use @cite{pragma Storage_Size} to
29806 specify a sufficiently large size for the stack of the task that contains
29807 this call.
29809 @geindex Windows NT
29811 @geindex Windows 95
29813 @geindex Windows 98
29815 @node Microsoft Windows Topics,Mac OS Topics,Specifying a Run-Time Library,Platform-Specific Information
29816 @anchor{gnat_ugn/platform_specific_information microsoft-windows-topics}@anchor{2e}@anchor{gnat_ugn/platform_specific_information id9}@anchor{267}
29817 @section Microsoft Windows Topics
29820 This section describes topics that are specific to the Microsoft Windows
29821 platforms.
29827 @menu
29828 * Using GNAT on Windows:: 
29829 * Using a network installation of GNAT:: 
29830 * CONSOLE and WINDOWS subsystems:: 
29831 * Temporary Files:: 
29832 * Mixed-Language Programming on Windows:: 
29834 @end menu
29836 @node Using GNAT on Windows,Using a network installation of GNAT,,Microsoft Windows Topics
29837 @anchor{gnat_ugn/platform_specific_information using-gnat-on-windows}@anchor{268}@anchor{gnat_ugn/platform_specific_information id10}@anchor{269}
29838 @subsection Using GNAT on Windows
29841 One of the strengths of the GNAT technology is that its tool set
29842 (@emph{gcc}, @emph{gnatbind}, @emph{gnatlink}, @emph{gnatmake}, the
29843 @cite{gdb} debugger, etc.) is used in the same way regardless of the
29844 platform.
29846 On Windows this tool set is complemented by a number of Microsoft-specific
29847 tools that have been provided to facilitate interoperability with Windows
29848 when this is required. With these tools:
29851 @itemize *
29853 @item 
29854 You can build applications using the @cite{CONSOLE} or @cite{WINDOWS}
29855 subsystems.
29857 @item 
29858 You can use any Dynamically Linked Library (DLL) in your Ada code (both
29859 relocatable and non-relocatable DLLs are supported).
29861 @item 
29862 You can build Ada DLLs for use in other applications. These applications
29863 can be written in a language other than Ada (e.g., C, C++, etc). Again both
29864 relocatable and non-relocatable Ada DLLs are supported.
29866 @item 
29867 You can include Windows resources in your Ada application.
29869 @item 
29870 You can use or create COM/DCOM objects.
29871 @end itemize
29873 Immediately below are listed all known general GNAT-for-Windows restrictions.
29874 Other restrictions about specific features like Windows Resources and DLLs
29875 are listed in separate sections below.
29878 @itemize *
29880 @item 
29881 It is not possible to use @cite{GetLastError} and @cite{SetLastError}
29882 when tasking, protected records, or exceptions are used. In these
29883 cases, in order to implement Ada semantics, the GNAT run-time system
29884 calls certain Win32 routines that set the last error variable to 0 upon
29885 success. It should be possible to use @cite{GetLastError} and
29886 @cite{SetLastError} when tasking, protected record, and exception
29887 features are not used, but it is not guaranteed to work.
29889 @item 
29890 It is not possible to link against Microsoft C++ libraries except for
29891 import libraries. Interfacing must be done by the mean of DLLs.
29893 @item 
29894 It is possible to link against Microsoft C libraries. Yet the preferred
29895 solution is to use C/C++ compiler that comes with GNAT, since it
29896 doesn't require having two different development environments and makes the
29897 inter-language debugging experience smoother.
29899 @item 
29900 When the compilation environment is located on FAT32 drives, users may
29901 experience recompilations of the source files that have not changed if
29902 Daylight Saving Time (DST) state has changed since the last time files
29903 were compiled. NTFS drives do not have this problem.
29905 @item 
29906 No components of the GNAT toolset use any entries in the Windows
29907 registry. The only entries that can be created are file associations and
29908 PATH settings, provided the user has chosen to create them at installation
29909 time, as well as some minimal book-keeping information needed to correctly
29910 uninstall or integrate different GNAT products.
29911 @end itemize
29913 @node Using a network installation of GNAT,CONSOLE and WINDOWS subsystems,Using GNAT on Windows,Microsoft Windows Topics
29914 @anchor{gnat_ugn/platform_specific_information id11}@anchor{26a}@anchor{gnat_ugn/platform_specific_information using-a-network-installation-of-gnat}@anchor{26b}
29915 @subsection Using a network installation of GNAT
29918 Make sure the system on which GNAT is installed is accessible from the
29919 current machine, i.e., the install location is shared over the network.
29920 Shared resources are accessed on Windows by means of UNC paths, which
29921 have the format @cite{\\server\sharename\path}
29923 In order to use such a network installation, simply add the UNC path of the
29924 @code{bin} directory of your GNAT installation in front of your PATH. For
29925 example, if GNAT is installed in @code{\GNAT} directory of a share location
29926 called @code{c-drive} on a machine @code{LOKI}, the following command will
29927 make it available:
29929 @quotation
29931 @example
29932 $ path \\loki\c-drive\gnat\bin;%path%`
29933 @end example
29934 @end quotation
29936 Be aware that every compilation using the network installation results in the
29937 transfer of large amounts of data across the network and will likely cause
29938 serious performance penalty.
29940 @node CONSOLE and WINDOWS subsystems,Temporary Files,Using a network installation of GNAT,Microsoft Windows Topics
29941 @anchor{gnat_ugn/platform_specific_information id12}@anchor{26c}@anchor{gnat_ugn/platform_specific_information console-and-windows-subsystems}@anchor{26d}
29942 @subsection CONSOLE and WINDOWS subsystems
29945 @geindex CONSOLE Subsystem
29947 @geindex WINDOWS Subsystem
29949 @geindex -mwindows
29951 There are two main subsystems under Windows. The @cite{CONSOLE} subsystem
29952 (which is the default subsystem) will always create a console when
29953 launching the application. This is not something desirable when the
29954 application has a Windows GUI. To get rid of this console the
29955 application must be using the @cite{WINDOWS} subsystem. To do so
29956 the @emph{-mwindows} linker option must be specified.
29958 @quotation
29960 @example
29961 $ gnatmake winprog -largs -mwindows
29962 @end example
29963 @end quotation
29965 @node Temporary Files,Mixed-Language Programming on Windows,CONSOLE and WINDOWS subsystems,Microsoft Windows Topics
29966 @anchor{gnat_ugn/platform_specific_information id13}@anchor{26e}@anchor{gnat_ugn/platform_specific_information temporary-files}@anchor{26f}
29967 @subsection Temporary Files
29970 @geindex Temporary files
29972 It is possible to control where temporary files gets created by setting
29973 the 
29974 @geindex TMP
29975 @geindex environment variable; TMP
29976 @code{TMP} environment variable. The file will be created:
29979 @itemize *
29981 @item 
29982 Under the directory pointed to by the 
29983 @geindex TMP
29984 @geindex environment variable; TMP
29985 @code{TMP} environment variable if
29986 this directory exists.
29988 @item 
29989 Under @code{c:\temp}, if the 
29990 @geindex TMP
29991 @geindex environment variable; TMP
29992 @code{TMP} environment variable is not
29993 set (or not pointing to a directory) and if this directory exists.
29995 @item 
29996 Under the current working directory otherwise.
29997 @end itemize
29999 This allows you to determine exactly where the temporary
30000 file will be created. This is particularly useful in networked
30001 environments where you may not have write access to some
30002 directories.
30004 @node Mixed-Language Programming on Windows,,Temporary Files,Microsoft Windows Topics
30005 @anchor{gnat_ugn/platform_specific_information mixed-language-programming-on-windows}@anchor{270}@anchor{gnat_ugn/platform_specific_information id14}@anchor{271}
30006 @subsection Mixed-Language Programming on Windows
30009 Developing pure Ada applications on Windows is no different than on
30010 other GNAT-supported platforms. However, when developing or porting an
30011 application that contains a mix of Ada and C/C++, the choice of your
30012 Windows C/C++ development environment conditions your overall
30013 interoperability strategy.
30015 If you use @emph{gcc} or Microsoft C to compile the non-Ada part of
30016 your application, there are no Windows-specific restrictions that
30017 affect the overall interoperability with your Ada code. If you do want
30018 to use the Microsoft tools for your C++ code, you have two choices:
30021 @itemize *
30023 @item 
30024 Encapsulate your C++ code in a DLL to be linked with your Ada
30025 application. In this case, use the Microsoft or whatever environment to
30026 build the DLL and use GNAT to build your executable
30027 (@ref{272,,Using DLLs with GNAT}).
30029 @item 
30030 Or you can encapsulate your Ada code in a DLL to be linked with the
30031 other part of your application. In this case, use GNAT to build the DLL
30032 (@ref{273,,Building DLLs with GNAT Project files}) and use the Microsoft
30033 or whatever environment to build your executable.
30034 @end itemize
30036 In addition to the description about C main in
30037 @ref{46,,Mixed Language Programming} section, if the C main uses a
30038 stand-alone library it is required on x86-windows to
30039 setup the SEH context. For this the C main must looks like this:
30041 @quotation
30043 @example
30044 /* main.c */
30045 extern void adainit (void);
30046 extern void adafinal (void);
30047 extern void __gnat_initialize(void*);
30048 extern void call_to_ada (void);
30050 int main (int argc, char *argv[])
30052   int SEH [2];
30054   /* Initialize the SEH context */
30055   __gnat_initialize (&SEH);
30057   adainit();
30059   /* Then call Ada services in the stand-alone library */
30061   call_to_ada();
30063   adafinal();
30065 @end example
30066 @end quotation
30068 Note that this is not needed on x86_64-windows where the Windows
30069 native SEH support is used.
30071 @menu
30072 * Windows Calling Conventions:: 
30073 * Introduction to Dynamic Link Libraries (DLLs): Introduction to Dynamic Link Libraries DLLs. 
30074 * Using DLLs with GNAT:: 
30075 * Building DLLs with GNAT Project files:: 
30076 * Building DLLs with GNAT:: 
30077 * Building DLLs with gnatdll:: 
30078 * Ada DLLs and Finalization:: 
30079 * Creating a Spec for Ada DLLs:: 
30080 * GNAT and Windows Resources:: 
30081 * Debugging a DLL:: 
30082 * Setting Stack Size from gnatlink:: 
30083 * Setting Heap Size from gnatlink:: 
30085 @end menu
30087 @node Windows Calling Conventions,Introduction to Dynamic Link Libraries DLLs,,Mixed-Language Programming on Windows
30088 @anchor{gnat_ugn/platform_specific_information windows-calling-conventions}@anchor{274}@anchor{gnat_ugn/platform_specific_information id15}@anchor{275}
30089 @subsubsection Windows Calling Conventions
30092 @geindex Stdcall
30094 @geindex APIENTRY
30096 This section pertain only to Win32. On Win64 there is a single native
30097 calling convention. All convention specifiers are ignored on this
30098 platform.
30100 When a subprogram @cite{F} (caller) calls a subprogram @cite{G}
30101 (callee), there are several ways to push @cite{G}'s parameters on the
30102 stack and there are several possible scenarios to clean up the stack
30103 upon @cite{G}'s return. A calling convention is an agreed upon software
30104 protocol whereby the responsibilities between the caller (@cite{F}) and
30105 the callee (@cite{G}) are clearly defined. Several calling conventions
30106 are available for Windows:
30109 @itemize *
30111 @item 
30112 @cite{C} (Microsoft defined)
30114 @item 
30115 @cite{Stdcall} (Microsoft defined)
30117 @item 
30118 @cite{Win32} (GNAT specific)
30120 @item 
30121 @cite{DLL} (GNAT specific)
30122 @end itemize
30124 @menu
30125 * C Calling Convention:: 
30126 * Stdcall Calling Convention:: 
30127 * Win32 Calling Convention:: 
30128 * DLL Calling Convention:: 
30130 @end menu
30132 @node C Calling Convention,Stdcall Calling Convention,,Windows Calling Conventions
30133 @anchor{gnat_ugn/platform_specific_information c-calling-convention}@anchor{276}@anchor{gnat_ugn/platform_specific_information id16}@anchor{277}
30134 @subsubsection @cite{C} Calling Convention
30137 This is the default calling convention used when interfacing to C/C++
30138 routines compiled with either @emph{gcc} or Microsoft Visual C++.
30140 In the @cite{C} calling convention subprogram parameters are pushed on the
30141 stack by the caller from right to left. The caller itself is in charge of
30142 cleaning up the stack after the call. In addition, the name of a routine
30143 with @cite{C} calling convention is mangled by adding a leading underscore.
30145 The name to use on the Ada side when importing (or exporting) a routine
30146 with @cite{C} calling convention is the name of the routine. For
30147 instance the C function:
30149 @quotation
30151 @example
30152 int get_val (long);
30153 @end example
30154 @end quotation
30156 should be imported from Ada as follows:
30158 @quotation
30160 @example
30161 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
30162 pragma Import (C, Get_Val, External_Name => "get_val");
30163 @end example
30164 @end quotation
30166 Note that in this particular case the @cite{External_Name} parameter could
30167 have been omitted since, when missing, this parameter is taken to be the
30168 name of the Ada entity in lower case. When the @cite{Link_Name} parameter
30169 is missing, as in the above example, this parameter is set to be the
30170 @cite{External_Name} with a leading underscore.
30172 When importing a variable defined in C, you should always use the @cite{C}
30173 calling convention unless the object containing the variable is part of a
30174 DLL (in which case you should use the @cite{Stdcall} calling
30175 convention, @ref{278,,Stdcall Calling Convention}).
30177 @node Stdcall Calling Convention,Win32 Calling Convention,C Calling Convention,Windows Calling Conventions
30178 @anchor{gnat_ugn/platform_specific_information stdcall-calling-convention}@anchor{278}@anchor{gnat_ugn/platform_specific_information id17}@anchor{279}
30179 @subsubsection @cite{Stdcall} Calling Convention
30182 This convention, which was the calling convention used for Pascal
30183 programs, is used by Microsoft for all the routines in the Win32 API for
30184 efficiency reasons. It must be used to import any routine for which this
30185 convention was specified.
30187 In the @cite{Stdcall} calling convention subprogram parameters are pushed
30188 on the stack by the caller from right to left. The callee (and not the
30189 caller) is in charge of cleaning the stack on routine exit. In addition,
30190 the name of a routine with @cite{Stdcall} calling convention is mangled by
30191 adding a leading underscore (as for the @cite{C} calling convention) and a
30192 trailing @code{@@@emph{nn}}, where @cite{nn} is the overall size (in
30193 bytes) of the parameters passed to the routine.
30195 The name to use on the Ada side when importing a C routine with a
30196 @cite{Stdcall} calling convention is the name of the C routine. The leading
30197 underscore and trailing @code{@@@emph{nn}} are added automatically by
30198 the compiler. For instance the Win32 function:
30200 @quotation
30202 @example
30203 APIENTRY int get_val (long);
30204 @end example
30205 @end quotation
30207 should be imported from Ada as follows:
30209 @quotation
30211 @example
30212 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
30213 pragma Import (Stdcall, Get_Val);
30214 --  On the x86 a long is 4 bytes, so the Link_Name is "_get_val@@4"
30215 @end example
30216 @end quotation
30218 As for the @cite{C} calling convention, when the @cite{External_Name}
30219 parameter is missing, it is taken to be the name of the Ada entity in lower
30220 case. If instead of writing the above import pragma you write:
30222 @quotation
30224 @example
30225 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
30226 pragma Import (Stdcall, Get_Val, External_Name => "retrieve_val");
30227 @end example
30228 @end quotation
30230 then the imported routine is @cite{_retrieve_val@@4}. However, if instead
30231 of specifying the @cite{External_Name} parameter you specify the
30232 @cite{Link_Name} as in the following example:
30234 @quotation
30236 @example
30237 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
30238 pragma Import (Stdcall, Get_Val, Link_Name => "retrieve_val");
30239 @end example
30240 @end quotation
30242 then the imported routine is @cite{retrieve_val}, that is, there is no
30243 decoration at all. No leading underscore and no Stdcall suffix
30244 @code{@@@emph{nn}}.
30246 This is especially important as in some special cases a DLL's entry
30247 point name lacks a trailing @code{@@@emph{nn}} while the exported
30248 name generated for a call has it.
30250 It is also possible to import variables defined in a DLL by using an
30251 import pragma for a variable. As an example, if a DLL contains a
30252 variable defined as:
30254 @quotation
30256 @example
30257 int my_var;
30258 @end example
30259 @end quotation
30261 then, to access this variable from Ada you should write:
30263 @quotation
30265 @example
30266 My_Var : Interfaces.C.int;
30267 pragma Import (Stdcall, My_Var);
30268 @end example
30269 @end quotation
30271 Note that to ease building cross-platform bindings this convention
30272 will be handled as a @cite{C} calling convention on non-Windows platforms.
30274 @node Win32 Calling Convention,DLL Calling Convention,Stdcall Calling Convention,Windows Calling Conventions
30275 @anchor{gnat_ugn/platform_specific_information id18}@anchor{27a}@anchor{gnat_ugn/platform_specific_information win32-calling-convention}@anchor{27b}
30276 @subsubsection @cite{Win32} Calling Convention
30279 This convention, which is GNAT-specific is fully equivalent to the
30280 @cite{Stdcall} calling convention described above.
30282 @node DLL Calling Convention,,Win32 Calling Convention,Windows Calling Conventions
30283 @anchor{gnat_ugn/platform_specific_information id19}@anchor{27c}@anchor{gnat_ugn/platform_specific_information dll-calling-convention}@anchor{27d}
30284 @subsubsection @cite{DLL} Calling Convention
30287 This convention, which is GNAT-specific is fully equivalent to the
30288 @cite{Stdcall} calling convention described above.
30290 @node Introduction to Dynamic Link Libraries DLLs,Using DLLs with GNAT,Windows Calling Conventions,Mixed-Language Programming on Windows
30291 @anchor{gnat_ugn/platform_specific_information id20}@anchor{27e}@anchor{gnat_ugn/platform_specific_information introduction-to-dynamic-link-libraries-dlls}@anchor{27f}
30292 @subsubsection Introduction to Dynamic Link Libraries (DLLs)
30295 @geindex DLL
30297 A Dynamically Linked Library (DLL) is a library that can be shared by
30298 several applications running under Windows. A DLL can contain any number of
30299 routines and variables.
30301 One advantage of DLLs is that you can change and enhance them without
30302 forcing all the applications that depend on them to be relinked or
30303 recompiled. However, you should be aware than all calls to DLL routines are
30304 slower since, as you will understand below, such calls are indirect.
30306 To illustrate the remainder of this section, suppose that an application
30307 wants to use the services of a DLL @code{API.dll}. To use the services
30308 provided by @code{API.dll} you must statically link against the DLL or
30309 an import library which contains a jump table with an entry for each
30310 routine and variable exported by the DLL. In the Microsoft world this
30311 import library is called @code{API.lib}. When using GNAT this import
30312 library is called either @code{libAPI.dll.a}, @code{libapi.dll.a},
30313 @code{libAPI.a} or @code{libapi.a} (names are case insensitive).
30315 After you have linked your application with the DLL or the import library
30316 and you run your application, here is what happens:
30319 @itemize *
30321 @item 
30322 Your application is loaded into memory.
30324 @item 
30325 The DLL @code{API.dll} is mapped into the address space of your
30326 application. This means that:
30329 @itemize -
30331 @item 
30332 The DLL will use the stack of the calling thread.
30334 @item 
30335 The DLL will use the virtual address space of the calling process.
30337 @item 
30338 The DLL will allocate memory from the virtual address space of the calling
30339 process.
30341 @item 
30342 Handles (pointers) can be safely exchanged between routines in the DLL
30343 routines and routines in the application using the DLL.
30344 @end itemize
30346 @item 
30347 The entries in the jump table (from the import library @code{libAPI.dll.a}
30348 or @code{API.lib} or automatically created when linking against a DLL)
30349 which is part of your application are initialized with the addresses
30350 of the routines and variables in @code{API.dll}.
30352 @item 
30353 If present in @code{API.dll}, routines @cite{DllMain} or
30354 @cite{DllMainCRTStartup} are invoked. These routines typically contain
30355 the initialization code needed for the well-being of the routines and
30356 variables exported by the DLL.
30357 @end itemize
30359 There is an additional point which is worth mentioning. In the Windows
30360 world there are two kind of DLLs: relocatable and non-relocatable
30361 DLLs. Non-relocatable DLLs can only be loaded at a very specific address
30362 in the target application address space. If the addresses of two
30363 non-relocatable DLLs overlap and these happen to be used by the same
30364 application, a conflict will occur and the application will run
30365 incorrectly. Hence, when possible, it is always preferable to use and
30366 build relocatable DLLs. Both relocatable and non-relocatable DLLs are
30367 supported by GNAT. Note that the @emph{-s} linker option (see GNU Linker
30368 User's Guide) removes the debugging symbols from the DLL but the DLL can
30369 still be relocated.
30371 As a side note, an interesting difference between Microsoft DLLs and
30372 Unix shared libraries, is the fact that on most Unix systems all public
30373 routines are exported by default in a Unix shared library, while under
30374 Windows it is possible (but not required) to list exported routines in
30375 a definition file (see @ref{280,,The Definition File}).
30377 @node Using DLLs with GNAT,Building DLLs with GNAT Project files,Introduction to Dynamic Link Libraries DLLs,Mixed-Language Programming on Windows
30378 @anchor{gnat_ugn/platform_specific_information id21}@anchor{281}@anchor{gnat_ugn/platform_specific_information using-dlls-with-gnat}@anchor{272}
30379 @subsubsection Using DLLs with GNAT
30382 To use the services of a DLL, say @code{API.dll}, in your Ada application
30383 you must have:
30386 @itemize *
30388 @item 
30389 The Ada spec for the routines and/or variables you want to access in
30390 @code{API.dll}. If not available this Ada spec must be built from the C/C++
30391 header files provided with the DLL.
30393 @item 
30394 The import library (@code{libAPI.dll.a} or @code{API.lib}). As previously
30395 mentioned an import library is a statically linked library containing the
30396 import table which will be filled at load time to point to the actual
30397 @code{API.dll} routines. Sometimes you don't have an import library for the
30398 DLL you want to use. The following sections will explain how to build
30399 one. Note that this is optional.
30401 @item 
30402 The actual DLL, @code{API.dll}.
30403 @end itemize
30405 Once you have all the above, to compile an Ada application that uses the
30406 services of @code{API.dll} and whose main subprogram is @cite{My_Ada_App},
30407 you simply issue the command
30409 @quotation
30411 @example
30412 $ gnatmake my_ada_app -largs -lAPI
30413 @end example
30414 @end quotation
30416 The argument @emph{-largs -lAPI} at the end of the @emph{gnatmake} command
30417 tells the GNAT linker to look for an import library. The linker will
30418 look for a library name in this specific order:
30421 @itemize *
30423 @item 
30424 @code{libAPI.dll.a}
30426 @item 
30427 @code{API.dll.a}
30429 @item 
30430 @code{libAPI.a}
30432 @item 
30433 @code{API.lib}
30435 @item 
30436 @code{libAPI.dll}
30438 @item 
30439 @code{API.dll}
30440 @end itemize
30442 The first three are the GNU style import libraries. The third is the
30443 Microsoft style import libraries. The last two are the actual DLL names.
30445 Note that if the Ada package spec for @code{API.dll} contains the
30446 following pragma
30448 @quotation
30450 @example
30451 pragma Linker_Options ("-lAPI");
30452 @end example
30453 @end quotation
30455 you do not have to add @emph{-largs -lAPI} at the end of the
30456 @emph{gnatmake} command.
30458 If any one of the items above is missing you will have to create it
30459 yourself. The following sections explain how to do so using as an
30460 example a fictitious DLL called @code{API.dll}.
30462 @menu
30463 * Creating an Ada Spec for the DLL Services:: 
30464 * Creating an Import Library:: 
30466 @end menu
30468 @node Creating an Ada Spec for the DLL Services,Creating an Import Library,,Using DLLs with GNAT
30469 @anchor{gnat_ugn/platform_specific_information creating-an-ada-spec-for-the-dll-services}@anchor{282}@anchor{gnat_ugn/platform_specific_information id22}@anchor{283}
30470 @subsubsection Creating an Ada Spec for the DLL Services
30473 A DLL typically comes with a C/C++ header file which provides the
30474 definitions of the routines and variables exported by the DLL. The Ada
30475 equivalent of this header file is a package spec that contains definitions
30476 for the imported entities. If the DLL you intend to use does not come with
30477 an Ada spec you have to generate one such spec yourself. For example if
30478 the header file of @code{API.dll} is a file @code{api.h} containing the
30479 following two definitions:
30481 @quotation
30483 @example
30484 int some_var;
30485 int get (char *);
30486 @end example
30487 @end quotation
30489 then the equivalent Ada spec could be:
30491 @quotation
30493 @example
30494 with Interfaces.C.Strings;
30495 package API is
30496    use Interfaces;
30498    Some_Var : C.int;
30499    function Get (Str : C.Strings.Chars_Ptr) return C.int;
30501 private
30502    pragma Import (C, Get);
30503    pragma Import (DLL, Some_Var);
30504 end API;
30505 @end example
30506 @end quotation
30508 @node Creating an Import Library,,Creating an Ada Spec for the DLL Services,Using DLLs with GNAT
30509 @anchor{gnat_ugn/platform_specific_information id23}@anchor{284}@anchor{gnat_ugn/platform_specific_information creating-an-import-library}@anchor{285}
30510 @subsubsection Creating an Import Library
30513 @geindex Import library
30515 If a Microsoft-style import library @code{API.lib} or a GNAT-style
30516 import library @code{libAPI.dll.a} or @code{libAPI.a} is available
30517 with @code{API.dll} you can skip this section. You can also skip this
30518 section if @code{API.dll} or @code{libAPI.dll} is built with GNU tools
30519 as in this case it is possible to link directly against the
30520 DLL. Otherwise read on.
30522 @geindex Definition file
30523 @anchor{gnat_ugn/platform_specific_information the-definition-file}@anchor{280}
30524 @subsubheading The Definition File
30527 As previously mentioned, and unlike Unix systems, the list of symbols
30528 that are exported from a DLL must be provided explicitly in Windows.
30529 The main goal of a definition file is precisely that: list the symbols
30530 exported by a DLL. A definition file (usually a file with a @cite{.def}
30531 suffix) has the following structure:
30533 @quotation
30535 @example
30536 [LIBRARY `name`]
30537 [DESCRIPTION `string`]
30538 EXPORTS
30539    `symbol1`
30540    `symbol2`
30541    ...
30542 @end example
30543 @end quotation
30546 @table @asis
30548 @item @emph{LIBRARY `name`}
30550 This section, which is optional, gives the name of the DLL.
30552 @item @emph{DESCRIPTION `string`}
30554 This section, which is optional, gives a description string that will be
30555 embedded in the import library.
30557 @item @emph{EXPORTS}
30559 This section gives the list of exported symbols (procedures, functions or
30560 variables). For instance in the case of @code{API.dll} the @cite{EXPORTS}
30561 section of @code{API.def} looks like:
30563 @example
30564 EXPORTS
30565    some_var
30566    get
30567 @end example
30568 @end table
30570 Note that you must specify the correct suffix (@code{@@@emph{nn}})
30571 (see @ref{274,,Windows Calling Conventions}) for a Stdcall
30572 calling convention function in the exported symbols list.
30574 There can actually be other sections in a definition file, but these
30575 sections are not relevant to the discussion at hand.
30577 @subsubheading GNAT-Style Import Library
30579 @anchor{gnat_ugn/platform_specific_information gnat-style-import-library}@anchor{286}
30580 To create a static import library from @code{API.dll} with the GNAT tools
30581 you should proceed as follows:
30584 @itemize *
30586 @item 
30587 Create the definition file @code{API.def}
30588 (see @ref{280,,The Definition File}).
30589 For that use the @cite{dll2def} tool as follows:
30591 @example
30592 $ dll2def API.dll > API.def
30593 @end example
30595 @cite{dll2def} is a very simple tool: it takes as input a DLL and prints
30596 to standard output the list of entry points in the DLL. Note that if
30597 some routines in the DLL have the @cite{Stdcall} convention
30598 (@ref{274,,Windows Calling Conventions}) with stripped @code{@@@emph{nn}}
30599 suffix then you'll have to edit @code{api.def} to add it, and specify
30600 @emph{-k} to @emph{gnatdll} when creating the import library.
30602 Here are some hints to find the right @code{@@@emph{nn}} suffix.
30605 @itemize -
30607 @item 
30608 If you have the Microsoft import library (.lib), it is possible to get
30609 the right symbols by using Microsoft @cite{dumpbin} tool (see the
30610 corresponding Microsoft documentation for further details).
30612 @example
30613 $ dumpbin /exports api.lib
30614 @end example
30616 @item 
30617 If you have a message about a missing symbol at link time the compiler
30618 tells you what symbol is expected. You just have to go back to the
30619 definition file and add the right suffix.
30620 @end itemize
30622 @item 
30623 Build the import library @cite{libAPI.dll.a}, using @cite{gnatdll}
30624 (see @ref{287,,Using gnatdll}) as follows:
30626 @example
30627 $ gnatdll -e API.def -d API.dll
30628 @end example
30630 @cite{gnatdll} takes as input a definition file @code{API.def} and the
30631 name of the DLL containing the services listed in the definition file
30632 @code{API.dll}. The name of the static import library generated is
30633 computed from the name of the definition file as follows: if the
30634 definition file name is @cite{xyz`}.def`, the import library name will
30635 be @cite{lib`@w{`}xyz`}.a`. Note that in the previous example option
30636 @emph{-e} could have been removed because the name of the definition
30637 file (before the '@cite{.def}' suffix) is the same as the name of the
30638 DLL (@ref{287,,Using gnatdll} for more information about @cite{gnatdll}).
30639 @end itemize
30641 @subsubheading Microsoft-Style Import Library
30644 With GNAT you can either use a GNAT-style or Microsoft-style import
30645 library. A Microsoft import library is needed only if you plan to make an
30646 Ada DLL available to applications developed with Microsoft
30647 tools (@ref{270,,Mixed-Language Programming on Windows}).
30649 To create a Microsoft-style import library for @code{API.dll} you
30650 should proceed as follows:
30653 @itemize *
30655 @item 
30656 Create the definition file @code{API.def} from the DLL. For this use either
30657 the @cite{dll2def} tool as described above or the Microsoft @cite{dumpbin}
30658 tool (see the corresponding Microsoft documentation for further details).
30660 @item 
30661 Build the actual import library using Microsoft's @cite{lib} utility:
30663 @example
30664 $ lib -machine:IX86 -def:API.def -out:API.lib
30665 @end example
30667 If you use the above command the definition file @code{API.def} must
30668 contain a line giving the name of the DLL:
30670 @example
30671 LIBRARY      "API"
30672 @end example
30674 See the Microsoft documentation for further details about the usage of
30675 @cite{lib}.
30676 @end itemize
30678 @node Building DLLs with GNAT Project files,Building DLLs with GNAT,Using DLLs with GNAT,Mixed-Language Programming on Windows
30679 @anchor{gnat_ugn/platform_specific_information id24}@anchor{288}@anchor{gnat_ugn/platform_specific_information building-dlls-with-gnat-project-files}@anchor{273}
30680 @subsubsection Building DLLs with GNAT Project files
30683 @geindex DLLs
30684 @geindex building
30686 There is nothing specific to Windows in the build process.
30687 @ref{8a,,Library Projects}.
30689 Due to a system limitation, it is not possible under Windows to create threads
30690 when inside the @cite{DllMain} routine which is used for auto-initialization
30691 of shared libraries, so it is not possible to have library level tasks in SALs.
30693 @node Building DLLs with GNAT,Building DLLs with gnatdll,Building DLLs with GNAT Project files,Mixed-Language Programming on Windows
30694 @anchor{gnat_ugn/platform_specific_information building-dlls-with-gnat}@anchor{289}@anchor{gnat_ugn/platform_specific_information id25}@anchor{28a}
30695 @subsubsection Building DLLs with GNAT
30698 @geindex DLLs
30699 @geindex building
30701 This section explain how to build DLLs using the GNAT built-in DLL
30702 support. With the following procedure it is straight forward to build
30703 and use DLLs with GNAT.
30706 @itemize *
30708 @item 
30709 Building object files.
30710 The first step is to build all objects files that are to be included
30711 into the DLL. This is done by using the standard @emph{gnatmake} tool.
30713 @item 
30714 Building the DLL.
30715 To build the DLL you must use @emph{gcc}'s @emph{-shared} and
30716 @emph{-shared-libgcc} options. It is quite simple to use this method:
30718 @example
30719 $ gcc -shared -shared-libgcc -o api.dll obj1.o obj2.o ...
30720 @end example
30722 It is important to note that in this case all symbols found in the
30723 object files are automatically exported. It is possible to restrict
30724 the set of symbols to export by passing to @emph{gcc} a definition
30725 file (see @ref{280,,The Definition File}).
30726 For example:
30728 @example
30729 $ gcc -shared -shared-libgcc -o api.dll api.def obj1.o obj2.o ...
30730 @end example
30732 If you use a definition file you must export the elaboration procedures
30733 for every package that required one. Elaboration procedures are named
30734 using the package name followed by "_E".
30736 @item 
30737 Preparing DLL to be used.
30738 For the DLL to be used by client programs the bodies must be hidden
30739 from it and the .ali set with read-only attribute. This is very important
30740 otherwise GNAT will recompile all packages and will not actually use
30741 the code in the DLL. For example:
30743 @example
30744 $ mkdir apilib
30745 $ copy *.ads *.ali api.dll apilib
30746 $ attrib +R apilib\\*.ali
30747 @end example
30748 @end itemize
30750 At this point it is possible to use the DLL by directly linking
30751 against it. Note that you must use the GNAT shared runtime when using
30752 GNAT shared libraries. This is achieved by using @emph{-shared} binder's
30753 option.
30755 @quotation
30757 @example
30758 $ gnatmake main -Iapilib -bargs -shared -largs -Lapilib -lAPI
30759 @end example
30760 @end quotation
30762 @node Building DLLs with gnatdll,Ada DLLs and Finalization,Building DLLs with GNAT,Mixed-Language Programming on Windows
30763 @anchor{gnat_ugn/platform_specific_information building-dlls-with-gnatdll}@anchor{28b}@anchor{gnat_ugn/platform_specific_information id26}@anchor{28c}
30764 @subsubsection Building DLLs with gnatdll
30767 @geindex DLLs
30768 @geindex building
30770 Note that it is preferred to use GNAT Project files
30771 (@ref{273,,Building DLLs with GNAT Project files}) or the built-in GNAT
30772 DLL support (@ref{289,,Building DLLs with GNAT}) or to build DLLs.
30774 This section explains how to build DLLs containing Ada code using
30775 @cite{gnatdll}. These DLLs will be referred to as Ada DLLs in the
30776 remainder of this section.
30778 The steps required to build an Ada DLL that is to be used by Ada as well as
30779 non-Ada applications are as follows:
30782 @itemize *
30784 @item 
30785 You need to mark each Ada @emph{entity} exported by the DLL with a @cite{C} or
30786 @cite{Stdcall} calling convention to avoid any Ada name mangling for the
30787 entities exported by the DLL
30788 (see @ref{28d,,Exporting Ada Entities}). You can
30789 skip this step if you plan to use the Ada DLL only from Ada applications.
30791 @item 
30792 Your Ada code must export an initialization routine which calls the routine
30793 @cite{adainit} generated by @emph{gnatbind} to perform the elaboration of
30794 the Ada code in the DLL (@ref{28e,,Ada DLLs and Elaboration}). The initialization
30795 routine exported by the Ada DLL must be invoked by the clients of the DLL
30796 to initialize the DLL.
30798 @item 
30799 When useful, the DLL should also export a finalization routine which calls
30800 routine @cite{adafinal} generated by @emph{gnatbind} to perform the
30801 finalization of the Ada code in the DLL (@ref{28f,,Ada DLLs and Finalization}).
30802 The finalization routine exported by the Ada DLL must be invoked by the
30803 clients of the DLL when the DLL services are no further needed.
30805 @item 
30806 You must provide a spec for the services exported by the Ada DLL in each
30807 of the programming languages to which you plan to make the DLL available.
30809 @item 
30810 You must provide a definition file listing the exported entities
30811 (@ref{280,,The Definition File}).
30813 @item 
30814 Finally you must use @cite{gnatdll} to produce the DLL and the import
30815 library (@ref{287,,Using gnatdll}).
30816 @end itemize
30818 Note that a relocatable DLL stripped using the @cite{strip}
30819 binutils tool will not be relocatable anymore. To build a DLL without
30820 debug information pass @cite{-largs -s} to @cite{gnatdll}. This
30821 restriction does not apply to a DLL built using a Library Project.
30822 See @ref{8a,,Library Projects}.
30824 @c Limitations_When_Using_Ada_DLLs_from Ada:
30826 @menu
30827 * Limitations When Using Ada DLLs from Ada:: 
30828 * Exporting Ada Entities:: 
30829 * Ada DLLs and Elaboration:: 
30831 @end menu
30833 @node Limitations When Using Ada DLLs from Ada,Exporting Ada Entities,,Building DLLs with gnatdll
30834 @anchor{gnat_ugn/platform_specific_information limitations-when-using-ada-dlls-from-ada}@anchor{290}
30835 @subsubsection Limitations When Using Ada DLLs from Ada
30838 When using Ada DLLs from Ada applications there is a limitation users
30839 should be aware of. Because on Windows the GNAT run time is not in a DLL of
30840 its own, each Ada DLL includes a part of the GNAT run time. Specifically,
30841 each Ada DLL includes the services of the GNAT run time that are necessary
30842 to the Ada code inside the DLL. As a result, when an Ada program uses an
30843 Ada DLL there are two independent GNAT run times: one in the Ada DLL and
30844 one in the main program.
30846 It is therefore not possible to exchange GNAT run-time objects between the
30847 Ada DLL and the main Ada program. Example of GNAT run-time objects are file
30848 handles (e.g., @cite{Text_IO.File_Type}), tasks types, protected objects
30849 types, etc.
30851 It is completely safe to exchange plain elementary, array or record types,
30852 Windows object handles, etc.
30854 @node Exporting Ada Entities,Ada DLLs and Elaboration,Limitations When Using Ada DLLs from Ada,Building DLLs with gnatdll
30855 @anchor{gnat_ugn/platform_specific_information exporting-ada-entities}@anchor{28d}@anchor{gnat_ugn/platform_specific_information id27}@anchor{291}
30856 @subsubsection Exporting Ada Entities
30859 @geindex Export table
30861 Building a DLL is a way to encapsulate a set of services usable from any
30862 application. As a result, the Ada entities exported by a DLL should be
30863 exported with the @cite{C} or @cite{Stdcall} calling conventions to avoid
30864 any Ada name mangling. As an example here is an Ada package
30865 @cite{API}, spec and body, exporting two procedures, a function, and a
30866 variable:
30868 @quotation
30870 @example
30871 with Interfaces.C; use Interfaces;
30872 package API is
30873    Count : C.int := 0;
30874    function Factorial (Val : C.int) return C.int;
30876    procedure Initialize_API;
30877    procedure Finalize_API;
30878    --  Initialization & Finalization routines. More in the next section.
30879 private
30880    pragma Export (C, Initialize_API);
30881    pragma Export (C, Finalize_API);
30882    pragma Export (C, Count);
30883    pragma Export (C, Factorial);
30884 end API;
30885 @end example
30887 @example
30888 package body API is
30889    function Factorial (Val : C.int) return C.int is
30890       Fact : C.int := 1;
30891    begin
30892       Count := Count + 1;
30893       for K in 1 .. Val loop
30894          Fact := Fact * K;
30895       end loop;
30896       return Fact;
30897    end Factorial;
30899    procedure Initialize_API is
30900       procedure Adainit;
30901       pragma Import (C, Adainit);
30902    begin
30903       Adainit;
30904    end Initialize_API;
30906    procedure Finalize_API is
30907       procedure Adafinal;
30908       pragma Import (C, Adafinal);
30909    begin
30910       Adafinal;
30911    end Finalize_API;
30912 end API;
30913 @end example
30914 @end quotation
30916 If the Ada DLL you are building will only be used by Ada applications
30917 you do not have to export Ada entities with a @cite{C} or @cite{Stdcall}
30918 convention. As an example, the previous package could be written as
30919 follows:
30921 @quotation
30923 @example
30924 package API is
30925    Count : Integer := 0;
30926    function Factorial (Val : Integer) return Integer;
30928    procedure Initialize_API;
30929    procedure Finalize_API;
30930    --  Initialization and Finalization routines.
30931 end API;
30932 @end example
30934 @example
30935 package body API is
30936    function Factorial (Val : Integer) return Integer is
30937       Fact : Integer := 1;
30938    begin
30939       Count := Count + 1;
30940       for K in 1 .. Val loop
30941          Fact := Fact * K;
30942       end loop;
30943       return Fact;
30944    end Factorial;
30946    ...
30947    --  The remainder of this package body is unchanged.
30948 end API;
30949 @end example
30950 @end quotation
30952 Note that if you do not export the Ada entities with a @cite{C} or
30953 @cite{Stdcall} convention you will have to provide the mangled Ada names
30954 in the definition file of the Ada DLL
30955 (@ref{292,,Creating the Definition File}).
30957 @node Ada DLLs and Elaboration,,Exporting Ada Entities,Building DLLs with gnatdll
30958 @anchor{gnat_ugn/platform_specific_information ada-dlls-and-elaboration}@anchor{28e}@anchor{gnat_ugn/platform_specific_information id28}@anchor{293}
30959 @subsubsection Ada DLLs and Elaboration
30962 @geindex DLLs and elaboration
30964 The DLL that you are building contains your Ada code as well as all the
30965 routines in the Ada library that are needed by it. The first thing a
30966 user of your DLL must do is elaborate the Ada code
30967 (@ref{11,,Elaboration Order Handling in GNAT}).
30969 To achieve this you must export an initialization routine
30970 (@cite{Initialize_API} in the previous example), which must be invoked
30971 before using any of the DLL services. This elaboration routine must call
30972 the Ada elaboration routine @cite{adainit} generated by the GNAT binder
30973 (@ref{ba,,Binding with Non-Ada Main Programs}). See the body of
30974 @cite{Initialize_Api} for an example. Note that the GNAT binder is
30975 automatically invoked during the DLL build process by the @cite{gnatdll}
30976 tool (@ref{287,,Using gnatdll}).
30978 When a DLL is loaded, Windows systematically invokes a routine called
30979 @cite{DllMain}. It would therefore be possible to call @cite{adainit}
30980 directly from @cite{DllMain} without having to provide an explicit
30981 initialization routine. Unfortunately, it is not possible to call
30982 @cite{adainit} from the @cite{DllMain} if your program has library level
30983 tasks because access to the @cite{DllMain} entry point is serialized by
30984 the system (that is, only a single thread can execute 'through' it at a
30985 time), which means that the GNAT run time will deadlock waiting for the
30986 newly created task to complete its initialization.
30988 @node Ada DLLs and Finalization,Creating a Spec for Ada DLLs,Building DLLs with gnatdll,Mixed-Language Programming on Windows
30989 @anchor{gnat_ugn/platform_specific_information id29}@anchor{294}@anchor{gnat_ugn/platform_specific_information ada-dlls-and-finalization}@anchor{28f}
30990 @subsubsection Ada DLLs and Finalization
30993 @geindex DLLs and finalization
30995 When the services of an Ada DLL are no longer needed, the client code should
30996 invoke the DLL finalization routine, if available. The DLL finalization
30997 routine is in charge of releasing all resources acquired by the DLL. In the
30998 case of the Ada code contained in the DLL, this is achieved by calling
30999 routine @cite{adafinal} generated by the GNAT binder
31000 (@ref{ba,,Binding with Non-Ada Main Programs}).
31001 See the body of @cite{Finalize_Api} for an
31002 example. As already pointed out the GNAT binder is automatically invoked
31003 during the DLL build process by the @cite{gnatdll} tool
31004 (@ref{287,,Using gnatdll}).
31006 @node Creating a Spec for Ada DLLs,GNAT and Windows Resources,Ada DLLs and Finalization,Mixed-Language Programming on Windows
31007 @anchor{gnat_ugn/platform_specific_information id30}@anchor{295}@anchor{gnat_ugn/platform_specific_information creating-a-spec-for-ada-dlls}@anchor{296}
31008 @subsubsection Creating a Spec for Ada DLLs
31011 To use the services exported by the Ada DLL from another programming
31012 language (e.g., C), you have to translate the specs of the exported Ada
31013 entities in that language. For instance in the case of @cite{API.dll},
31014 the corresponding C header file could look like:
31016 @quotation
31018 @example
31019 extern int *_imp__count;
31020 #define count (*_imp__count)
31021 int factorial (int);
31022 @end example
31023 @end quotation
31025 It is important to understand that when building an Ada DLL to be used by
31026 other Ada applications, you need two different specs for the packages
31027 contained in the DLL: one for building the DLL and the other for using
31028 the DLL. This is because the @cite{DLL} calling convention is needed to
31029 use a variable defined in a DLL, but when building the DLL, the variable
31030 must have either the @cite{Ada} or @cite{C} calling convention. As an
31031 example consider a DLL comprising the following package @cite{API}:
31033 @quotation
31035 @example
31036 package API is
31037    Count : Integer := 0;
31038    ...
31039    --  Remainder of the package omitted.
31040 end API;
31041 @end example
31042 @end quotation
31044 After producing a DLL containing package @cite{API}, the spec that
31045 must be used to import @cite{API.Count} from Ada code outside of the
31046 DLL is:
31048 @quotation
31050 @example
31051 package API is
31052    Count : Integer;
31053    pragma Import (DLL, Count);
31054 end API;
31055 @end example
31056 @end quotation
31058 @menu
31059 * Creating the Definition File:: 
31060 * Using gnatdll:: 
31062 @end menu
31064 @node Creating the Definition File,Using gnatdll,,Creating a Spec for Ada DLLs
31065 @anchor{gnat_ugn/platform_specific_information creating-the-definition-file}@anchor{292}@anchor{gnat_ugn/platform_specific_information id31}@anchor{297}
31066 @subsubsection Creating the Definition File
31069 The definition file is the last file needed to build the DLL. It lists
31070 the exported symbols. As an example, the definition file for a DLL
31071 containing only package @cite{API} (where all the entities are exported
31072 with a @cite{C} calling convention) is:
31074 @quotation
31076 @example
31077 EXPORTS
31078     count
31079     factorial
31080     finalize_api
31081     initialize_api
31082 @end example
31083 @end quotation
31085 If the @cite{C} calling convention is missing from package @cite{API},
31086 then the definition file contains the mangled Ada names of the above
31087 entities, which in this case are:
31089 @quotation
31091 @example
31092 EXPORTS
31093     api__count
31094     api__factorial
31095     api__finalize_api
31096     api__initialize_api
31097 @end example
31098 @end quotation
31100 @node Using gnatdll,,Creating the Definition File,Creating a Spec for Ada DLLs
31101 @anchor{gnat_ugn/platform_specific_information using-gnatdll}@anchor{287}@anchor{gnat_ugn/platform_specific_information id32}@anchor{298}
31102 @subsubsection Using @cite{gnatdll}
31105 @geindex gnatdll
31107 @cite{gnatdll} is a tool to automate the DLL build process once all the Ada
31108 and non-Ada sources that make up your DLL have been compiled.
31109 @cite{gnatdll} is actually in charge of two distinct tasks: build the
31110 static import library for the DLL and the actual DLL. The form of the
31111 @cite{gnatdll} command is
31113 @quotation
31115 @example
31116 $ gnatdll [`switches`] `list-of-files` [-largs `opts`]
31117 @end example
31118 @end quotation
31120 where @cite{list-of-files} is a list of ALI and object files. The object
31121 file list must be the exact list of objects corresponding to the non-Ada
31122 sources whose services are to be included in the DLL. The ALI file list
31123 must be the exact list of ALI files for the corresponding Ada sources
31124 whose services are to be included in the DLL. If @cite{list-of-files} is
31125 missing, only the static import library is generated.
31127 You may specify any of the following switches to @cite{gnatdll}:
31129 @quotation
31131 @geindex -a (gnatdll)
31132 @end quotation
31135 @table @asis
31137 @item @code{-a[@emph{address}]}
31139 Build a non-relocatable DLL at @cite{address}. If @cite{address} is not
31140 specified the default address @cite{0x11000000} will be used. By default,
31141 when this switch is missing, @cite{gnatdll} builds relocatable DLL. We
31142 advise the reader to build relocatable DLL.
31144 @geindex -b (gnatdll)
31146 @item @code{-b @emph{address}}
31148 Set the relocatable DLL base address. By default the address is
31149 @cite{0x11000000}.
31151 @geindex -bargs (gnatdll)
31153 @item @code{-bargs @emph{opts}}
31155 Binder options. Pass @cite{opts} to the binder.
31157 @geindex -d (gnatdll)
31159 @item @code{-d @emph{dllfile}}
31161 @cite{dllfile} is the name of the DLL. This switch must be present for
31162 @cite{gnatdll} to do anything. The name of the generated import library is
31163 obtained algorithmically from @cite{dllfile} as shown in the following
31164 example: if @cite{dllfile} is @cite{xyz.dll}, the import library name is
31165 @cite{libxyz.dll.a}. The name of the definition file to use (if not specified
31166 by option @emph{-e}) is obtained algorithmically from @cite{dllfile}
31167 as shown in the following example:
31168 if @cite{dllfile} is @cite{xyz.dll}, the definition
31169 file used is @cite{xyz.def}.
31171 @geindex -e (gnatdll)
31173 @item @code{-e @emph{deffile}}
31175 @cite{deffile} is the name of the definition file.
31177 @geindex -g (gnatdll)
31179 @item @code{-g}
31181 Generate debugging information. This information is stored in the object
31182 file and copied from there to the final DLL file by the linker,
31183 where it can be read by the debugger. You must use the
31184 @emph{-g} switch if you plan on using the debugger or the symbolic
31185 stack traceback.
31187 @geindex -h (gnatdll)
31189 @item @code{-h}
31191 Help mode. Displays @cite{gnatdll} switch usage information.
31193 @geindex -I (gnatdll)
31195 @item @code{-I@emph{dir}}
31197 Direct @cite{gnatdll} to search the @cite{dir} directory for source and
31198 object files needed to build the DLL.
31199 (@ref{8e,,Search Paths and the Run-Time Library (RTL)}).
31201 @geindex -k (gnatdll)
31203 @item @code{-k}
31205 Removes the @code{@@@emph{nn}} suffix from the import library's exported
31206 names, but keeps them for the link names. You must specify this
31207 option if you want to use a @cite{Stdcall} function in a DLL for which
31208 the @code{@@@emph{nn}} suffix has been removed. This is the case for most
31209 of the Windows NT DLL for example. This option has no effect when
31210 @emph{-n} option is specified.
31212 @geindex -l (gnatdll)
31214 @item @code{-l @emph{file}}
31216 The list of ALI and object files used to build the DLL are listed in
31217 @cite{file}, instead of being given in the command line. Each line in
31218 @cite{file} contains the name of an ALI or object file.
31220 @geindex -n (gnatdll)
31222 @item @code{-n}
31224 No Import. Do not create the import library.
31226 @geindex -q (gnatdll)
31228 @item @code{-q}
31230 Quiet mode. Do not display unnecessary messages.
31232 @geindex -v (gnatdll)
31234 @item @code{-v}
31236 Verbose mode. Display extra information.
31238 @geindex -largs (gnatdll)
31240 @item @code{-largs @emph{opts}}
31242 Linker options. Pass @cite{opts} to the linker.
31243 @end table
31245 @subsubheading @cite{gnatdll} Example
31248 As an example the command to build a relocatable DLL from @code{api.adb}
31249 once @code{api.adb} has been compiled and @code{api.def} created is
31251 @quotation
31253 @example
31254 $ gnatdll -d api.dll api.ali
31255 @end example
31256 @end quotation
31258 The above command creates two files: @code{libapi.dll.a} (the import
31259 library) and @code{api.dll} (the actual DLL). If you want to create
31260 only the DLL, just type:
31262 @quotation
31264 @example
31265 $ gnatdll -d api.dll -n api.ali
31266 @end example
31267 @end quotation
31269 Alternatively if you want to create just the import library, type:
31271 @quotation
31273 @example
31274 $ gnatdll -d api.dll
31275 @end example
31276 @end quotation
31278 @subsubheading @cite{gnatdll} behind the Scenes
31281 This section details the steps involved in creating a DLL. @cite{gnatdll}
31282 does these steps for you. Unless you are interested in understanding what
31283 goes on behind the scenes, you should skip this section.
31285 We use the previous example of a DLL containing the Ada package @cite{API},
31286 to illustrate the steps necessary to build a DLL. The starting point is a
31287 set of objects that will make up the DLL and the corresponding ALI
31288 files. In the case of this example this means that @code{api.o} and
31289 @code{api.ali} are available. To build a relocatable DLL, @cite{gnatdll} does
31290 the following:
31293 @itemize *
31295 @item 
31296 @cite{gnatdll} builds the base file (@code{api.base}). A base file gives
31297 the information necessary to generate relocation information for the
31298 DLL.
31300 @example
31301 $ gnatbind -n api
31302 $ gnatlink api -o api.jnk -mdll -Wl,--base-file,api.base
31303 @end example
31305 In addition to the base file, the @emph{gnatlink} command generates an
31306 output file @code{api.jnk} which can be discarded. The @emph{-mdll} switch
31307 asks @emph{gnatlink} to generate the routines @cite{DllMain} and
31308 @cite{DllMainCRTStartup} that are called by the Windows loader when the DLL
31309 is loaded into memory.
31311 @item 
31312 @cite{gnatdll} uses @cite{dlltool} (see @ref{299,,Using dlltool}) to build the
31313 export table (@code{api.exp}). The export table contains the relocation
31314 information in a form which can be used during the final link to ensure
31315 that the Windows loader is able to place the DLL anywhere in memory.
31317 @example
31318 $ dlltool --dllname api.dll --def api.def --base-file api.base \\
31319           --output-exp api.exp
31320 @end example
31322 @item 
31323 @cite{gnatdll} builds the base file using the new export table. Note that
31324 @emph{gnatbind} must be called once again since the binder generated file
31325 has been deleted during the previous call to @emph{gnatlink}.
31327 @example
31328 $ gnatbind -n api
31329 $ gnatlink api -o api.jnk api.exp -mdll
31330       -Wl,--base-file,api.base
31331 @end example
31333 @item 
31334 @cite{gnatdll} builds the new export table using the new base file and
31335 generates the DLL import library @code{libAPI.dll.a}.
31337 @example
31338 $ dlltool --dllname api.dll --def api.def --base-file api.base \\
31339           --output-exp api.exp --output-lib libAPI.a
31340 @end example
31342 @item 
31343 Finally @cite{gnatdll} builds the relocatable DLL using the final export
31344 table.
31346 @example
31347 $ gnatbind -n api
31348 $ gnatlink api api.exp -o api.dll -mdll
31349 @end example
31350 @end itemize
31351 @anchor{gnat_ugn/platform_specific_information using-dlltool}@anchor{299}
31352 @subsubheading Using @cite{dlltool}
31355 @cite{dlltool} is the low-level tool used by @cite{gnatdll} to build
31356 DLLs and static import libraries. This section summarizes the most
31357 common @cite{dlltool} switches. The form of the @cite{dlltool} command
31360 @quotation
31362 @example
31363 $ dlltool [`switches`]
31364 @end example
31365 @end quotation
31367 @cite{dlltool} switches include:
31369 @geindex --base-file (dlltool)
31372 @table @asis
31374 @item @code{--base-file @emph{basefile}}
31376 Read the base file @cite{basefile} generated by the linker. This switch
31377 is used to create a relocatable DLL.
31378 @end table
31380 @geindex --def (dlltool)
31383 @table @asis
31385 @item @code{--def @emph{deffile}}
31387 Read the definition file.
31388 @end table
31390 @geindex --dllname (dlltool)
31393 @table @asis
31395 @item @code{--dllname @emph{name}}
31397 Gives the name of the DLL. This switch is used to embed the name of the
31398 DLL in the static import library generated by @cite{dlltool} with switch
31399 @emph{--output-lib}.
31400 @end table
31402 @geindex -k (dlltool)
31405 @table @asis
31407 @item @code{-k}
31409 Kill @code{@@@emph{nn}} from exported names
31410 (@ref{274,,Windows Calling Conventions}
31411 for a discussion about @cite{Stdcall}-style symbols.
31412 @end table
31414 @geindex --help (dlltool)
31417 @table @asis
31419 @item @code{--help}
31421 Prints the @cite{dlltool} switches with a concise description.
31422 @end table
31424 @geindex --output-exp (dlltool)
31427 @table @asis
31429 @item @code{--output-exp @emph{exportfile}}
31431 Generate an export file @cite{exportfile}. The export file contains the
31432 export table (list of symbols in the DLL) and is used to create the DLL.
31433 @end table
31435 @geindex --output-lib (dlltool)
31438 @table @asis
31440 @item @code{--output-lib @emph{libfile}}
31442 Generate a static import library @cite{libfile}.
31443 @end table
31445 @geindex -v (dlltool)
31448 @table @asis
31450 @item @code{-v}
31452 Verbose mode.
31453 @end table
31455 @geindex --as (dlltool)
31458 @table @asis
31460 @item @code{--as @emph{assembler-name}}
31462 Use @cite{assembler-name} as the assembler. The default is @cite{as}.
31463 @end table
31465 @node GNAT and Windows Resources,Debugging a DLL,Creating a Spec for Ada DLLs,Mixed-Language Programming on Windows
31466 @anchor{gnat_ugn/platform_specific_information gnat-and-windows-resources}@anchor{29a}@anchor{gnat_ugn/platform_specific_information id33}@anchor{29b}
31467 @subsubsection GNAT and Windows Resources
31470 @geindex Resources
31471 @geindex windows
31473 Resources are an easy way to add Windows specific objects to your
31474 application. The objects that can be added as resources include:
31477 @itemize *
31479 @item 
31480 menus
31482 @item 
31483 accelerators
31485 @item 
31486 dialog boxes
31488 @item 
31489 string tables
31491 @item 
31492 bitmaps
31494 @item 
31495 cursors
31497 @item 
31498 icons
31500 @item 
31501 fonts
31503 @item 
31504 version information
31505 @end itemize
31507 For example, a version information resource can be defined as follow and
31508 embedded into an executable or DLL:
31510 A version information resource can be used to embed information into an
31511 executable or a DLL. These information can be viewed using the file properties
31512 from the Windows Explorer. Here is an example of a version information
31513 resource:
31515 @quotation
31517 @example
31518 1 VERSIONINFO
31519 FILEVERSION     1,0,0,0
31520 PRODUCTVERSION  1,0,0,0
31521 BEGIN
31522   BLOCK "StringFileInfo"
31523   BEGIN
31524     BLOCK "080904E4"
31525     BEGIN
31526       VALUE "CompanyName", "My Company Name"
31527       VALUE "FileDescription", "My application"
31528       VALUE "FileVersion", "1.0"
31529       VALUE "InternalName", "my_app"
31530       VALUE "LegalCopyright", "My Name"
31531       VALUE "OriginalFilename", "my_app.exe"
31532       VALUE "ProductName", "My App"
31533       VALUE "ProductVersion", "1.0"
31534     END
31535   END
31537   BLOCK "VarFileInfo"
31538   BEGIN
31539     VALUE "Translation", 0x809, 1252
31540   END
31542 @end example
31543 @end quotation
31545 The value @cite{0809} (langID) is for the U.K English language and
31546 @cite{04E4} (charsetID), which is equal to @cite{1252} decimal, for
31547 multilingual.
31549 This section explains how to build, compile and use resources. Note that this
31550 section does not cover all resource objects, for a complete description see
31551 the corresponding Microsoft documentation.
31553 @menu
31554 * Building Resources:: 
31555 * Compiling Resources:: 
31556 * Using Resources:: 
31558 @end menu
31560 @node Building Resources,Compiling Resources,,GNAT and Windows Resources
31561 @anchor{gnat_ugn/platform_specific_information building-resources}@anchor{29c}@anchor{gnat_ugn/platform_specific_information id34}@anchor{29d}
31562 @subsubsection Building Resources
31565 @geindex Resources
31566 @geindex building
31568 A resource file is an ASCII file. By convention resource files have an
31569 @code{.rc} extension.
31570 The easiest way to build a resource file is to use Microsoft tools
31571 such as @cite{imagedit.exe} to build bitmaps, icons and cursors and
31572 @cite{dlgedit.exe} to build dialogs.
31573 It is always possible to build an @code{.rc} file yourself by writing a
31574 resource script.
31576 It is not our objective to explain how to write a resource file. A
31577 complete description of the resource script language can be found in the
31578 Microsoft documentation.
31580 @node Compiling Resources,Using Resources,Building Resources,GNAT and Windows Resources
31581 @anchor{gnat_ugn/platform_specific_information compiling-resources}@anchor{29e}@anchor{gnat_ugn/platform_specific_information id35}@anchor{29f}
31582 @subsubsection Compiling Resources
31585 @geindex rc
31587 @geindex windres
31589 @geindex Resources
31590 @geindex compiling
31592 This section describes how to build a GNAT-compatible (COFF) object file
31593 containing the resources. This is done using the Resource Compiler
31594 @cite{windres} as follows:
31596 @quotation
31598 @example
31599 $ windres -i myres.rc -o myres.o
31600 @end example
31601 @end quotation
31603 By default @cite{windres} will run @emph{gcc} to preprocess the @code{.rc}
31604 file. You can specify an alternate preprocessor (usually named
31605 @code{cpp.exe}) using the @cite{windres} @emph{--preprocessor}
31606 parameter. A list of all possible options may be obtained by entering
31607 the command @cite{windres} @emph{--help}.
31609 It is also possible to use the Microsoft resource compiler @cite{rc.exe}
31610 to produce a @code{.res} file (binary resource file). See the
31611 corresponding Microsoft documentation for further details. In this case
31612 you need to use @cite{windres} to translate the @code{.res} file to a
31613 GNAT-compatible object file as follows:
31615 @quotation
31617 @example
31618 $ windres -i myres.res -o myres.o
31619 @end example
31620 @end quotation
31622 @node Using Resources,,Compiling Resources,GNAT and Windows Resources
31623 @anchor{gnat_ugn/platform_specific_information id36}@anchor{2a0}@anchor{gnat_ugn/platform_specific_information using-resources}@anchor{2a1}
31624 @subsubsection Using Resources
31627 @geindex Resources
31628 @geindex using
31630 To include the resource file in your program just add the
31631 GNAT-compatible object file for the resource(s) to the linker
31632 arguments. With @emph{gnatmake} this is done by using the @emph{-largs}
31633 option:
31635 @quotation
31637 @example
31638 $ gnatmake myprog -largs myres.o
31639 @end example
31640 @end quotation
31642 @node Debugging a DLL,Setting Stack Size from gnatlink,GNAT and Windows Resources,Mixed-Language Programming on Windows
31643 @anchor{gnat_ugn/platform_specific_information id37}@anchor{2a2}@anchor{gnat_ugn/platform_specific_information debugging-a-dll}@anchor{2a3}
31644 @subsubsection Debugging a DLL
31647 @geindex DLL debugging
31649 Debugging a DLL is similar to debugging a standard program. But
31650 we have to deal with two different executable parts: the DLL and the
31651 program that uses it. We have the following four possibilities:
31654 @itemize *
31656 @item 
31657 The program and the DLL are built with @cite{GCC/GNAT}.
31659 @item 
31660 The program is built with foreign tools and the DLL is built with
31661 @cite{GCC/GNAT}.
31663 @item 
31664 The program is built with @cite{GCC/GNAT} and the DLL is built with
31665 foreign tools.
31666 @end itemize
31668 In this section we address only cases one and two above.
31669 There is no point in trying to debug
31670 a DLL with @cite{GNU/GDB}, if there is no GDB-compatible debugging
31671 information in it. To do so you must use a debugger compatible with the
31672 tools suite used to build the DLL.
31674 @menu
31675 * Program and DLL Both Built with GCC/GNAT:: 
31676 * Program Built with Foreign Tools and DLL Built with GCC/GNAT:: 
31678 @end menu
31680 @node Program and DLL Both Built with GCC/GNAT,Program Built with Foreign Tools and DLL Built with GCC/GNAT,,Debugging a DLL
31681 @anchor{gnat_ugn/platform_specific_information program-and-dll-both-built-with-gcc-gnat}@anchor{2a4}@anchor{gnat_ugn/platform_specific_information id38}@anchor{2a5}
31682 @subsubsection Program and DLL Both Built with GCC/GNAT
31685 This is the simplest case. Both the DLL and the program have @cite{GDB}
31686 compatible debugging information. It is then possible to break anywhere in
31687 the process. Let's suppose here that the main procedure is named
31688 @cite{ada_main} and that in the DLL there is an entry point named
31689 @cite{ada_dll}.
31691 The DLL (@ref{27f,,Introduction to Dynamic Link Libraries (DLLs)}) and
31692 program must have been built with the debugging information (see GNAT -g
31693 switch). Here are the step-by-step instructions for debugging it:
31696 @itemize *
31698 @item 
31699 Launch @cite{GDB} on the main program.
31701 @example
31702 $ gdb -nw ada_main
31703 @end example
31705 @item 
31706 Start the program and stop at the beginning of the main procedure
31708 @example
31709 (gdb) start
31710 @end example
31712 This step is required to be able to set a breakpoint inside the DLL. As long
31713 as the program is not run, the DLL is not loaded. This has the
31714 consequence that the DLL debugging information is also not loaded, so it is not
31715 possible to set a breakpoint in the DLL.
31717 @item 
31718 Set a breakpoint inside the DLL
31720 @example
31721 (gdb) break ada_dll
31722 (gdb) cont
31723 @end example
31724 @end itemize
31726 At this stage a breakpoint is set inside the DLL. From there on
31727 you can use the standard approach to debug the whole program
31728 (@ref{26,,Running and Debugging Ada Programs}).
31730 @node Program Built with Foreign Tools and DLL Built with GCC/GNAT,,Program and DLL Both Built with GCC/GNAT,Debugging a DLL
31731 @anchor{gnat_ugn/platform_specific_information program-built-with-foreign-tools-and-dll-built-with-gcc-gnat}@anchor{2a6}@anchor{gnat_ugn/platform_specific_information id39}@anchor{2a7}
31732 @subsubsection Program Built with Foreign Tools and DLL Built with GCC/GNAT
31735 In this case things are slightly more complex because it is not possible to
31736 start the main program and then break at the beginning to load the DLL and the
31737 associated DLL debugging information. It is not possible to break at the
31738 beginning of the program because there is no @cite{GDB} debugging information,
31739 and therefore there is no direct way of getting initial control. This
31740 section addresses this issue by describing some methods that can be used
31741 to break somewhere in the DLL to debug it.
31743 First suppose that the main procedure is named @cite{main} (this is for
31744 example some C code built with Microsoft Visual C) and that there is a
31745 DLL named @cite{test.dll} containing an Ada entry point named
31746 @cite{ada_dll}.
31748 The DLL (see @ref{27f,,Introduction to Dynamic Link Libraries (DLLs)}) must have
31749 been built with debugging information (see GNAT @cite{-g} option).
31751 @subsubheading Debugging the DLL Directly
31755 @itemize *
31757 @item 
31758 Find out the executable starting address
31760 @example
31761 $ objdump --file-header main.exe
31762 @end example
31764 The starting address is reported on the last line. For example:
31766 @example
31767 main.exe:     file format pei-i386
31768 architecture: i386, flags 0x0000010a:
31769 EXEC_P, HAS_DEBUG, D_PAGED
31770 start address 0x00401010
31771 @end example
31773 @item 
31774 Launch the debugger on the executable.
31776 @example
31777 $ gdb main.exe
31778 @end example
31780 @item 
31781 Set a breakpoint at the starting address, and launch the program.
31783 @example
31784 $ (gdb) break *0x00401010
31785 $ (gdb) run
31786 @end example
31788 The program will stop at the given address.
31790 @item 
31791 Set a breakpoint on a DLL subroutine.
31793 @example
31794 (gdb) break ada_dll.adb:45
31795 @end example
31797 Or if you want to break using a symbol on the DLL, you need first to
31798 select the Ada language (language used by the DLL).
31800 @example
31801 (gdb) set language ada
31802 (gdb) break ada_dll
31803 @end example
31805 @item 
31806 Continue the program.
31808 @example
31809 (gdb) cont
31810 @end example
31812 This will run the program until it reaches the breakpoint that has been
31813 set. From that point you can use the standard way to debug a program
31814 as described in (@ref{26,,Running and Debugging Ada Programs}).
31815 @end itemize
31817 It is also possible to debug the DLL by attaching to a running process.
31819 @subsubheading Attaching to a Running Process
31822 @geindex DLL debugging
31823 @geindex attach to process
31825 With @cite{GDB} it is always possible to debug a running process by
31826 attaching to it. It is possible to debug a DLL this way. The limitation
31827 of this approach is that the DLL must run long enough to perform the
31828 attach operation. It may be useful for instance to insert a time wasting
31829 loop in the code of the DLL to meet this criterion.
31832 @itemize *
31834 @item 
31835 Launch the main program @code{main.exe}.
31837 @example
31838 $ main
31839 @end example
31841 @item 
31842 Use the Windows @emph{Task Manager} to find the process ID. Let's say
31843 that the process PID for @code{main.exe} is 208.
31845 @item 
31846 Launch gdb.
31848 @example
31849 $ gdb
31850 @end example
31852 @item 
31853 Attach to the running process to be debugged.
31855 @example
31856 (gdb) attach 208
31857 @end example
31859 @item 
31860 Load the process debugging information.
31862 @example
31863 (gdb) symbol-file main.exe
31864 @end example
31866 @item 
31867 Break somewhere in the DLL.
31869 @example
31870 (gdb) break ada_dll
31871 @end example
31873 @item 
31874 Continue process execution.
31876 @example
31877 (gdb) cont
31878 @end example
31879 @end itemize
31881 This last step will resume the process execution, and stop at
31882 the breakpoint we have set. From there you can use the standard
31883 approach to debug a program as described in
31884 @ref{26,,Running and Debugging Ada Programs}.
31886 @node Setting Stack Size from gnatlink,Setting Heap Size from gnatlink,Debugging a DLL,Mixed-Language Programming on Windows
31887 @anchor{gnat_ugn/platform_specific_information setting-stack-size-from-gnatlink}@anchor{13a}@anchor{gnat_ugn/platform_specific_information id40}@anchor{2a8}
31888 @subsubsection Setting Stack Size from @emph{gnatlink}
31891 It is possible to specify the program stack size at link time. On modern
31892 versions of Windows, starting with XP, this is mostly useful to set the size of
31893 the main stack (environment task). The other task stacks are set with pragma
31894 Storage_Size or with the @emph{gnatbind -d} command.
31896 Since older versions of Windows (2000, NT4, etc.) do not allow setting the
31897 reserve size of individual tasks, the link-time stack size applies to all
31898 tasks, and pragma Storage_Size has no effect.
31899 In particular, Stack Overflow checks are made against this
31900 link-time specified size.
31902 This setting can be done with @emph{gnatlink} using either of the following:
31905 @itemize *
31907 @item 
31908 @emph{-Xlinker} linker option
31910 @example
31911 $ gnatlink hello -Xlinker --stack=0x10000,0x1000
31912 @end example
31914 This sets the stack reserve size to 0x10000 bytes and the stack commit
31915 size to 0x1000 bytes.
31917 @item 
31918 @emph{-Wl} linker option
31920 @example
31921 $ gnatlink hello -Wl,--stack=0x1000000
31922 @end example
31924 This sets the stack reserve size to 0x1000000 bytes. Note that with
31925 @emph{-Wl} option it is not possible to set the stack commit size
31926 because the coma is a separator for this option.
31927 @end itemize
31929 @node Setting Heap Size from gnatlink,,Setting Stack Size from gnatlink,Mixed-Language Programming on Windows
31930 @anchor{gnat_ugn/platform_specific_information setting-heap-size-from-gnatlink}@anchor{13b}@anchor{gnat_ugn/platform_specific_information id41}@anchor{2a9}
31931 @subsubsection Setting Heap Size from @emph{gnatlink}
31934 Under Windows systems, it is possible to specify the program heap size from
31935 @emph{gnatlink} using either of the following:
31938 @itemize *
31940 @item 
31941 @emph{-Xlinker} linker option
31943 @example
31944 $ gnatlink hello -Xlinker --heap=0x10000,0x1000
31945 @end example
31947 This sets the heap reserve size to 0x10000 bytes and the heap commit
31948 size to 0x1000 bytes.
31950 @item 
31951 @emph{-Wl} linker option
31953 @example
31954 $ gnatlink hello -Wl,--heap=0x1000000
31955 @end example
31957 This sets the heap reserve size to 0x1000000 bytes. Note that with
31958 @emph{-Wl} option it is not possible to set the heap commit size
31959 because the coma is a separator for this option.
31960 @end itemize
31962 @node Mac OS Topics,,Microsoft Windows Topics,Platform-Specific Information
31963 @anchor{gnat_ugn/platform_specific_information mac-os-topics}@anchor{2f}@anchor{gnat_ugn/platform_specific_information id42}@anchor{2aa}
31964 @section Mac OS Topics
31967 @geindex OS X
31969 This section describes topics that are specific to Apple's OS X
31970 platform.
31972 @menu
31973 * Codesigning the Debugger:: 
31975 @end menu
31977 @node Codesigning the Debugger,,,Mac OS Topics
31978 @anchor{gnat_ugn/platform_specific_information codesigning-the-debugger}@anchor{2ab}
31979 @subsection Codesigning the Debugger
31982 The Darwin Kernel requires the debugger to have special permissions
31983 before it is allowed to control other processes. These permissions
31984 are granted by codesigning the GDB executable. Without these
31985 permissions, the debugger will report error messages such as:
31987 @example
31988 Starting program: /x/y/foo
31989 Unable to find Mach task port for process-id 28885: (os/kern) failure (0x5).
31990 (please check gdb is codesigned - see taskgated(8))
31991 @end example
31993 Codesigning requires a certificate.  The following procedure explains
31994 how to create one:
31997 @itemize *
31999 @item 
32000 Start the Keychain Access application (in
32001 /Applications/Utilities/Keychain Access.app)
32003 @item 
32004 Select the Keychain Access -> Certificate Assistant ->
32005 Create a Certificate... menu
32007 @item 
32008 Then:
32011 @itemize *
32013 @item 
32014 Choose a name for the new certificate (this procedure will use
32015 "gdb-cert" as an example)
32017 @item 
32018 Set "Identity Type" to "Self Signed Root"
32020 @item 
32021 Set "Certificate Type" to "Code Signing"
32023 @item 
32024 Activate the "Let me override defaults" option
32025 @end itemize
32027 @item 
32028 Click several times on "Continue" until the "Specify a Location
32029 For The Certificate" screen appears, then set "Keychain" to "System"
32031 @item 
32032 Click on "Continue" until the certificate is created
32034 @item 
32035 Finally, in the view, double-click on the new certificate,
32036 and set "When using this certificate" to "Always Trust"
32038 @item 
32039 Exit the Keychain Access application and restart the computer
32040 (this is unfortunately required)
32041 @end itemize
32043 Once a certificate has been created, the debugger can be codesigned
32044 as follow. In a Terminal, run the following command:
32046 @quotation
32048 @example
32049 $ codesign -f -s  "gdb-cert"  <gnat_install_prefix>/bin/gdb
32050 @end example
32051 @end quotation
32053 where "gdb-cert" should be replaced by the actual certificate
32054 name chosen above, and <gnat_install_prefix> should be replaced by
32055 the location where you installed GNAT.  Also, be sure that users are
32056 in the Unix group @code{_developer}.
32058 @node Example of Binder Output File,Elaboration Order Handling in GNAT,Platform-Specific Information,Top
32059 @anchor{gnat_ugn/example_of_binder_output example-of-binder-output-file}@anchor{10}@anchor{gnat_ugn/example_of_binder_output doc}@anchor{2ac}@anchor{gnat_ugn/example_of_binder_output id1}@anchor{2ad}
32060 @chapter Example of Binder Output File
32063 @geindex Binder output (example)
32065 This Appendix displays the source code for the output file
32066 generated by @emph{gnatbind} for a simple 'Hello World' program.
32067 Comments have been added for clarification purposes.
32069 @example
32070 --  The package is called Ada_Main unless this name is actually used
32071 --  as a unit name in the partition, in which case some other unique
32072 --  name is used.
32074 pragma Ada_95;
32075 with System;
32076 package ada_main is
32077    pragma Warnings (Off);
32079    --  The main program saves the parameters (argument count,
32080    --  argument values, environment pointer) in global variables
32081    --  for later access by other units including
32082    --  Ada.Command_Line.
32084    gnat_argc : Integer;
32085    gnat_argv : System.Address;
32086    gnat_envp : System.Address;
32088    --  The actual variables are stored in a library routine. This
32089    --  is useful for some shared library situations, where there
32090    --  are problems if variables are not in the library.
32092    pragma Import (C, gnat_argc);
32093    pragma Import (C, gnat_argv);
32094    pragma Import (C, gnat_envp);
32096    --  The exit status is similarly an external location
32098    gnat_exit_status : Integer;
32099    pragma Import (C, gnat_exit_status);
32101    GNAT_Version : constant String :=
32102                     "GNAT Version: Pro 7.4.0w (20141119-49)" & ASCII.NUL;
32103    pragma Export (C, GNAT_Version, "__gnat_version");
32105    Ada_Main_Program_Name : constant String := "_ada_hello" & ASCII.NUL;
32106    pragma Export (C, Ada_Main_Program_Name, "__gnat_ada_main_program_name");
32108    --  This is the generated adainit routine that performs
32109    --  initialization at the start of execution. In the case
32110    --  where Ada is the main program, this main program makes
32111    --  a call to adainit at program startup.
32113    procedure adainit;
32114    pragma Export (C, adainit, "adainit");
32116    --  This is the generated adafinal routine that performs
32117    --  finalization at the end of execution. In the case where
32118    --  Ada is the main program, this main program makes a call
32119    --  to adafinal at program termination.
32121    procedure adafinal;
32122    pragma Export (C, adafinal, "adafinal");
32124    --  This routine is called at the start of execution. It is
32125    --  a dummy routine that is used by the debugger to breakpoint
32126    --  at the start of execution.
32128    --  This is the actual generated main program (it would be
32129    --  suppressed if the no main program switch were used). As
32130    --  required by standard system conventions, this program has
32131    --  the external name main.
32133    function main
32134      (argc : Integer;
32135       argv : System.Address;
32136       envp : System.Address)
32137       return Integer;
32138    pragma Export (C, main, "main");
32140    --  The following set of constants give the version
32141    --  identification values for every unit in the bound
32142    --  partition. This identification is computed from all
32143    --  dependent semantic units, and corresponds to the
32144    --  string that would be returned by use of the
32145    --  Body_Version or Version attributes.
32147    --  The following Export pragmas export the version numbers
32148    --  with symbolic names ending in B (for body) or S
32149    --  (for spec) so that they can be located in a link. The
32150    --  information provided here is sufficient to track down
32151    --  the exact versions of units used in a given build.
32153    type Version_32 is mod 2 ** 32;
32154    u00001 : constant Version_32 := 16#8ad6e54a#;
32155    pragma Export (C, u00001, "helloB");
32156    u00002 : constant Version_32 := 16#fbff4c67#;
32157    pragma Export (C, u00002, "system__standard_libraryB");
32158    u00003 : constant Version_32 := 16#1ec6fd90#;
32159    pragma Export (C, u00003, "system__standard_libraryS");
32160    u00004 : constant Version_32 := 16#3ffc8e18#;
32161    pragma Export (C, u00004, "adaS");
32162    u00005 : constant Version_32 := 16#28f088c2#;
32163    pragma Export (C, u00005, "ada__text_ioB");
32164    u00006 : constant Version_32 := 16#f372c8ac#;
32165    pragma Export (C, u00006, "ada__text_ioS");
32166    u00007 : constant Version_32 := 16#2c143749#;
32167    pragma Export (C, u00007, "ada__exceptionsB");
32168    u00008 : constant Version_32 := 16#f4f0cce8#;
32169    pragma Export (C, u00008, "ada__exceptionsS");
32170    u00009 : constant Version_32 := 16#a46739c0#;
32171    pragma Export (C, u00009, "ada__exceptions__last_chance_handlerB");
32172    u00010 : constant Version_32 := 16#3aac8c92#;
32173    pragma Export (C, u00010, "ada__exceptions__last_chance_handlerS");
32174    u00011 : constant Version_32 := 16#1d274481#;
32175    pragma Export (C, u00011, "systemS");
32176    u00012 : constant Version_32 := 16#a207fefe#;
32177    pragma Export (C, u00012, "system__soft_linksB");
32178    u00013 : constant Version_32 := 16#467d9556#;
32179    pragma Export (C, u00013, "system__soft_linksS");
32180    u00014 : constant Version_32 := 16#b01dad17#;
32181    pragma Export (C, u00014, "system__parametersB");
32182    u00015 : constant Version_32 := 16#630d49fe#;
32183    pragma Export (C, u00015, "system__parametersS");
32184    u00016 : constant Version_32 := 16#b19b6653#;
32185    pragma Export (C, u00016, "system__secondary_stackB");
32186    u00017 : constant Version_32 := 16#b6468be8#;
32187    pragma Export (C, u00017, "system__secondary_stackS");
32188    u00018 : constant Version_32 := 16#39a03df9#;
32189    pragma Export (C, u00018, "system__storage_elementsB");
32190    u00019 : constant Version_32 := 16#30e40e85#;
32191    pragma Export (C, u00019, "system__storage_elementsS");
32192    u00020 : constant Version_32 := 16#41837d1e#;
32193    pragma Export (C, u00020, "system__stack_checkingB");
32194    u00021 : constant Version_32 := 16#93982f69#;
32195    pragma Export (C, u00021, "system__stack_checkingS");
32196    u00022 : constant Version_32 := 16#393398c1#;
32197    pragma Export (C, u00022, "system__exception_tableB");
32198    u00023 : constant Version_32 := 16#b33e2294#;
32199    pragma Export (C, u00023, "system__exception_tableS");
32200    u00024 : constant Version_32 := 16#ce4af020#;
32201    pragma Export (C, u00024, "system__exceptionsB");
32202    u00025 : constant Version_32 := 16#75442977#;
32203    pragma Export (C, u00025, "system__exceptionsS");
32204    u00026 : constant Version_32 := 16#37d758f1#;
32205    pragma Export (C, u00026, "system__exceptions__machineS");
32206    u00027 : constant Version_32 := 16#b895431d#;
32207    pragma Export (C, u00027, "system__exceptions_debugB");
32208    u00028 : constant Version_32 := 16#aec55d3f#;
32209    pragma Export (C, u00028, "system__exceptions_debugS");
32210    u00029 : constant Version_32 := 16#570325c8#;
32211    pragma Export (C, u00029, "system__img_intB");
32212    u00030 : constant Version_32 := 16#1ffca443#;
32213    pragma Export (C, u00030, "system__img_intS");
32214    u00031 : constant Version_32 := 16#b98c3e16#;
32215    pragma Export (C, u00031, "system__tracebackB");
32216    u00032 : constant Version_32 := 16#831a9d5a#;
32217    pragma Export (C, u00032, "system__tracebackS");
32218    u00033 : constant Version_32 := 16#9ed49525#;
32219    pragma Export (C, u00033, "system__traceback_entriesB");
32220    u00034 : constant Version_32 := 16#1d7cb2f1#;
32221    pragma Export (C, u00034, "system__traceback_entriesS");
32222    u00035 : constant Version_32 := 16#8c33a517#;
32223    pragma Export (C, u00035, "system__wch_conB");
32224    u00036 : constant Version_32 := 16#065a6653#;
32225    pragma Export (C, u00036, "system__wch_conS");
32226    u00037 : constant Version_32 := 16#9721e840#;
32227    pragma Export (C, u00037, "system__wch_stwB");
32228    u00038 : constant Version_32 := 16#2b4b4a52#;
32229    pragma Export (C, u00038, "system__wch_stwS");
32230    u00039 : constant Version_32 := 16#92b797cb#;
32231    pragma Export (C, u00039, "system__wch_cnvB");
32232    u00040 : constant Version_32 := 16#09eddca0#;
32233    pragma Export (C, u00040, "system__wch_cnvS");
32234    u00041 : constant Version_32 := 16#6033a23f#;
32235    pragma Export (C, u00041, "interfacesS");
32236    u00042 : constant Version_32 := 16#ece6fdb6#;
32237    pragma Export (C, u00042, "system__wch_jisB");
32238    u00043 : constant Version_32 := 16#899dc581#;
32239    pragma Export (C, u00043, "system__wch_jisS");
32240    u00044 : constant Version_32 := 16#10558b11#;
32241    pragma Export (C, u00044, "ada__streamsB");
32242    u00045 : constant Version_32 := 16#2e6701ab#;
32243    pragma Export (C, u00045, "ada__streamsS");
32244    u00046 : constant Version_32 := 16#db5c917c#;
32245    pragma Export (C, u00046, "ada__io_exceptionsS");
32246    u00047 : constant Version_32 := 16#12c8cd7d#;
32247    pragma Export (C, u00047, "ada__tagsB");
32248    u00048 : constant Version_32 := 16#ce72c228#;
32249    pragma Export (C, u00048, "ada__tagsS");
32250    u00049 : constant Version_32 := 16#c3335bfd#;
32251    pragma Export (C, u00049, "system__htableB");
32252    u00050 : constant Version_32 := 16#99e5f76b#;
32253    pragma Export (C, u00050, "system__htableS");
32254    u00051 : constant Version_32 := 16#089f5cd0#;
32255    pragma Export (C, u00051, "system__string_hashB");
32256    u00052 : constant Version_32 := 16#3bbb9c15#;
32257    pragma Export (C, u00052, "system__string_hashS");
32258    u00053 : constant Version_32 := 16#807fe041#;
32259    pragma Export (C, u00053, "system__unsigned_typesS");
32260    u00054 : constant Version_32 := 16#d27be59e#;
32261    pragma Export (C, u00054, "system__val_lluB");
32262    u00055 : constant Version_32 := 16#fa8db733#;
32263    pragma Export (C, u00055, "system__val_lluS");
32264    u00056 : constant Version_32 := 16#27b600b2#;
32265    pragma Export (C, u00056, "system__val_utilB");
32266    u00057 : constant Version_32 := 16#b187f27f#;
32267    pragma Export (C, u00057, "system__val_utilS");
32268    u00058 : constant Version_32 := 16#d1060688#;
32269    pragma Export (C, u00058, "system__case_utilB");
32270    u00059 : constant Version_32 := 16#392e2d56#;
32271    pragma Export (C, u00059, "system__case_utilS");
32272    u00060 : constant Version_32 := 16#84a27f0d#;
32273    pragma Export (C, u00060, "interfaces__c_streamsB");
32274    u00061 : constant Version_32 := 16#8bb5f2c0#;
32275    pragma Export (C, u00061, "interfaces__c_streamsS");
32276    u00062 : constant Version_32 := 16#6db6928f#;
32277    pragma Export (C, u00062, "system__crtlS");
32278    u00063 : constant Version_32 := 16#4e6a342b#;
32279    pragma Export (C, u00063, "system__file_ioB");
32280    u00064 : constant Version_32 := 16#ba56a5e4#;
32281    pragma Export (C, u00064, "system__file_ioS");
32282    u00065 : constant Version_32 := 16#b7ab275c#;
32283    pragma Export (C, u00065, "ada__finalizationB");
32284    u00066 : constant Version_32 := 16#19f764ca#;
32285    pragma Export (C, u00066, "ada__finalizationS");
32286    u00067 : constant Version_32 := 16#95817ed8#;
32287    pragma Export (C, u00067, "system__finalization_rootB");
32288    u00068 : constant Version_32 := 16#52d53711#;
32289    pragma Export (C, u00068, "system__finalization_rootS");
32290    u00069 : constant Version_32 := 16#769e25e6#;
32291    pragma Export (C, u00069, "interfaces__cB");
32292    u00070 : constant Version_32 := 16#4a38bedb#;
32293    pragma Export (C, u00070, "interfaces__cS");
32294    u00071 : constant Version_32 := 16#07e6ee66#;
32295    pragma Export (C, u00071, "system__os_libB");
32296    u00072 : constant Version_32 := 16#d7b69782#;
32297    pragma Export (C, u00072, "system__os_libS");
32298    u00073 : constant Version_32 := 16#1a817b8e#;
32299    pragma Export (C, u00073, "system__stringsB");
32300    u00074 : constant Version_32 := 16#639855e7#;
32301    pragma Export (C, u00074, "system__stringsS");
32302    u00075 : constant Version_32 := 16#e0b8de29#;
32303    pragma Export (C, u00075, "system__file_control_blockS");
32304    u00076 : constant Version_32 := 16#b5b2aca1#;
32305    pragma Export (C, u00076, "system__finalization_mastersB");
32306    u00077 : constant Version_32 := 16#69316dc1#;
32307    pragma Export (C, u00077, "system__finalization_mastersS");
32308    u00078 : constant Version_32 := 16#57a37a42#;
32309    pragma Export (C, u00078, "system__address_imageB");
32310    u00079 : constant Version_32 := 16#bccbd9bb#;
32311    pragma Export (C, u00079, "system__address_imageS");
32312    u00080 : constant Version_32 := 16#7268f812#;
32313    pragma Export (C, u00080, "system__img_boolB");
32314    u00081 : constant Version_32 := 16#e8fe356a#;
32315    pragma Export (C, u00081, "system__img_boolS");
32316    u00082 : constant Version_32 := 16#d7aac20c#;
32317    pragma Export (C, u00082, "system__ioB");
32318    u00083 : constant Version_32 := 16#8365b3ce#;
32319    pragma Export (C, u00083, "system__ioS");
32320    u00084 : constant Version_32 := 16#6d4d969a#;
32321    pragma Export (C, u00084, "system__storage_poolsB");
32322    u00085 : constant Version_32 := 16#e87cc305#;
32323    pragma Export (C, u00085, "system__storage_poolsS");
32324    u00086 : constant Version_32 := 16#e34550ca#;
32325    pragma Export (C, u00086, "system__pool_globalB");
32326    u00087 : constant Version_32 := 16#c88d2d16#;
32327    pragma Export (C, u00087, "system__pool_globalS");
32328    u00088 : constant Version_32 := 16#9d39c675#;
32329    pragma Export (C, u00088, "system__memoryB");
32330    u00089 : constant Version_32 := 16#445a22b5#;
32331    pragma Export (C, u00089, "system__memoryS");
32332    u00090 : constant Version_32 := 16#6a859064#;
32333    pragma Export (C, u00090, "system__storage_pools__subpoolsB");
32334    u00091 : constant Version_32 := 16#e3b008dc#;
32335    pragma Export (C, u00091, "system__storage_pools__subpoolsS");
32336    u00092 : constant Version_32 := 16#63f11652#;
32337    pragma Export (C, u00092, "system__storage_pools__subpools__finalizationB");
32338    u00093 : constant Version_32 := 16#fe2f4b3a#;
32339    pragma Export (C, u00093, "system__storage_pools__subpools__finalizationS");
32341    --  BEGIN ELABORATION ORDER
32342    --  ada%s
32343    --  interfaces%s
32344    --  system%s
32345    --  system.case_util%s
32346    --  system.case_util%b
32347    --  system.htable%s
32348    --  system.img_bool%s
32349    --  system.img_bool%b
32350    --  system.img_int%s
32351    --  system.img_int%b
32352    --  system.io%s
32353    --  system.io%b
32354    --  system.parameters%s
32355    --  system.parameters%b
32356    --  system.crtl%s
32357    --  interfaces.c_streams%s
32358    --  interfaces.c_streams%b
32359    --  system.standard_library%s
32360    --  system.exceptions_debug%s
32361    --  system.exceptions_debug%b
32362    --  system.storage_elements%s
32363    --  system.storage_elements%b
32364    --  system.stack_checking%s
32365    --  system.stack_checking%b
32366    --  system.string_hash%s
32367    --  system.string_hash%b
32368    --  system.htable%b
32369    --  system.strings%s
32370    --  system.strings%b
32371    --  system.os_lib%s
32372    --  system.traceback_entries%s
32373    --  system.traceback_entries%b
32374    --  ada.exceptions%s
32375    --  system.soft_links%s
32376    --  system.unsigned_types%s
32377    --  system.val_llu%s
32378    --  system.val_util%s
32379    --  system.val_util%b
32380    --  system.val_llu%b
32381    --  system.wch_con%s
32382    --  system.wch_con%b
32383    --  system.wch_cnv%s
32384    --  system.wch_jis%s
32385    --  system.wch_jis%b
32386    --  system.wch_cnv%b
32387    --  system.wch_stw%s
32388    --  system.wch_stw%b
32389    --  ada.exceptions.last_chance_handler%s
32390    --  ada.exceptions.last_chance_handler%b
32391    --  system.address_image%s
32392    --  system.exception_table%s
32393    --  system.exception_table%b
32394    --  ada.io_exceptions%s
32395    --  ada.tags%s
32396    --  ada.streams%s
32397    --  ada.streams%b
32398    --  interfaces.c%s
32399    --  system.exceptions%s
32400    --  system.exceptions%b
32401    --  system.exceptions.machine%s
32402    --  system.finalization_root%s
32403    --  system.finalization_root%b
32404    --  ada.finalization%s
32405    --  ada.finalization%b
32406    --  system.storage_pools%s
32407    --  system.storage_pools%b
32408    --  system.finalization_masters%s
32409    --  system.storage_pools.subpools%s
32410    --  system.storage_pools.subpools.finalization%s
32411    --  system.storage_pools.subpools.finalization%b
32412    --  system.memory%s
32413    --  system.memory%b
32414    --  system.standard_library%b
32415    --  system.pool_global%s
32416    --  system.pool_global%b
32417    --  system.file_control_block%s
32418    --  system.file_io%s
32419    --  system.secondary_stack%s
32420    --  system.file_io%b
32421    --  system.storage_pools.subpools%b
32422    --  system.finalization_masters%b
32423    --  interfaces.c%b
32424    --  ada.tags%b
32425    --  system.soft_links%b
32426    --  system.os_lib%b
32427    --  system.secondary_stack%b
32428    --  system.address_image%b
32429    --  system.traceback%s
32430    --  ada.exceptions%b
32431    --  system.traceback%b
32432    --  ada.text_io%s
32433    --  ada.text_io%b
32434    --  hello%b
32435    --  END ELABORATION ORDER
32437 end ada_main;
32438 @end example
32440 @example
32441 pragma Ada_95;
32442 --  The following source file name pragmas allow the generated file
32443 --  names to be unique for different main programs. They are needed
32444 --  since the package name will always be Ada_Main.
32446 pragma Source_File_Name (ada_main, Spec_File_Name => "b~hello.ads");
32447 pragma Source_File_Name (ada_main, Body_File_Name => "b~hello.adb");
32449 pragma Suppress (Overflow_Check);
32450 with Ada.Exceptions;
32452 --  Generated package body for Ada_Main starts here
32454 package body ada_main is
32455    pragma Warnings (Off);
32457    --  These values are reference counter associated to units which have
32458    --  been elaborated. It is also used to avoid elaborating the
32459    --  same unit twice.
32461    E72 : Short_Integer; pragma Import (Ada, E72, "system__os_lib_E");
32462    E13 : Short_Integer; pragma Import (Ada, E13, "system__soft_links_E");
32463    E23 : Short_Integer; pragma Import (Ada, E23, "system__exception_table_E");
32464    E46 : Short_Integer; pragma Import (Ada, E46, "ada__io_exceptions_E");
32465    E48 : Short_Integer; pragma Import (Ada, E48, "ada__tags_E");
32466    E45 : Short_Integer; pragma Import (Ada, E45, "ada__streams_E");
32467    E70 : Short_Integer; pragma Import (Ada, E70, "interfaces__c_E");
32468    E25 : Short_Integer; pragma Import (Ada, E25, "system__exceptions_E");
32469    E68 : Short_Integer; pragma Import (Ada, E68, "system__finalization_root_E");
32470    E66 : Short_Integer; pragma Import (Ada, E66, "ada__finalization_E");
32471    E85 : Short_Integer; pragma Import (Ada, E85, "system__storage_pools_E");
32472    E77 : Short_Integer; pragma Import (Ada, E77, "system__finalization_masters_E");
32473    E91 : Short_Integer; pragma Import (Ada, E91, "system__storage_pools__subpools_E");
32474    E87 : Short_Integer; pragma Import (Ada, E87, "system__pool_global_E");
32475    E75 : Short_Integer; pragma Import (Ada, E75, "system__file_control_block_E");
32476    E64 : Short_Integer; pragma Import (Ada, E64, "system__file_io_E");
32477    E17 : Short_Integer; pragma Import (Ada, E17, "system__secondary_stack_E");
32478    E06 : Short_Integer; pragma Import (Ada, E06, "ada__text_io_E");
32480    Local_Priority_Specific_Dispatching : constant String := "";
32481    Local_Interrupt_States : constant String := "";
32483    Is_Elaborated : Boolean := False;
32485    procedure finalize_library is
32486    begin
32487       E06 := E06 - 1;
32488       declare
32489          procedure F1;
32490          pragma Import (Ada, F1, "ada__text_io__finalize_spec");
32491       begin
32492          F1;
32493       end;
32494       E77 := E77 - 1;
32495       E91 := E91 - 1;
32496       declare
32497          procedure F2;
32498          pragma Import (Ada, F2, "system__file_io__finalize_body");
32499       begin
32500          E64 := E64 - 1;
32501          F2;
32502       end;
32503       declare
32504          procedure F3;
32505          pragma Import (Ada, F3, "system__file_control_block__finalize_spec");
32506       begin
32507          E75 := E75 - 1;
32508          F3;
32509       end;
32510       E87 := E87 - 1;
32511       declare
32512          procedure F4;
32513          pragma Import (Ada, F4, "system__pool_global__finalize_spec");
32514       begin
32515          F4;
32516       end;
32517       declare
32518          procedure F5;
32519          pragma Import (Ada, F5, "system__storage_pools__subpools__finalize_spec");
32520       begin
32521          F5;
32522       end;
32523       declare
32524          procedure F6;
32525          pragma Import (Ada, F6, "system__finalization_masters__finalize_spec");
32526       begin
32527          F6;
32528       end;
32529       declare
32530          procedure Reraise_Library_Exception_If_Any;
32531          pragma Import (Ada, Reraise_Library_Exception_If_Any, "__gnat_reraise_library_exception_if_any");
32532       begin
32533          Reraise_Library_Exception_If_Any;
32534       end;
32535    end finalize_library;
32537    -------------
32538    -- adainit --
32539    -------------
32541    procedure adainit is
32543       Main_Priority : Integer;
32544       pragma Import (C, Main_Priority, "__gl_main_priority");
32545       Time_Slice_Value : Integer;
32546       pragma Import (C, Time_Slice_Value, "__gl_time_slice_val");
32547       WC_Encoding : Character;
32548       pragma Import (C, WC_Encoding, "__gl_wc_encoding");
32549       Locking_Policy : Character;
32550       pragma Import (C, Locking_Policy, "__gl_locking_policy");
32551       Queuing_Policy : Character;
32552       pragma Import (C, Queuing_Policy, "__gl_queuing_policy");
32553       Task_Dispatching_Policy : Character;
32554       pragma Import (C, Task_Dispatching_Policy, "__gl_task_dispatching_policy");
32555       Priority_Specific_Dispatching : System.Address;
32556       pragma Import (C, Priority_Specific_Dispatching, "__gl_priority_specific_dispatching");
32557       Num_Specific_Dispatching : Integer;
32558       pragma Import (C, Num_Specific_Dispatching, "__gl_num_specific_dispatching");
32559       Main_CPU : Integer;
32560       pragma Import (C, Main_CPU, "__gl_main_cpu");
32561       Interrupt_States : System.Address;
32562       pragma Import (C, Interrupt_States, "__gl_interrupt_states");
32563       Num_Interrupt_States : Integer;
32564       pragma Import (C, Num_Interrupt_States, "__gl_num_interrupt_states");
32565       Unreserve_All_Interrupts : Integer;
32566       pragma Import (C, Unreserve_All_Interrupts, "__gl_unreserve_all_interrupts");
32567       Detect_Blocking : Integer;
32568       pragma Import (C, Detect_Blocking, "__gl_detect_blocking");
32569       Default_Stack_Size : Integer;
32570       pragma Import (C, Default_Stack_Size, "__gl_default_stack_size");
32571       Leap_Seconds_Support : Integer;
32572       pragma Import (C, Leap_Seconds_Support, "__gl_leap_seconds_support");
32574       procedure Runtime_Initialize;
32575       pragma Import (C, Runtime_Initialize, "__gnat_runtime_initialize");
32577       Finalize_Library_Objects : No_Param_Proc;
32578       pragma Import (C, Finalize_Library_Objects, "__gnat_finalize_library_objects");
32580    --  Start of processing for adainit
32582    begin
32584       --  Record various information for this partition.  The values
32585       --  are derived by the binder from information stored in the ali
32586       --  files by the compiler.
32588       if Is_Elaborated then
32589          return;
32590       end if;
32591       Is_Elaborated := True;
32592       Main_Priority := -1;
32593       Time_Slice_Value := -1;
32594       WC_Encoding := 'b';
32595       Locking_Policy := ' ';
32596       Queuing_Policy := ' ';
32597       Task_Dispatching_Policy := ' ';
32598       Priority_Specific_Dispatching :=
32599         Local_Priority_Specific_Dispatching'Address;
32600       Num_Specific_Dispatching := 0;
32601       Main_CPU := -1;
32602       Interrupt_States := Local_Interrupt_States'Address;
32603       Num_Interrupt_States := 0;
32604       Unreserve_All_Interrupts := 0;
32605       Detect_Blocking := 0;
32606       Default_Stack_Size := -1;
32607       Leap_Seconds_Support := 0;
32609       Runtime_Initialize;
32611       Finalize_Library_Objects := finalize_library'access;
32613       --  Now we have the elaboration calls for all units in the partition.
32614       --  The Elab_Spec and Elab_Body attributes generate references to the
32615       --  implicit elaboration procedures generated by the compiler for
32616       --  each unit that requires elaboration. Increment a counter of
32617       --  reference for each unit.
32619       System.Soft_Links'Elab_Spec;
32620       System.Exception_Table'Elab_Body;
32621       E23 := E23 + 1;
32622       Ada.Io_Exceptions'Elab_Spec;
32623       E46 := E46 + 1;
32624       Ada.Tags'Elab_Spec;
32625       Ada.Streams'Elab_Spec;
32626       E45 := E45 + 1;
32627       Interfaces.C'Elab_Spec;
32628       System.Exceptions'Elab_Spec;
32629       E25 := E25 + 1;
32630       System.Finalization_Root'Elab_Spec;
32631       E68 := E68 + 1;
32632       Ada.Finalization'Elab_Spec;
32633       E66 := E66 + 1;
32634       System.Storage_Pools'Elab_Spec;
32635       E85 := E85 + 1;
32636       System.Finalization_Masters'Elab_Spec;
32637       System.Storage_Pools.Subpools'Elab_Spec;
32638       System.Pool_Global'Elab_Spec;
32639       E87 := E87 + 1;
32640       System.File_Control_Block'Elab_Spec;
32641       E75 := E75 + 1;
32642       System.File_Io'Elab_Body;
32643       E64 := E64 + 1;
32644       E91 := E91 + 1;
32645       System.Finalization_Masters'Elab_Body;
32646       E77 := E77 + 1;
32647       E70 := E70 + 1;
32648       Ada.Tags'Elab_Body;
32649       E48 := E48 + 1;
32650       System.Soft_Links'Elab_Body;
32651       E13 := E13 + 1;
32652       System.Os_Lib'Elab_Body;
32653       E72 := E72 + 1;
32654       System.Secondary_Stack'Elab_Body;
32655       E17 := E17 + 1;
32656       Ada.Text_Io'Elab_Spec;
32657       Ada.Text_Io'Elab_Body;
32658       E06 := E06 + 1;
32659    end adainit;
32661    --------------
32662    -- adafinal --
32663    --------------
32665    procedure adafinal is
32666       procedure s_stalib_adafinal;
32667       pragma Import (C, s_stalib_adafinal, "system__standard_library__adafinal");
32669       procedure Runtime_Finalize;
32670       pragma Import (C, Runtime_Finalize, "__gnat_runtime_finalize");
32672    begin
32673       if not Is_Elaborated then
32674          return;
32675       end if;
32676       Is_Elaborated := False;
32677       Runtime_Finalize;
32678       s_stalib_adafinal;
32679    end adafinal;
32681    --  We get to the main program of the partition by using
32682    --  pragma Import because if we try to with the unit and
32683    --  call it Ada style, then not only do we waste time
32684    --  recompiling it, but also, we don't really know the right
32685    --  switches (e.g.@@: identifier character set) to be used
32686    --  to compile it.
32688    procedure Ada_Main_Program;
32689    pragma Import (Ada, Ada_Main_Program, "_ada_hello");
32691    ----------
32692    -- main --
32693    ----------
32695    --  main is actually a function, as in the ANSI C standard,
32696    --  defined to return the exit status. The three parameters
32697    --  are the argument count, argument values and environment
32698    --  pointer.
32700    function main
32701      (argc : Integer;
32702       argv : System.Address;
32703       envp : System.Address)
32704       return Integer
32705    is
32706       --  The initialize routine performs low level system
32707       --  initialization using a standard library routine which
32708       --  sets up signal handling and performs any other
32709       --  required setup. The routine can be found in file
32710       --  a-init.c.
32712       procedure initialize;
32713       pragma Import (C, initialize, "__gnat_initialize");
32715       --  The finalize routine performs low level system
32716       --  finalization using a standard library routine. The
32717       --  routine is found in file a-final.c and in the standard
32718       --  distribution is a dummy routine that does nothing, so
32719       --  really this is a hook for special user finalization.
32721       procedure finalize;
32722       pragma Import (C, finalize, "__gnat_finalize");
32724       --  The following is to initialize the SEH exceptions
32726       SEH : aliased array (1 .. 2) of Integer;
32728       Ensure_Reference : aliased System.Address := Ada_Main_Program_Name'Address;
32729       pragma Volatile (Ensure_Reference);
32731    --  Start of processing for main
32733    begin
32734       --  Save global variables
32736       gnat_argc := argc;
32737       gnat_argv := argv;
32738       gnat_envp := envp;
32740       --  Call low level system initialization
32742       Initialize (SEH'Address);
32744       --  Call our generated Ada initialization routine
32746       adainit;
32748       --  Now we call the main program of the partition
32750       Ada_Main_Program;
32752       --  Perform Ada finalization
32754       adafinal;
32756       --  Perform low level system finalization
32758       Finalize;
32760       --  Return the proper exit status
32761       return (gnat_exit_status);
32762    end;
32764 --  This section is entirely comments, so it has no effect on the
32765 --  compilation of the Ada_Main package. It provides the list of
32766 --  object files and linker options, as well as some standard
32767 --  libraries needed for the link. The gnatlink utility parses
32768 --  this b~hello.adb file to read these comment lines to generate
32769 --  the appropriate command line arguments for the call to the
32770 --  system linker. The BEGIN/END lines are used for sentinels for
32771 --  this parsing operation.
32773 --  The exact file names will of course depend on the environment,
32774 --  host/target and location of files on the host system.
32776 -- BEGIN Object file/option list
32777    --   ./hello.o
32778    --   -L./
32779    --   -L/usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/
32780    --   /usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
32781 -- END Object file/option list
32783 end ada_main;
32784 @end example
32786 The Ada code in the above example is exactly what is generated by the
32787 binder. We have added comments to more clearly indicate the function
32788 of each part of the generated @cite{Ada_Main} package.
32790 The code is standard Ada in all respects, and can be processed by any
32791 tools that handle Ada. In particular, it is possible to use the debugger
32792 in Ada mode to debug the generated @cite{Ada_Main} package. For example,
32793 suppose that for reasons that you do not understand, your program is crashing
32794 during elaboration of the body of @cite{Ada.Text_IO}. To locate this bug,
32795 you can place a breakpoint on the call:
32797 @quotation
32799 @example
32800 Ada.Text_Io'Elab_Body;
32801 @end example
32802 @end quotation
32804 and trace the elaboration routine for this package to find out where
32805 the problem might be (more usually of course you would be debugging
32806 elaboration code in your own application).
32808 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
32810 @node Elaboration Order Handling in GNAT,Inline Assembler,Example of Binder Output File,Top
32811 @anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-order-handling-in-gnat}@anchor{11}@anchor{gnat_ugn/elaboration_order_handling_in_gnat doc}@anchor{2ae}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id1}@anchor{2af}
32812 @chapter Elaboration Order Handling in GNAT
32815 @geindex Order of elaboration
32817 @geindex Elaboration control
32819 This appendix describes the handling of elaboration code in Ada and
32820 in GNAT, and discusses how the order of elaboration of program units can
32821 be controlled in GNAT, either automatically or with explicit programming
32822 features.
32824 @menu
32825 * Elaboration Code:: 
32826 * Checking the Elaboration Order:: 
32827 * Controlling the Elaboration Order:: 
32828 * Controlling Elaboration in GNAT - Internal Calls:: 
32829 * Controlling Elaboration in GNAT - External Calls:: 
32830 * Default Behavior in GNAT - Ensuring Safety:: 
32831 * Treatment of Pragma Elaborate:: 
32832 * Elaboration Issues for Library Tasks:: 
32833 * Mixing Elaboration Models:: 
32834 * What to Do If the Default Elaboration Behavior Fails:: 
32835 * Elaboration for Indirect Calls:: 
32836 * Summary of Procedures for Elaboration Control:: 
32837 * Other Elaboration Order Considerations:: 
32838 * Determining the Chosen Elaboration Order:: 
32840 @end menu
32842 @node Elaboration Code,Checking the Elaboration Order,,Elaboration Order Handling in GNAT
32843 @anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-code}@anchor{2b0}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id2}@anchor{2b1}
32844 @section Elaboration Code
32847 Ada provides rather general mechanisms for executing code at elaboration
32848 time, that is to say before the main program starts executing. Such code arises
32849 in three contexts:
32852 @itemize *
32854 @item 
32855 @emph{Initializers for variables}
32857 Variables declared at the library level, in package specs or bodies, can
32858 require initialization that is performed at elaboration time, as in:
32860 @example
32861 Sqrt_Half : Float := Sqrt (0.5);
32862 @end example
32864 @item 
32865 @emph{Package initialization code}
32867 Code in a @cite{BEGIN-END} section at the outer level of a package body is
32868 executed as part of the package body elaboration code.
32870 @item 
32871 @emph{Library level task allocators}
32873 Tasks that are declared using task allocators at the library level
32874 start executing immediately and hence can execute at elaboration time.
32875 @end itemize
32877 Subprogram calls are possible in any of these contexts, which means that
32878 any arbitrary part of the program may be executed as part of the elaboration
32879 code. It is even possible to write a program which does all its work at
32880 elaboration time, with a null main program, although stylistically this
32881 would usually be considered an inappropriate way to structure
32882 a program.
32884 An important concern arises in the context of elaboration code:
32885 we have to be sure that it is executed in an appropriate order. What we
32886 have is a series of elaboration code sections, potentially one section
32887 for each unit in the program. It is important that these execute
32888 in the correct order. Correctness here means that, taking the above
32889 example of the declaration of @cite{Sqrt_Half},
32890 if some other piece of
32891 elaboration code references @cite{Sqrt_Half},
32892 then it must run after the
32893 section of elaboration code that contains the declaration of
32894 @cite{Sqrt_Half}.
32896 There would never be any order of elaboration problem if we made a rule
32897 that whenever you @emph{with} a unit, you must elaborate both the spec and body
32898 of that unit before elaborating the unit doing the @emph{with}ing:
32900 @example
32901 with Unit_1;
32902 package Unit_2 is ...
32903 @end example
32905 would require that both the body and spec of @cite{Unit_1} be elaborated
32906 before the spec of @cite{Unit_2}. However, a rule like that would be far too
32907 restrictive. In particular, it would make it impossible to have routines
32908 in separate packages that were mutually recursive.
32910 You might think that a clever enough compiler could look at the actual
32911 elaboration code and determine an appropriate correct order of elaboration,
32912 but in the general case, this is not possible. Consider the following
32913 example.
32915 In the body of @cite{Unit_1}, we have a procedure @cite{Func_1}
32916 that references
32917 the variable @cite{Sqrt_1}, which is declared in the elaboration code
32918 of the body of @cite{Unit_1}:
32920 @example
32921 Sqrt_1 : Float := Sqrt (0.1);
32922 @end example
32924 The elaboration code of the body of @cite{Unit_1} also contains:
32926 @example
32927 if expression_1 = 1 then
32928    Q := Unit_2.Func_2;
32929 end if;
32930 @end example
32932 @cite{Unit_2} is exactly parallel,
32933 it has a procedure @cite{Func_2} that references
32934 the variable @cite{Sqrt_2}, which is declared in the elaboration code of
32935 the body @cite{Unit_2}:
32937 @example
32938 Sqrt_2 : Float := Sqrt (0.1);
32939 @end example
32941 The elaboration code of the body of @cite{Unit_2} also contains:
32943 @example
32944 if expression_2 = 2 then
32945    Q := Unit_1.Func_1;
32946 end if;
32947 @end example
32949 Now the question is, which of the following orders of elaboration is
32950 acceptable:
32952 @example
32953 Spec of Unit_1
32954 Spec of Unit_2
32955 Body of Unit_1
32956 Body of Unit_2
32957 @end example
32961 @example
32962 Spec of Unit_2
32963 Spec of Unit_1
32964 Body of Unit_2
32965 Body of Unit_1
32966 @end example
32968 If you carefully analyze the flow here, you will see that you cannot tell
32969 at compile time the answer to this question.
32970 If @cite{expression_1} is not equal to 1,
32971 and @cite{expression_2} is not equal to 2,
32972 then either order is acceptable, because neither of the function calls is
32973 executed. If both tests evaluate to true, then neither order is acceptable
32974 and in fact there is no correct order.
32976 If one of the two expressions is true, and the other is false, then one
32977 of the above orders is correct, and the other is incorrect. For example,
32978 if @cite{expression_1} /= 1 and @cite{expression_2} = 2,
32979 then the call to @cite{Func_1}
32980 will occur, but not the call to @cite{Func_2.}
32981 This means that it is essential
32982 to elaborate the body of @cite{Unit_1} before
32983 the body of @cite{Unit_2}, so the first
32984 order of elaboration is correct and the second is wrong.
32986 By making @cite{expression_1} and @cite{expression_2}
32987 depend on input data, or perhaps
32988 the time of day, we can make it impossible for the compiler or binder
32989 to figure out which of these expressions will be true, and hence it
32990 is impossible to guarantee a safe order of elaboration at run time.
32992 @node Checking the Elaboration Order,Controlling the Elaboration Order,Elaboration Code,Elaboration Order Handling in GNAT
32993 @anchor{gnat_ugn/elaboration_order_handling_in_gnat checking-the-elaboration-order}@anchor{2b2}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id3}@anchor{2b3}
32994 @section Checking the Elaboration Order
32997 In some languages that involve the same kind of elaboration problems,
32998 e.g., Java and C++, the programmer needs to take these
32999 ordering problems into account, and it is common to
33000 write a program in which an incorrect elaboration order  gives
33001 surprising results, because it references variables before they
33002 are initialized.
33003 Ada is designed to be a safe language, and a programmer-beware approach is
33004 clearly not sufficient. Consequently, the language provides three lines
33005 of defense:
33008 @itemize *
33010 @item 
33011 @emph{Standard rules}
33013 Some standard rules restrict the possible choice of elaboration
33014 order. In particular, if you @emph{with} a unit, then its spec is always
33015 elaborated before the unit doing the @emph{with}. Similarly, a parent
33016 spec is always elaborated before the child spec, and finally
33017 a spec is always elaborated before its corresponding body.
33018 @end itemize
33020 @geindex Elaboration checks
33022 @geindex Checks
33023 @geindex elaboration
33026 @itemize *
33028 @item 
33029 @emph{Dynamic elaboration checks}
33031 Dynamic checks are made at run time, so that if some entity is accessed
33032 before it is elaborated (typically  by means of a subprogram call)
33033 then the exception (@cite{Program_Error}) is raised.
33035 @item 
33036 @emph{Elaboration control}
33038 Facilities are provided for the programmer to specify the desired order
33039 of elaboration.
33040 @end itemize
33042 Let's look at these facilities in more detail. First, the rules for
33043 dynamic checking. One possible rule would be simply to say that the
33044 exception is raised if you access a variable which has not yet been
33045 elaborated. The trouble with this approach is that it could require
33046 expensive checks on every variable reference. Instead Ada has two
33047 rules which are a little more restrictive, but easier to check, and
33048 easier to state:
33051 @itemize *
33053 @item 
33054 @emph{Restrictions on calls}
33056 A subprogram can only be called at elaboration time if its body
33057 has been elaborated. The rules for elaboration given above guarantee
33058 that the spec of the subprogram has been elaborated before the
33059 call, but not the body. If this rule is violated, then the
33060 exception @cite{Program_Error} is raised.
33062 @item 
33063 @emph{Restrictions on instantiations}
33065 A generic unit can only be instantiated if the body of the generic
33066 unit has been elaborated. Again, the rules for elaboration given above
33067 guarantee that the spec of the generic unit has been elaborated
33068 before the instantiation, but not the body. If this rule is
33069 violated, then the exception @cite{Program_Error} is raised.
33070 @end itemize
33072 The idea is that if the body has been elaborated, then any variables
33073 it references must have been elaborated; by checking for the body being
33074 elaborated we guarantee that none of its references causes any
33075 trouble. As we noted above, this is a little too restrictive, because a
33076 subprogram that has no non-local references in its body may in fact be safe
33077 to call. However, it really would be unsafe to rely on this, because
33078 it would mean that the caller was aware of details of the implementation
33079 in the body. This goes against the basic tenets of Ada.
33081 A plausible implementation can be described as follows.
33082 A Boolean variable is associated with each subprogram
33083 and each generic unit. This variable is initialized to False, and is set to
33084 True at the point body is elaborated. Every call or instantiation checks the
33085 variable, and raises @cite{Program_Error} if the variable is False.
33087 Note that one might think that it would be good enough to have one Boolean
33088 variable for each package, but that would not deal with cases of trying
33089 to call a body in the same package as the call
33090 that has not been elaborated yet.
33091 Of course a compiler may be able to do enough analysis to optimize away
33092 some of the Boolean variables as unnecessary, and @cite{GNAT} indeed
33093 does such optimizations, but still the easiest conceptual model is to
33094 think of there being one variable per subprogram.
33096 @node Controlling the Elaboration Order,Controlling Elaboration in GNAT - Internal Calls,Checking the Elaboration Order,Elaboration Order Handling in GNAT
33097 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id4}@anchor{2b4}@anchor{gnat_ugn/elaboration_order_handling_in_gnat controlling-the-elaboration-order}@anchor{2b5}
33098 @section Controlling the Elaboration Order
33101 In the previous section we discussed the rules in Ada which ensure
33102 that @cite{Program_Error} is raised if an incorrect elaboration order is
33103 chosen. This prevents erroneous executions, but we need mechanisms to
33104 specify a correct execution and avoid the exception altogether.
33105 To achieve this, Ada provides a number of features for controlling
33106 the order of elaboration. We discuss these features in this section.
33108 First, there are several ways of indicating to the compiler that a given
33109 unit has no elaboration problems:
33112 @itemize *
33114 @item 
33115 @emph{packages that do not require a body}
33117 A library package that does not require a body does not permit
33118 a body (this rule was introduced in Ada 95).
33119 Thus if we have a such a package, as in:
33121 @example
33122 package Definitions is
33123    generic
33124       type m is new integer;
33125    package Subp is
33126       type a is array (1 .. 10) of m;
33127       type b is array (1 .. 20) of m;
33128    end Subp;
33129 end Definitions;
33130 @end example
33132 A package that @emph{with}s @cite{Definitions} may safely instantiate
33133 @cite{Definitions.Subp} because the compiler can determine that there
33134 definitely is no package body to worry about in this case
33135 @end itemize
33137 @geindex pragma Pure
33140 @itemize *
33142 @item 
33143 @emph{pragma Pure}
33145 This pragma places sufficient restrictions on a unit to guarantee that
33146 no call to any subprogram in the unit can result in an
33147 elaboration problem. This means that the compiler does not need
33148 to worry about the point of elaboration of such units, and in
33149 particular, does not need to check any calls to any subprograms
33150 in this unit.
33151 @end itemize
33153 @geindex pragma Preelaborate
33156 @itemize *
33158 @item 
33159 @emph{pragma Preelaborate}
33161 This pragma places slightly less stringent restrictions on a unit than
33162 does pragma Pure,
33163 but these restrictions are still sufficient to ensure that there
33164 are no elaboration problems with any calls to the unit.
33165 @end itemize
33167 @geindex pragma Elaborate_Body
33170 @itemize *
33172 @item 
33173 @emph{pragma Elaborate_Body}
33175 This pragma requires that the body of a unit be elaborated immediately
33176 after its spec. Suppose a unit @cite{A} has such a pragma,
33177 and unit @cite{B} does
33178 a @emph{with} of unit @cite{A}. Recall that the standard rules require
33179 the spec of unit @cite{A}
33180 to be elaborated before the @emph{with}ing unit; given the pragma in
33181 @cite{A}, we also know that the body of @cite{A}
33182 will be elaborated before @cite{B}, so
33183 that calls to @cite{A} are safe and do not need a check.
33185 Note that, unlike pragma @cite{Pure} and pragma @cite{Preelaborate},
33186 the use of @cite{Elaborate_Body} does not guarantee that the program is
33187 free of elaboration problems, because it may not be possible
33188 to satisfy the requested elaboration order.
33189 Let's go back to the example with @cite{Unit_1} and @cite{Unit_2}.
33190 If a programmer marks @cite{Unit_1} as @cite{Elaborate_Body},
33191 and not @cite{Unit_2@comma{}} then the order of
33192 elaboration will be:
33194 @example
33195 Spec of Unit_2
33196 Spec of Unit_1
33197 Body of Unit_1
33198 Body of Unit_2
33199 @end example
33201 Now that means that the call to @cite{Func_1} in @cite{Unit_2}
33202 need not be checked,
33203 it must be safe. But the call to @cite{Func_2} in
33204 @cite{Unit_1} may still fail if
33205 @cite{Expression_1} is equal to 1,
33206 and the programmer must still take
33207 responsibility for this not being the case.
33209 If all units carry a pragma @cite{Elaborate_Body}, then all problems are
33210 eliminated, except for calls entirely within a body, which are
33211 in any case fully under programmer control. However, using the pragma
33212 everywhere is not always possible.
33213 In particular, for our @cite{Unit_1}/@cite{Unit_2} example, if
33214 we marked both of them as having pragma @cite{Elaborate_Body}, then
33215 clearly there would be no possible elaboration order.
33216 @end itemize
33218 The above pragmas allow a server to guarantee safe use by clients, and
33219 clearly this is the preferable approach. Consequently a good rule
33220 is to mark units as @cite{Pure} or @cite{Preelaborate} if possible,
33221 and if this is not possible,
33222 mark them as @cite{Elaborate_Body} if possible.
33223 As we have seen, there are situations where neither of these
33224 three pragmas can be used.
33225 So we also provide methods for clients to control the
33226 order of elaboration of the servers on which they depend:
33228 @geindex pragma Elaborate
33231 @itemize *
33233 @item 
33234 @emph{pragma Elaborate (unit)}
33236 This pragma is placed in the context clause, after a @emph{with} clause,
33237 and it requires that the body of the named unit be elaborated before
33238 the unit in which the pragma occurs. The idea is to use this pragma
33239 if the current unit calls at elaboration time, directly or indirectly,
33240 some subprogram in the named unit.
33241 @end itemize
33243 @geindex pragma Elaborate_All
33246 @itemize *
33248 @item 
33249 @emph{pragma Elaborate_All (unit)}
33251 This is a stronger version of the Elaborate pragma. Consider the
33252 following example:
33254 @example
33255 Unit A |withs| unit B and calls B.Func in elab code
33256 Unit B |withs| unit C, and B.Func calls C.Func
33257 @end example
33259 Now if we put a pragma @cite{Elaborate (B)}
33260 in unit @cite{A}, this ensures that the
33261 body of @cite{B} is elaborated before the call, but not the
33262 body of @cite{C}, so
33263 the call to @cite{C.Func} could still cause @cite{Program_Error} to
33264 be raised.
33266 The effect of a pragma @cite{Elaborate_All} is stronger, it requires
33267 not only that the body of the named unit be elaborated before the
33268 unit doing the @emph{with}, but also the bodies of all units that the
33269 named unit uses, following @emph{with} links transitively. For example,
33270 if we put a pragma @cite{Elaborate_All (B)} in unit @cite{A},
33271 then it requires not only that the body of @cite{B} be elaborated before @cite{A},
33272 but also the body of @cite{C}, because @cite{B} @emph{with}s @cite{C}.
33273 @end itemize
33275 We are now in a position to give a usage rule in Ada for avoiding
33276 elaboration problems, at least if dynamic dispatching and access to
33277 subprogram values are not used. We will handle these cases separately
33278 later.
33280 The rule is simple:
33282 @emph{If a unit has elaboration code that can directly or
33283 indirectly make a call to a subprogram in a |withed| unit, or instantiate
33284 a generic package in a |withed| unit,
33285 then if the |withed| unit does not have
33286 pragma `Pure` or `Preelaborate`, then the client should have
33287 a pragma `Elaborate_All`for the |withed| unit.*}
33289 By following this rule a client is
33290 assured that calls can be made without risk of an exception.
33292 For generic subprogram instantiations, the rule can be relaxed to
33293 require only a pragma @cite{Elaborate} since elaborating the body
33294 of a subprogram cannot cause any transitive elaboration (we are
33295 not calling the subprogram in this case, just elaborating its
33296 declaration).
33298 If this rule is not followed, then a program may be in one of four
33299 states:
33302 @itemize *
33304 @item 
33305 @emph{No order exists}
33307 No order of elaboration exists which follows the rules, taking into
33308 account any @cite{Elaborate}, @cite{Elaborate_All},
33309 or @cite{Elaborate_Body} pragmas. In
33310 this case, an Ada compiler must diagnose the situation at bind
33311 time, and refuse to build an executable program.
33313 @item 
33314 @emph{One or more orders exist, all incorrect}
33316 One or more acceptable elaboration orders exist, and all of them
33317 generate an elaboration order problem. In this case, the binder
33318 can build an executable program, but @cite{Program_Error} will be raised
33319 when the program is run.
33321 @item 
33322 @emph{Several orders exist, some right, some incorrect}
33324 One or more acceptable elaboration orders exists, and some of them
33325 work, and some do not. The programmer has not controlled
33326 the order of elaboration, so the binder may or may not pick one of
33327 the correct orders, and the program may or may not raise an
33328 exception when it is run. This is the worst case, because it means
33329 that the program may fail when moved to another compiler, or even
33330 another version of the same compiler.
33332 @item 
33333 @emph{One or more orders exists, all correct}
33335 One ore more acceptable elaboration orders exist, and all of them
33336 work. In this case the program runs successfully. This state of
33337 affairs can be guaranteed by following the rule we gave above, but
33338 may be true even if the rule is not followed.
33339 @end itemize
33341 Note that one additional advantage of following our rules on the use
33342 of @cite{Elaborate} and @cite{Elaborate_All}
33343 is that the program continues to stay in the ideal (all orders OK) state
33344 even if maintenance
33345 changes some bodies of some units. Conversely, if a program that does
33346 not follow this rule happens to be safe at some point, this state of affairs
33347 may deteriorate silently as a result of maintenance changes.
33349 You may have noticed that the above discussion did not mention
33350 the use of @cite{Elaborate_Body}. This was a deliberate omission. If you
33351 @emph{with} an @cite{Elaborate_Body} unit, it still may be the case that
33352 code in the body makes calls to some other unit, so it is still necessary
33353 to use @cite{Elaborate_All} on such units.
33355 @node Controlling Elaboration in GNAT - Internal Calls,Controlling Elaboration in GNAT - External Calls,Controlling the Elaboration Order,Elaboration Order Handling in GNAT
33356 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id5}@anchor{2b6}@anchor{gnat_ugn/elaboration_order_handling_in_gnat controlling-elaboration-in-gnat-internal-calls}@anchor{2b7}
33357 @section Controlling Elaboration in GNAT - Internal Calls
33360 In the case of internal calls, i.e., calls within a single package, the
33361 programmer has full control over the order of elaboration, and it is up
33362 to the programmer to elaborate declarations in an appropriate order. For
33363 example writing:
33365 @example
33366 function One return Float;
33368 Q : Float := One;
33370 function One return Float is
33371 begin
33372      return 1.0;
33373 end One;
33374 @end example
33376 will obviously raise @cite{Program_Error} at run time, because function
33377 One will be called before its body is elaborated. In this case GNAT will
33378 generate a warning that the call will raise @cite{Program_Error}:
33380 @example
33381  1. procedure y is
33382  2.    function One return Float;
33383  3.
33384  4.    Q : Float := One;
33385                     |
33386     >>> warning: cannot call "One" before body is elaborated
33387     >>> warning: Program_Error will be raised at run time
33389  5.
33390  6.    function One return Float is
33391  7.    begin
33392  8.         return 1.0;
33393  9.    end One;
33395 11. begin
33396 12.    null;
33397 13. end;
33398 @end example
33400 Note that in this particular case, it is likely that the call is safe, because
33401 the function @cite{One} does not access any global variables.
33402 Nevertheless in Ada, we do not want the validity of the check to depend on
33403 the contents of the body (think about the separate compilation case), so this
33404 is still wrong, as we discussed in the previous sections.
33406 The error is easily corrected by rearranging the declarations so that the
33407 body of @cite{One} appears before the declaration containing the call
33408 (note that in Ada 95 as well as later versions of the Ada standard,
33409 declarations can appear in any order, so there is no restriction that
33410 would prevent this reordering, and if we write:
33412 @example
33413 function One return Float;
33415 function One return Float is
33416 begin
33417      return 1.0;
33418 end One;
33420 Q : Float := One;
33421 @end example
33423 then all is well, no warning is generated, and no
33424 @cite{Program_Error} exception
33425 will be raised.
33426 Things are more complicated when a chain of subprograms is executed:
33428 @example
33429 function A return Integer;
33430 function B return Integer;
33431 function C return Integer;
33433 function B return Integer is begin return A; end;
33434 function C return Integer is begin return B; end;
33436 X : Integer := C;
33438 function A return Integer is begin return 1; end;
33439 @end example
33441 Now the call to @cite{C}
33442 at elaboration time in the declaration of @cite{X} is correct, because
33443 the body of @cite{C} is already elaborated,
33444 and the call to @cite{B} within the body of
33445 @cite{C} is correct, but the call
33446 to @cite{A} within the body of @cite{B} is incorrect, because the body
33447 of @cite{A} has not been elaborated, so @cite{Program_Error}
33448 will be raised on the call to @cite{A}.
33449 In this case GNAT will generate a
33450 warning that @cite{Program_Error} may be
33451 raised at the point of the call. Let's look at the warning:
33453 @example
33454  1. procedure x is
33455  2.    function A return Integer;
33456  3.    function B return Integer;
33457  4.    function C return Integer;
33458  5.
33459  6.    function B return Integer is begin return A; end;
33460                                                     |
33461     >>> warning: call to "A" before body is elaborated may
33462                  raise Program_Error
33463     >>> warning: "B" called at line 7
33464     >>> warning: "C" called at line 9
33466  7.    function C return Integer is begin return B; end;
33467  8.
33468  9.    X : Integer := C;
33470 11.    function A return Integer is begin return 1; end;
33472 13. begin
33473 14.    null;
33474 15. end;
33475 @end example
33477 Note that the message here says 'may raise', instead of the direct case,
33478 where the message says 'will be raised'. That's because whether
33479 @cite{A} is
33480 actually called depends in general on run-time flow of control.
33481 For example, if the body of @cite{B} said
33483 @example
33484 function B return Integer is
33485 begin
33486    if some-condition-depending-on-input-data then
33487       return A;
33488    else
33489       return 1;
33490    end if;
33491 end B;
33492 @end example
33494 then we could not know until run time whether the incorrect call to A would
33495 actually occur, so @cite{Program_Error} might
33496 or might not be raised. It is possible for a compiler to
33497 do a better job of analyzing bodies, to
33498 determine whether or not @cite{Program_Error}
33499 might be raised, but it certainly
33500 couldn't do a perfect job (that would require solving the halting problem
33501 and is provably impossible), and because this is a warning anyway, it does
33502 not seem worth the effort to do the analysis. Cases in which it
33503 would be relevant are rare.
33505 In practice, warnings of either of the forms given
33506 above will usually correspond to
33507 real errors, and should be examined carefully and eliminated.
33508 In the rare case where a warning is bogus, it can be suppressed by any of
33509 the following methods:
33512 @itemize *
33514 @item 
33515 Compile with the @emph{-gnatws} switch set
33517 @item 
33518 Suppress @cite{Elaboration_Check} for the called subprogram
33520 @item 
33521 Use pragma @cite{Warnings_Off} to turn warnings off for the call
33522 @end itemize
33524 For the internal elaboration check case,
33525 GNAT by default generates the
33526 necessary run-time checks to ensure
33527 that @cite{Program_Error} is raised if any
33528 call fails an elaboration check. Of course this can only happen if a
33529 warning has been issued as described above. The use of pragma
33530 @cite{Suppress (Elaboration_Check)} may (but is not guaranteed to) suppress
33531 some of these checks, meaning that it may be possible (but is not
33532 guaranteed) for a program to be able to call a subprogram whose body
33533 is not yet elaborated, without raising a @cite{Program_Error} exception.
33535 @node Controlling Elaboration in GNAT - External Calls,Default Behavior in GNAT - Ensuring Safety,Controlling Elaboration in GNAT - Internal Calls,Elaboration Order Handling in GNAT
33536 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id6}@anchor{2b8}@anchor{gnat_ugn/elaboration_order_handling_in_gnat controlling-elaboration-in-gnat-external-calls}@anchor{2b9}
33537 @section Controlling Elaboration in GNAT - External Calls
33540 The previous section discussed the case in which the execution of a
33541 particular thread of elaboration code occurred entirely within a
33542 single unit. This is the easy case to handle, because a programmer
33543 has direct and total control over the order of elaboration, and
33544 furthermore, checks need only be generated in cases which are rare
33545 and which the compiler can easily detect.
33546 The situation is more complex when separate compilation is taken into account.
33547 Consider the following:
33549 @example
33550 package Math is
33551    function Sqrt (Arg : Float) return Float;
33552 end Math;
33554 package body Math is
33555    function Sqrt (Arg : Float) return Float is
33556    begin
33557          ...
33558    end Sqrt;
33559 end Math;
33561 with Math;
33562 package Stuff is
33563    X : Float := Math.Sqrt (0.5);
33564 end Stuff;
33566 with Stuff;
33567 procedure Main is
33568 begin
33569    ...
33570 end Main;
33571 @end example
33573 where @cite{Main} is the main program. When this program is executed, the
33574 elaboration code must first be executed, and one of the jobs of the
33575 binder is to determine the order in which the units of a program are
33576 to be elaborated. In this case we have four units: the spec and body
33577 of @cite{Math},
33578 the spec of @cite{Stuff} and the body of @cite{Main}).
33579 In what order should the four separate sections of elaboration code
33580 be executed?
33582 There are some restrictions in the order of elaboration that the binder
33583 can choose. In particular, if unit U has a @emph{with}
33584 for a package @cite{X}, then you
33585 are assured that the spec of @cite{X}
33586 is elaborated before U , but you are
33587 not assured that the body of @cite{X}
33588 is elaborated before U.
33589 This means that in the above case, the binder is allowed to choose the
33590 order:
33592 @example
33593 spec of Math
33594 spec of Stuff
33595 body of Math
33596 body of Main
33597 @end example
33599 but that's not good, because now the call to @cite{Math.Sqrt}
33600 that happens during
33601 the elaboration of the @cite{Stuff}
33602 spec happens before the body of @cite{Math.Sqrt} is
33603 elaborated, and hence causes @cite{Program_Error} exception to be raised.
33604 At first glance, one might say that the binder is misbehaving, because
33605 obviously you want to elaborate the body of something you @emph{with} first, but
33606 that is not a general rule that can be followed in all cases. Consider
33608 @example
33609 package X is ...
33611 package Y is ...
33613 with X;
33614 package body Y is ...
33616 with Y;
33617 package body X is ...
33618 @end example
33620 This is a common arrangement, and, apart from the order of elaboration
33621 problems that might arise in connection with elaboration code, this works fine.
33622 A rule that says that you must first elaborate the body of anything you
33623 @emph{with} cannot work in this case:
33624 the body of @cite{X} @emph{with}s @cite{Y},
33625 which means you would have to
33626 elaborate the body of @cite{Y} first, but that @emph{with}s @cite{X},
33627 which means
33628 you have to elaborate the body of @cite{X} first, but ... and we have a
33629 loop that cannot be broken.
33631 It is true that the binder can in many cases guess an order of elaboration
33632 that is unlikely to cause a @cite{Program_Error}
33633 exception to be raised, and it tries to do so (in the
33634 above example of @cite{Math/Stuff/Spec}, the GNAT binder will
33635 by default
33636 elaborate the body of @cite{Math} right after its spec, so all will be well).
33638 However, a program that blindly relies on the binder to be helpful can
33639 get into trouble, as we discussed in the previous sections, so GNAT
33640 provides a number of facilities for assisting the programmer in
33641 developing programs that are robust with respect to elaboration order.
33643 @node Default Behavior in GNAT - Ensuring Safety,Treatment of Pragma Elaborate,Controlling Elaboration in GNAT - External Calls,Elaboration Order Handling in GNAT
33644 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id7}@anchor{2ba}@anchor{gnat_ugn/elaboration_order_handling_in_gnat default-behavior-in-gnat-ensuring-safety}@anchor{2bb}
33645 @section Default Behavior in GNAT - Ensuring Safety
33648 The default behavior in GNAT ensures elaboration safety. In its
33649 default mode GNAT implements the
33650 rule we previously described as the right approach. Let's restate it:
33652 @emph{If a unit has elaboration code that can directly or indirectly make a
33653 call to a subprogram in a |withed| unit, or instantiate a generic
33654 package in a |withed| unit, then if the |withed| unit
33655 does not have pragma `Pure` or `Preelaborate`, then the client should have an
33656 `Elaborate_All` pragma for the |withed| unit.}
33658 @emph{In the case of instantiating a generic subprogram, it is always
33659 sufficient to have only an `Elaborate` pragma for the
33660 |withed| unit.}
33662 By following this rule a client is assured that calls and instantiations
33663 can be made without risk of an exception.
33665 In this mode GNAT traces all calls that are potentially made from
33666 elaboration code, and puts in any missing implicit @cite{Elaborate}
33667 and @cite{Elaborate_All} pragmas.
33668 The advantage of this approach is that no elaboration problems
33669 are possible if the binder can find an elaboration order that is
33670 consistent with these implicit @cite{Elaborate} and
33671 @cite{Elaborate_All} pragmas. The
33672 disadvantage of this approach is that no such order may exist.
33674 If the binder does not generate any diagnostics, then it means that it has
33675 found an elaboration order that is guaranteed to be safe. However, the binder
33676 may still be relying on implicitly generated @cite{Elaborate} and
33677 @cite{Elaborate_All} pragmas so portability to other compilers than GNAT is not
33678 guaranteed.
33680 If it is important to guarantee portability, then the compilations should
33681 use the @emph{-gnatel}
33682 (info messages for elaboration pragmas) switch. This will cause info messages
33683 to be generated indicating the missing @cite{Elaborate} and
33684 @cite{Elaborate_All} pragmas.
33685 Consider the following source program:
33687 @example
33688 with k;
33689 package j is
33690   m : integer := k.r;
33691 end;
33692 @end example
33694 where it is clear that there
33695 should be a pragma @cite{Elaborate_All}
33696 for unit @cite{k}. An implicit pragma will be generated, and it is
33697 likely that the binder will be able to honor it. However, if you want
33698 to port this program to some other Ada compiler than GNAT.
33699 it is safer to include the pragma explicitly in the source. If this
33700 unit is compiled with the @emph{-gnatel}
33701 switch, then the compiler outputs an information message:
33703 @example
33704 1. with k;
33705 2. package j is
33706 3.   m : integer := k.r;
33707                      |
33708    >>> info: call to "r" may raise Program_Error
33709    >>> info: missing pragma Elaborate_All for "k"
33711 4. end;
33712 @end example
33714 and these messages can be used as a guide for supplying manually
33715 the missing pragmas. It is usually a bad idea to use this
33716 option during development. That's because it will tell you when
33717 you need to put in a pragma, but cannot tell you when it is time
33718 to take it out. So the use of pragma @cite{Elaborate_All} may lead to
33719 unnecessary dependencies and even false circularities.
33721 This default mode is more restrictive than the Ada Reference
33722 Manual, and it is possible to construct programs which will compile
33723 using the dynamic model described there, but will run into a
33724 circularity using the safer static model we have described.
33726 Of course any Ada compiler must be able to operate in a mode
33727 consistent with the requirements of the Ada Reference Manual,
33728 and in particular must have the capability of implementing the
33729 standard dynamic model of elaboration with run-time checks.
33731 In GNAT, this standard mode can be achieved either by the use of
33732 the @emph{-gnatE} switch on the compiler (@emph{gcc} or
33733 @emph{gnatmake}) command, or by the use of the configuration pragma:
33735 @example
33736 pragma Elaboration_Checks (DYNAMIC);
33737 @end example
33739 Either approach will cause the unit affected to be compiled using the
33740 standard dynamic run-time elaboration checks described in the Ada
33741 Reference Manual. The static model is generally preferable, since it
33742 is clearly safer to rely on compile and link time checks rather than
33743 run-time checks. However, in the case of legacy code, it may be
33744 difficult to meet the requirements of the static model. This
33745 issue is further discussed in
33746 @ref{2bc,,What to Do If the Default Elaboration Behavior Fails}.
33748 Note that the static model provides a strict subset of the allowed
33749 behavior and programs of the Ada Reference Manual, so if you do
33750 adhere to the static model and no circularities exist,
33751 then you are assured that your program will
33752 work using the dynamic model, providing that you remove any
33753 pragma Elaborate statements from the source.
33755 @node Treatment of Pragma Elaborate,Elaboration Issues for Library Tasks,Default Behavior in GNAT - Ensuring Safety,Elaboration Order Handling in GNAT
33756 @anchor{gnat_ugn/elaboration_order_handling_in_gnat treatment-of-pragma-elaborate}@anchor{2bd}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id8}@anchor{2be}
33757 @section Treatment of Pragma Elaborate
33760 @geindex Pragma Elaborate
33762 The use of @cite{pragma Elaborate}
33763 should generally be avoided in Ada 95 and Ada 2005 programs,
33764 since there is no guarantee that transitive calls
33765 will be properly handled. Indeed at one point, this pragma was placed
33766 in Annex J (Obsolescent Features), on the grounds that it is never useful.
33768 Now that's a bit restrictive. In practice, the case in which
33769 @cite{pragma Elaborate} is useful is when the caller knows that there
33770 are no transitive calls, or that the called unit contains all necessary
33771 transitive @cite{pragma Elaborate} statements, and legacy code often
33772 contains such uses.
33774 Strictly speaking the static mode in GNAT should ignore such pragmas,
33775 since there is no assurance at compile time that the necessary safety
33776 conditions are met. In practice, this would cause GNAT to be incompatible
33777 with correctly written Ada 83 code that had all necessary
33778 @cite{pragma Elaborate} statements in place. Consequently, we made the
33779 decision that GNAT in its default mode will believe that if it encounters
33780 a @cite{pragma Elaborate} then the programmer knows what they are doing,
33781 and it will trust that no elaboration errors can occur.
33783 The result of this decision is two-fold. First to be safe using the
33784 static mode, you should remove all @cite{pragma Elaborate} statements.
33785 Second, when fixing circularities in existing code, you can selectively
33786 use @cite{pragma Elaborate} statements to convince the static mode of
33787 GNAT that it need not generate an implicit @cite{pragma Elaborate_All}
33788 statement.
33790 When using the static mode with @emph{-gnatwl}, any use of
33791 @cite{pragma Elaborate} will generate a warning about possible
33792 problems.
33794 @node Elaboration Issues for Library Tasks,Mixing Elaboration Models,Treatment of Pragma Elaborate,Elaboration Order Handling in GNAT
33795 @anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-issues-for-library-tasks}@anchor{2bf}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id9}@anchor{2c0}
33796 @section Elaboration Issues for Library Tasks
33799 @geindex Library tasks
33800 @geindex elaboration issues
33802 @geindex Elaboration of library tasks
33804 In this section we examine special elaboration issues that arise for
33805 programs that declare library level tasks.
33807 Generally the model of execution of an Ada program is that all units are
33808 elaborated, and then execution of the program starts. However, the
33809 declaration of library tasks definitely does not fit this model. The
33810 reason for this is that library tasks start as soon as they are declared
33811 (more precisely, as soon as the statement part of the enclosing package
33812 body is reached), that is to say before elaboration
33813 of the program is complete. This means that if such a task calls a
33814 subprogram, or an entry in another task, the callee may or may not be
33815 elaborated yet, and in the standard
33816 Reference Manual model of dynamic elaboration checks, you can even
33817 get timing dependent Program_Error exceptions, since there can be
33818 a race between the elaboration code and the task code.
33820 The static model of elaboration in GNAT seeks to avoid all such
33821 dynamic behavior, by being conservative, and the conservative
33822 approach in this particular case is to assume that all the code
33823 in a task body is potentially executed at elaboration time if
33824 a task is declared at the library level.
33826 This can definitely result in unexpected circularities. Consider
33827 the following example
33829 @example
33830 package Decls is
33831   task Lib_Task is
33832      entry Start;
33833   end Lib_Task;
33835   type My_Int is new Integer;
33837   function Ident (M : My_Int) return My_Int;
33838 end Decls;
33840 with Utils;
33841 package body Decls is
33842   task body Lib_Task is
33843   begin
33844      accept Start;
33845      Utils.Put_Val (2);
33846   end Lib_Task;
33848   function Ident (M : My_Int) return My_Int is
33849   begin
33850      return M;
33851   end Ident;
33852 end Decls;
33854 with Decls;
33855 package Utils is
33856   procedure Put_Val (Arg : Decls.My_Int);
33857 end Utils;
33859 with Text_IO;
33860 package body Utils is
33861   procedure Put_Val (Arg : Decls.My_Int) is
33862   begin
33863      Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
33864   end Put_Val;
33865 end Utils;
33867 with Decls;
33868 procedure Main is
33869 begin
33870    Decls.Lib_Task.Start;
33871 end;
33872 @end example
33874 If the above example is compiled in the default static elaboration
33875 mode, then a circularity occurs. The circularity comes from the call
33876 @cite{Utils.Put_Val} in the task body of @cite{Decls.Lib_Task}. Since
33877 this call occurs in elaboration code, we need an implicit pragma
33878 @cite{Elaborate_All} for @cite{Utils}. This means that not only must
33879 the spec and body of @cite{Utils} be elaborated before the body
33880 of @cite{Decls}, but also the spec and body of any unit that is
33881 @emph{with}ed by the body of @cite{Utils} must also be elaborated before
33882 the body of @cite{Decls}. This is the transitive implication of
33883 pragma @cite{Elaborate_All} and it makes sense, because in general
33884 the body of @cite{Put_Val} might have a call to something in a
33885 @emph{with}ed unit.
33887 In this case, the body of Utils (actually its spec) @emph{with}s
33888 @cite{Decls}. Unfortunately this means that the body of @cite{Decls}
33889 must be elaborated before itself, in case there is a call from the
33890 body of @cite{Utils}.
33892 Here is the exact chain of events we are worrying about:
33895 @itemize *
33897 @item 
33898 In the body of @cite{Decls} a call is made from within the body of a library
33899 task to a subprogram in the package @cite{Utils}. Since this call may
33900 occur at elaboration time (given that the task is activated at elaboration
33901 time), we have to assume the worst, i.e., that the
33902 call does happen at elaboration time.
33904 @item 
33905 This means that the body and spec of @cite{Util} must be elaborated before
33906 the body of @cite{Decls} so that this call does not cause an access before
33907 elaboration.
33909 @item 
33910 Within the body of @cite{Util}, specifically within the body of
33911 @cite{Util.Put_Val} there may be calls to any unit @emph{with}ed
33912 by this package.
33914 @item 
33915 One such @emph{with}ed package is package @cite{Decls}, so there
33916 might be a call to a subprogram in @cite{Decls} in @cite{Put_Val}.
33917 In fact there is such a call in this example, but we would have to
33918 assume that there was such a call even if it were not there, since
33919 we are not supposed to write the body of @cite{Decls} knowing what
33920 is in the body of @cite{Utils}; certainly in the case of the
33921 static elaboration model, the compiler does not know what is in
33922 other bodies and must assume the worst.
33924 @item 
33925 This means that the spec and body of @cite{Decls} must also be
33926 elaborated before we elaborate the unit containing the call, but
33927 that unit is @cite{Decls}! This means that the body of @cite{Decls}
33928 must be elaborated before itself, and that's a circularity.
33929 @end itemize
33931 Indeed, if you add an explicit pragma @cite{Elaborate_All} for @cite{Utils} in
33932 the body of @cite{Decls} you will get a true Ada Reference Manual
33933 circularity that makes the program illegal.
33935 In practice, we have found that problems with the static model of
33936 elaboration in existing code often arise from library tasks, so
33937 we must address this particular situation.
33939 Note that if we compile and run the program above, using the dynamic model of
33940 elaboration (that is to say use the @emph{-gnatE} switch),
33941 then it compiles, binds,
33942 links, and runs, printing the expected result of 2. Therefore in some sense
33943 the circularity here is only apparent, and we need to capture
33944 the properties of this program that  distinguish it from other library-level
33945 tasks that have real elaboration problems.
33947 We have four possible answers to this question:
33950 @itemize *
33952 @item 
33953 Use the dynamic model of elaboration.
33955 If we use the @emph{-gnatE} switch, then as noted above, the program works.
33956 Why is this? If we examine the task body, it is apparent that the task cannot
33957 proceed past the
33958 @cite{accept} statement until after elaboration has been completed, because
33959 the corresponding entry call comes from the main program, not earlier.
33960 This is why the dynamic model works here. But that's really giving
33961 up on a precise analysis, and we prefer to take this approach only if we cannot
33962 solve the
33963 problem in any other manner. So let us examine two ways to reorganize
33964 the program to avoid the potential elaboration problem.
33966 @item 
33967 Split library tasks into separate packages.
33969 Write separate packages, so that library tasks are isolated from
33970 other declarations as much as possible. Let us look at a variation on
33971 the above program.
33973 @example
33974 package Decls1 is
33975   task Lib_Task is
33976      entry Start;
33977   end Lib_Task;
33978 end Decls1;
33980 with Utils;
33981 package body Decls1 is
33982   task body Lib_Task is
33983   begin
33984      accept Start;
33985      Utils.Put_Val (2);
33986   end Lib_Task;
33987 end Decls1;
33989 package Decls2 is
33990   type My_Int is new Integer;
33991   function Ident (M : My_Int) return My_Int;
33992 end Decls2;
33994 with Utils;
33995 package body Decls2 is
33996   function Ident (M : My_Int) return My_Int is
33997   begin
33998      return M;
33999   end Ident;
34000 end Decls2;
34002 with Decls2;
34003 package Utils is
34004   procedure Put_Val (Arg : Decls2.My_Int);
34005 end Utils;
34007 with Text_IO;
34008 package body Utils is
34009   procedure Put_Val (Arg : Decls2.My_Int) is
34010   begin
34011      Text_IO.Put_Line (Decls2.My_Int'Image (Decls2.Ident (Arg)));
34012   end Put_Val;
34013 end Utils;
34015 with Decls1;
34016 procedure Main is
34017 begin
34018    Decls1.Lib_Task.Start;
34019 end;
34020 @end example
34022 All we have done is to split @cite{Decls} into two packages, one
34023 containing the library task, and one containing everything else. Now
34024 there is no cycle, and the program compiles, binds, links and executes
34025 using the default static model of elaboration.
34027 @item 
34028 Declare separate task types.
34030 A significant part of the problem arises because of the use of the
34031 single task declaration form. This means that the elaboration of
34032 the task type, and the elaboration of the task itself (i.e., the
34033 creation of the task) happen at the same time. A good rule
34034 of style in Ada is to always create explicit task types. By
34035 following the additional step of placing task objects in separate
34036 packages from the task type declaration, many elaboration problems
34037 are avoided. Here is another modified example of the example program:
34039 @example
34040 package Decls is
34041   task type Lib_Task_Type is
34042      entry Start;
34043   end Lib_Task_Type;
34045   type My_Int is new Integer;
34047   function Ident (M : My_Int) return My_Int;
34048 end Decls;
34050 with Utils;
34051 package body Decls is
34052   task body Lib_Task_Type is
34053   begin
34054      accept Start;
34055      Utils.Put_Val (2);
34056   end Lib_Task_Type;
34058   function Ident (M : My_Int) return My_Int is
34059   begin
34060      return M;
34061   end Ident;
34062 end Decls;
34064 with Decls;
34065 package Utils is
34066   procedure Put_Val (Arg : Decls.My_Int);
34067 end Utils;
34069 with Text_IO;
34070 package body Utils is
34071   procedure Put_Val (Arg : Decls.My_Int) is
34072   begin
34073      Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
34074   end Put_Val;
34075 end Utils;
34077 with Decls;
34078 package Declst is
34079    Lib_Task : Decls.Lib_Task_Type;
34080 end Declst;
34082 with Declst;
34083 procedure Main is
34084 begin
34085    Declst.Lib_Task.Start;
34086 end;
34087 @end example
34089 What we have done here is to replace the @cite{task} declaration in
34090 package @cite{Decls} with a @cite{task type} declaration. Then we
34091 introduce a separate package @cite{Declst} to contain the actual
34092 task object. This separates the elaboration issues for
34093 the @cite{task type}
34094 declaration, which causes no trouble, from the elaboration issues
34095 of the task object, which is also unproblematic, since it is now independent
34096 of the elaboration of  @cite{Utils}.
34097 This separation of concerns also corresponds to
34098 a generally sound engineering principle of separating declarations
34099 from instances. This version of the program also compiles, binds, links,
34100 and executes, generating the expected output.
34101 @end itemize
34103 @geindex No_Entry_Calls_In_Elaboration_Code restriction
34106 @itemize *
34108 @item 
34109 Use No_Entry_Calls_In_Elaboration_Code restriction.
34111 The previous two approaches described how a program can be restructured
34112 to avoid the special problems caused by library task bodies. in practice,
34113 however, such restructuring may be difficult to apply to existing legacy code,
34114 so we must consider solutions that do not require massive rewriting.
34116 Let us consider more carefully why our original sample program works
34117 under the dynamic model of elaboration. The reason is that the code
34118 in the task body blocks immediately on the @cite{accept}
34119 statement. Now of course there is nothing to prohibit elaboration
34120 code from making entry calls (for example from another library level task),
34121 so we cannot tell in isolation that
34122 the task will not execute the accept statement  during elaboration.
34124 However, in practice it is very unusual to see elaboration code
34125 make any entry calls, and the pattern of tasks starting
34126 at elaboration time and then immediately blocking on @cite{accept} or
34127 @cite{select} statements is very common. What this means is that
34128 the compiler is being too pessimistic when it analyzes the
34129 whole package body as though it might be executed at elaboration
34130 time.
34132 If we know that the elaboration code contains no entry calls, (a very safe
34133 assumption most of the time, that could almost be made the default
34134 behavior), then we can compile all units of the program under control
34135 of the following configuration pragma:
34137 @example
34138 pragma Restrictions (No_Entry_Calls_In_Elaboration_Code);
34139 @end example
34141 This pragma can be placed in the @code{gnat.adc} file in the usual
34142 manner. If we take our original unmodified program and compile it
34143 in the presence of a @code{gnat.adc} containing the above pragma,
34144 then once again, we can compile, bind, link, and execute, obtaining
34145 the expected result. In the presence of this pragma, the compiler does
34146 not trace calls in a task body, that appear after the first @cite{accept}
34147 or @cite{select} statement, and therefore does not report a potential
34148 circularity in the original program.
34150 The compiler will check to the extent it can that the above
34151 restriction is not violated, but it is not always possible to do a
34152 complete check at compile time, so it is important to use this
34153 pragma only if the stated restriction is in fact met, that is to say
34154 no task receives an entry call before elaboration of all units is completed.
34155 @end itemize
34157 @node Mixing Elaboration Models,What to Do If the Default Elaboration Behavior Fails,Elaboration Issues for Library Tasks,Elaboration Order Handling in GNAT
34158 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id10}@anchor{2c1}@anchor{gnat_ugn/elaboration_order_handling_in_gnat mixing-elaboration-models}@anchor{2c2}
34159 @section Mixing Elaboration Models
34162 So far, we have assumed that the entire program is either compiled
34163 using the dynamic model or static model, ensuring consistency. It
34164 is possible to mix the two models, but rules have to be followed
34165 if this mixing is done to ensure that elaboration checks are not
34166 omitted.
34168 The basic rule is that
34169 @strong{a unit compiled with the static model cannot
34170 be |withed| by a unit compiled with the dynamic model}.
34171 The reason for this is that in the static model, a unit assumes that
34172 its clients guarantee to use (the equivalent of) pragma
34173 @cite{Elaborate_All} so that no elaboration checks are required
34174 in inner subprograms, and this assumption is violated if the
34175 client is compiled with dynamic checks.
34177 The precise rule is as follows. A unit that is compiled with dynamic
34178 checks can only @emph{with} a unit that meets at least one of the
34179 following criteria:
34182 @itemize *
34184 @item 
34185 The @emph{with}ed unit is itself compiled with dynamic elaboration
34186 checks (that is with the @emph{-gnatE} switch.
34188 @item 
34189 The @emph{with}ed unit is an internal GNAT implementation unit from
34190 the System, Interfaces, Ada, or GNAT hierarchies.
34192 @item 
34193 The @emph{with}ed unit has pragma Preelaborate or pragma Pure.
34195 @item 
34196 The @emph{with}ing unit (that is the client) has an explicit pragma
34197 @cite{Elaborate_All} for the @emph{with}ed unit.
34198 @end itemize
34200 If this rule is violated, that is if a unit with dynamic elaboration
34201 checks @emph{with}s a unit that does not meet one of the above four
34202 criteria, then the binder (@cite{gnatbind}) will issue a warning
34203 similar to that in the following example:
34205 @example
34206 warning: "x.ads" has dynamic elaboration checks and with's
34207 warning:   "y.ads" which has static elaboration checks
34208 @end example
34210 These warnings indicate that the rule has been violated, and that as a result
34211 elaboration checks may be missed in the resulting executable file.
34212 This warning may be suppressed using the @emph{-ws} binder switch
34213 in the usual manner.
34215 One useful application of this mixing rule is in the case of a subsystem
34216 which does not itself @emph{with} units from the remainder of the
34217 application. In this case, the entire subsystem can be compiled with
34218 dynamic checks to resolve a circularity in the subsystem, while
34219 allowing the main application that uses this subsystem to be compiled
34220 using the more reliable default static model.
34222 @node What to Do If the Default Elaboration Behavior Fails,Elaboration for Indirect Calls,Mixing Elaboration Models,Elaboration Order Handling in GNAT
34223 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id11}@anchor{2c3}@anchor{gnat_ugn/elaboration_order_handling_in_gnat what-to-do-if-the-default-elaboration-behavior-fails}@anchor{2bc}
34224 @section What to Do If the Default Elaboration Behavior Fails
34227 If the binder cannot find an acceptable order, it outputs detailed
34228 diagnostics. For example:
34230 @example
34231 error: elaboration circularity detected
34232 info:   "proc (body)" must be elaborated before "pack (body)"
34233 info:     reason: Elaborate_All probably needed in unit "pack (body)"
34234 info:     recompile "pack (body)" with -gnatel
34235 info:                             for full details
34236 info:       "proc (body)"
34237 info:         is needed by its spec:
34238 info:       "proc (spec)"
34239 info:         which is withed by:
34240 info:       "pack (body)"
34241 info:  "pack (body)" must be elaborated before "proc (body)"
34242 info:     reason: pragma Elaborate in unit "proc (body)"
34243 @end example
34245 In this case we have a cycle that the binder cannot break. On the one
34246 hand, there is an explicit pragma Elaborate in @cite{proc} for
34247 @cite{pack}. This means that the body of @cite{pack} must be elaborated
34248 before the body of @cite{proc}. On the other hand, there is elaboration
34249 code in @cite{pack} that calls a subprogram in @cite{proc}. This means
34250 that for maximum safety, there should really be a pragma
34251 Elaborate_All in @cite{pack} for @cite{proc} which would require that
34252 the body of @cite{proc} be elaborated before the body of
34253 @cite{pack}. Clearly both requirements cannot be satisfied.
34254 Faced with a circularity of this kind, you have three different options.
34257 @itemize *
34259 @item 
34260 @emph{Fix the program}
34262 The most desirable option from the point of view of long-term maintenance
34263 is to rearrange the program so that the elaboration problems are avoided.
34264 One useful technique is to place the elaboration code into separate
34265 child packages. Another is to move some of the initialization code to
34266 explicitly called subprograms, where the program controls the order
34267 of initialization explicitly. Although this is the most desirable option,
34268 it may be impractical and involve too much modification, especially in
34269 the case of complex legacy code.
34271 @item 
34272 @emph{Perform dynamic checks}
34274 If the compilations are done using the @emph{-gnatE}
34275 (dynamic elaboration check) switch, then GNAT behaves in a quite different
34276 manner. Dynamic checks are generated for all calls that could possibly result
34277 in raising an exception. With this switch, the compiler does not generate
34278 implicit @cite{Elaborate} or @cite{Elaborate_All} pragmas. The behavior then is
34279 exactly as specified in the @cite{Ada Reference Manual}.
34280 The binder will generate
34281 an executable program that may or may not raise @cite{Program_Error}, and then
34282 it is the programmer's job to ensure that it does not raise an exception. Note
34283 that it is important to compile all units with the switch, it cannot be used
34284 selectively.
34286 @item 
34287 @emph{Suppress checks}
34289 The drawback of dynamic checks is that they generate a
34290 significant overhead at run time, both in space and time. If you
34291 are absolutely sure that your program cannot raise any elaboration
34292 exceptions, and you still want to use the dynamic elaboration model,
34293 then you can use the configuration pragma
34294 @cite{Suppress (Elaboration_Check)} to suppress all such checks. For
34295 example this pragma could be placed in the @code{gnat.adc} file.
34297 @item 
34298 @emph{Suppress checks selectively}
34300 When you know that certain calls or instantiations in elaboration code cannot
34301 possibly lead to an elaboration error, and the binder nevertheless complains
34302 about implicit @cite{Elaborate} and @cite{Elaborate_All} pragmas that lead to
34303 elaboration circularities, it is possible to remove those warnings locally and
34304 obtain a program that will bind. Clearly this can be unsafe, and it is the
34305 responsibility of the programmer to make sure that the resulting program has no
34306 elaboration anomalies. The pragma @cite{Suppress (Elaboration_Check)} can be
34307 used with different granularity to suppress warnings and break elaboration
34308 circularities:
34311 @itemize *
34313 @item 
34314 Place the pragma that names the called subprogram in the declarative part
34315 that contains the call.
34317 @item 
34318 Place the pragma in the declarative part, without naming an entity. This
34319 disables warnings on all calls in the corresponding  declarative region.
34321 @item 
34322 Place the pragma in the package spec that declares the called subprogram,
34323 and name the subprogram. This disables warnings on all elaboration calls to
34324 that subprogram.
34326 @item 
34327 Place the pragma in the package spec that declares the called subprogram,
34328 without naming any entity. This disables warnings on all elaboration calls to
34329 all subprograms declared in this spec.
34331 @item 
34332 Use Pragma Elaborate.
34334 As previously described in section @ref{2bd,,Treatment of Pragma Elaborate},
34335 GNAT in static mode assumes that a @cite{pragma} Elaborate indicates correctly
34336 that no elaboration checks are required on calls to the designated unit.
34337 There may be cases in which the caller knows that no transitive calls
34338 can occur, so that a @cite{pragma Elaborate} will be sufficient in a
34339 case where @cite{pragma Elaborate_All} would cause a circularity.
34340 @end itemize
34342 These five cases are listed in order of decreasing safety, and therefore
34343 require increasing programmer care in their application. Consider the
34344 following program:
34346 @example
34347 package Pack1 is
34348   function F1 return Integer;
34349   X1 : Integer;
34350 end Pack1;
34352 package Pack2 is
34353   function F2 return Integer;
34354   function Pure (x : integer) return integer;
34355   --  pragma Suppress (Elaboration_Check, On => Pure);  -- (3)
34356   --  pragma Suppress (Elaboration_Check);              -- (4)
34357 end Pack2;
34359 with Pack2;
34360 package body Pack1 is
34361   function F1 return Integer is
34362   begin
34363     return 100;
34364   end F1;
34365   Val : integer := Pack2.Pure (11);    --  Elab. call (1)
34366 begin
34367   declare
34368     --  pragma Suppress(Elaboration_Check, Pack2.F2);   -- (1)
34369     --  pragma Suppress(Elaboration_Check);             -- (2)
34370   begin
34371     X1 := Pack2.F2 + 1;                --  Elab. call (2)
34372   end;
34373 end Pack1;
34375 with Pack1;
34376 package body Pack2 is
34377   function F2 return Integer is
34378   begin
34379      return Pack1.F1;
34380   end F2;
34381   function Pure (x : integer) return integer is
34382   begin
34383      return x ** 3 - 3 * x;
34384   end;
34385 end Pack2;
34387 with Pack1, Ada.Text_IO;
34388 procedure Proc3 is
34389 begin
34390   Ada.Text_IO.Put_Line(Pack1.X1'Img); -- 101
34391 end Proc3;
34392 @end example
34394 In the absence of any pragmas, an attempt to bind this program produces
34395 the following diagnostics:
34397 @example
34398 error: elaboration circularity detected
34399 info:    "pack1 (body)" must be elaborated before "pack1 (body)"
34400 info:       reason: Elaborate_All probably needed in unit "pack1 (body)"
34401 info:       recompile "pack1 (body)" with -gnatel for full details
34402 info:          "pack1 (body)"
34403 info:             must be elaborated along with its spec:
34404 info:          "pack1 (spec)"
34405 info:             which is withed by:
34406 info:          "pack2 (body)"
34407 info:             which must be elaborated along with its spec:
34408 info:          "pack2 (spec)"
34409 info:             which is withed by:
34410 info:          "pack1 (body)"
34411 @end example
34413 The sources of the circularity are the two calls to @cite{Pack2.Pure} and
34414 @cite{Pack2.F2} in the body of @cite{Pack1}. We can see that the call to
34415 F2 is safe, even though F2 calls F1, because the call appears after the
34416 elaboration of the body of F1. Therefore the pragma (1) is safe, and will
34417 remove the warning on the call. It is also possible to use pragma (2)
34418 because there are no other potentially unsafe calls in the block.
34420 The call to @cite{Pure} is safe because this function does not depend on the
34421 state of @cite{Pack2}. Therefore any call to this function is safe, and it
34422 is correct to place pragma (3) in the corresponding package spec.
34424 Finally, we could place pragma (4) in the spec of @cite{Pack2} to disable
34425 warnings on all calls to functions declared therein. Note that this is not
34426 necessarily safe, and requires more detailed examination of the subprogram
34427 bodies involved. In particular, a call to @cite{F2} requires that @cite{F1}
34428 be already elaborated.
34429 @end itemize
34431 It is hard to generalize on which of these four approaches should be
34432 taken. Obviously if it is possible to fix the program so that the default
34433 treatment works, this is preferable, but this may not always be practical.
34434 It is certainly simple enough to use @emph{-gnatE}
34435 but the danger in this case is that, even if the GNAT binder
34436 finds a correct elaboration order, it may not always do so,
34437 and certainly a binder from another Ada compiler might not. A
34438 combination of testing and analysis (for which the
34439 information messages generated with the @emph{-gnatel}
34440 switch can be useful) must be used to ensure that the program is free
34441 of errors. One switch that is useful in this testing is the
34442 @emph{-p (pessimistic elaboration order)} switch for @cite{gnatbind}.
34443 Normally the binder tries to find an order that has the best chance
34444 of avoiding elaboration problems. However, if this switch is used, the binder
34445 plays a devil's advocate role, and tries to choose the order that
34446 has the best chance of failing. If your program works even with this
34447 switch, then it has a better chance of being error free, but this is still
34448 not a guarantee.
34450 For an example of this approach in action, consider the C-tests (executable
34451 tests) from the ACATS suite. If these are compiled and run with the default
34452 treatment, then all but one of them succeed without generating any error
34453 diagnostics from the binder. However, there is one test that fails, and
34454 this is not surprising, because the whole point of this test is to ensure
34455 that the compiler can handle cases where it is impossible to determine
34456 a correct order statically, and it checks that an exception is indeed
34457 raised at run time.
34459 This one test must be compiled and run using the @emph{-gnatE}
34460 switch, and then it passes. Alternatively, the entire suite can
34461 be run using this switch. It is never wrong to run with the dynamic
34462 elaboration switch if your code is correct, and we assume that the
34463 C-tests are indeed correct (it is less efficient, but efficiency is
34464 not a factor in running the ACATS tests.)
34466 @node Elaboration for Indirect Calls,Summary of Procedures for Elaboration Control,What to Do If the Default Elaboration Behavior Fails,Elaboration Order Handling in GNAT
34467 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id12}@anchor{2c4}@anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-for-indirect-calls}@anchor{2c5}
34468 @section Elaboration for Indirect Calls
34471 @geindex Dispatching calls
34473 @geindex Indirect calls
34475 In rare cases, the static elaboration model fails to prevent
34476 dispatching calls to not-yet-elaborated subprograms. In such cases, we
34477 fall back to run-time checks; premature calls to any primitive
34478 operation of a tagged type before the body of the operation has been
34479 elaborated will raise @cite{Program_Error}.
34481 Access-to-subprogram types, however, are handled conservatively, and
34482 do not require run-time checks. This was not true in earlier versions
34483 of the compiler; you can use the @emph{-gnatd.U} debug switch to
34484 revert to the old behavior if the new conservative behavior causes
34485 elaboration cycles. Here, 'conservative' means that if you do
34486 @cite{P'Access} during elaboration, the compiler will assume that you
34487 might call @cite{P} indirectly during elaboration, so it adds an
34488 implicit @cite{pragma Elaborate_All} on the library unit containing
34489 @cite{P}. The @emph{-gnatd.U} switch is safe if you know there are
34490 no such calls. If the program worked before, it will continue to work
34491 with @emph{-gnatd.U}. But beware that code modifications such as
34492 adding an indirect call can cause erroneous behavior in the presence
34493 of @emph{-gnatd.U}.
34495 @node Summary of Procedures for Elaboration Control,Other Elaboration Order Considerations,Elaboration for Indirect Calls,Elaboration Order Handling in GNAT
34496 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id13}@anchor{2c6}@anchor{gnat_ugn/elaboration_order_handling_in_gnat summary-of-procedures-for-elaboration-control}@anchor{2c7}
34497 @section Summary of Procedures for Elaboration Control
34500 @geindex Elaboration control
34502 First, compile your program with the default options, using none of
34503 the special elaboration control switches. If the binder successfully
34504 binds your program, then you can be confident that, apart from issues
34505 raised by the use of access-to-subprogram types and dynamic dispatching,
34506 the program is free of elaboration errors. If it is important that the
34507 program be portable to other compilers than GNAT, then use the
34508 @emph{-gnatel}
34509 switch to generate messages about missing @cite{Elaborate} or
34510 @cite{Elaborate_All} pragmas, and supply the missing pragmas.
34512 If the program fails to bind using the default static elaboration
34513 handling, then you can fix the program to eliminate the binder
34514 message, or recompile the entire program with the
34515 @emph{-gnatE} switch to generate dynamic elaboration checks,
34516 and, if you are sure there really are no elaboration problems,
34517 use a global pragma @cite{Suppress (Elaboration_Check)}.
34519 @node Other Elaboration Order Considerations,Determining the Chosen Elaboration Order,Summary of Procedures for Elaboration Control,Elaboration Order Handling in GNAT
34520 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id14}@anchor{2c8}@anchor{gnat_ugn/elaboration_order_handling_in_gnat other-elaboration-order-considerations}@anchor{2c9}
34521 @section Other Elaboration Order Considerations
34524 This section has been entirely concerned with the issue of finding a valid
34525 elaboration order, as defined by the Ada Reference Manual. In a case
34526 where several elaboration orders are valid, the task is to find one
34527 of the possible valid elaboration orders (and the static model in GNAT
34528 will ensure that this is achieved).
34530 The purpose of the elaboration rules in the Ada Reference Manual is to
34531 make sure that no entity is accessed before it has been elaborated. For
34532 a subprogram, this means that the spec and body must have been elaborated
34533 before the subprogram is called. For an object, this means that the object
34534 must have been elaborated before its value is read or written. A violation
34535 of either of these two requirements is an access before elaboration order,
34536 and this section has been all about avoiding such errors.
34538 In the case where more than one order of elaboration is possible, in the
34539 sense that access before elaboration errors are avoided, then any one of
34540 the orders is 'correct' in the sense that it meets the requirements of
34541 the Ada Reference Manual, and no such error occurs.
34543 However, it may be the case for a given program, that there are
34544 constraints on the order of elaboration that come not from consideration
34545 of avoiding elaboration errors, but rather from extra-lingual logic
34546 requirements. Consider this example:
34548 @example
34549 with Init_Constants;
34550 package Constants is
34551    X : Integer := 0;
34552    Y : Integer := 0;
34553 end Constants;
34555 package Init_Constants is
34556    procedure P; --* require a body*
34557 end Init_Constants;
34559 with Constants;
34560 package body Init_Constants is
34561    procedure P is begin null; end;
34562 begin
34563    Constants.X := 3;
34564    Constants.Y := 4;
34565 end Init_Constants;
34567 with Constants;
34568 package Calc is
34569    Z : Integer := Constants.X + Constants.Y;
34570 end Calc;
34572 with Calc;
34573 with Text_IO; use Text_IO;
34574 procedure Main is
34575 begin
34576    Put_Line (Calc.Z'Img);
34577 end Main;
34578 @end example
34580 In this example, there is more than one valid order of elaboration. For
34581 example both the following are correct orders:
34583 @example
34584 Init_Constants spec
34585 Constants spec
34586 Calc spec
34587 Init_Constants body
34588 Main body
34589 @end example
34593 @example
34594 Init_Constants spec
34595 Init_Constants body
34596 Constants spec
34597 Calc spec
34598 Main body
34599 @end example
34601 There is no language rule to prefer one or the other, both are correct
34602 from an order of elaboration point of view. But the programmatic effects
34603 of the two orders are very different. In the first, the elaboration routine
34604 of @cite{Calc} initializes @cite{Z} to zero, and then the main program
34605 runs with this value of zero. But in the second order, the elaboration
34606 routine of @cite{Calc} runs after the body of Init_Constants has set
34607 @cite{X} and @cite{Y} and thus @cite{Z} is set to 7 before @cite{Main} runs.
34609 One could perhaps by applying pretty clever non-artificial intelligence
34610 to the situation guess that it is more likely that the second order of
34611 elaboration is the one desired, but there is no formal linguistic reason
34612 to prefer one over the other. In fact in this particular case, GNAT will
34613 prefer the second order, because of the rule that bodies are elaborated
34614 as soon as possible, but it's just luck that this is what was wanted
34615 (if indeed the second order was preferred).
34617 If the program cares about the order of elaboration routines in a case like
34618 this, it is important to specify the order required. In this particular
34619 case, that could have been achieved by adding to the spec of Calc:
34621 @example
34622 pragma Elaborate_All (Constants);
34623 @end example
34625 which requires that the body (if any) and spec of @cite{Constants},
34626 as well as the body and spec of any unit @emph{with}ed by
34627 @cite{Constants} be elaborated before @cite{Calc} is elaborated.
34629 Clearly no automatic method can always guess which alternative you require,
34630 and if you are working with legacy code that had constraints of this kind
34631 which were not properly specified by adding @cite{Elaborate} or
34632 @cite{Elaborate_All} pragmas, then indeed it is possible that two different
34633 compilers can choose different orders.
34635 However, GNAT does attempt to diagnose the common situation where there
34636 are uninitialized variables in the visible part of a package spec, and the
34637 corresponding package body has an elaboration block that directly or
34638 indirectly initialized one or more of these variables. This is the situation
34639 in which a pragma Elaborate_Body is usually desirable, and GNAT will generate
34640 a warning that suggests this addition if it detects this situation.
34642 The @cite{gnatbind} @emph{-p} switch may be useful in smoking
34643 out problems. This switch causes bodies to be elaborated as late as possible
34644 instead of as early as possible. In the example above, it would have forced
34645 the choice of the first elaboration order. If you get different results
34646 when using this switch, and particularly if one set of results is right,
34647 and one is wrong as far as you are concerned, it shows that you have some
34648 missing @cite{Elaborate} pragmas. For the example above, we have the
34649 following output:
34651 @example
34652 $ gnatmake -f -q main
34653 $ main
34655 $ gnatmake -f -q main -bargs -p
34656 $ main
34658 @end example
34660 It is of course quite unlikely that both these results are correct, so
34661 it is up to you in a case like this to investigate the source of the
34662 difference, by looking at the two elaboration orders that are chosen,
34663 and figuring out which is correct, and then adding the necessary
34664 @cite{Elaborate} or @cite{Elaborate_All} pragmas to ensure the desired order.
34666 @node Determining the Chosen Elaboration Order,,Other Elaboration Order Considerations,Elaboration Order Handling in GNAT
34667 @anchor{gnat_ugn/elaboration_order_handling_in_gnat determining-the-chosen-elaboration-order}@anchor{2ca}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id15}@anchor{2cb}
34668 @section Determining the Chosen Elaboration Order
34671 To see the elaboration order that the binder chooses, you can look at
34672 the last part of the file:@cite{b~xxx.adb} binder output file. Here is an example:
34674 @example
34675 System.Soft_Links'Elab_Body;
34676 E14 := True;
34677 System.Secondary_Stack'Elab_Body;
34678 E18 := True;
34679 System.Exception_Table'Elab_Body;
34680 E24 := True;
34681 Ada.Io_Exceptions'Elab_Spec;
34682 E67 := True;
34683 Ada.Tags'Elab_Spec;
34684 Ada.Streams'Elab_Spec;
34685 E43 := True;
34686 Interfaces.C'Elab_Spec;
34687 E69 := True;
34688 System.Finalization_Root'Elab_Spec;
34689 E60 := True;
34690 System.Os_Lib'Elab_Body;
34691 E71 := True;
34692 System.Finalization_Implementation'Elab_Spec;
34693 System.Finalization_Implementation'Elab_Body;
34694 E62 := True;
34695 Ada.Finalization'Elab_Spec;
34696 E58 := True;
34697 Ada.Finalization.List_Controller'Elab_Spec;
34698 E76 := True;
34699 System.File_Control_Block'Elab_Spec;
34700 E74 := True;
34701 System.File_Io'Elab_Body;
34702 E56 := True;
34703 Ada.Tags'Elab_Body;
34704 E45 := True;
34705 Ada.Text_Io'Elab_Spec;
34706 Ada.Text_Io'Elab_Body;
34707 E07 := True;
34708 @end example
34710 Here Elab_Spec elaborates the spec
34711 and Elab_Body elaborates the body. The assignments to the @code{E@emph{xx}} flags
34712 flag that the corresponding body is now elaborated.
34714 You can also ask the binder to generate a more
34715 readable list of the elaboration order using the
34716 @cite{-l} switch when invoking the binder. Here is
34717 an example of the output generated by this switch:
34719 @example
34720 ada (spec)
34721 interfaces (spec)
34722 system (spec)
34723 system.case_util (spec)
34724 system.case_util (body)
34725 system.concat_2 (spec)
34726 system.concat_2 (body)
34727 system.concat_3 (spec)
34728 system.concat_3 (body)
34729 system.htable (spec)
34730 system.parameters (spec)
34731 system.parameters (body)
34732 system.crtl (spec)
34733 interfaces.c_streams (spec)
34734 interfaces.c_streams (body)
34735 system.restrictions (spec)
34736 system.restrictions (body)
34737 system.standard_library (spec)
34738 system.exceptions (spec)
34739 system.exceptions (body)
34740 system.storage_elements (spec)
34741 system.storage_elements (body)
34742 system.secondary_stack (spec)
34743 system.stack_checking (spec)
34744 system.stack_checking (body)
34745 system.string_hash (spec)
34746 system.string_hash (body)
34747 system.htable (body)
34748 system.strings (spec)
34749 system.strings (body)
34750 system.traceback (spec)
34751 system.traceback (body)
34752 system.traceback_entries (spec)
34753 system.traceback_entries (body)
34754 ada.exceptions (spec)
34755 ada.exceptions.last_chance_handler (spec)
34756 system.soft_links (spec)
34757 system.soft_links (body)
34758 ada.exceptions.last_chance_handler (body)
34759 system.secondary_stack (body)
34760 system.exception_table (spec)
34761 system.exception_table (body)
34762 ada.io_exceptions (spec)
34763 ada.tags (spec)
34764 ada.streams (spec)
34765 interfaces.c (spec)
34766 interfaces.c (body)
34767 system.finalization_root (spec)
34768 system.finalization_root (body)
34769 system.memory (spec)
34770 system.memory (body)
34771 system.standard_library (body)
34772 system.os_lib (spec)
34773 system.os_lib (body)
34774 system.unsigned_types (spec)
34775 system.stream_attributes (spec)
34776 system.stream_attributes (body)
34777 system.finalization_implementation (spec)
34778 system.finalization_implementation (body)
34779 ada.finalization (spec)
34780 ada.finalization (body)
34781 ada.finalization.list_controller (spec)
34782 ada.finalization.list_controller (body)
34783 system.file_control_block (spec)
34784 system.file_io (spec)
34785 system.file_io (body)
34786 system.val_uns (spec)
34787 system.val_util (spec)
34788 system.val_util (body)
34789 system.val_uns (body)
34790 system.wch_con (spec)
34791 system.wch_con (body)
34792 system.wch_cnv (spec)
34793 system.wch_jis (spec)
34794 system.wch_jis (body)
34795 system.wch_cnv (body)
34796 system.wch_stw (spec)
34797 system.wch_stw (body)
34798 ada.tags (body)
34799 ada.exceptions (body)
34800 ada.text_io (spec)
34801 ada.text_io (body)
34802 text_io (spec)
34803 gdbstr (body)
34804 @end example
34806 @node Inline Assembler,GNU Free Documentation License,Elaboration Order Handling in GNAT,Top
34807 @anchor{gnat_ugn/inline_assembler inline-assembler}@anchor{12}@anchor{gnat_ugn/inline_assembler doc}@anchor{2cc}@anchor{gnat_ugn/inline_assembler id1}@anchor{2cd}
34808 @chapter Inline Assembler
34811 @geindex Inline Assembler
34813 If you need to write low-level software that interacts directly
34814 with the hardware, Ada provides two ways to incorporate assembly
34815 language code into your program.  First, you can import and invoke
34816 external routines written in assembly language, an Ada feature fully
34817 supported by GNAT.  However, for small sections of code it may be simpler
34818 or more efficient to include assembly language statements directly
34819 in your Ada source program, using the facilities of the implementation-defined
34820 package @cite{System.Machine_Code}, which incorporates the gcc
34821 Inline Assembler.  The Inline Assembler approach offers a number of advantages,
34822 including the following:
34825 @itemize *
34827 @item 
34828 No need to use non-Ada tools
34830 @item 
34831 Consistent interface over different targets
34833 @item 
34834 Automatic usage of the proper calling conventions
34836 @item 
34837 Access to Ada constants and variables
34839 @item 
34840 Definition of intrinsic routines
34842 @item 
34843 Possibility of inlining a subprogram comprising assembler code
34845 @item 
34846 Code optimizer can take Inline Assembler code into account
34847 @end itemize
34849 This appendix presents a series of examples to show you how to use
34850 the Inline Assembler.  Although it focuses on the Intel x86,
34851 the general approach applies also to other processors.
34852 It is assumed that you are familiar with Ada
34853 and with assembly language programming.
34855 @menu
34856 * Basic Assembler Syntax:: 
34857 * A Simple Example of Inline Assembler:: 
34858 * Output Variables in Inline Assembler:: 
34859 * Input Variables in Inline Assembler:: 
34860 * Inlining Inline Assembler Code:: 
34861 * Other Asm Functionality:: 
34863 @end menu
34865 @node Basic Assembler Syntax,A Simple Example of Inline Assembler,,Inline Assembler
34866 @anchor{gnat_ugn/inline_assembler id2}@anchor{2ce}@anchor{gnat_ugn/inline_assembler basic-assembler-syntax}@anchor{2cf}
34867 @section Basic Assembler Syntax
34870 The assembler used by GNAT and gcc is based not on the Intel assembly
34871 language, but rather on a language that descends from the AT&T Unix
34872 assembler @emph{as} (and which is often referred to as 'AT&T syntax').
34873 The following table summarizes the main features of @emph{as} syntax
34874 and points out the differences from the Intel conventions.
34875 See the gcc @emph{as} and @emph{gas} (an @emph{as} macro
34876 pre-processor) documentation for further information.
34879 @display
34880 @emph{Register names}@w{ }
34881 @display
34882 gcc / @emph{as}: Prefix with '%'; for example @cite{%eax}@w{ }
34883 Intel: No extra punctuation; for example @cite{eax}@w{ }
34884 @end display
34885 @end display
34890 @display
34891 @emph{Immediate operand}@w{ }
34892 @display
34893 gcc / @emph{as}: Prefix with '$'; for example @cite{$4}@w{ }
34894 Intel: No extra punctuation; for example @cite{4}@w{ }
34895 @end display
34896 @end display
34901 @display
34902 @emph{Address}@w{ }
34903 @display
34904 gcc / @emph{as}: Prefix with '$'; for example @cite{$loc}@w{ }
34905 Intel: No extra punctuation; for example @cite{loc}@w{ }
34906 @end display
34907 @end display
34912 @display
34913 @emph{Memory contents}@w{ }
34914 @display
34915 gcc / @emph{as}: No extra punctuation; for example @cite{loc}@w{ }
34916 Intel: Square brackets; for example @cite{[loc]}@w{ }
34917 @end display
34918 @end display
34923 @display
34924 @emph{Register contents}@w{ }
34925 @display
34926 gcc / @emph{as}: Parentheses; for example @cite{(%eax)}@w{ }
34927 Intel: Square brackets; for example @cite{[eax]}@w{ }
34928 @end display
34929 @end display
34934 @display
34935 @emph{Hexadecimal numbers}@w{ }
34936 @display
34937 gcc / @emph{as}: Leading '0x' (C language syntax); for example @cite{0xA0}@w{ }
34938 Intel: Trailing 'h'; for example @cite{A0h}@w{ }
34939 @end display
34940 @end display
34945 @display
34946 @emph{Operand size}@w{ }
34947 @display
34948 gcc / @emph{as}: Explicit in op code; for example @cite{movw} to move a 16-bit word@w{ }
34949 Intel: Implicit, deduced by assembler; for example @cite{mov}@w{ }
34950 @end display
34951 @end display
34956 @display
34957 @emph{Instruction repetition}@w{ }
34958 @display
34959 gcc / @emph{as}: Split into two lines; for example@w{ }
34960 @display
34961 @cite{rep}@w{ }
34962 @cite{stosl}@w{ }
34963 @end display
34964 Intel: Keep on one line; for example @cite{rep stosl}@w{ }
34965 @end display
34966 @end display
34971 @display
34972 @emph{Order of operands}@w{ }
34973 @display
34974 gcc / @emph{as}: Source first; for example @cite{movw $4@comma{} %eax}@w{ }
34975 Intel: Destination first; for example @cite{mov eax@comma{} 4}@w{ }
34976 @end display
34977 @end display
34981 @node A Simple Example of Inline Assembler,Output Variables in Inline Assembler,Basic Assembler Syntax,Inline Assembler
34982 @anchor{gnat_ugn/inline_assembler a-simple-example-of-inline-assembler}@anchor{2d0}@anchor{gnat_ugn/inline_assembler id3}@anchor{2d1}
34983 @section A Simple Example of Inline Assembler
34986 The following example will generate a single assembly language statement,
34987 @cite{nop}, which does nothing.  Despite its lack of run-time effect,
34988 the example will be useful in illustrating the basics of
34989 the Inline Assembler facility.
34991 @quotation
34993 @example
34994 with System.Machine_Code; use System.Machine_Code;
34995 procedure Nothing is
34996 begin
34997    Asm ("nop");
34998 end Nothing;
34999 @end example
35000 @end quotation
35002 @cite{Asm} is a procedure declared in package @cite{System.Machine_Code};
35003 here it takes one parameter, a @emph{template string} that must be a static
35004 expression and that will form the generated instruction.
35005 @cite{Asm} may be regarded as a compile-time procedure that parses
35006 the template string and additional parameters (none here),
35007 from which it generates a sequence of assembly language instructions.
35009 The examples in this chapter will illustrate several of the forms
35010 for invoking @cite{Asm}; a complete specification of the syntax
35011 is found in the @cite{Machine_Code_Insertions} section of the
35012 @cite{GNAT Reference Manual}.
35014 Under the standard GNAT conventions, the @cite{Nothing} procedure
35015 should be in a file named @code{nothing.adb}.
35016 You can build the executable in the usual way:
35018 @quotation
35020 @example
35021 $ gnatmake nothing
35022 @end example
35023 @end quotation
35025 However, the interesting aspect of this example is not its run-time behavior
35026 but rather the generated assembly code.
35027 To see this output, invoke the compiler as follows:
35029 @quotation
35031 @example
35032 $  gcc -c -S -fomit-frame-pointer -gnatp nothing.adb
35033 @end example
35034 @end quotation
35036 where the options are:
35039 @itemize *
35041 @item 
35043 @table @asis
35045 @item @code{-c}
35047 compile only (no bind or link)
35048 @end table
35050 @item 
35052 @table @asis
35054 @item @code{-S}
35056 generate assembler listing
35057 @end table
35059 @item 
35061 @table @asis
35063 @item @code{-fomit-frame-pointer}
35065 do not set up separate stack frames
35066 @end table
35068 @item 
35070 @table @asis
35072 @item @code{-gnatp}
35074 do not add runtime checks
35075 @end table
35076 @end itemize
35078 This gives a human-readable assembler version of the code. The resulting
35079 file will have the same name as the Ada source file, but with a @cite{.s}
35080 extension. In our example, the file @code{nothing.s} has the following
35081 contents:
35083 @quotation
35085 @example
35086 .file "nothing.adb"
35087 gcc2_compiled.:
35088 ___gnu_compiled_ada:
35089 .text
35090    .align 4
35091 .globl __ada_nothing
35092 __ada_nothing:
35093 #APP
35094    nop
35095 #NO_APP
35096    jmp L1
35097    .align 2,0x90
35099    ret
35100 @end example
35101 @end quotation
35103 The assembly code you included is clearly indicated by
35104 the compiler, between the @cite{#APP} and @cite{#NO_APP}
35105 delimiters. The character before the 'APP' and 'NOAPP'
35106 can differ on different targets. For example, GNU/Linux uses '#APP' while
35107 on NT you will see '/APP'.
35109 If you make a mistake in your assembler code (such as using the
35110 wrong size modifier, or using a wrong operand for the instruction) GNAT
35111 will report this error in a temporary file, which will be deleted when
35112 the compilation is finished.  Generating an assembler file will help
35113 in such cases, since you can assemble this file separately using the
35114 @emph{as} assembler that comes with gcc.
35116 Assembling the file using the command
35118 @quotation
35120 @example
35121 $ as nothing.s
35122 @end example
35123 @end quotation
35125 will give you error messages whose lines correspond to the assembler
35126 input file, so you can easily find and correct any mistakes you made.
35127 If there are no errors, @emph{as} will generate an object file
35128 @code{nothing.out}.
35130 @node Output Variables in Inline Assembler,Input Variables in Inline Assembler,A Simple Example of Inline Assembler,Inline Assembler
35131 @anchor{gnat_ugn/inline_assembler id4}@anchor{2d2}@anchor{gnat_ugn/inline_assembler output-variables-in-inline-assembler}@anchor{2d3}
35132 @section Output Variables in Inline Assembler
35135 The examples in this section, showing how to access the processor flags,
35136 illustrate how to specify the destination operands for assembly language
35137 statements.
35139 @quotation
35141 @example
35142 with Interfaces; use Interfaces;
35143 with Ada.Text_IO; use Ada.Text_IO;
35144 with System.Machine_Code; use System.Machine_Code;
35145 procedure Get_Flags is
35146    Flags : Unsigned_32;
35147    use ASCII;
35148 begin
35149    Asm ("pushfl"          & LF & HT & -- push flags on stack
35150         "popl %%eax"      & LF & HT & -- load eax with flags
35151         "movl %%eax, %0",             -- store flags in variable
35152         Outputs => Unsigned_32'Asm_Output ("=g", Flags));
35153    Put_Line ("Flags register:" & Flags'Img);
35154 end Get_Flags;
35155 @end example
35156 @end quotation
35158 In order to have a nicely aligned assembly listing, we have separated
35159 multiple assembler statements in the Asm template string with linefeed
35160 (ASCII.LF) and horizontal tab (ASCII.HT) characters.
35161 The resulting section of the assembly output file is:
35163 @quotation
35165 @example
35166 #APP
35167    pushfl
35168    popl %eax
35169    movl %eax, -40(%ebp)
35170 #NO_APP
35171 @end example
35172 @end quotation
35174 It would have been legal to write the Asm invocation as:
35176 @quotation
35178 @example
35179 Asm ("pushfl popl %%eax movl %%eax, %0")
35180 @end example
35181 @end quotation
35183 but in the generated assembler file, this would come out as:
35185 @quotation
35187 @example
35188 #APP
35189    pushfl popl %eax movl %eax, -40(%ebp)
35190 #NO_APP
35191 @end example
35192 @end quotation
35194 which is not so convenient for the human reader.
35196 We use Ada comments
35197 at the end of each line to explain what the assembler instructions
35198 actually do.  This is a useful convention.
35200 When writing Inline Assembler instructions, you need to precede each register
35201 and variable name with a percent sign.  Since the assembler already requires
35202 a percent sign at the beginning of a register name, you need two consecutive
35203 percent signs for such names in the Asm template string, thus @cite{%%eax}.
35204 In the generated assembly code, one of the percent signs will be stripped off.
35206 Names such as @cite{%0}, @cite{%1}, @cite{%2}, etc., denote input or output
35207 variables: operands you later define using @cite{Input} or @cite{Output}
35208 parameters to @cite{Asm}.
35209 An output variable is illustrated in
35210 the third statement in the Asm template string:
35212 @quotation
35214 @example
35215 movl %%eax, %0
35216 @end example
35217 @end quotation
35219 The intent is to store the contents of the eax register in a variable that can
35220 be accessed in Ada.  Simply writing @cite{movl %%eax@comma{} Flags} would not
35221 necessarily work, since the compiler might optimize by using a register
35222 to hold Flags, and the expansion of the @cite{movl} instruction would not be
35223 aware of this optimization.  The solution is not to store the result directly
35224 but rather to advise the compiler to choose the correct operand form;
35225 that is the purpose of the @cite{%0} output variable.
35227 Information about the output variable is supplied in the @cite{Outputs}
35228 parameter to @cite{Asm}:
35230 @quotation
35232 @example
35233 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
35234 @end example
35235 @end quotation
35237 The output is defined by the @cite{Asm_Output} attribute of the target type;
35238 the general format is
35240 @quotation
35242 @example
35243 Type'Asm_Output (constraint_string, variable_name)
35244 @end example
35245 @end quotation
35247 The constraint string directs the compiler how
35248 to store/access the associated variable.  In the example
35250 @quotation
35252 @example
35253 Unsigned_32'Asm_Output ("=m", Flags);
35254 @end example
35255 @end quotation
35257 the @cite{"m"} (memory) constraint tells the compiler that the variable
35258 @cite{Flags} should be stored in a memory variable, thus preventing
35259 the optimizer from keeping it in a register.  In contrast,
35261 @quotation
35263 @example
35264 Unsigned_32'Asm_Output ("=r", Flags);
35265 @end example
35266 @end quotation
35268 uses the @cite{"r"} (register) constraint, telling the compiler to
35269 store the variable in a register.
35271 If the constraint is preceded by the equal character '=', it tells
35272 the compiler that the variable will be used to store data into it.
35274 In the @cite{Get_Flags} example, we used the @cite{"g"} (global) constraint,
35275 allowing the optimizer to choose whatever it deems best.
35277 There are a fairly large number of constraints, but the ones that are
35278 most useful (for the Intel x86 processor) are the following:
35280 @quotation
35283 @multitable {xxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} 
35284 @item
35286 @emph{=}
35288 @tab
35290 output constraint
35292 @item
35294 @emph{g}
35296 @tab
35298 global (i.e., can be stored anywhere)
35300 @item
35302 @emph{m}
35304 @tab
35306 in memory
35308 @item
35310 @emph{I}
35312 @tab
35314 a constant
35316 @item
35318 @emph{a}
35320 @tab
35322 use eax
35324 @item
35326 @emph{b}
35328 @tab
35330 use ebx
35332 @item
35334 @emph{c}
35336 @tab
35338 use ecx
35340 @item
35342 @emph{d}
35344 @tab
35346 use edx
35348 @item
35350 @emph{S}
35352 @tab
35354 use esi
35356 @item
35358 @emph{D}
35360 @tab
35362 use edi
35364 @item
35366 @emph{r}
35368 @tab
35370 use one of eax, ebx, ecx or edx
35372 @item
35374 @emph{q}
35376 @tab
35378 use one of eax, ebx, ecx, edx, esi or edi
35380 @end multitable
35382 @end quotation
35384 The full set of constraints is described in the gcc and @emph{as}
35385 documentation; note that it is possible to combine certain constraints
35386 in one constraint string.
35388 You specify the association of an output variable with an assembler operand
35389 through the @code{%@emph{n}} notation, where @emph{n} is a non-negative
35390 integer.  Thus in
35392 @quotation
35394 @example
35395 Asm ("pushfl"          & LF & HT & -- push flags on stack
35396      "popl %%eax"      & LF & HT & -- load eax with flags
35397      "movl %%eax, %0",             -- store flags in variable
35398      Outputs => Unsigned_32'Asm_Output ("=g", Flags));
35399 @end example
35400 @end quotation
35402 @cite{%0} will be replaced in the expanded code by the appropriate operand,
35403 whatever
35404 the compiler decided for the @cite{Flags} variable.
35406 In general, you may have any number of output variables:
35409 @itemize *
35411 @item 
35412 Count the operands starting at 0; thus @cite{%0}, @cite{%1}, etc.
35414 @item 
35415 Specify the @cite{Outputs} parameter as a parenthesized comma-separated list
35416 of @cite{Asm_Output} attributes
35417 @end itemize
35419 For example:
35421 @quotation
35423 @example
35424 Asm ("movl %%eax, %0" & LF & HT &
35425      "movl %%ebx, %1" & LF & HT &
35426      "movl %%ecx, %2",
35427      Outputs => (Unsigned_32'Asm_Output ("=g", Var_A),   --  %0 = Var_A
35428                  Unsigned_32'Asm_Output ("=g", Var_B),   --  %1 = Var_B
35429                  Unsigned_32'Asm_Output ("=g", Var_C))); --  %2 = Var_C
35430 @end example
35431 @end quotation
35433 where @cite{Var_A}, @cite{Var_B}, and @cite{Var_C} are variables
35434 in the Ada program.
35436 As a variation on the @cite{Get_Flags} example, we can use the constraints
35437 string to direct the compiler to store the eax register into the @cite{Flags}
35438 variable, instead of including the store instruction explicitly in the
35439 @cite{Asm} template string:
35441 @quotation
35443 @example
35444 with Interfaces; use Interfaces;
35445 with Ada.Text_IO; use Ada.Text_IO;
35446 with System.Machine_Code; use System.Machine_Code;
35447 procedure Get_Flags_2 is
35448    Flags : Unsigned_32;
35449    use ASCII;
35450 begin
35451    Asm ("pushfl"      & LF & HT & -- push flags on stack
35452         "popl %%eax",             -- save flags in eax
35453         Outputs => Unsigned_32'Asm_Output ("=a", Flags));
35454    Put_Line ("Flags register:" & Flags'Img);
35455 end Get_Flags_2;
35456 @end example
35457 @end quotation
35459 The @cite{"a"} constraint tells the compiler that the @cite{Flags}
35460 variable will come from the eax register. Here is the resulting code:
35462 @quotation
35464 @example
35465 #APP
35466    pushfl
35467    popl %eax
35468 #NO_APP
35469    movl %eax,-40(%ebp)
35470 @end example
35471 @end quotation
35473 The compiler generated the store of eax into Flags after
35474 expanding the assembler code.
35476 Actually, there was no need to pop the flags into the eax register;
35477 more simply, we could just pop the flags directly into the program variable:
35479 @quotation
35481 @example
35482 with Interfaces; use Interfaces;
35483 with Ada.Text_IO; use Ada.Text_IO;
35484 with System.Machine_Code; use System.Machine_Code;
35485 procedure Get_Flags_3 is
35486    Flags : Unsigned_32;
35487    use ASCII;
35488 begin
35489    Asm ("pushfl"  & LF & HT & -- push flags on stack
35490         "pop %0",             -- save flags in Flags
35491         Outputs => Unsigned_32'Asm_Output ("=g", Flags));
35492    Put_Line ("Flags register:" & Flags'Img);
35493 end Get_Flags_3;
35494 @end example
35495 @end quotation
35497 @node Input Variables in Inline Assembler,Inlining Inline Assembler Code,Output Variables in Inline Assembler,Inline Assembler
35498 @anchor{gnat_ugn/inline_assembler id5}@anchor{2d4}@anchor{gnat_ugn/inline_assembler input-variables-in-inline-assembler}@anchor{2d5}
35499 @section Input Variables in Inline Assembler
35502 The example in this section illustrates how to specify the source operands
35503 for assembly language statements.
35504 The program simply increments its input value by 1:
35506 @quotation
35508 @example
35509 with Interfaces; use Interfaces;
35510 with Ada.Text_IO; use Ada.Text_IO;
35511 with System.Machine_Code; use System.Machine_Code;
35512 procedure Increment is
35514    function Incr (Value : Unsigned_32) return Unsigned_32 is
35515       Result : Unsigned_32;
35516    begin
35517       Asm ("incl %0",
35518            Outputs => Unsigned_32'Asm_Output ("=a", Result),
35519            Inputs  => Unsigned_32'Asm_Input ("a", Value));
35520       return Result;
35521    end Incr;
35523    Value : Unsigned_32;
35525 begin
35526    Value := 5;
35527    Put_Line ("Value before is" & Value'Img);
35528    Value := Incr (Value);
35529   Put_Line ("Value after is" & Value'Img);
35530 end Increment;
35531 @end example
35532 @end quotation
35534 The @cite{Outputs} parameter to @cite{Asm} specifies
35535 that the result will be in the eax register and that it is to be stored
35536 in the @cite{Result} variable.
35538 The @cite{Inputs} parameter looks much like the @cite{Outputs} parameter,
35539 but with an @cite{Asm_Input} attribute.
35540 The @cite{"="} constraint, indicating an output value, is not present.
35542 You can have multiple input variables, in the same way that you can have more
35543 than one output variable.
35545 The parameter count (%0, %1) etc, still starts at the first output statement,
35546 and continues with the input statements.
35548 Just as the @cite{Outputs} parameter causes the register to be stored into the
35549 target variable after execution of the assembler statements, so does the
35550 @cite{Inputs} parameter cause its variable to be loaded into the register
35551 before execution of the assembler statements.
35553 Thus the effect of the @cite{Asm} invocation is:
35556 @itemize *
35558 @item 
35559 load the 32-bit value of @cite{Value} into eax
35561 @item 
35562 execute the @cite{incl %eax} instruction
35564 @item 
35565 store the contents of eax into the @cite{Result} variable
35566 @end itemize
35568 The resulting assembler file (with @emph{-O2} optimization) contains:
35570 @quotation
35572 @example
35573 _increment__incr.1:
35574    subl $4,%esp
35575    movl 8(%esp),%eax
35576 #APP
35577    incl %eax
35578 #NO_APP
35579    movl %eax,%edx
35580    movl %ecx,(%esp)
35581    addl $4,%esp
35582    ret
35583 @end example
35584 @end quotation
35586 @node Inlining Inline Assembler Code,Other Asm Functionality,Input Variables in Inline Assembler,Inline Assembler
35587 @anchor{gnat_ugn/inline_assembler id6}@anchor{2d6}@anchor{gnat_ugn/inline_assembler inlining-inline-assembler-code}@anchor{2d7}
35588 @section Inlining Inline Assembler Code
35591 For a short subprogram such as the @cite{Incr} function in the previous
35592 section, the overhead of the call and return (creating / deleting the stack
35593 frame) can be significant, compared to the amount of code in the subprogram
35594 body.  A solution is to apply Ada's @cite{Inline} pragma to the subprogram,
35595 which directs the compiler to expand invocations of the subprogram at the
35596 point(s) of call, instead of setting up a stack frame for out-of-line calls.
35597 Here is the resulting program:
35599 @quotation
35601 @example
35602 with Interfaces; use Interfaces;
35603 with Ada.Text_IO; use Ada.Text_IO;
35604 with System.Machine_Code; use System.Machine_Code;
35605 procedure Increment_2 is
35607    function Incr (Value : Unsigned_32) return Unsigned_32 is
35608       Result : Unsigned_32;
35609    begin
35610       Asm ("incl %0",
35611            Outputs => Unsigned_32'Asm_Output ("=a", Result),
35612            Inputs  => Unsigned_32'Asm_Input ("a", Value));
35613       return Result;
35614    end Incr;
35615    pragma Inline (Increment);
35617    Value : Unsigned_32;
35619 begin
35620    Value := 5;
35621    Put_Line ("Value before is" & Value'Img);
35622    Value := Increment (Value);
35623    Put_Line ("Value after is" & Value'Img);
35624 end Increment_2;
35625 @end example
35626 @end quotation
35628 Compile the program with both optimization (@emph{-O2}) and inlining
35629 (@emph{-gnatn}) enabled.
35631 The @cite{Incr} function is still compiled as usual, but at the
35632 point in @cite{Increment} where our function used to be called:
35634 @quotation
35636 @example
35637 pushl %edi
35638 call _increment__incr.1
35639 @end example
35640 @end quotation
35642 the code for the function body directly appears:
35644 @quotation
35646 @example
35647 movl %esi,%eax
35648 #APP
35649    incl %eax
35650 #NO_APP
35651    movl %eax,%edx
35652 @end example
35653 @end quotation
35655 thus saving the overhead of stack frame setup and an out-of-line call.
35657 @node Other Asm Functionality,,Inlining Inline Assembler Code,Inline Assembler
35658 @anchor{gnat_ugn/inline_assembler other-asm-functionality}@anchor{2d8}@anchor{gnat_ugn/inline_assembler id7}@anchor{2d9}
35659 @section Other @cite{Asm} Functionality
35662 This section describes two important parameters to the @cite{Asm}
35663 procedure: @cite{Clobber}, which identifies register usage;
35664 and @cite{Volatile}, which inhibits unwanted optimizations.
35666 @menu
35667 * The Clobber Parameter:: 
35668 * The Volatile Parameter:: 
35670 @end menu
35672 @node The Clobber Parameter,The Volatile Parameter,,Other Asm Functionality
35673 @anchor{gnat_ugn/inline_assembler the-clobber-parameter}@anchor{2da}@anchor{gnat_ugn/inline_assembler id8}@anchor{2db}
35674 @subsection The @cite{Clobber} Parameter
35677 One of the dangers of intermixing assembly language and a compiled language
35678 such as Ada is that the compiler needs to be aware of which registers are
35679 being used by the assembly code.  In some cases, such as the earlier examples,
35680 the constraint string is sufficient to indicate register usage (e.g.,
35681 @cite{"a"} for
35682 the eax register).  But more generally, the compiler needs an explicit
35683 identification of the registers that are used by the Inline Assembly
35684 statements.
35686 Using a register that the compiler doesn't know about
35687 could be a side effect of an instruction (like @cite{mull}
35688 storing its result in both eax and edx).
35689 It can also arise from explicit register usage in your
35690 assembly code; for example:
35692 @quotation
35694 @example
35695 Asm ("movl %0, %%ebx" & LF & HT &
35696      "movl %%ebx, %1",
35697      Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
35698      Inputs  => Unsigned_32'Asm_Input  ("g", Var_In));
35699 @end example
35700 @end quotation
35702 where the compiler (since it does not analyze the @cite{Asm} template string)
35703 does not know you are using the ebx register.
35705 In such cases you need to supply the @cite{Clobber} parameter to @cite{Asm},
35706 to identify the registers that will be used by your assembly code:
35708 @quotation
35710 @example
35711 Asm ("movl %0, %%ebx" & LF & HT &
35712      "movl %%ebx, %1",
35713      Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
35714      Inputs  => Unsigned_32'Asm_Input  ("g", Var_In),
35715      Clobber => "ebx");
35716 @end example
35717 @end quotation
35719 The Clobber parameter is a static string expression specifying the
35720 register(s) you are using.  Note that register names are @emph{not} prefixed
35721 by a percent sign. Also, if more than one register is used then their names
35722 are separated by commas; e.g., @cite{"eax@comma{} ebx"}
35724 The @cite{Clobber} parameter has several additional uses:
35727 @itemize *
35729 @item 
35730 Use 'register' name @cite{cc} to indicate that flags might have changed
35732 @item 
35733 Use 'register' name @cite{memory} if you changed a memory location
35734 @end itemize
35736 @node The Volatile Parameter,,The Clobber Parameter,Other Asm Functionality
35737 @anchor{gnat_ugn/inline_assembler the-volatile-parameter}@anchor{2dc}@anchor{gnat_ugn/inline_assembler id9}@anchor{2dd}
35738 @subsection The @cite{Volatile} Parameter
35741 @geindex Volatile parameter
35743 Compiler optimizations in the presence of Inline Assembler may sometimes have
35744 unwanted effects.  For example, when an @cite{Asm} invocation with an input
35745 variable is inside a loop, the compiler might move the loading of the input
35746 variable outside the loop, regarding it as a one-time initialization.
35748 If this effect is not desired, you can disable such optimizations by setting
35749 the @cite{Volatile} parameter to @cite{True}; for example:
35751 @quotation
35753 @example
35754 Asm ("movl %0, %%ebx" & LF & HT &
35755      "movl %%ebx, %1",
35756      Outputs  => Unsigned_32'Asm_Output ("=g", Var_Out),
35757      Inputs   => Unsigned_32'Asm_Input  ("g", Var_In),
35758      Clobber  => "ebx",
35759      Volatile => True);
35760 @end example
35761 @end quotation
35763 By default, @cite{Volatile} is set to @cite{False} unless there is no
35764 @cite{Outputs} parameter.
35766 Although setting @cite{Volatile} to @cite{True} prevents unwanted
35767 optimizations, it will also disable other optimizations that might be
35768 important for efficiency. In general, you should set @cite{Volatile}
35769 to @cite{True} only if the compiler's optimizations have created
35770 problems.
35772 @node GNU Free Documentation License,Index,Inline Assembler,Top
35773 @anchor{share/gnu_free_documentation_license gnu-fdl}@anchor{1}@anchor{share/gnu_free_documentation_license doc}@anchor{2de}@anchor{share/gnu_free_documentation_license gnu-free-documentation-license}@anchor{2df}
35774 @chapter GNU Free Documentation License
35777 Version 1.3, 3 November 2008
35779 Copyright  2000, 2001, 2002, 2007, 2008  Free Software Foundation, Inc
35780 @indicateurl{http://fsf.org/}
35782 Everyone is permitted to copy and distribute verbatim copies of this
35783 license document, but changing it is not allowed.
35785 @strong{Preamble}
35787 The purpose of this License is to make a manual, textbook, or other
35788 functional and useful document "free" in the sense of freedom: to
35789 assure everyone the effective freedom to copy and redistribute it,
35790 with or without modifying it, either commercially or noncommercially.
35791 Secondarily, this License preserves for the author and publisher a way
35792 to get credit for their work, while not being considered responsible
35793 for modifications made by others.
35795 This License is a kind of "copyleft", which means that derivative
35796 works of the document must themselves be free in the same sense.  It
35797 complements the GNU General Public License, which is a copyleft
35798 license designed for free software.
35800 We have designed this License in order to use it for manuals for free
35801 software, because free software needs free documentation: a free
35802 program should come with manuals providing the same freedoms that the
35803 software does.  But this License is not limited to software manuals;
35804 it can be used for any textual work, regardless of subject matter or
35805 whether it is published as a printed book.  We recommend this License
35806 principally for works whose purpose is instruction or reference.
35808 @strong{1. APPLICABILITY AND DEFINITIONS}
35810 This License applies to any manual or other work, in any medium, that
35811 contains a notice placed by the copyright holder saying it can be
35812 distributed under the terms of this License.  Such a notice grants a
35813 world-wide, royalty-free license, unlimited in duration, to use that
35814 work under the conditions stated herein.  The @strong{Document}, below,
35815 refers to any such manual or work.  Any member of the public is a
35816 licensee, and is addressed as "@strong{you}".  You accept the license if you
35817 copy, modify or distribute the work in a way requiring permission
35818 under copyright law.
35820 A "@strong{Modified Version}" of the Document means any work containing the
35821 Document or a portion of it, either copied verbatim, or with
35822 modifications and/or translated into another language.
35824 A "@strong{Secondary Section}" is a named appendix or a front-matter section of
35825 the Document that deals exclusively with the relationship of the
35826 publishers or authors of the Document to the Document's overall subject
35827 (or to related matters) and contains nothing that could fall directly
35828 within that overall subject.  (Thus, if the Document is in part a
35829 textbook of mathematics, a Secondary Section may not explain any
35830 mathematics.)  The relationship could be a matter of historical
35831 connection with the subject or with related matters, or of legal,
35832 commercial, philosophical, ethical or political position regarding
35833 them.
35835 The "@strong{Invariant Sections}" are certain Secondary Sections whose titles
35836 are designated, as being those of Invariant Sections, in the notice
35837 that says that the Document is released under this License.  If a
35838 section does not fit the above definition of Secondary then it is not
35839 allowed to be designated as Invariant.  The Document may contain zero
35840 Invariant Sections.  If the Document does not identify any Invariant
35841 Sections then there are none.
35843 The "@strong{Cover Texts}" are certain short passages of text that are listed,
35844 as Front-Cover Texts or Back-Cover Texts, in the notice that says that
35845 the Document is released under this License.  A Front-Cover Text may
35846 be at most 5 words, and a Back-Cover Text may be at most 25 words.
35848 A "@strong{Transparent}" copy of the Document means a machine-readable copy,
35849 represented in a format whose specification is available to the
35850 general public, that is suitable for revising the document
35851 straightforwardly with generic text editors or (for images composed of
35852 pixels) generic paint programs or (for drawings) some widely available
35853 drawing editor, and that is suitable for input to text formatters or
35854 for automatic translation to a variety of formats suitable for input
35855 to text formatters.  A copy made in an otherwise Transparent file
35856 format whose markup, or absence of markup, has been arranged to thwart
35857 or discourage subsequent modification by readers is not Transparent.
35858 An image format is not Transparent if used for any substantial amount
35859 of text.  A copy that is not "Transparent" is called @strong{Opaque}.
35861 Examples of suitable formats for Transparent copies include plain
35862 ASCII without markup, Texinfo input format, LaTeX input format, SGML
35863 or XML using a publicly available DTD, and standard-conforming simple
35864 HTML, PostScript or PDF designed for human modification.  Examples of
35865 transparent image formats include PNG, XCF and JPG.  Opaque formats
35866 include proprietary formats that can be read and edited only by
35867 proprietary word processors, SGML or XML for which the DTD and/or
35868 processing tools are not generally available, and the
35869 machine-generated HTML, PostScript or PDF produced by some word
35870 processors for output purposes only.
35872 The "@strong{Title Page}" means, for a printed book, the title page itself,
35873 plus such following pages as are needed to hold, legibly, the material
35874 this License requires to appear in the title page.  For works in
35875 formats which do not have any title page as such, "Title Page" means
35876 the text near the most prominent appearance of the work's title,
35877 preceding the beginning of the body of the text.
35879 The "@strong{publisher}" means any person or entity that distributes
35880 copies of the Document to the public.
35882 A section "@strong{Entitled XYZ}" means a named subunit of the Document whose
35883 title either is precisely XYZ or contains XYZ in parentheses following
35884 text that translates XYZ in another language.  (Here XYZ stands for a
35885 specific section name mentioned below, such as "@strong{Acknowledgements}",
35886 "@strong{Dedications}", "@strong{Endorsements}", or "@strong{History}".)
35887 To "@strong{Preserve the Title}"
35888 of such a section when you modify the Document means that it remains a
35889 section "Entitled XYZ" according to this definition.
35891 The Document may include Warranty Disclaimers next to the notice which
35892 states that this License applies to the Document.  These Warranty
35893 Disclaimers are considered to be included by reference in this
35894 License, but only as regards disclaiming warranties: any other
35895 implication that these Warranty Disclaimers may have is void and has
35896 no effect on the meaning of this License.
35898 @strong{2. VERBATIM COPYING}
35900 You may copy and distribute the Document in any medium, either
35901 commercially or noncommercially, provided that this License, the
35902 copyright notices, and the license notice saying this License applies
35903 to the Document are reproduced in all copies, and that you add no other
35904 conditions whatsoever to those of this License.  You may not use
35905 technical measures to obstruct or control the reading or further
35906 copying of the copies you make or distribute.  However, you may accept
35907 compensation in exchange for copies.  If you distribute a large enough
35908 number of copies you must also follow the conditions in section 3.
35910 You may also lend copies, under the same conditions stated above, and
35911 you may publicly display copies.
35913 @strong{3. COPYING IN QUANTITY}
35915 If you publish printed copies (or copies in media that commonly have
35916 printed covers) of the Document, numbering more than 100, and the
35917 Document's license notice requires Cover Texts, you must enclose the
35918 copies in covers that carry, clearly and legibly, all these Cover
35919 Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on
35920 the back cover.  Both covers must also clearly and legibly identify
35921 you as the publisher of these copies.  The front cover must present
35922 the full title with all words of the title equally prominent and
35923 visible.  You may add other material on the covers in addition.
35924 Copying with changes limited to the covers, as long as they preserve
35925 the title of the Document and satisfy these conditions, can be treated
35926 as verbatim copying in other respects.
35928 If the required texts for either cover are too voluminous to fit
35929 legibly, you should put the first ones listed (as many as fit
35930 reasonably) on the actual cover, and continue the rest onto adjacent
35931 pages.
35933 If you publish or distribute Opaque copies of the Document numbering
35934 more than 100, you must either include a machine-readable Transparent
35935 copy along with each Opaque copy, or state in or with each Opaque copy
35936 a computer-network location from which the general network-using
35937 public has access to download using public-standard network protocols
35938 a complete Transparent copy of the Document, free of added material.
35939 If you use the latter option, you must take reasonably prudent steps,
35940 when you begin distribution of Opaque copies in quantity, to ensure
35941 that this Transparent copy will remain thus accessible at the stated
35942 location until at least one year after the last time you distribute an
35943 Opaque copy (directly or through your agents or retailers) of that
35944 edition to the public.
35946 It is requested, but not required, that you contact the authors of the
35947 Document well before redistributing any large number of copies, to give
35948 them a chance to provide you with an updated version of the Document.
35950 @strong{4. MODIFICATIONS}
35952 You may copy and distribute a Modified Version of the Document under
35953 the conditions of sections 2 and 3 above, provided that you release
35954 the Modified Version under precisely this License, with the Modified
35955 Version filling the role of the Document, thus licensing distribution
35956 and modification of the Modified Version to whoever possesses a copy
35957 of it.  In addition, you must do these things in the Modified Version:
35960 @enumerate A
35962 @item 
35963 Use in the Title Page (and on the covers, if any) a title distinct
35964 from that of the Document, and from those of previous versions
35965 (which should, if there were any, be listed in the History section
35966 of the Document).  You may use the same title as a previous version
35967 if the original publisher of that version gives permission.
35969 @item 
35970 List on the Title Page, as authors, one or more persons or entities
35971 responsible for authorship of the modifications in the Modified
35972 Version, together with at least five of the principal authors of the
35973 Document (all of its principal authors, if it has fewer than five),
35974 unless they release you from this requirement.
35976 @item 
35977 State on the Title page the name of the publisher of the
35978 Modified Version, as the publisher.
35980 @item 
35981 Preserve all the copyright notices of the Document.
35983 @item 
35984 Add an appropriate copyright notice for your modifications
35985 adjacent to the other copyright notices.
35987 @item 
35988 Include, immediately after the copyright notices, a license notice
35989 giving the public permission to use the Modified Version under the
35990 terms of this License, in the form shown in the Addendum below.
35992 @item 
35993 Preserve in that license notice the full lists of Invariant Sections
35994 and required Cover Texts given in the Document's license notice.
35996 @item 
35997 Include an unaltered copy of this License.
35999 @item 
36000 Preserve the section Entitled "History", Preserve its Title, and add
36001 to it an item stating at least the title, year, new authors, and
36002 publisher of the Modified Version as given on the Title Page.  If
36003 there is no section Entitled "History" in the Document, create one
36004 stating the title, year, authors, and publisher of the Document as
36005 given on its Title Page, then add an item describing the Modified
36006 Version as stated in the previous sentence.
36008 @item 
36009 Preserve the network location, if any, given in the Document for
36010 public access to a Transparent copy of the Document, and likewise
36011 the network locations given in the Document for previous versions
36012 it was based on.  These may be placed in the "History" section.
36013 You may omit a network location for a work that was published at
36014 least four years before the Document itself, or if the original
36015 publisher of the version it refers to gives permission.
36017 @item 
36018 For any section Entitled "Acknowledgements" or "Dedications",
36019 Preserve the Title of the section, and preserve in the section all
36020 the substance and tone of each of the contributor acknowledgements
36021 and/or dedications given therein.
36023 @item 
36024 Preserve all the Invariant Sections of the Document,
36025 unaltered in their text and in their titles.  Section numbers
36026 or the equivalent are not considered part of the section titles.
36028 @item 
36029 Delete any section Entitled "Endorsements".  Such a section
36030 may not be included in the Modified Version.
36032 @item 
36033 Do not retitle any existing section to be Entitled "Endorsements"
36034 or to conflict in title with any Invariant Section.
36036 @item 
36037 Preserve any Warranty Disclaimers.
36038 @end enumerate
36040 If the Modified Version includes new front-matter sections or
36041 appendices that qualify as Secondary Sections and contain no material
36042 copied from the Document, you may at your option designate some or all
36043 of these sections as invariant.  To do this, add their titles to the
36044 list of Invariant Sections in the Modified Version's license notice.
36045 These titles must be distinct from any other section titles.
36047 You may add a section Entitled "Endorsements", provided it contains
36048 nothing but endorsements of your Modified Version by various
36049 parties---for example, statements of peer review or that the text has
36050 been approved by an organization as the authoritative definition of a
36051 standard.
36053 You may add a passage of up to five words as a Front-Cover Text, and a
36054 passage of up to 25 words as a Back-Cover Text, to the end of the list
36055 of Cover Texts in the Modified Version.  Only one passage of
36056 Front-Cover Text and one of Back-Cover Text may be added by (or
36057 through arrangements made by) any one entity.  If the Document already
36058 includes a cover text for the same cover, previously added by you or
36059 by arrangement made by the same entity you are acting on behalf of,
36060 you may not add another; but you may replace the old one, on explicit
36061 permission from the previous publisher that added the old one.
36063 The author(s) and publisher(s) of the Document do not by this License
36064 give permission to use their names for publicity for or to assert or
36065 imply endorsement of any Modified Version.
36067 @strong{5. COMBINING DOCUMENTS}
36069 You may combine the Document with other documents released under this
36070 License, under the terms defined in section 4 above for modified
36071 versions, provided that you include in the combination all of the
36072 Invariant Sections of all of the original documents, unmodified, and
36073 list them all as Invariant Sections of your combined work in its
36074 license notice, and that you preserve all their Warranty Disclaimers.
36076 The combined work need only contain one copy of this License, and
36077 multiple identical Invariant Sections may be replaced with a single
36078 copy.  If there are multiple Invariant Sections with the same name but
36079 different contents, make the title of each such section unique by
36080 adding at the end of it, in parentheses, the name of the original
36081 author or publisher of that section if known, or else a unique number.
36082 Make the same adjustment to the section titles in the list of
36083 Invariant Sections in the license notice of the combined work.
36085 In the combination, you must combine any sections Entitled "History"
36086 in the various original documents, forming one section Entitled
36087 "History"; likewise combine any sections Entitled "Acknowledgements",
36088 and any sections Entitled "Dedications".  You must delete all sections
36089 Entitled "Endorsements".
36091 @strong{6. COLLECTIONS OF DOCUMENTS}
36093 You may make a collection consisting of the Document and other documents
36094 released under this License, and replace the individual copies of this
36095 License in the various documents with a single copy that is included in
36096 the collection, provided that you follow the rules of this License for
36097 verbatim copying of each of the documents in all other respects.
36099 You may extract a single document from such a collection, and distribute
36100 it individually under this License, provided you insert a copy of this
36101 License into the extracted document, and follow this License in all
36102 other respects regarding verbatim copying of that document.
36104 @strong{7. AGGREGATION WITH INDEPENDENT WORKS}
36106 A compilation of the Document or its derivatives with other separate
36107 and independent documents or works, in or on a volume of a storage or
36108 distribution medium, is called an "aggregate" if the copyright
36109 resulting from the compilation is not used to limit the legal rights
36110 of the compilation's users beyond what the individual works permit.
36111 When the Document is included in an aggregate, this License does not
36112 apply to the other works in the aggregate which are not themselves
36113 derivative works of the Document.
36115 If the Cover Text requirement of section 3 is applicable to these
36116 copies of the Document, then if the Document is less than one half of
36117 the entire aggregate, the Document's Cover Texts may be placed on
36118 covers that bracket the Document within the aggregate, or the
36119 electronic equivalent of covers if the Document is in electronic form.
36120 Otherwise they must appear on printed covers that bracket the whole
36121 aggregate.
36123 @strong{8. TRANSLATION}
36125 Translation is considered a kind of modification, so you may
36126 distribute translations of the Document under the terms of section 4.
36127 Replacing Invariant Sections with translations requires special
36128 permission from their copyright holders, but you may include
36129 translations of some or all Invariant Sections in addition to the
36130 original versions of these Invariant Sections.  You may include a
36131 translation of this License, and all the license notices in the
36132 Document, and any Warranty Disclaimers, provided that you also include
36133 the original English version of this License and the original versions
36134 of those notices and disclaimers.  In case of a disagreement between
36135 the translation and the original version of this License or a notice
36136 or disclaimer, the original version will prevail.
36138 If a section in the Document is Entitled "Acknowledgements",
36139 "Dedications", or "History", the requirement (section 4) to Preserve
36140 its Title (section 1) will typically require changing the actual
36141 title.
36143 @strong{9. TERMINATION}
36145 You may not copy, modify, sublicense, or distribute the Document
36146 except as expressly provided under this License.  Any attempt
36147 otherwise to copy, modify, sublicense, or distribute it is void, and
36148 will automatically terminate your rights under this License.
36150 However, if you cease all violation of this License, then your license
36151 from a particular copyright holder is reinstated (a) provisionally,
36152 unless and until the copyright holder explicitly and finally
36153 terminates your license, and (b) permanently, if the copyright holder
36154 fails to notify you of the violation by some reasonable means prior to
36155 60 days after the cessation.
36157 Moreover, your license from a particular copyright holder is
36158 reinstated permanently if the copyright holder notifies you of the
36159 violation by some reasonable means, this is the first time you have
36160 received notice of violation of this License (for any work) from that
36161 copyright holder, and you cure the violation prior to 30 days after
36162 your receipt of the notice.
36164 Termination of your rights under this section does not terminate the
36165 licenses of parties who have received copies or rights from you under
36166 this License.  If your rights have been terminated and not permanently
36167 reinstated, receipt of a copy of some or all of the same material does
36168 not give you any rights to use it.
36170 @strong{10. FUTURE REVISIONS OF THIS LICENSE}
36172 The Free Software Foundation may publish new, revised versions
36173 of the GNU Free Documentation License from time to time.  Such new
36174 versions will be similar in spirit to the present version, but may
36175 differ in detail to address new problems or concerns.  See
36176 @indicateurl{http://www.gnu.org/copyleft/}.
36178 Each version of the License is given a distinguishing version number.
36179 If the Document specifies that a particular numbered version of this
36180 License "or any later version" applies to it, you have the option of
36181 following the terms and conditions either of that specified version or
36182 of any later version that has been published (not as a draft) by the
36183 Free Software Foundation.  If the Document does not specify a version
36184 number of this License, you may choose any version ever published (not
36185 as a draft) by the Free Software Foundation.  If the Document
36186 specifies that a proxy can decide which future versions of this
36187 License can be used, that proxy's public statement of acceptance of a
36188 version permanently authorizes you to choose that version for the
36189 Document.
36191 @strong{11. RELICENSING}
36193 "Massive Multiauthor Collaboration Site" (or "MMC Site") means any
36194 World Wide Web server that publishes copyrightable works and also
36195 provides prominent facilities for anybody to edit those works.  A
36196 public wiki that anybody can edit is an example of such a server.  A
36197 "Massive Multiauthor Collaboration" (or "MMC") contained in the
36198 site means any set of copyrightable works thus published on the MMC
36199 site.
36201 "CC-BY-SA" means the Creative Commons Attribution-Share Alike 3.0
36202 license published by Creative Commons Corporation, a not-for-profit
36203 corporation with a principal place of business in San Francisco,
36204 California, as well as future copyleft versions of that license
36205 published by that same organization.
36207 "Incorporate" means to publish or republish a Document, in whole or
36208 in part, as part of another Document.
36210 An MMC is "eligible for relicensing" if it is licensed under this
36211 License, and if all works that were first published under this License
36212 somewhere other than this MMC, and subsequently incorporated in whole
36213 or in part into the MMC, (1) had no cover texts or invariant sections,
36214 and (2) were thus incorporated prior to November 1, 2008.
36216 The operator of an MMC Site may republish an MMC contained in the site
36217 under CC-BY-SA on the same site at any time before August 1, 2009,
36218 provided the MMC is eligible for relicensing.
36220 @strong{ADDENDUM: How to use this License for your documents}
36222 To use this License in a document you have written, include a copy of
36223 the License in the document and put the following copyright and
36224 license notices just after the title page:
36226 @quotation
36228 Copyright © YEAR  YOUR NAME.
36229 Permission is granted to copy, distribute and/or modify this document
36230 under the terms of the GNU Free Documentation License, Version 1.3
36231 or any later version published by the Free Software Foundation;
36232 with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
36233 A copy of the license is included in the section entitled "GNU
36234 Free Documentation License".
36235 @end quotation
36237 If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts,
36238 replace the "with ... Texts." line with this:
36240 @quotation
36242 with the Invariant Sections being LIST THEIR TITLES, with the
36243 Front-Cover Texts being LIST, and with the Back-Cover Texts being LIST.
36244 @end quotation
36246 If you have Invariant Sections without Cover Texts, or some other
36247 combination of the three, merge those two alternatives to suit the
36248 situation.
36250 If your document contains nontrivial examples of program code, we
36251 recommend releasing these examples in parallel under your choice of
36252 free software license, such as the GNU General Public License,
36253 to permit their use in free software.
36255 @node Index,,GNU Free Documentation License,Top
36256 @unnumbered Index
36259 @printindex ge
36262 @c %**end of body
36263 @bye