[Ada] Various typo fixes and reformatting of comments
[gcc.git] / gcc / ada / gnat_ugn.texi
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.4.6.@*
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
17
18 @definfoenclose strong,`,'
19 @definfoenclose emph,`,'
20 @c %**end of header
21
22 @copying
23 @quotation
24 GNAT User's Guide for Native Platforms , Jul 01, 2020
25
26 AdaCore
27
28 Copyright @copyright{} 2008-2020, Free Software Foundation
29 @end quotation
30
31 @end copying
32
33 @titlepage
34 @title GNAT User's Guide for Native Platforms
35 @insertcopying
36 @end titlepage
37 @contents
38
39 @c %** start of user preamble
40
41 @c %** end of user preamble
42
43 @ifnottex
44 @node Top
45 @top GNAT User's Guide for Native Platforms
46 @insertcopying
47 @end ifnottex
48
49 @c %**start of body
50 @anchor{gnat_ugn doc}@anchor{0}
51 @emph{GNAT, The GNU Ada Development Environment}
52
53
54 @include gcc-common.texi
55 GCC version @value{version-GCC}@*
56 AdaCore
57
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}.
65
66 @menu
67 * About This Guide::
68 * Getting Started with GNAT::
69 * The GNAT Compilation Model::
70 * Building Executable Programs with GNAT::
71 * GNAT Utility Programs::
72 * GNAT and Program Execution::
73 * Platform-Specific Information::
74 * Example of Binder Output File::
75 * Elaboration Order Handling in GNAT::
76 * Inline Assembler::
77 * GNU Free Documentation License::
78 * Index::
79
80 @detailmenu
81 --- The Detailed Node Listing ---
82
83 About This Guide
84
85 * What This Guide Contains::
86 * What You Should Know before Reading This Guide::
87 * Related Information::
88 * A Note to Readers of Previous Versions of the Manual::
89 * Conventions::
90
91 Getting Started with GNAT
92
93 * Running GNAT::
94 * Running a Simple Ada Program::
95 * Running a Program with Multiple Units::
96 * Using the gnatmake Utility::
97
98 The GNAT Compilation Model
99
100 * Source Representation::
101 * Foreign Language Representation::
102 * File Naming Topics and Utilities::
103 * Configuration Pragmas::
104 * Generating Object Files::
105 * Source Dependencies::
106 * The Ada Library Information Files::
107 * Binding an Ada Program::
108 * GNAT and Libraries::
109 * Conditional Compilation::
110 * Mixed Language Programming::
111 * GNAT and Other Compilation Models::
112 * Using GNAT Files with External Tools::
113
114 Foreign Language Representation
115
116 * Latin-1::
117 * Other 8-Bit Codes::
118 * Wide_Character Encodings::
119 * Wide_Wide_Character Encodings::
120
121 File Naming Topics and Utilities
122
123 * File Naming Rules::
124 * Using Other File Names::
125 * Alternative File Naming Schemes::
126 * Handling Arbitrary File Naming Conventions with gnatname::
127 * File Name Krunching with gnatkr::
128 * Renaming Files with gnatchop::
129
130 Handling Arbitrary File Naming Conventions with gnatname
131
132 * Arbitrary File Naming Conventions::
133 * Running gnatname::
134 * Switches for gnatname::
135 * Examples of gnatname Usage::
136
137 File Name Krunching with gnatkr
138
139 * About gnatkr::
140 * Using gnatkr::
141 * Krunching Method::
142 * Examples of gnatkr Usage::
143
144 Renaming Files with gnatchop
145
146 * Handling Files with Multiple Units::
147 * Operating gnatchop in Compilation Mode::
148 * Command Line for gnatchop::
149 * Switches for gnatchop::
150 * Examples of gnatchop Usage::
151
152 Configuration Pragmas
153
154 * Handling of Configuration Pragmas::
155 * The Configuration Pragmas Files::
156
157 GNAT and Libraries
158
159 * Introduction to Libraries in GNAT::
160 * General Ada Libraries::
161 * Stand-alone Ada Libraries::
162 * Rebuilding the GNAT Run-Time Library::
163
164 General Ada Libraries
165
166 * Building a library::
167 * Installing a library::
168 * Using a library::
169
170 Stand-alone Ada Libraries
171
172 * Introduction to Stand-alone Libraries::
173 * Building a Stand-alone Library::
174 * Creating a Stand-alone Library to be used in a non-Ada context::
175 * Restrictions in Stand-alone Libraries::
176
177 Conditional Compilation
178
179 * Modeling Conditional Compilation in Ada::
180 * Preprocessing with gnatprep::
181 * Integrated Preprocessing::
182
183 Modeling Conditional Compilation in Ada
184
185 * Use of Boolean Constants::
186 * Debugging - A Special Case::
187 * Conditionalizing Declarations::
188 * Use of Alternative Implementations::
189 * Preprocessing::
190
191 Preprocessing with gnatprep
192
193 * Preprocessing Symbols::
194 * Using gnatprep::
195 * Switches for gnatprep::
196 * Form of Definitions File::
197 * Form of Input Text for gnatprep::
198
199 Mixed Language Programming
200
201 * Interfacing to C::
202 * Calling Conventions::
203 * Building Mixed Ada and C++ Programs::
204 * Generating Ada Bindings for C and C++ headers::
205 * Generating C Headers for Ada Specifications::
206
207 Building Mixed Ada and C++ Programs
208
209 * Interfacing to C++::
210 * Linking a Mixed C++ & Ada Program::
211 * A Simple Example::
212 * Interfacing with C++ constructors::
213 * Interfacing with C++ at the Class Level::
214
215 Generating Ada Bindings for C and C++ headers
216
217 * Running the Binding Generator::
218 * Generating Bindings for C++ Headers::
219 * Switches::
220
221 Generating C Headers for Ada Specifications
222
223 * Running the C Header Generator::
224
225 GNAT and Other Compilation Models
226
227 * Comparison between GNAT and C/C++ Compilation Models::
228 * Comparison between GNAT and Conventional Ada Library Models::
229
230 Using GNAT Files with External Tools
231
232 * Using Other Utility Programs with GNAT::
233 * The External Symbol Naming Scheme of GNAT::
234
235 Building Executable Programs with GNAT
236
237 * Building with gnatmake::
238 * Compiling with gcc::
239 * Compiler Switches::
240 * Linker Switches::
241 * Binding with gnatbind::
242 * Linking with gnatlink::
243 * Using the GNU make Utility::
244
245 Building with gnatmake
246
247 * Running gnatmake::
248 * Switches for gnatmake::
249 * Mode Switches for gnatmake::
250 * Notes on the Command Line::
251 * How gnatmake Works::
252 * Examples of gnatmake Usage::
253
254 Compiling with gcc
255
256 * Compiling Programs::
257 * Search Paths and the Run-Time Library (RTL): Search Paths and the Run-Time Library RTL.
258 * Order of Compilation Issues::
259 * Examples::
260
261 Compiler Switches
262
263 * Alphabetical List of All Switches::
264 * Output and Error Message Control::
265 * Warning Message Control::
266 * Debugging and Assertion Control::
267 * Validity Checking::
268 * Style Checking::
269 * Run-Time Checks::
270 * Using gcc for Syntax Checking::
271 * Using gcc for Semantic Checking::
272 * Compiling Different Versions of Ada::
273 * Character Set Control::
274 * File Naming Control::
275 * Subprogram Inlining Control::
276 * Auxiliary Output Control::
277 * Debugging Control::
278 * Exception Handling Control::
279 * Units to Sources Mapping Files::
280 * Code Generation Control::
281
282 Binding with gnatbind
283
284 * Running gnatbind::
285 * Switches for gnatbind::
286 * Command-Line Access::
287 * Search Paths for gnatbind::
288 * Examples of gnatbind Usage::
289
290 Switches for gnatbind
291
292 * Consistency-Checking Modes::
293 * Binder Error Message Control::
294 * Elaboration Control::
295 * Output Control::
296 * Dynamic Allocation Control::
297 * Binding with Non-Ada Main Programs::
298 * Binding Programs with No Main Subprogram::
299
300 Linking with gnatlink
301
302 * Running gnatlink::
303 * Switches for gnatlink::
304
305 Using the GNU make Utility
306
307 * Using gnatmake in a Makefile::
308 * Automatically Creating a List of Directories::
309 * Generating the Command Line Switches::
310 * Overcoming Command Line Length Limits::
311
312 GNAT Utility Programs
313
314 * The File Cleanup Utility gnatclean::
315 * The GNAT Library Browser gnatls::
316 * The Cross-Referencing Tools gnatxref and gnatfind::
317 * The Ada to HTML Converter gnathtml::
318
319 The File Cleanup Utility gnatclean
320
321 * Running gnatclean::
322 * Switches for gnatclean::
323
324 The GNAT Library Browser gnatls
325
326 * Running gnatls::
327 * Switches for gnatls::
328 * Example of gnatls Usage::
329
330 The Cross-Referencing Tools gnatxref and gnatfind
331
332 * gnatxref Switches::
333 * gnatfind Switches::
334 * Configuration Files for gnatxref and gnatfind::
335 * Regular Expressions in gnatfind and gnatxref::
336 * Examples of gnatxref Usage::
337 * Examples of gnatfind Usage::
338
339 Examples of gnatxref Usage
340
341 * General Usage::
342 * Using gnatxref with vi::
343
344 The Ada to HTML Converter gnathtml
345
346 * Invoking gnathtml::
347 * Installing gnathtml::
348
349 GNAT and Program Execution
350
351 * Running and Debugging Ada Programs::
352 * Profiling::
353 * Improving Performance::
354 * Overflow Check Handling in GNAT::
355 * Performing Dimensionality Analysis in GNAT::
356 * Stack Related Facilities::
357 * Memory Management Issues::
358
359 Running and Debugging Ada Programs
360
361 * The GNAT Debugger GDB::
362 * Running GDB::
363 * Introduction to GDB Commands::
364 * Using Ada Expressions::
365 * Calling User-Defined Subprograms::
366 * Using the next Command in a Function::
367 * Stopping When Ada Exceptions Are Raised::
368 * Ada Tasks::
369 * Debugging Generic Units::
370 * Remote Debugging with gdbserver::
371 * GNAT Abnormal Termination or Failure to Terminate::
372 * Naming Conventions for GNAT Source Files::
373 * Getting Internal Debugging Information::
374 * Stack Traceback::
375 * Pretty-Printers for the GNAT runtime::
376
377 Stack Traceback
378
379 * Non-Symbolic Traceback::
380 * Symbolic Traceback::
381
382 Profiling
383
384 * Profiling an Ada Program with gprof::
385
386 Profiling an Ada Program with gprof
387
388 * Compilation for profiling::
389 * Program execution::
390 * Running gprof::
391 * Interpretation of profiling results::
392
393 Improving Performance
394
395 * Performance Considerations::
396 * Text_IO Suggestions::
397 * Reducing Size of Executables with Unused Subprogram/Data Elimination::
398
399 Performance Considerations
400
401 * Controlling Run-Time Checks::
402 * Use of Restrictions::
403 * Optimization Levels::
404 * Debugging Optimized Code::
405 * Inlining of Subprograms::
406 * Floating_Point_Operations::
407 * Vectorization of loops::
408 * Other Optimization Switches::
409 * Optimization and Strict Aliasing::
410 * Aliased Variables and Optimization::
411 * Atomic Variables and Optimization::
412 * Passive Task Optimization::
413
414 Reducing Size of Executables with Unused Subprogram/Data Elimination
415
416 * About unused subprogram/data elimination::
417 * Compilation options::
418 * Example of unused subprogram/data elimination::
419
420 Overflow Check Handling in GNAT
421
422 * Background::
423 * Management of Overflows in GNAT::
424 * Specifying the Desired Mode::
425 * Default Settings::
426 * Implementation Notes::
427
428 Stack Related Facilities
429
430 * Stack Overflow Checking::
431 * Static Stack Usage Analysis::
432 * Dynamic Stack Usage Analysis::
433
434 Memory Management Issues
435
436 * Some Useful Memory Pools::
437 * The GNAT Debug Pool Facility::
438
439 Platform-Specific Information
440
441 * Run-Time Libraries::
442 * Specifying a Run-Time Library::
443 * GNU/Linux Topics::
444 * Microsoft Windows Topics::
445 * Mac OS Topics::
446
447 Run-Time Libraries
448
449 * Summary of Run-Time Configurations::
450
451 Specifying a Run-Time Library
452
453 * Choosing the Scheduling Policy::
454
455 GNU/Linux Topics
456
457 * Required Packages on GNU/Linux::
458
459 Microsoft Windows Topics
460
461 * Using GNAT on Windows::
462 * Using a network installation of GNAT::
463 * CONSOLE and WINDOWS subsystems::
464 * Temporary Files::
465 * Disabling Command Line Argument Expansion::
466 * Windows Socket Timeouts::
467 * Mixed-Language Programming on Windows::
468 * Windows Specific Add-Ons::
469
470 Mixed-Language Programming on Windows
471
472 * Windows Calling Conventions::
473 * Introduction to Dynamic Link Libraries (DLLs): Introduction to Dynamic Link Libraries DLLs.
474 * Using DLLs with GNAT::
475 * Building DLLs with GNAT Project files::
476 * Building DLLs with GNAT::
477 * Building DLLs with gnatdll::
478 * Ada DLLs and Finalization::
479 * Creating a Spec for Ada DLLs::
480 * GNAT and Windows Resources::
481 * Using GNAT DLLs from Microsoft Visual Studio Applications::
482 * Debugging a DLL::
483 * Setting Stack Size from gnatlink::
484 * Setting Heap Size from gnatlink::
485
486 Windows Calling Conventions
487
488 * C Calling Convention::
489 * Stdcall Calling Convention::
490 * Win32 Calling Convention::
491 * DLL Calling Convention::
492
493 Using DLLs with GNAT
494
495 * Creating an Ada Spec for the DLL Services::
496 * Creating an Import Library::
497
498 Building DLLs with gnatdll
499
500 * Limitations When Using Ada DLLs from Ada::
501 * Exporting Ada Entities::
502 * Ada DLLs and Elaboration::
503
504 Creating a Spec for Ada DLLs
505
506 * Creating the Definition File::
507 * Using gnatdll::
508
509 GNAT and Windows Resources
510
511 * Building Resources::
512 * Compiling Resources::
513 * Using Resources::
514
515 Debugging a DLL
516
517 * Program and DLL Both Built with GCC/GNAT::
518 * Program Built with Foreign Tools and DLL Built with GCC/GNAT::
519
520 Windows Specific Add-Ons
521
522 * Win32Ada::
523 * wPOSIX::
524
525 Mac OS Topics
526
527 * Codesigning the Debugger::
528
529 Elaboration Order Handling in GNAT
530
531 * Elaboration Code::
532 * Elaboration Order::
533 * Checking the Elaboration Order::
534 * Controlling the Elaboration Order in Ada::
535 * Controlling the Elaboration Order in GNAT::
536 * Mixing Elaboration Models::
537 * ABE Diagnostics::
538 * SPARK Diagnostics::
539 * Elaboration Circularities::
540 * Resolving Elaboration Circularities::
541 * Elaboration-related Compiler Switches::
542 * Summary of Procedures for Elaboration Control::
543 * Inspecting the Chosen Elaboration Order::
544
545 Inline Assembler
546
547 * Basic Assembler Syntax::
548 * A Simple Example of Inline Assembler::
549 * Output Variables in Inline Assembler::
550 * Input Variables in Inline Assembler::
551 * Inlining Inline Assembler Code::
552 * Other Asm Functionality::
553
554 Other Asm Functionality
555
556 * The Clobber Parameter::
557 * The Volatile Parameter::
558
559 @end detailmenu
560 @end menu
561
562 @node About This Guide,Getting Started with GNAT,Top,Top
563 @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}
564 @chapter About This Guide
565
566
567
568 This guide describes the use of GNAT,
569 a compiler and software development
570 toolset for the full Ada programming language.
571 It documents the features of the compiler and tools, and explains
572 how to use them to build Ada applications.
573
574 GNAT implements Ada 95, Ada 2005 and Ada 2012, and it may also be
575 invoked in Ada 83 compatibility mode.
576 By default, GNAT assumes Ada 2012, but you can override with a
577 compiler switch (@ref{6,,Compiling Different Versions of Ada})
578 to explicitly specify the language version.
579 Throughout this manual, references to 'Ada' without a year suffix
580 apply to all Ada 95/2005/2012 versions of the language.
581
582 @menu
583 * What This Guide Contains::
584 * What You Should Know before Reading This Guide::
585 * Related Information::
586 * A Note to Readers of Previous Versions of the Manual::
587 * Conventions::
588
589 @end menu
590
591 @node What This Guide Contains,What You Should Know before Reading This Guide,,About This Guide
592 @anchor{gnat_ugn/about_this_guide what-this-guide-contains}@anchor{7}
593 @section What This Guide Contains
594
595
596 This guide contains the following chapters:
597
598
599 @itemize *
600
601 @item
602 @ref{8,,Getting Started with GNAT} describes how to get started compiling
603 and running Ada programs with the GNAT Ada programming environment.
604
605 @item
606 @ref{9,,The GNAT Compilation Model} describes the compilation model used
607 by GNAT.
608
609 @item
610 @ref{a,,Building Executable Programs with GNAT} describes how to use the
611 main GNAT tools to build executable programs, and it also gives examples of
612 using the GNU make utility with GNAT.
613
614 @item
615 @ref{b,,GNAT Utility Programs} explains the various utility programs that
616 are included in the GNAT environment
617
618 @item
619 @ref{c,,GNAT and Program Execution} covers a number of topics related to
620 running, debugging, and tuning the performace of programs developed
621 with GNAT
622 @end itemize
623
624 Appendices cover several additional topics:
625
626
627 @itemize *
628
629 @item
630 @ref{d,,Platform-Specific Information} describes the different run-time
631 library implementations and also presents information on how to use
632 GNAT on several specific platforms
633
634 @item
635 @ref{e,,Example of Binder Output File} shows the source code for the binder
636 output file for a sample program.
637
638 @item
639 @ref{f,,Elaboration Order Handling in GNAT} describes how GNAT helps
640 you deal with elaboration order issues.
641
642 @item
643 @ref{10,,Inline Assembler} shows how to use the inline assembly facility
644 in an Ada program.
645 @end itemize
646
647 @node What You Should Know before Reading This Guide,Related Information,What This Guide Contains,About This Guide
648 @anchor{gnat_ugn/about_this_guide what-you-should-know-before-reading-this-guide}@anchor{11}
649 @section What You Should Know before Reading This Guide
650
651
652 @geindex Ada 95 Language Reference Manual
653
654 @geindex Ada 2005 Language Reference Manual
655
656 This guide assumes a basic familiarity with the Ada 95 language, as
657 described in the International Standard ANSI/ISO/IEC-8652:1995, January
658 1995.
659 It does not require knowledge of the features introduced by Ada 2005
660 or Ada 2012.
661 Reference manuals for Ada 95, Ada 2005, and Ada 2012 are included in
662 the GNAT documentation package.
663
664 @node Related Information,A Note to Readers of Previous Versions of the Manual,What You Should Know before Reading This Guide,About This Guide
665 @anchor{gnat_ugn/about_this_guide related-information}@anchor{12}
666 @section Related Information
667
668
669 For further information about Ada and related tools, please refer to the
670 following documents:
671
672
673 @itemize *
674
675 @item
676 @cite{Ada 95 Reference Manual}, @cite{Ada 2005 Reference Manual}, and
677 @cite{Ada 2012 Reference Manual}, which contain reference
678 material for the several revisions of the Ada language standard.
679
680 @item
681 @cite{GNAT Reference_Manual}, which contains all reference material for the GNAT
682 implementation of Ada.
683
684 @item
685 @cite{Using GNAT Studio}, which describes the GNAT Studio
686 Integrated Development Environment.
687
688 @item
689 @cite{GNAT Studio Tutorial}, which introduces the
690 main GNAT Studio features through examples.
691
692 @item
693 @cite{Debugging with GDB},
694 for all details on the use of the GNU source-level debugger.
695
696 @item
697 @cite{GNU Emacs Manual},
698 for full information on the extensible editor and programming
699 environment Emacs.
700 @end itemize
701
702 @node A Note to Readers of Previous Versions of the Manual,Conventions,Related Information,About This Guide
703 @anchor{gnat_ugn/about_this_guide a-note-to-readers-of-previous-versions-of-the-manual}@anchor{13}
704 @section A Note to Readers of Previous Versions of the Manual
705
706
707 In early 2015 the GNAT manuals were transitioned to the
708 reStructuredText (rst) / Sphinx documentation generator technology.
709 During that process the @cite{GNAT User's Guide} was reorganized
710 so that related topics would be described together in the same chapter
711 or appendix. Here's a summary of the major changes realized in
712 the new document structure.
713
714
715 @itemize *
716
717 @item
718 @ref{9,,The GNAT Compilation Model} has been extended so that it now covers
719 the following material:
720
721
722 @itemize -
723
724 @item
725 The @code{gnatname}, @code{gnatkr}, and @code{gnatchop} tools
726
727 @item
728 @ref{14,,Configuration Pragmas}
729
730 @item
731 @ref{15,,GNAT and Libraries}
732
733 @item
734 @ref{16,,Conditional Compilation} including @ref{17,,Preprocessing with gnatprep}
735 and @ref{18,,Integrated Preprocessing}
736
737 @item
738 @ref{19,,Generating Ada Bindings for C and C++ headers}
739
740 @item
741 @ref{1a,,Using GNAT Files with External Tools}
742 @end itemize
743
744 @item
745 @ref{a,,Building Executable Programs with GNAT} is a new chapter consolidating
746 the following content:
747
748
749 @itemize -
750
751 @item
752 @ref{1b,,Building with gnatmake}
753
754 @item
755 @ref{1c,,Compiling with gcc}
756
757 @item
758 @ref{1d,,Binding with gnatbind}
759
760 @item
761 @ref{1e,,Linking with gnatlink}
762
763 @item
764 @ref{1f,,Using the GNU make Utility}
765 @end itemize
766
767 @item
768 @ref{b,,GNAT Utility Programs} is a new chapter consolidating the information about several
769 GNAT tools:
770
771
772
773 @itemize -
774
775 @item
776 @ref{20,,The File Cleanup Utility gnatclean}
777
778 @item
779 @ref{21,,The GNAT Library Browser gnatls}
780
781 @item
782 @ref{22,,The Cross-Referencing Tools gnatxref and gnatfind}
783
784 @item
785 @ref{23,,The Ada to HTML Converter gnathtml}
786 @end itemize
787
788 @item
789 @ref{c,,GNAT and Program Execution} is a new chapter consolidating the following:
790
791
792 @itemize -
793
794 @item
795 @ref{24,,Running and Debugging Ada Programs}
796
797 @item
798 @ref{25,,Profiling}
799
800 @item
801 @ref{26,,Improving Performance}
802
803 @item
804 @ref{27,,Overflow Check Handling in GNAT}
805
806 @item
807 @ref{28,,Performing Dimensionality Analysis in GNAT}
808
809 @item
810 @ref{29,,Stack Related Facilities}
811
812 @item
813 @ref{2a,,Memory Management Issues}
814 @end itemize
815
816 @item
817 @ref{d,,Platform-Specific Information} is a new appendix consolidating the following:
818
819
820 @itemize -
821
822 @item
823 @ref{2b,,Run-Time Libraries}
824
825 @item
826 @ref{2c,,Microsoft Windows Topics}
827
828 @item
829 @ref{2d,,Mac OS Topics}
830 @end itemize
831
832 @item
833 The @emph{Compatibility and Porting Guide} appendix has been moved to the
834 @cite{GNAT Reference Manual}. It now includes a section
835 @emph{Writing Portable Fixed-Point Declarations} which was previously
836 a separate chapter in the @cite{GNAT User's Guide}.
837 @end itemize
838
839 @node Conventions,,A Note to Readers of Previous Versions of the Manual,About This Guide
840 @anchor{gnat_ugn/about_this_guide conventions}@anchor{2e}
841 @section Conventions
842
843
844 @geindex Conventions
845 @geindex typographical
846
847 @geindex Typographical conventions
848
849 Following are examples of the typographical and graphic conventions used
850 in this guide:
851
852
853 @itemize *
854
855 @item
856 @code{Functions}, @code{utility program names}, @code{standard names},
857 and @code{classes}.
858
859 @item
860 @code{Option flags}
861
862 @item
863 @code{File names}
864
865 @item
866 @code{Variables}
867
868 @item
869 @emph{Emphasis}
870
871 @item
872 [optional information or parameters]
873
874 @item
875 Examples are described by text
876
877 @example
878 and then shown this way.
879 @end example
880
881 @item
882 Commands that are entered by the user are shown as preceded by a prompt string
883 comprising the @code{$} character followed by a space.
884
885 @item
886 Full file names are shown with the '/' character
887 as the directory separator; e.g., @code{parent-dir/subdir/myfile.adb}.
888 If you are using GNAT on a Windows platform, please note that
889 the '\' character should be used instead.
890 @end itemize
891
892 @node Getting Started with GNAT,The GNAT Compilation Model,About This Guide,Top
893 @anchor{gnat_ugn/getting_started_with_gnat getting-started-with-gnat}@anchor{8}@anchor{gnat_ugn/getting_started_with_gnat doc}@anchor{2f}@anchor{gnat_ugn/getting_started_with_gnat id1}@anchor{30}
894 @chapter Getting Started with GNAT
895
896
897 This chapter describes how to use GNAT's command line interface to build
898 executable Ada programs.
899 On most platforms a visually oriented Integrated Development Environment
900 is also available, the GNAT Programming Studio (GNAT Studio).
901 GNAT Studio offers a graphical "look and feel", support for development in
902 other programming languages, comprehensive browsing features, and
903 many other capabilities.
904 For information on GNAT Studio please refer to
905 @cite{Using the GNAT Programming Studio}.
906
907 @menu
908 * Running GNAT::
909 * Running a Simple Ada Program::
910 * Running a Program with Multiple Units::
911 * Using the gnatmake Utility::
912
913 @end menu
914
915 @node Running GNAT,Running a Simple Ada Program,,Getting Started with GNAT
916 @anchor{gnat_ugn/getting_started_with_gnat running-gnat}@anchor{31}@anchor{gnat_ugn/getting_started_with_gnat id2}@anchor{32}
917 @section Running GNAT
918
919
920 Three steps are needed to create an executable file from an Ada source
921 file:
922
923
924 @itemize *
925
926 @item
927 The source file(s) must be compiled.
928
929 @item
930 The file(s) must be bound using the GNAT binder.
931
932 @item
933 All appropriate object files must be linked to produce an executable.
934 @end itemize
935
936 All three steps are most commonly handled by using the @code{gnatmake}
937 utility program that, given the name of the main program, automatically
938 performs the necessary compilation, binding and linking steps.
939
940 @node Running a Simple Ada Program,Running a Program with Multiple Units,Running GNAT,Getting Started with GNAT
941 @anchor{gnat_ugn/getting_started_with_gnat running-a-simple-ada-program}@anchor{33}@anchor{gnat_ugn/getting_started_with_gnat id3}@anchor{34}
942 @section Running a Simple Ada Program
943
944
945 Any text editor may be used to prepare an Ada program.
946 (If Emacs is used, the optional Ada mode may be helpful in laying out the
947 program.)
948 The program text is a normal text file. We will assume in our initial
949 example that you have used your editor to prepare the following
950 standard format text file:
951
952 @example
953 with Ada.Text_IO; use Ada.Text_IO;
954 procedure Hello is
955 begin
956 Put_Line ("Hello WORLD!");
957 end Hello;
958 @end example
959
960 This file should be named @code{hello.adb}.
961 With the normal default file naming conventions, GNAT requires
962 that each file
963 contain a single compilation unit whose file name is the
964 unit name,
965 with periods replaced by hyphens; the
966 extension is @code{ads} for a
967 spec and @code{adb} for a body.
968 You can override this default file naming convention by use of the
969 special pragma @code{Source_File_Name} (for further information please
970 see @ref{35,,Using Other File Names}).
971 Alternatively, if you want to rename your files according to this default
972 convention, which is probably more convenient if you will be using GNAT
973 for all your compilations, then the @code{gnatchop} utility
974 can be used to generate correctly-named source files
975 (see @ref{36,,Renaming Files with gnatchop}).
976
977 You can compile the program using the following command (@code{$} is used
978 as the command prompt in the examples in this document):
979
980 @example
981 $ gcc -c hello.adb
982 @end example
983
984 @code{gcc} is the command used to run the compiler. This compiler is
985 capable of compiling programs in several languages, including Ada and
986 C. It assumes that you have given it an Ada program if the file extension is
987 either @code{.ads} or @code{.adb}, and it will then call
988 the GNAT compiler to compile the specified file.
989
990 The @code{-c} switch is required. It tells @code{gcc} to only do a
991 compilation. (For C programs, @code{gcc} can also do linking, but this
992 capability is not used directly for Ada programs, so the @code{-c}
993 switch must always be present.)
994
995 This compile command generates a file
996 @code{hello.o}, which is the object
997 file corresponding to your Ada program. It also generates
998 an 'Ada Library Information' file @code{hello.ali},
999 which contains additional information used to check
1000 that an Ada program is consistent.
1001 To build an executable file,
1002 use @code{gnatbind} to bind the program
1003 and @code{gnatlink} to link it. The
1004 argument to both @code{gnatbind} and @code{gnatlink} is the name of the
1005 @code{ALI} file, but the default extension of @code{.ali} can
1006 be omitted. This means that in the most common case, the argument
1007 is simply the name of the main program:
1008
1009 @example
1010 $ gnatbind hello
1011 $ gnatlink hello
1012 @end example
1013
1014 A simpler method of carrying out these steps is to use @code{gnatmake},
1015 a master program that invokes all the required
1016 compilation, binding and linking tools in the correct order. In particular,
1017 @code{gnatmake} automatically recompiles any sources that have been
1018 modified since they were last compiled, or sources that depend
1019 on such modified sources, so that 'version skew' is avoided.
1020
1021 @geindex Version skew (avoided by `@w{`}gnatmake`@w{`})
1022
1023 @example
1024 $ gnatmake hello.adb
1025 @end example
1026
1027 The result is an executable program called @code{hello}, which can be
1028 run by entering:
1029
1030 @example
1031 $ hello
1032 @end example
1033
1034 assuming that the current directory is on the search path
1035 for executable programs.
1036
1037 and, if all has gone well, you will see:
1038
1039 @example
1040 Hello WORLD!
1041 @end example
1042
1043 appear in response to this command.
1044
1045 @node Running a Program with Multiple Units,Using the gnatmake Utility,Running a Simple Ada Program,Getting Started with GNAT
1046 @anchor{gnat_ugn/getting_started_with_gnat id4}@anchor{37}@anchor{gnat_ugn/getting_started_with_gnat running-a-program-with-multiple-units}@anchor{38}
1047 @section Running a Program with Multiple Units
1048
1049
1050 Consider a slightly more complicated example that has three files: a
1051 main program, and the spec and body of a package:
1052
1053 @example
1054 package Greetings is
1055 procedure Hello;
1056 procedure Goodbye;
1057 end Greetings;
1058
1059 with Ada.Text_IO; use Ada.Text_IO;
1060 package body Greetings is
1061 procedure Hello is
1062 begin
1063 Put_Line ("Hello WORLD!");
1064 end Hello;
1065
1066 procedure Goodbye is
1067 begin
1068 Put_Line ("Goodbye WORLD!");
1069 end Goodbye;
1070 end Greetings;
1071
1072 with Greetings;
1073 procedure Gmain is
1074 begin
1075 Greetings.Hello;
1076 Greetings.Goodbye;
1077 end Gmain;
1078 @end example
1079
1080 Following the one-unit-per-file rule, place this program in the
1081 following three separate files:
1082
1083
1084 @table @asis
1085
1086 @item @emph{greetings.ads}
1087
1088 spec of package @code{Greetings}
1089
1090 @item @emph{greetings.adb}
1091
1092 body of package @code{Greetings}
1093
1094 @item @emph{gmain.adb}
1095
1096 body of main program
1097 @end table
1098
1099 To build an executable version of
1100 this program, we could use four separate steps to compile, bind, and link
1101 the program, as follows:
1102
1103 @example
1104 $ gcc -c gmain.adb
1105 $ gcc -c greetings.adb
1106 $ gnatbind gmain
1107 $ gnatlink gmain
1108 @end example
1109
1110 Note that there is no required order of compilation when using GNAT.
1111 In particular it is perfectly fine to compile the main program first.
1112 Also, it is not necessary to compile package specs in the case where
1113 there is an accompanying body; you only need to compile the body. If you want
1114 to submit these files to the compiler for semantic checking and not code
1115 generation, then use the @code{-gnatc} switch:
1116
1117 @example
1118 $ gcc -c greetings.ads -gnatc
1119 @end example
1120
1121 Although the compilation can be done in separate steps as in the
1122 above example, in practice it is almost always more convenient
1123 to use the @code{gnatmake} tool. All you need to know in this case
1124 is the name of the main program's source file. The effect of the above four
1125 commands can be achieved with a single one:
1126
1127 @example
1128 $ gnatmake gmain.adb
1129 @end example
1130
1131 In the next section we discuss the advantages of using @code{gnatmake} in
1132 more detail.
1133
1134 @node Using the gnatmake Utility,,Running a Program with Multiple Units,Getting Started with GNAT
1135 @anchor{gnat_ugn/getting_started_with_gnat using-the-gnatmake-utility}@anchor{39}@anchor{gnat_ugn/getting_started_with_gnat id5}@anchor{3a}
1136 @section Using the @code{gnatmake} Utility
1137
1138
1139 If you work on a program by compiling single components at a time using
1140 @code{gcc}, you typically keep track of the units you modify. In order to
1141 build a consistent system, you compile not only these units, but also any
1142 units that depend on the units you have modified.
1143 For example, in the preceding case,
1144 if you edit @code{gmain.adb}, you only need to recompile that file. But if
1145 you edit @code{greetings.ads}, you must recompile both
1146 @code{greetings.adb} and @code{gmain.adb}, because both files contain
1147 units that depend on @code{greetings.ads}.
1148
1149 @code{gnatbind} will warn you if you forget one of these compilation
1150 steps, so that it is impossible to generate an inconsistent program as a
1151 result of forgetting to do a compilation. Nevertheless it is tedious and
1152 error-prone to keep track of dependencies among units.
1153 One approach to handle the dependency-bookkeeping is to use a
1154 makefile. However, makefiles present maintenance problems of their own:
1155 if the dependencies change as you change the program, you must make
1156 sure that the makefile is kept up-to-date manually, which is also an
1157 error-prone process.
1158
1159 The @code{gnatmake} utility takes care of these details automatically.
1160 Invoke it using either one of the following forms:
1161
1162 @example
1163 $ gnatmake gmain.adb
1164 $ gnatmake gmain
1165 @end example
1166
1167 The argument is the name of the file containing the main program;
1168 you may omit the extension. @code{gnatmake}
1169 examines the environment, automatically recompiles any files that need
1170 recompiling, and binds and links the resulting set of object files,
1171 generating the executable file, @code{gmain}.
1172 In a large program, it
1173 can be extremely helpful to use @code{gnatmake}, because working out by hand
1174 what needs to be recompiled can be difficult.
1175
1176 Note that @code{gnatmake} takes into account all the Ada rules that
1177 establish dependencies among units. These include dependencies that result
1178 from inlining subprogram bodies, and from
1179 generic instantiation. Unlike some other
1180 Ada make tools, @code{gnatmake} does not rely on the dependencies that were
1181 found by the compiler on a previous compilation, which may possibly
1182 be wrong when sources change. @code{gnatmake} determines the exact set of
1183 dependencies from scratch each time it is run.
1184
1185 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
1186
1187 @node The GNAT Compilation Model,Building Executable Programs with GNAT,Getting Started with GNAT,Top
1188 @anchor{gnat_ugn/the_gnat_compilation_model doc}@anchor{3b}@anchor{gnat_ugn/the_gnat_compilation_model the-gnat-compilation-model}@anchor{9}@anchor{gnat_ugn/the_gnat_compilation_model id1}@anchor{3c}
1189 @chapter The GNAT Compilation Model
1190
1191
1192 @geindex GNAT compilation model
1193
1194 @geindex Compilation model
1195
1196 This chapter describes the compilation model used by GNAT. Although
1197 similar to that used by other languages such as C and C++, this model
1198 is substantially different from the traditional Ada compilation models,
1199 which are based on a centralized program library. The chapter covers
1200 the following material:
1201
1202
1203 @itemize *
1204
1205 @item
1206 Topics related to source file makeup and naming
1207
1208
1209 @itemize *
1210
1211 @item
1212 @ref{3d,,Source Representation}
1213
1214 @item
1215 @ref{3e,,Foreign Language Representation}
1216
1217 @item
1218 @ref{3f,,File Naming Topics and Utilities}
1219 @end itemize
1220
1221 @item
1222 @ref{14,,Configuration Pragmas}
1223
1224 @item
1225 @ref{40,,Generating Object Files}
1226
1227 @item
1228 @ref{41,,Source Dependencies}
1229
1230 @item
1231 @ref{42,,The Ada Library Information Files}
1232
1233 @item
1234 @ref{43,,Binding an Ada Program}
1235
1236 @item
1237 @ref{15,,GNAT and Libraries}
1238
1239 @item
1240 @ref{16,,Conditional Compilation}
1241
1242 @item
1243 @ref{44,,Mixed Language Programming}
1244
1245 @item
1246 @ref{45,,GNAT and Other Compilation Models}
1247
1248 @item
1249 @ref{1a,,Using GNAT Files with External Tools}
1250 @end itemize
1251
1252 @menu
1253 * Source Representation::
1254 * Foreign Language Representation::
1255 * File Naming Topics and Utilities::
1256 * Configuration Pragmas::
1257 * Generating Object Files::
1258 * Source Dependencies::
1259 * The Ada Library Information Files::
1260 * Binding an Ada Program::
1261 * GNAT and Libraries::
1262 * Conditional Compilation::
1263 * Mixed Language Programming::
1264 * GNAT and Other Compilation Models::
1265 * Using GNAT Files with External Tools::
1266
1267 @end menu
1268
1269 @node Source Representation,Foreign Language Representation,,The GNAT Compilation Model
1270 @anchor{gnat_ugn/the_gnat_compilation_model source-representation}@anchor{3d}@anchor{gnat_ugn/the_gnat_compilation_model id2}@anchor{46}
1271 @section Source Representation
1272
1273
1274 @geindex Latin-1
1275
1276 @geindex VT
1277 @geindex HT
1278 @geindex CR
1279 @geindex LF
1280 @geindex FF
1281
1282 Ada source programs are represented in standard text files, using
1283 Latin-1 coding. Latin-1 is an 8-bit code that includes the familiar
1284 7-bit ASCII set, plus additional characters used for
1285 representing foreign languages (see @ref{3e,,Foreign Language Representation}
1286 for support of non-USA character sets). The format effector characters
1287 are represented using their standard ASCII encodings, as follows:
1288
1289 @quotation
1290
1291
1292 @multitable {xxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxx}
1293 @item
1294
1295 Character
1296
1297 @tab
1298
1299 Effect
1300
1301 @tab
1302
1303 Code
1304
1305 @item
1306
1307 @code{VT}
1308
1309 @tab
1310
1311 Vertical tab
1312
1313 @tab
1314
1315 @code{16#0B#}
1316
1317 @item
1318
1319 @code{HT}
1320
1321 @tab
1322
1323 Horizontal tab
1324
1325 @tab
1326
1327 @code{16#09#}
1328
1329 @item
1330
1331 @code{CR}
1332
1333 @tab
1334
1335 Carriage return
1336
1337 @tab
1338
1339 @code{16#0D#}
1340
1341 @item
1342
1343 @code{LF}
1344
1345 @tab
1346
1347 Line feed
1348
1349 @tab
1350
1351 @code{16#0A#}
1352
1353 @item
1354
1355 @code{FF}
1356
1357 @tab
1358
1359 Form feed
1360
1361 @tab
1362
1363 @code{16#0C#}
1364
1365 @end multitable
1366
1367 @end quotation
1368
1369 Source files are in standard text file format. In addition, GNAT will
1370 recognize a wide variety of stream formats, in which the end of
1371 physical lines is marked by any of the following sequences:
1372 @code{LF}, @code{CR}, @code{CR-LF}, or @code{LF-CR}. This is useful
1373 in accommodating files that are imported from other operating systems.
1374
1375 @geindex End of source file; Source file@comma{} end
1376
1377 @geindex SUB (control character)
1378
1379 The end of a source file is normally represented by the physical end of
1380 file. However, the control character @code{16#1A#} (@code{SUB}) is also
1381 recognized as signalling the end of the source file. Again, this is
1382 provided for compatibility with other operating systems where this
1383 code is used to represent the end of file.
1384
1385 @geindex spec (definition)
1386 @geindex compilation (definition)
1387
1388 Each file contains a single Ada compilation unit, including any pragmas
1389 associated with the unit. For example, this means you must place a
1390 package declaration (a package @emph{spec}) and the corresponding body in
1391 separate files. An Ada @emph{compilation} (which is a sequence of
1392 compilation units) is represented using a sequence of files. Similarly,
1393 you will place each subunit or child unit in a separate file.
1394
1395 @node Foreign Language Representation,File Naming Topics and Utilities,Source Representation,The GNAT Compilation Model
1396 @anchor{gnat_ugn/the_gnat_compilation_model foreign-language-representation}@anchor{3e}@anchor{gnat_ugn/the_gnat_compilation_model id3}@anchor{47}
1397 @section Foreign Language Representation
1398
1399
1400 GNAT supports the standard character sets defined in Ada as well as
1401 several other non-standard character sets for use in localized versions
1402 of the compiler (@ref{48,,Character Set Control}).
1403
1404 @menu
1405 * Latin-1::
1406 * Other 8-Bit Codes::
1407 * Wide_Character Encodings::
1408 * Wide_Wide_Character Encodings::
1409
1410 @end menu
1411
1412 @node Latin-1,Other 8-Bit Codes,,Foreign Language Representation
1413 @anchor{gnat_ugn/the_gnat_compilation_model id4}@anchor{49}@anchor{gnat_ugn/the_gnat_compilation_model latin-1}@anchor{4a}
1414 @subsection Latin-1
1415
1416
1417 @geindex Latin-1
1418
1419 The basic character set is Latin-1. This character set is defined by ISO
1420 standard 8859, part 1. The lower half (character codes @code{16#00#}
1421 ... @code{16#7F#)} is identical to standard ASCII coding, but the upper
1422 half is used to represent additional characters. These include extended letters
1423 used by European languages, such as French accents, the vowels with umlauts
1424 used in German, and the extra letter A-ring used in Swedish.
1425
1426 @geindex Ada.Characters.Latin_1
1427
1428 For a complete list of Latin-1 codes and their encodings, see the source
1429 file of library unit @code{Ada.Characters.Latin_1} in file
1430 @code{a-chlat1.ads}.
1431 You may use any of these extended characters freely in character or
1432 string literals. In addition, the extended characters that represent
1433 letters can be used in identifiers.
1434
1435 @node Other 8-Bit Codes,Wide_Character Encodings,Latin-1,Foreign Language Representation
1436 @anchor{gnat_ugn/the_gnat_compilation_model other-8-bit-codes}@anchor{4b}@anchor{gnat_ugn/the_gnat_compilation_model id5}@anchor{4c}
1437 @subsection Other 8-Bit Codes
1438
1439
1440 GNAT also supports several other 8-bit coding schemes:
1441
1442 @geindex Latin-2
1443
1444 @geindex ISO 8859-2
1445
1446
1447 @table @asis
1448
1449 @item @emph{ISO 8859-2 (Latin-2)}
1450
1451 Latin-2 letters allowed in identifiers, with uppercase and lowercase
1452 equivalence.
1453 @end table
1454
1455 @geindex Latin-3
1456
1457 @geindex ISO 8859-3
1458
1459
1460 @table @asis
1461
1462 @item @emph{ISO 8859-3 (Latin-3)}
1463
1464 Latin-3 letters allowed in identifiers, with uppercase and lowercase
1465 equivalence.
1466 @end table
1467
1468 @geindex Latin-4
1469
1470 @geindex ISO 8859-4
1471
1472
1473 @table @asis
1474
1475 @item @emph{ISO 8859-4 (Latin-4)}
1476
1477 Latin-4 letters allowed in identifiers, with uppercase and lowercase
1478 equivalence.
1479 @end table
1480
1481 @geindex ISO 8859-5
1482
1483 @geindex Cyrillic
1484
1485
1486 @table @asis
1487
1488 @item @emph{ISO 8859-5 (Cyrillic)}
1489
1490 ISO 8859-5 letters (Cyrillic) allowed in identifiers, with uppercase and
1491 lowercase equivalence.
1492 @end table
1493
1494 @geindex ISO 8859-15
1495
1496 @geindex Latin-9
1497
1498
1499 @table @asis
1500
1501 @item @emph{ISO 8859-15 (Latin-9)}
1502
1503 ISO 8859-15 (Latin-9) letters allowed in identifiers, with uppercase and
1504 lowercase equivalence
1505 @end table
1506
1507 @geindex code page 437 (IBM PC)
1508
1509
1510 @table @asis
1511
1512 @item @emph{IBM PC (code page 437)}
1513
1514 This code page is the normal default for PCs in the U.S. It corresponds
1515 to the original IBM PC character set. This set has some, but not all, of
1516 the extended Latin-1 letters, but these letters do not have the same
1517 encoding as Latin-1. In this mode, these letters are allowed in
1518 identifiers with uppercase and lowercase equivalence.
1519 @end table
1520
1521 @geindex code page 850 (IBM PC)
1522
1523
1524 @table @asis
1525
1526 @item @emph{IBM PC (code page 850)}
1527
1528 This code page is a modification of 437 extended to include all the
1529 Latin-1 letters, but still not with the usual Latin-1 encoding. In this
1530 mode, all these letters are allowed in identifiers with uppercase and
1531 lowercase equivalence.
1532
1533 @item @emph{Full Upper 8-bit}
1534
1535 Any character in the range 80-FF allowed in identifiers, and all are
1536 considered distinct. In other words, there are no uppercase and lowercase
1537 equivalences in this range. This is useful in conjunction with
1538 certain encoding schemes used for some foreign character sets (e.g.,
1539 the typical method of representing Chinese characters on the PC).
1540
1541 @item @emph{No Upper-Half}
1542
1543 No upper-half characters in the range 80-FF are allowed in identifiers.
1544 This gives Ada 83 compatibility for identifier names.
1545 @end table
1546
1547 For precise data on the encodings permitted, and the uppercase and lowercase
1548 equivalences that are recognized, see the file @code{csets.adb} in
1549 the GNAT compiler sources. You will need to obtain a full source release
1550 of GNAT to obtain this file.
1551
1552 @node Wide_Character Encodings,Wide_Wide_Character Encodings,Other 8-Bit Codes,Foreign Language Representation
1553 @anchor{gnat_ugn/the_gnat_compilation_model id6}@anchor{4d}@anchor{gnat_ugn/the_gnat_compilation_model wide-character-encodings}@anchor{4e}
1554 @subsection Wide_Character Encodings
1555
1556
1557 GNAT allows wide character codes to appear in character and string
1558 literals, and also optionally in identifiers, by means of the following
1559 possible encoding schemes:
1560
1561
1562 @table @asis
1563
1564 @item @emph{Hex Coding}
1565
1566 In this encoding, a wide character is represented by the following five
1567 character sequence:
1568
1569 @example
1570 ESC a b c d
1571 @end example
1572
1573 where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1574 characters (using uppercase letters) of the wide character code. For
1575 example, ESC A345 is used to represent the wide character with code
1576 @code{16#A345#}.
1577 This scheme is compatible with use of the full Wide_Character set.
1578
1579 @item @emph{Upper-Half Coding}
1580
1581 @geindex Upper-Half Coding
1582
1583 The wide character with encoding @code{16#abcd#} where the upper bit is on
1584 (in other words, 'a' is in the range 8-F) is represented as two bytes,
1585 @code{16#ab#} and @code{16#cd#}. The second byte cannot be a format control
1586 character, but is not required to be in the upper half. This method can
1587 be also used for shift-JIS or EUC, where the internal coding matches the
1588 external coding.
1589
1590 @item @emph{Shift JIS Coding}
1591
1592 @geindex Shift JIS Coding
1593
1594 A wide character is represented by a two-character sequence,
1595 @code{16#ab#} and
1596 @code{16#cd#}, with the restrictions described for upper-half encoding as
1597 described above. The internal character code is the corresponding JIS
1598 character according to the standard algorithm for Shift-JIS
1599 conversion. Only characters defined in the JIS code set table can be
1600 used with this encoding method.
1601
1602 @item @emph{EUC Coding}
1603
1604 @geindex EUC Coding
1605
1606 A wide character is represented by a two-character sequence
1607 @code{16#ab#} and
1608 @code{16#cd#}, with both characters being in the upper half. The internal
1609 character code is the corresponding JIS character according to the EUC
1610 encoding algorithm. Only characters defined in the JIS code set table
1611 can be used with this encoding method.
1612
1613 @item @emph{UTF-8 Coding}
1614
1615 A wide character is represented using
1616 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
1617 10646-1/Am.2. Depending on the character value, the representation
1618 is a one, two, or three byte sequence:
1619
1620 @example
1621 16#0000#-16#007f#: 2#0xxxxxxx#
1622 16#0080#-16#07ff#: 2#110xxxxx# 2#10xxxxxx#
1623 16#0800#-16#ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx#
1624 @end example
1625
1626 where the @code{xxx} bits correspond to the left-padded bits of the
1627 16-bit character value. Note that all lower half ASCII characters
1628 are represented as ASCII bytes and all upper half characters and
1629 other wide characters are represented as sequences of upper-half
1630 (The full UTF-8 scheme allows for encoding 31-bit characters as
1631 6-byte sequences, and in the following section on wide wide
1632 characters, the use of these sequences is documented).
1633
1634 @item @emph{Brackets Coding}
1635
1636 In this encoding, a wide character is represented by the following eight
1637 character sequence:
1638
1639 @example
1640 [ " a b c d " ]
1641 @end example
1642
1643 where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1644 characters (using uppercase letters) of the wide character code. For
1645 example, ['A345'] is used to represent the wide character with code
1646 @code{16#A345#}. It is also possible (though not required) to use the
1647 Brackets coding for upper half characters. For example, the code
1648 @code{16#A3#} can be represented as @code{['A3']}.
1649
1650 This scheme is compatible with use of the full Wide_Character set,
1651 and is also the method used for wide character encoding in some standard
1652 ACATS (Ada Conformity Assessment Test Suite) test suite distributions.
1653 @end table
1654
1655 @cartouche
1656 @quotation Note
1657 Some of these coding schemes do not permit the full use of the
1658 Ada character set. For example, neither Shift JIS nor EUC allow the
1659 use of the upper half of the Latin-1 set.
1660 @end quotation
1661 @end cartouche
1662
1663 @node Wide_Wide_Character Encodings,,Wide_Character Encodings,Foreign Language Representation
1664 @anchor{gnat_ugn/the_gnat_compilation_model id7}@anchor{4f}@anchor{gnat_ugn/the_gnat_compilation_model wide-wide-character-encodings}@anchor{50}
1665 @subsection Wide_Wide_Character Encodings
1666
1667
1668 GNAT allows wide wide character codes to appear in character and string
1669 literals, and also optionally in identifiers, by means of the following
1670 possible encoding schemes:
1671
1672
1673 @table @asis
1674
1675 @item @emph{UTF-8 Coding}
1676
1677 A wide character is represented using
1678 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
1679 10646-1/Am.2. Depending on the character value, the representation
1680 of character codes with values greater than 16#FFFF# is a
1681 is a four, five, or six byte sequence:
1682
1683 @example
1684 16#01_0000#-16#10_FFFF#: 11110xxx 10xxxxxx 10xxxxxx
1685 10xxxxxx
1686 16#0020_0000#-16#03FF_FFFF#: 111110xx 10xxxxxx 10xxxxxx
1687 10xxxxxx 10xxxxxx
1688 16#0400_0000#-16#7FFF_FFFF#: 1111110x 10xxxxxx 10xxxxxx
1689 10xxxxxx 10xxxxxx 10xxxxxx
1690 @end example
1691
1692 where the @code{xxx} bits correspond to the left-padded bits of the
1693 32-bit character value.
1694
1695 @item @emph{Brackets Coding}
1696
1697 In this encoding, a wide wide character is represented by the following ten or
1698 twelve byte character sequence:
1699
1700 @example
1701 [ " a b c d e f " ]
1702 [ " a b c d e f g h " ]
1703 @end example
1704
1705 where @code{a-h} are the six or eight hexadecimal
1706 characters (using uppercase letters) of the wide wide character code. For
1707 example, ["1F4567"] is used to represent the wide wide character with code
1708 @code{16#001F_4567#}.
1709
1710 This scheme is compatible with use of the full Wide_Wide_Character set,
1711 and is also the method used for wide wide character encoding in some standard
1712 ACATS (Ada Conformity Assessment Test Suite) test suite distributions.
1713 @end table
1714
1715 @node File Naming Topics and Utilities,Configuration Pragmas,Foreign Language Representation,The GNAT Compilation Model
1716 @anchor{gnat_ugn/the_gnat_compilation_model id8}@anchor{51}@anchor{gnat_ugn/the_gnat_compilation_model file-naming-topics-and-utilities}@anchor{3f}
1717 @section File Naming Topics and Utilities
1718
1719
1720 GNAT has a default file naming scheme and also provides the user with
1721 a high degree of control over how the names and extensions of the
1722 source files correspond to the Ada compilation units that they contain.
1723
1724 @menu
1725 * File Naming Rules::
1726 * Using Other File Names::
1727 * Alternative File Naming Schemes::
1728 * Handling Arbitrary File Naming Conventions with gnatname::
1729 * File Name Krunching with gnatkr::
1730 * Renaming Files with gnatchop::
1731
1732 @end menu
1733
1734 @node File Naming Rules,Using Other File Names,,File Naming Topics and Utilities
1735 @anchor{gnat_ugn/the_gnat_compilation_model file-naming-rules}@anchor{52}@anchor{gnat_ugn/the_gnat_compilation_model id9}@anchor{53}
1736 @subsection File Naming Rules
1737
1738
1739 The default file name is determined by the name of the unit that the
1740 file contains. The name is formed by taking the full expanded name of
1741 the unit and replacing the separating dots with hyphens and using
1742 lowercase for all letters.
1743
1744 An exception arises if the file name generated by the above rules starts
1745 with one of the characters
1746 @code{a}, @code{g}, @code{i}, or @code{s}, and the second character is a
1747 minus. In this case, the character tilde is used in place
1748 of the minus. The reason for this special rule is to avoid clashes with
1749 the standard names for child units of the packages System, Ada,
1750 Interfaces, and GNAT, which use the prefixes
1751 @code{s-}, @code{a-}, @code{i-}, and @code{g-},
1752 respectively.
1753
1754 The file extension is @code{.ads} for a spec and
1755 @code{.adb} for a body. The following table shows some
1756 examples of these rules.
1757
1758 @quotation
1759
1760
1761 @multitable {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
1762 @item
1763
1764 Source File
1765
1766 @tab
1767
1768 Ada Compilation Unit
1769
1770 @item
1771
1772 @code{main.ads}
1773
1774 @tab
1775
1776 Main (spec)
1777
1778 @item
1779
1780 @code{main.adb}
1781
1782 @tab
1783
1784 Main (body)
1785
1786 @item
1787
1788 @code{arith_functions.ads}
1789
1790 @tab
1791
1792 Arith_Functions (package spec)
1793
1794 @item
1795
1796 @code{arith_functions.adb}
1797
1798 @tab
1799
1800 Arith_Functions (package body)
1801
1802 @item
1803
1804 @code{func-spec.ads}
1805
1806 @tab
1807
1808 Func.Spec (child package spec)
1809
1810 @item
1811
1812 @code{func-spec.adb}
1813
1814 @tab
1815
1816 Func.Spec (child package body)
1817
1818 @item
1819
1820 @code{main-sub.adb}
1821
1822 @tab
1823
1824 Sub (subunit of Main)
1825
1826 @item
1827
1828 @code{a~bad.adb}
1829
1830 @tab
1831
1832 A.Bad (child package body)
1833
1834 @end multitable
1835
1836 @end quotation
1837
1838 Following these rules can result in excessively long
1839 file names if corresponding
1840 unit names are long (for example, if child units or subunits are
1841 heavily nested). An option is available to shorten such long file names
1842 (called file name 'krunching'). This may be particularly useful when
1843 programs being developed with GNAT are to be used on operating systems
1844 with limited file name lengths. @ref{54,,Using gnatkr}.
1845
1846 Of course, no file shortening algorithm can guarantee uniqueness over
1847 all possible unit names; if file name krunching is used, it is your
1848 responsibility to ensure no name clashes occur. Alternatively you
1849 can specify the exact file names that you want used, as described
1850 in the next section. Finally, if your Ada programs are migrating from a
1851 compiler with a different naming convention, you can use the gnatchop
1852 utility to produce source files that follow the GNAT naming conventions.
1853 (For details see @ref{36,,Renaming Files with gnatchop}.)
1854
1855 Note: in the case of Windows or Mac OS operating systems, case is not
1856 significant. So for example on Windows if the canonical name is
1857 @code{main-sub.adb}, you can use the file name @code{Main-Sub.adb} instead.
1858 However, case is significant for other operating systems, so for example,
1859 if you want to use other than canonically cased file names on a Unix system,
1860 you need to follow the procedures described in the next section.
1861
1862 @node Using Other File Names,Alternative File Naming Schemes,File Naming Rules,File Naming Topics and Utilities
1863 @anchor{gnat_ugn/the_gnat_compilation_model id10}@anchor{55}@anchor{gnat_ugn/the_gnat_compilation_model using-other-file-names}@anchor{35}
1864 @subsection Using Other File Names
1865
1866
1867 @geindex File names
1868
1869 In the previous section, we have described the default rules used by
1870 GNAT to determine the file name in which a given unit resides. It is
1871 often convenient to follow these default rules, and if you follow them,
1872 the compiler knows without being explicitly told where to find all
1873 the files it needs.
1874
1875 @geindex Source_File_Name pragma
1876
1877 However, in some cases, particularly when a program is imported from
1878 another Ada compiler environment, it may be more convenient for the
1879 programmer to specify which file names contain which units. GNAT allows
1880 arbitrary file names to be used by means of the Source_File_Name pragma.
1881 The form of this pragma is as shown in the following examples:
1882
1883 @example
1884 pragma Source_File_Name (My_Utilities.Stacks,
1885 Spec_File_Name => "myutilst_a.ada");
1886 pragma Source_File_name (My_Utilities.Stacks,
1887 Body_File_Name => "myutilst.ada");
1888 @end example
1889
1890 As shown in this example, the first argument for the pragma is the unit
1891 name (in this example a child unit). The second argument has the form
1892 of a named association. The identifier
1893 indicates whether the file name is for a spec or a body;
1894 the file name itself is given by a string literal.
1895
1896 The source file name pragma is a configuration pragma, which means that
1897 normally it will be placed in the @code{gnat.adc}
1898 file used to hold configuration
1899 pragmas that apply to a complete compilation environment.
1900 For more details on how the @code{gnat.adc} file is created and used
1901 see @ref{56,,Handling of Configuration Pragmas}.
1902
1903 @geindex gnat.adc
1904
1905 GNAT allows completely arbitrary file names to be specified using the
1906 source file name pragma. However, if the file name specified has an
1907 extension other than @code{.ads} or @code{.adb} it is necessary to use
1908 a special syntax when compiling the file. The name in this case must be
1909 preceded by the special sequence @code{-x} followed by a space and the name
1910 of the language, here @code{ada}, as in:
1911
1912 @example
1913 $ gcc -c -x ada peculiar_file_name.sim
1914 @end example
1915
1916 @code{gnatmake} handles non-standard file names in the usual manner (the
1917 non-standard file name for the main program is simply used as the
1918 argument to gnatmake). Note that if the extension is also non-standard,
1919 then it must be included in the @code{gnatmake} command, it may not
1920 be omitted.
1921
1922 @node Alternative File Naming Schemes,Handling Arbitrary File Naming Conventions with gnatname,Using Other File Names,File Naming Topics and Utilities
1923 @anchor{gnat_ugn/the_gnat_compilation_model id11}@anchor{57}@anchor{gnat_ugn/the_gnat_compilation_model alternative-file-naming-schemes}@anchor{58}
1924 @subsection Alternative File Naming Schemes
1925
1926
1927 @geindex File naming schemes
1928 @geindex alternative
1929
1930 @geindex File names
1931
1932 The previous section described the use of the @code{Source_File_Name}
1933 pragma to allow arbitrary names to be assigned to individual source files.
1934 However, this approach requires one pragma for each file, and especially in
1935 large systems can result in very long @code{gnat.adc} files, and also create
1936 a maintenance problem.
1937
1938 @geindex Source_File_Name pragma
1939
1940 GNAT also provides a facility for specifying systematic file naming schemes
1941 other than the standard default naming scheme previously described. An
1942 alternative scheme for naming is specified by the use of
1943 @code{Source_File_Name} pragmas having the following format:
1944
1945 @example
1946 pragma Source_File_Name (
1947 Spec_File_Name => FILE_NAME_PATTERN
1948 [ , Casing => CASING_SPEC]
1949 [ , Dot_Replacement => STRING_LITERAL ] );
1950
1951 pragma Source_File_Name (
1952 Body_File_Name => FILE_NAME_PATTERN
1953 [ , Casing => CASING_SPEC ]
1954 [ , Dot_Replacement => STRING_LITERAL ] ) ;
1955
1956 pragma Source_File_Name (
1957 Subunit_File_Name => FILE_NAME_PATTERN
1958 [ , Casing => CASING_SPEC ]
1959 [ , Dot_Replacement => STRING_LITERAL ] ) ;
1960
1961 FILE_NAME_PATTERN ::= STRING_LITERAL
1962 CASING_SPEC ::= Lowercase | Uppercase | Mixedcase
1963 @end example
1964
1965 The @code{FILE_NAME_PATTERN} string shows how the file name is constructed.
1966 It contains a single asterisk character, and the unit name is substituted
1967 systematically for this asterisk. The optional parameter
1968 @code{Casing} indicates
1969 whether the unit name is to be all upper-case letters, all lower-case letters,
1970 or mixed-case. If no
1971 @code{Casing} parameter is used, then the default is all
1972 lower-case.
1973
1974 The optional @code{Dot_Replacement} string is used to replace any periods
1975 that occur in subunit or child unit names. If no @code{Dot_Replacement}
1976 argument is used then separating dots appear unchanged in the resulting
1977 file name.
1978 Although the above syntax indicates that the
1979 @code{Casing} argument must appear
1980 before the @code{Dot_Replacement} argument, but it
1981 is also permissible to write these arguments in the opposite order.
1982
1983 As indicated, it is possible to specify different naming schemes for
1984 bodies, specs, and subunits. Quite often the rule for subunits is the
1985 same as the rule for bodies, in which case, there is no need to give
1986 a separate @code{Subunit_File_Name} rule, and in this case the
1987 @code{Body_File_name} rule is used for subunits as well.
1988
1989 The separate rule for subunits can also be used to implement the rather
1990 unusual case of a compilation environment (e.g., a single directory) which
1991 contains a subunit and a child unit with the same unit name. Although
1992 both units cannot appear in the same partition, the Ada Reference Manual
1993 allows (but does not require) the possibility of the two units coexisting
1994 in the same environment.
1995
1996 The file name translation works in the following steps:
1997
1998
1999 @itemize *
2000
2001 @item
2002 If there is a specific @code{Source_File_Name} pragma for the given unit,
2003 then this is always used, and any general pattern rules are ignored.
2004
2005 @item
2006 If there is a pattern type @code{Source_File_Name} pragma that applies to
2007 the unit, then the resulting file name will be used if the file exists. If
2008 more than one pattern matches, the latest one will be tried first, and the
2009 first attempt resulting in a reference to a file that exists will be used.
2010
2011 @item
2012 If no pattern type @code{Source_File_Name} pragma that applies to the unit
2013 for which the corresponding file exists, then the standard GNAT default
2014 naming rules are used.
2015 @end itemize
2016
2017 As an example of the use of this mechanism, consider a commonly used scheme
2018 in which file names are all lower case, with separating periods copied
2019 unchanged to the resulting file name, and specs end with @code{.1.ada}, and
2020 bodies end with @code{.2.ada}. GNAT will follow this scheme if the following
2021 two pragmas appear:
2022
2023 @example
2024 pragma Source_File_Name
2025 (Spec_File_Name => ".1.ada");
2026 pragma Source_File_Name
2027 (Body_File_Name => ".2.ada");
2028 @end example
2029
2030 The default GNAT scheme is actually implemented by providing the following
2031 default pragmas internally:
2032
2033 @example
2034 pragma Source_File_Name
2035 (Spec_File_Name => ".ads", Dot_Replacement => "-");
2036 pragma Source_File_Name
2037 (Body_File_Name => ".adb", Dot_Replacement => "-");
2038 @end example
2039
2040 Our final example implements a scheme typically used with one of the
2041 Ada 83 compilers, where the separator character for subunits was '__'
2042 (two underscores), specs were identified by adding @code{_.ADA}, bodies
2043 by adding @code{.ADA}, and subunits by
2044 adding @code{.SEP}. All file names were
2045 upper case. Child units were not present of course since this was an
2046 Ada 83 compiler, but it seems reasonable to extend this scheme to use
2047 the same double underscore separator for child units.
2048
2049 @example
2050 pragma Source_File_Name
2051 (Spec_File_Name => "_.ADA",
2052 Dot_Replacement => "__",
2053 Casing = Uppercase);
2054 pragma Source_File_Name
2055 (Body_File_Name => ".ADA",
2056 Dot_Replacement => "__",
2057 Casing = Uppercase);
2058 pragma Source_File_Name
2059 (Subunit_File_Name => ".SEP",
2060 Dot_Replacement => "__",
2061 Casing = Uppercase);
2062 @end example
2063
2064 @geindex gnatname
2065
2066 @node Handling Arbitrary File Naming Conventions with gnatname,File Name Krunching with gnatkr,Alternative File Naming Schemes,File Naming Topics and Utilities
2067 @anchor{gnat_ugn/the_gnat_compilation_model handling-arbitrary-file-naming-conventions-with-gnatname}@anchor{59}@anchor{gnat_ugn/the_gnat_compilation_model id12}@anchor{5a}
2068 @subsection Handling Arbitrary File Naming Conventions with @code{gnatname}
2069
2070
2071 @geindex File Naming Conventions
2072
2073 @menu
2074 * Arbitrary File Naming Conventions::
2075 * Running gnatname::
2076 * Switches for gnatname::
2077 * Examples of gnatname Usage::
2078
2079 @end menu
2080
2081 @node Arbitrary File Naming Conventions,Running gnatname,,Handling Arbitrary File Naming Conventions with gnatname
2082 @anchor{gnat_ugn/the_gnat_compilation_model arbitrary-file-naming-conventions}@anchor{5b}@anchor{gnat_ugn/the_gnat_compilation_model id13}@anchor{5c}
2083 @subsubsection Arbitrary File Naming Conventions
2084
2085
2086 The GNAT compiler must be able to know the source file name of a compilation
2087 unit. When using the standard GNAT default file naming conventions
2088 (@code{.ads} for specs, @code{.adb} for bodies), the GNAT compiler
2089 does not need additional information.
2090
2091 When the source file names do not follow the standard GNAT default file naming
2092 conventions, the GNAT compiler must be given additional information through
2093 a configuration pragmas file (@ref{14,,Configuration Pragmas})
2094 or a project file.
2095 When the non-standard file naming conventions are well-defined,
2096 a small number of pragmas @code{Source_File_Name} specifying a naming pattern
2097 (@ref{58,,Alternative File Naming Schemes}) may be sufficient. However,
2098 if the file naming conventions are irregular or arbitrary, a number
2099 of pragma @code{Source_File_Name} for individual compilation units
2100 must be defined.
2101 To help maintain the correspondence between compilation unit names and
2102 source file names within the compiler,
2103 GNAT provides a tool @code{gnatname} to generate the required pragmas for a
2104 set of files.
2105
2106 @node Running gnatname,Switches for gnatname,Arbitrary File Naming Conventions,Handling Arbitrary File Naming Conventions with gnatname
2107 @anchor{gnat_ugn/the_gnat_compilation_model running-gnatname}@anchor{5d}@anchor{gnat_ugn/the_gnat_compilation_model id14}@anchor{5e}
2108 @subsubsection Running @code{gnatname}
2109
2110
2111 The usual form of the @code{gnatname} command is:
2112
2113 @example
2114 $ gnatname [ switches ] naming_pattern [ naming_patterns ]
2115 [--and [ switches ] naming_pattern [ naming_patterns ]]
2116 @end example
2117
2118 All of the arguments are optional. If invoked without any argument,
2119 @code{gnatname} will display its usage.
2120
2121 When used with at least one naming pattern, @code{gnatname} will attempt to
2122 find all the compilation units in files that follow at least one of the
2123 naming patterns. To find these compilation units,
2124 @code{gnatname} will use the GNAT compiler in syntax-check-only mode on all
2125 regular files.
2126
2127 One or several Naming Patterns may be given as arguments to @code{gnatname}.
2128 Each Naming Pattern is enclosed between double quotes (or single
2129 quotes on Windows).
2130 A Naming Pattern is a regular expression similar to the wildcard patterns
2131 used in file names by the Unix shells or the DOS prompt.
2132
2133 @code{gnatname} may be called with several sections of directories/patterns.
2134 Sections are separated by the switch @code{--and}. In each section, there must be
2135 at least one pattern. If no directory is specified in a section, the current
2136 directory (or the project directory if @code{-P} is used) is implied.
2137 The options other that the directory switches and the patterns apply globally
2138 even if they are in different sections.
2139
2140 Examples of Naming Patterns are:
2141
2142 @example
2143 "*.[12].ada"
2144 "*.ad[sb]*"
2145 "body_*" "spec_*"
2146 @end example
2147
2148 For a more complete description of the syntax of Naming Patterns,
2149 see the second kind of regular expressions described in @code{g-regexp.ads}
2150 (the 'Glob' regular expressions).
2151
2152 When invoked without the switch @code{-P}, @code{gnatname} will create a
2153 configuration pragmas file @code{gnat.adc} in the current working directory,
2154 with pragmas @code{Source_File_Name} for each file that contains a valid Ada
2155 unit.
2156
2157 @node Switches for gnatname,Examples of gnatname Usage,Running gnatname,Handling Arbitrary File Naming Conventions with gnatname
2158 @anchor{gnat_ugn/the_gnat_compilation_model id15}@anchor{5f}@anchor{gnat_ugn/the_gnat_compilation_model switches-for-gnatname}@anchor{60}
2159 @subsubsection Switches for @code{gnatname}
2160
2161
2162 Switches for @code{gnatname} must precede any specified Naming Pattern.
2163
2164 You may specify any of the following switches to @code{gnatname}:
2165
2166 @geindex --version (gnatname)
2167
2168
2169 @table @asis
2170
2171 @item @code{--version}
2172
2173 Display Copyright and version, then exit disregarding all other options.
2174 @end table
2175
2176 @geindex --help (gnatname)
2177
2178
2179 @table @asis
2180
2181 @item @code{--help}
2182
2183 If @code{--version} was not used, display usage, then exit disregarding
2184 all other options.
2185
2186 @item @code{--subdirs=@emph{dir}}
2187
2188 Real object, library or exec directories are subdirectories <dir> of the
2189 specified ones.
2190
2191 @item @code{--no-backup}
2192
2193 Do not create a backup copy of an existing project file.
2194
2195 @item @code{--and}
2196
2197 Start another section of directories/patterns.
2198 @end table
2199
2200 @geindex -c (gnatname)
2201
2202
2203 @table @asis
2204
2205 @item @code{-c@emph{filename}}
2206
2207 Create a configuration pragmas file @code{filename} (instead of the default
2208 @code{gnat.adc}).
2209 There may be zero, one or more space between @code{-c} and
2210 @code{filename}.
2211 @code{filename} may include directory information. @code{filename} must be
2212 writable. There may be only one switch @code{-c}.
2213 When a switch @code{-c} is
2214 specified, no switch @code{-P} may be specified (see below).
2215 @end table
2216
2217 @geindex -d (gnatname)
2218
2219
2220 @table @asis
2221
2222 @item @code{-d@emph{dir}}
2223
2224 Look for source files in directory @code{dir}. There may be zero, one or more
2225 spaces between @code{-d} and @code{dir}.
2226 @code{dir} may end with @code{/**}, that is it may be of the form
2227 @code{root_dir/**}. In this case, the directory @code{root_dir} and all of its
2228 subdirectories, recursively, have to be searched for sources.
2229 When a switch @code{-d}
2230 is specified, the current working directory will not be searched for source
2231 files, unless it is explicitly specified with a @code{-d}
2232 or @code{-D} switch.
2233 Several switches @code{-d} may be specified.
2234 If @code{dir} is a relative path, it is relative to the directory of
2235 the configuration pragmas file specified with switch
2236 @code{-c},
2237 or to the directory of the project file specified with switch
2238 @code{-P} or,
2239 if neither switch @code{-c}
2240 nor switch @code{-P} are specified, it is relative to the
2241 current working directory. The directory
2242 specified with switch @code{-d} must exist and be readable.
2243 @end table
2244
2245 @geindex -D (gnatname)
2246
2247
2248 @table @asis
2249
2250 @item @code{-D@emph{filename}}
2251
2252 Look for source files in all directories listed in text file @code{filename}.
2253 There may be zero, one or more spaces between @code{-D}
2254 and @code{filename}.
2255 @code{filename} must be an existing, readable text file.
2256 Each nonempty line in @code{filename} must be a directory.
2257 Specifying switch @code{-D} is equivalent to specifying as many
2258 switches @code{-d} as there are nonempty lines in
2259 @code{file}.
2260
2261 @item @code{-eL}
2262
2263 Follow symbolic links when processing project files.
2264
2265 @geindex -f (gnatname)
2266
2267 @item @code{-f@emph{pattern}}
2268
2269 Foreign patterns. Using this switch, it is possible to add sources of languages
2270 other than Ada to the list of sources of a project file.
2271 It is only useful if a -P switch is used.
2272 For example,
2273
2274 @example
2275 gnatname -Pprj -f"*.c" "*.ada"
2276 @end example
2277
2278 will look for Ada units in all files with the @code{.ada} extension,
2279 and will add to the list of file for project @code{prj.gpr} the C files
2280 with extension @code{.c}.
2281
2282 @geindex -h (gnatname)
2283
2284 @item @code{-h}
2285
2286 Output usage (help) information. The output is written to @code{stdout}.
2287
2288 @geindex -P (gnatname)
2289
2290 @item @code{-P@emph{proj}}
2291
2292 Create or update project file @code{proj}. There may be zero, one or more space
2293 between @code{-P} and @code{proj}. @code{proj} may include directory
2294 information. @code{proj} must be writable.
2295 There may be only one switch @code{-P}.
2296 When a switch @code{-P} is specified,
2297 no switch @code{-c} may be specified.
2298 On all platforms, except on VMS, when @code{gnatname} is invoked for an
2299 existing project file <proj>.gpr, a backup copy of the project file is created
2300 in the project directory with file name <proj>.gpr.saved_x. 'x' is the first
2301 non negative number that makes this backup copy a new file.
2302
2303 @geindex -v (gnatname)
2304
2305 @item @code{-v}
2306
2307 Verbose mode. Output detailed explanation of behavior to @code{stdout}.
2308 This includes name of the file written, the name of the directories to search
2309 and, for each file in those directories whose name matches at least one of
2310 the Naming Patterns, an indication of whether the file contains a unit,
2311 and if so the name of the unit.
2312 @end table
2313
2314 @geindex -v -v (gnatname)
2315
2316
2317 @table @asis
2318
2319 @item @code{-v -v}
2320
2321 Very Verbose mode. In addition to the output produced in verbose mode,
2322 for each file in the searched directories whose name matches none of
2323 the Naming Patterns, an indication is given that there is no match.
2324
2325 @geindex -x (gnatname)
2326
2327 @item @code{-x@emph{pattern}}
2328
2329 Excluded patterns. Using this switch, it is possible to exclude some files
2330 that would match the name patterns. For example,
2331
2332 @example
2333 gnatname -x "*_nt.ada" "*.ada"
2334 @end example
2335
2336 will look for Ada units in all files with the @code{.ada} extension,
2337 except those whose names end with @code{_nt.ada}.
2338 @end table
2339
2340 @node Examples of gnatname Usage,,Switches for gnatname,Handling Arbitrary File Naming Conventions with gnatname
2341 @anchor{gnat_ugn/the_gnat_compilation_model examples-of-gnatname-usage}@anchor{61}@anchor{gnat_ugn/the_gnat_compilation_model id16}@anchor{62}
2342 @subsubsection Examples of @code{gnatname} Usage
2343
2344
2345 @example
2346 $ gnatname -c /home/me/names.adc -d sources "[a-z]*.ada*"
2347 @end example
2348
2349 In this example, the directory @code{/home/me} must already exist
2350 and be writable. In addition, the directory
2351 @code{/home/me/sources} (specified by
2352 @code{-d sources}) must exist and be readable.
2353
2354 Note the optional spaces after @code{-c} and @code{-d}.
2355
2356 @example
2357 $ gnatname -P/home/me/proj -x "*_nt_body.ada"
2358 -dsources -dsources/plus -Dcommon_dirs.txt "body_*" "spec_*"
2359 @end example
2360
2361 Note that several switches @code{-d} may be used,
2362 even in conjunction with one or several switches
2363 @code{-D}. Several Naming Patterns and one excluded pattern
2364 are used in this example.
2365
2366 @node File Name Krunching with gnatkr,Renaming Files with gnatchop,Handling Arbitrary File Naming Conventions with gnatname,File Naming Topics and Utilities
2367 @anchor{gnat_ugn/the_gnat_compilation_model file-name-krunching-with-gnatkr}@anchor{63}@anchor{gnat_ugn/the_gnat_compilation_model id17}@anchor{64}
2368 @subsection File Name Krunching with @code{gnatkr}
2369
2370
2371 @geindex gnatkr
2372
2373 This section discusses the method used by the compiler to shorten
2374 the default file names chosen for Ada units so that they do not
2375 exceed the maximum length permitted. It also describes the
2376 @code{gnatkr} utility that can be used to determine the result of
2377 applying this shortening.
2378
2379 @menu
2380 * About gnatkr::
2381 * Using gnatkr::
2382 * Krunching Method::
2383 * Examples of gnatkr Usage::
2384
2385 @end menu
2386
2387 @node About gnatkr,Using gnatkr,,File Name Krunching with gnatkr
2388 @anchor{gnat_ugn/the_gnat_compilation_model id18}@anchor{65}@anchor{gnat_ugn/the_gnat_compilation_model about-gnatkr}@anchor{66}
2389 @subsubsection About @code{gnatkr}
2390
2391
2392 The default file naming rule in GNAT
2393 is that the file name must be derived from
2394 the unit name. The exact default rule is as follows:
2395
2396
2397 @itemize *
2398
2399 @item
2400 Take the unit name and replace all dots by hyphens.
2401
2402 @item
2403 If such a replacement occurs in the
2404 second character position of a name, and the first character is
2405 @code{a}, @code{g}, @code{s}, or @code{i},
2406 then replace the dot by the character
2407 @code{~} (tilde)
2408 instead of a minus.
2409
2410 The reason for this exception is to avoid clashes
2411 with the standard names for children of System, Ada, Interfaces,
2412 and GNAT, which use the prefixes
2413 @code{s-}, @code{a-}, @code{i-}, and @code{g-},
2414 respectively.
2415 @end itemize
2416
2417 The @code{-gnatk@emph{nn}}
2418 switch of the compiler activates a 'krunching'
2419 circuit that limits file names to nn characters (where nn is a decimal
2420 integer).
2421
2422 The @code{gnatkr} utility can be used to determine the krunched name for
2423 a given file, when krunched to a specified maximum length.
2424
2425 @node Using gnatkr,Krunching Method,About gnatkr,File Name Krunching with gnatkr
2426 @anchor{gnat_ugn/the_gnat_compilation_model id19}@anchor{67}@anchor{gnat_ugn/the_gnat_compilation_model using-gnatkr}@anchor{54}
2427 @subsubsection Using @code{gnatkr}
2428
2429
2430 The @code{gnatkr} command has the form:
2431
2432 @example
2433 $ gnatkr name [ length ]
2434 @end example
2435
2436 @code{name} is the uncrunched file name, derived from the name of the unit
2437 in the standard manner described in the previous section (i.e., in particular
2438 all dots are replaced by hyphens). The file name may or may not have an
2439 extension (defined as a suffix of the form period followed by arbitrary
2440 characters other than period). If an extension is present then it will
2441 be preserved in the output. For example, when krunching @code{hellofile.ads}
2442 to eight characters, the result will be hellofil.ads.
2443
2444 Note: for compatibility with previous versions of @code{gnatkr} dots may
2445 appear in the name instead of hyphens, but the last dot will always be
2446 taken as the start of an extension. So if @code{gnatkr} is given an argument
2447 such as @code{Hello.World.adb} it will be treated exactly as if the first
2448 period had been a hyphen, and for example krunching to eight characters
2449 gives the result @code{hellworl.adb}.
2450
2451 Note that the result is always all lower case.
2452 Characters of the other case are folded as required.
2453
2454 @code{length} represents the length of the krunched name. The default
2455 when no argument is given is 8 characters. A length of zero stands for
2456 unlimited, in other words do not chop except for system files where the
2457 implied crunching length is always eight characters.
2458
2459 The output is the krunched name. The output has an extension only if the
2460 original argument was a file name with an extension.
2461
2462 @node Krunching Method,Examples of gnatkr Usage,Using gnatkr,File Name Krunching with gnatkr
2463 @anchor{gnat_ugn/the_gnat_compilation_model id20}@anchor{68}@anchor{gnat_ugn/the_gnat_compilation_model krunching-method}@anchor{69}
2464 @subsubsection Krunching Method
2465
2466
2467 The initial file name is determined by the name of the unit that the file
2468 contains. The name is formed by taking the full expanded name of the
2469 unit and replacing the separating dots with hyphens and
2470 using lowercase
2471 for all letters, except that a hyphen in the second character position is
2472 replaced by a tilde if the first character is
2473 @code{a}, @code{i}, @code{g}, or @code{s}.
2474 The extension is @code{.ads} for a
2475 spec and @code{.adb} for a body.
2476 Krunching does not affect the extension, but the file name is shortened to
2477 the specified length by following these rules:
2478
2479
2480 @itemize *
2481
2482 @item
2483 The name is divided into segments separated by hyphens, tildes or
2484 underscores and all hyphens, tildes, and underscores are
2485 eliminated. If this leaves the name short enough, we are done.
2486
2487 @item
2488 If the name is too long, the longest segment is located (left-most
2489 if there are two of equal length), and shortened by dropping
2490 its last character. This is repeated until the name is short enough.
2491
2492 As an example, consider the krunching of @code{our-strings-wide_fixed.adb}
2493 to fit the name into 8 characters as required by some operating systems:
2494
2495 @example
2496 our-strings-wide_fixed 22
2497 our strings wide fixed 19
2498 our string wide fixed 18
2499 our strin wide fixed 17
2500 our stri wide fixed 16
2501 our stri wide fixe 15
2502 our str wide fixe 14
2503 our str wid fixe 13
2504 our str wid fix 12
2505 ou str wid fix 11
2506 ou st wid fix 10
2507 ou st wi fix 9
2508 ou st wi fi 8
2509 Final file name: oustwifi.adb
2510 @end example
2511
2512 @item
2513 The file names for all predefined units are always krunched to eight
2514 characters. The krunching of these predefined units uses the following
2515 special prefix replacements:
2516
2517
2518 @multitable {xxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxx}
2519 @item
2520
2521 Prefix
2522
2523 @tab
2524
2525 Replacement
2526
2527 @item
2528
2529 @code{ada-}
2530
2531 @tab
2532
2533 @code{a-}
2534
2535 @item
2536
2537 @code{gnat-}
2538
2539 @tab
2540
2541 @code{g-}
2542
2543 @item
2544
2545 @code{interfac es-}
2546
2547 @tab
2548
2549 @code{i-}
2550
2551 @item
2552
2553 @code{system-}
2554
2555 @tab
2556
2557 @code{s-}
2558
2559 @end multitable
2560
2561
2562 These system files have a hyphen in the second character position. That
2563 is why normal user files replace such a character with a
2564 tilde, to avoid confusion with system file names.
2565
2566 As an example of this special rule, consider
2567 @code{ada-strings-wide_fixed.adb}, which gets krunched as follows:
2568
2569 @example
2570 ada-strings-wide_fixed 22
2571 a- strings wide fixed 18
2572 a- string wide fixed 17
2573 a- strin wide fixed 16
2574 a- stri wide fixed 15
2575 a- stri wide fixe 14
2576 a- str wide fixe 13
2577 a- str wid fixe 12
2578 a- str wid fix 11
2579 a- st wid fix 10
2580 a- st wi fix 9
2581 a- st wi fi 8
2582 Final file name: a-stwifi.adb
2583 @end example
2584 @end itemize
2585
2586 Of course no file shortening algorithm can guarantee uniqueness over all
2587 possible unit names, and if file name krunching is used then it is your
2588 responsibility to ensure that no name clashes occur. The utility
2589 program @code{gnatkr} is supplied for conveniently determining the
2590 krunched name of a file.
2591
2592 @node Examples of gnatkr Usage,,Krunching Method,File Name Krunching with gnatkr
2593 @anchor{gnat_ugn/the_gnat_compilation_model id21}@anchor{6a}@anchor{gnat_ugn/the_gnat_compilation_model examples-of-gnatkr-usage}@anchor{6b}
2594 @subsubsection Examples of @code{gnatkr} Usage
2595
2596
2597 @example
2598 $ gnatkr very_long_unit_name.ads --> velounna.ads
2599 $ gnatkr grandparent-parent-child.ads --> grparchi.ads
2600 $ gnatkr Grandparent.Parent.Child.ads --> grparchi.ads
2601 $ gnatkr grandparent-parent-child --> grparchi
2602 $ gnatkr very_long_unit_name.ads/count=6 --> vlunna.ads
2603 $ gnatkr very_long_unit_name.ads/count=0 --> very_long_unit_name.ads
2604 @end example
2605
2606 @node Renaming Files with gnatchop,,File Name Krunching with gnatkr,File Naming Topics and Utilities
2607 @anchor{gnat_ugn/the_gnat_compilation_model id22}@anchor{6c}@anchor{gnat_ugn/the_gnat_compilation_model renaming-files-with-gnatchop}@anchor{36}
2608 @subsection Renaming Files with @code{gnatchop}
2609
2610
2611 @geindex gnatchop
2612
2613 This section discusses how to handle files with multiple units by using
2614 the @code{gnatchop} utility. This utility is also useful in renaming
2615 files to meet the standard GNAT default file naming conventions.
2616
2617 @menu
2618 * Handling Files with Multiple Units::
2619 * Operating gnatchop in Compilation Mode::
2620 * Command Line for gnatchop::
2621 * Switches for gnatchop::
2622 * Examples of gnatchop Usage::
2623
2624 @end menu
2625
2626 @node Handling Files with Multiple Units,Operating gnatchop in Compilation Mode,,Renaming Files with gnatchop
2627 @anchor{gnat_ugn/the_gnat_compilation_model id23}@anchor{6d}@anchor{gnat_ugn/the_gnat_compilation_model handling-files-with-multiple-units}@anchor{6e}
2628 @subsubsection Handling Files with Multiple Units
2629
2630
2631 The basic compilation model of GNAT requires that a file submitted to the
2632 compiler have only one unit and there be a strict correspondence
2633 between the file name and the unit name.
2634
2635 If you want to keep your files with multiple units,
2636 perhaps to maintain compatibility with some other Ada compilation system,
2637 you can use @code{gnatname} to generate or update your project files.
2638 Generated or modified project files can be processed by GNAT.
2639
2640 See @ref{59,,Handling Arbitrary File Naming Conventions with gnatname}
2641 for more details on how to use @cite{gnatname}.
2642
2643 Alternatively, if you want to permanently restructure a set of 'foreign'
2644 files so that they match the GNAT rules, and do the remaining development
2645 using the GNAT structure, you can simply use @code{gnatchop} once, generate the
2646 new set of files and work with them from that point on.
2647
2648 Note that if your file containing multiple units starts with a byte order
2649 mark (BOM) specifying UTF-8 encoding, then the files generated by gnatchop
2650 will each start with a copy of this BOM, meaning that they can be compiled
2651 automatically in UTF-8 mode without needing to specify an explicit encoding.
2652
2653 @node Operating gnatchop in Compilation Mode,Command Line for gnatchop,Handling Files with Multiple Units,Renaming Files with gnatchop
2654 @anchor{gnat_ugn/the_gnat_compilation_model operating-gnatchop-in-compilation-mode}@anchor{6f}@anchor{gnat_ugn/the_gnat_compilation_model id24}@anchor{70}
2655 @subsubsection Operating gnatchop in Compilation Mode
2656
2657
2658 The basic function of @code{gnatchop} is to take a file with multiple units
2659 and split it into separate files. The boundary between files is reasonably
2660 clear, except for the issue of comments and pragmas. In default mode, the
2661 rule is that any pragmas between units belong to the previous unit, except
2662 that configuration pragmas always belong to the following unit. Any comments
2663 belong to the following unit. These rules
2664 almost always result in the right choice of
2665 the split point without needing to mark it explicitly and most users will
2666 find this default to be what they want. In this default mode it is incorrect to
2667 submit a file containing only configuration pragmas, or one that ends in
2668 configuration pragmas, to @code{gnatchop}.
2669
2670 However, using a special option to activate 'compilation mode',
2671 @code{gnatchop}
2672 can perform another function, which is to provide exactly the semantics
2673 required by the RM for handling of configuration pragmas in a compilation.
2674 In the absence of configuration pragmas (at the main file level), this
2675 option has no effect, but it causes such configuration pragmas to be handled
2676 in a quite different manner.
2677
2678 First, in compilation mode, if @code{gnatchop} is given a file that consists of
2679 only configuration pragmas, then this file is appended to the
2680 @code{gnat.adc} file in the current directory. This behavior provides
2681 the required behavior described in the RM for the actions to be taken
2682 on submitting such a file to the compiler, namely that these pragmas
2683 should apply to all subsequent compilations in the same compilation
2684 environment. Using GNAT, the current directory, possibly containing a
2685 @code{gnat.adc} file is the representation
2686 of a compilation environment. For more information on the
2687 @code{gnat.adc} file, see @ref{56,,Handling of Configuration Pragmas}.
2688
2689 Second, in compilation mode, if @code{gnatchop}
2690 is given a file that starts with
2691 configuration pragmas, and contains one or more units, then these
2692 configuration pragmas are prepended to each of the chopped files. This
2693 behavior provides the required behavior described in the RM for the
2694 actions to be taken on compiling such a file, namely that the pragmas
2695 apply to all units in the compilation, but not to subsequently compiled
2696 units.
2697
2698 Finally, if configuration pragmas appear between units, they are appended
2699 to the previous unit. This results in the previous unit being illegal,
2700 since the compiler does not accept configuration pragmas that follow
2701 a unit. This provides the required RM behavior that forbids configuration
2702 pragmas other than those preceding the first compilation unit of a
2703 compilation.
2704
2705 For most purposes, @code{gnatchop} will be used in default mode. The
2706 compilation mode described above is used only if you need exactly
2707 accurate behavior with respect to compilations, and you have files
2708 that contain multiple units and configuration pragmas. In this
2709 circumstance the use of @code{gnatchop} with the compilation mode
2710 switch provides the required behavior, and is for example the mode
2711 in which GNAT processes the ACVC tests.
2712
2713 @node Command Line for gnatchop,Switches for gnatchop,Operating gnatchop in Compilation Mode,Renaming Files with gnatchop
2714 @anchor{gnat_ugn/the_gnat_compilation_model id25}@anchor{71}@anchor{gnat_ugn/the_gnat_compilation_model command-line-for-gnatchop}@anchor{72}
2715 @subsubsection Command Line for @code{gnatchop}
2716
2717
2718 The @code{gnatchop} command has the form:
2719
2720 @example
2721 $ gnatchop switches file_name [file_name ...]
2722 [directory]
2723 @end example
2724
2725 The only required argument is the file name of the file to be chopped.
2726 There are no restrictions on the form of this file name. The file itself
2727 contains one or more Ada units, in normal GNAT format, concatenated
2728 together. As shown, more than one file may be presented to be chopped.
2729
2730 When run in default mode, @code{gnatchop} generates one output file in
2731 the current directory for each unit in each of the files.
2732
2733 @code{directory}, if specified, gives the name of the directory to which
2734 the output files will be written. If it is not specified, all files are
2735 written to the current directory.
2736
2737 For example, given a
2738 file called @code{hellofiles} containing
2739
2740 @example
2741 procedure Hello;
2742
2743 with Ada.Text_IO; use Ada.Text_IO;
2744 procedure Hello is
2745 begin
2746 Put_Line ("Hello");
2747 end Hello;
2748 @end example
2749
2750 the command
2751
2752 @example
2753 $ gnatchop hellofiles
2754 @end example
2755
2756 generates two files in the current directory, one called
2757 @code{hello.ads} containing the single line that is the procedure spec,
2758 and the other called @code{hello.adb} containing the remaining text. The
2759 original file is not affected. The generated files can be compiled in
2760 the normal manner.
2761
2762 When gnatchop is invoked on a file that is empty or that contains only empty
2763 lines and/or comments, gnatchop will not fail, but will not produce any
2764 new sources.
2765
2766 For example, given a
2767 file called @code{toto.txt} containing
2768
2769 @example
2770 -- Just a comment
2771 @end example
2772
2773 the command
2774
2775 @example
2776 $ gnatchop toto.txt
2777 @end example
2778
2779 will not produce any new file and will result in the following warnings:
2780
2781 @example
2782 toto.txt:1:01: warning: empty file, contains no compilation units
2783 no compilation units found
2784 no source files written
2785 @end example
2786
2787 @node Switches for gnatchop,Examples of gnatchop Usage,Command Line for gnatchop,Renaming Files with gnatchop
2788 @anchor{gnat_ugn/the_gnat_compilation_model switches-for-gnatchop}@anchor{73}@anchor{gnat_ugn/the_gnat_compilation_model id26}@anchor{74}
2789 @subsubsection Switches for @code{gnatchop}
2790
2791
2792 @code{gnatchop} recognizes the following switches:
2793
2794 @geindex --version (gnatchop)
2795
2796
2797 @table @asis
2798
2799 @item @code{--version}
2800
2801 Display Copyright and version, then exit disregarding all other options.
2802 @end table
2803
2804 @geindex --help (gnatchop)
2805
2806
2807 @table @asis
2808
2809 @item @code{--help}
2810
2811 If @code{--version} was not used, display usage, then exit disregarding
2812 all other options.
2813 @end table
2814
2815 @geindex -c (gnatchop)
2816
2817
2818 @table @asis
2819
2820 @item @code{-c}
2821
2822 Causes @code{gnatchop} to operate in compilation mode, in which
2823 configuration pragmas are handled according to strict RM rules. See
2824 previous section for a full description of this mode.
2825
2826 @item @code{-gnat@emph{xxx}}
2827
2828 This passes the given @code{-gnat@emph{xxx}} switch to @code{gnat} which is
2829 used to parse the given file. Not all @emph{xxx} options make sense,
2830 but for example, the use of @code{-gnati2} allows @code{gnatchop} to
2831 process a source file that uses Latin-2 coding for identifiers.
2832
2833 @item @code{-h}
2834
2835 Causes @code{gnatchop} to generate a brief help summary to the standard
2836 output file showing usage information.
2837 @end table
2838
2839 @geindex -k (gnatchop)
2840
2841
2842 @table @asis
2843
2844 @item @code{-k@emph{mm}}
2845
2846 Limit generated file names to the specified number @code{mm}
2847 of characters.
2848 This is useful if the
2849 resulting set of files is required to be interoperable with systems
2850 which limit the length of file names.
2851 No space is allowed between the @code{-k} and the numeric value. The numeric
2852 value may be omitted in which case a default of @code{-k8},
2853 suitable for use
2854 with DOS-like file systems, is used. If no @code{-k} switch
2855 is present then
2856 there is no limit on the length of file names.
2857 @end table
2858
2859 @geindex -p (gnatchop)
2860
2861
2862 @table @asis
2863
2864 @item @code{-p}
2865
2866 Causes the file modification time stamp of the input file to be
2867 preserved and used for the time stamp of the output file(s). This may be
2868 useful for preserving coherency of time stamps in an environment where
2869 @code{gnatchop} is used as part of a standard build process.
2870 @end table
2871
2872 @geindex -q (gnatchop)
2873
2874
2875 @table @asis
2876
2877 @item @code{-q}
2878
2879 Causes output of informational messages indicating the set of generated
2880 files to be suppressed. Warnings and error messages are unaffected.
2881 @end table
2882
2883 @geindex -r (gnatchop)
2884
2885 @geindex Source_Reference pragmas
2886
2887
2888 @table @asis
2889
2890 @item @code{-r}
2891
2892 Generate @code{Source_Reference} pragmas. Use this switch if the output
2893 files are regarded as temporary and development is to be done in terms
2894 of the original unchopped file. This switch causes
2895 @code{Source_Reference} pragmas to be inserted into each of the
2896 generated files to refers back to the original file name and line number.
2897 The result is that all error messages refer back to the original
2898 unchopped file.
2899 In addition, the debugging information placed into the object file (when
2900 the @code{-g} switch of @code{gcc} or @code{gnatmake} is
2901 specified)
2902 also refers back to this original file so that tools like profilers and
2903 debuggers will give information in terms of the original unchopped file.
2904
2905 If the original file to be chopped itself contains
2906 a @code{Source_Reference}
2907 pragma referencing a third file, then gnatchop respects
2908 this pragma, and the generated @code{Source_Reference} pragmas
2909 in the chopped file refer to the original file, with appropriate
2910 line numbers. This is particularly useful when @code{gnatchop}
2911 is used in conjunction with @code{gnatprep} to compile files that
2912 contain preprocessing statements and multiple units.
2913 @end table
2914
2915 @geindex -v (gnatchop)
2916
2917
2918 @table @asis
2919
2920 @item @code{-v}
2921
2922 Causes @code{gnatchop} to operate in verbose mode. The version
2923 number and copyright notice are output, as well as exact copies of
2924 the gnat1 commands spawned to obtain the chop control information.
2925 @end table
2926
2927 @geindex -w (gnatchop)
2928
2929
2930 @table @asis
2931
2932 @item @code{-w}
2933
2934 Overwrite existing file names. Normally @code{gnatchop} regards it as a
2935 fatal error if there is already a file with the same name as a
2936 file it would otherwise output, in other words if the files to be
2937 chopped contain duplicated units. This switch bypasses this
2938 check, and causes all but the last instance of such duplicated
2939 units to be skipped.
2940 @end table
2941
2942 @geindex --GCC= (gnatchop)
2943
2944
2945 @table @asis
2946
2947 @item @code{--GCC=@emph{xxxx}}
2948
2949 Specify the path of the GNAT parser to be used. When this switch is used,
2950 no attempt is made to add the prefix to the GNAT parser executable.
2951 @end table
2952
2953 @node Examples of gnatchop Usage,,Switches for gnatchop,Renaming Files with gnatchop
2954 @anchor{gnat_ugn/the_gnat_compilation_model id27}@anchor{75}@anchor{gnat_ugn/the_gnat_compilation_model examples-of-gnatchop-usage}@anchor{76}
2955 @subsubsection Examples of @code{gnatchop} Usage
2956
2957
2958 @example
2959 $ gnatchop -w hello_s.ada prerelease/files
2960 @end example
2961
2962 Chops the source file @code{hello_s.ada}. The output files will be
2963 placed in the directory @code{prerelease/files},
2964 overwriting any
2965 files with matching names in that directory (no files in the current
2966 directory are modified).
2967
2968 @example
2969 $ gnatchop archive
2970 @end example
2971
2972 Chops the source file @code{archive}
2973 into the current directory. One
2974 useful application of @code{gnatchop} is in sending sets of sources
2975 around, for example in email messages. The required sources are simply
2976 concatenated (for example, using a Unix @code{cat}
2977 command), and then
2978 @code{gnatchop} is used at the other end to reconstitute the original
2979 file names.
2980
2981 @example
2982 $ gnatchop file1 file2 file3 direc
2983 @end example
2984
2985 Chops all units in files @code{file1}, @code{file2}, @code{file3}, placing
2986 the resulting files in the directory @code{direc}. Note that if any units
2987 occur more than once anywhere within this set of files, an error message
2988 is generated, and no files are written. To override this check, use the
2989 @code{-w} switch,
2990 in which case the last occurrence in the last file will
2991 be the one that is output, and earlier duplicate occurrences for a given
2992 unit will be skipped.
2993
2994 @node Configuration Pragmas,Generating Object Files,File Naming Topics and Utilities,The GNAT Compilation Model
2995 @anchor{gnat_ugn/the_gnat_compilation_model id28}@anchor{77}@anchor{gnat_ugn/the_gnat_compilation_model configuration-pragmas}@anchor{14}
2996 @section Configuration Pragmas
2997
2998
2999 @geindex Configuration pragmas
3000
3001 @geindex Pragmas
3002 @geindex configuration
3003
3004 Configuration pragmas include those pragmas described as
3005 such in the Ada Reference Manual, as well as
3006 implementation-dependent pragmas that are configuration pragmas.
3007 See the @code{Implementation_Defined_Pragmas} chapter in the
3008 @cite{GNAT_Reference_Manual} for details on these
3009 additional GNAT-specific configuration pragmas.
3010 Most notably, the pragma @code{Source_File_Name}, which allows
3011 specifying non-default names for source files, is a configuration
3012 pragma. The following is a complete list of configuration pragmas
3013 recognized by GNAT:
3014
3015 @example
3016 Ada_83
3017 Ada_95
3018 Ada_05
3019 Ada_2005
3020 Ada_12
3021 Ada_2012
3022 Allow_Integer_Address
3023 Annotate
3024 Assertion_Policy
3025 Assume_No_Invalid_Values
3026 C_Pass_By_Copy
3027 Check_Float_Overflow
3028 Check_Name
3029 Check_Policy
3030 Compile_Time_Error
3031 Compile_Time_Warning
3032 Compiler_Unit
3033 Compiler_Unit_Warning
3034 Component_Alignment
3035 Convention_Identifier
3036 Debug_Policy
3037 Detect_Blocking
3038 Default_Scalar_Storage_Order
3039 Default_Storage_Pool
3040 Disable_Atomic_Synchronization
3041 Discard_Names
3042 Elaboration_Checks
3043 Eliminate
3044 Enable_Atomic_Synchronization
3045 Extend_System
3046 Extensions_Allowed
3047 External_Name_Casing
3048 Fast_Math
3049 Favor_Top_Level
3050 Ignore_Pragma
3051 Implicit_Packing
3052 Initialize_Scalars
3053 Interrupt_State
3054 License
3055 Locking_Policy
3056 No_Component_Reordering
3057 No_Heap_Finalization
3058 No_Run_Time
3059 No_Strict_Aliasing
3060 Normalize_Scalars
3061 Optimize_Alignment
3062 Overflow_Mode
3063 Overriding_Renamings
3064 Partition_Elaboration_Policy
3065 Persistent_BSS
3066 Polling
3067 Prefix_Exception_Messages
3068 Priority_Specific_Dispatching
3069 Profile
3070 Profile_Warnings
3071 Propagate_Exceptions
3072 Queuing_Policy
3073 Rational
3074 Ravenscar
3075 Rename_Pragma
3076 Restricted_Run_Time
3077 Restrictions
3078 Restrictions_Warnings
3079 Reviewable
3080 Short_Circuit_And_Or
3081 Short_Descriptors
3082 Source_File_Name
3083 Source_File_Name_Project
3084 SPARK_Mode
3085 Style_Checks
3086 Suppress
3087 Suppress_Exception_Locations
3088 Task_Dispatching_Policy
3089 Unevaluated_Use_Of_Old
3090 Universal_Data
3091 Unsuppress
3092 Use_VADS_Size
3093 Validity_Checks
3094 Warning_As_Error
3095 Warnings
3096 Wide_Character_Encoding
3097 @end example
3098
3099 @menu
3100 * Handling of Configuration Pragmas::
3101 * The Configuration Pragmas Files::
3102
3103 @end menu
3104
3105 @node Handling of Configuration Pragmas,The Configuration Pragmas Files,,Configuration Pragmas
3106 @anchor{gnat_ugn/the_gnat_compilation_model id29}@anchor{78}@anchor{gnat_ugn/the_gnat_compilation_model handling-of-configuration-pragmas}@anchor{56}
3107 @subsection Handling of Configuration Pragmas
3108
3109
3110 Configuration pragmas may either appear at the start of a compilation
3111 unit, or they can appear in a configuration pragma file to apply to
3112 all compilations performed in a given compilation environment.
3113
3114 GNAT also provides the @code{gnatchop} utility to provide an automatic
3115 way to handle configuration pragmas following the semantics for
3116 compilations (that is, files with multiple units), described in the RM.
3117 See @ref{6f,,Operating gnatchop in Compilation Mode} for details.
3118 However, for most purposes, it will be more convenient to edit the
3119 @code{gnat.adc} file that contains configuration pragmas directly,
3120 as described in the following section.
3121
3122 In the case of @code{Restrictions} pragmas appearing as configuration
3123 pragmas in individual compilation units, the exact handling depends on
3124 the type of restriction.
3125
3126 Restrictions that require partition-wide consistency (like
3127 @code{No_Tasking}) are
3128 recognized wherever they appear
3129 and can be freely inherited, e.g. from a @emph{with}ed unit to the @emph{with}ing
3130 unit. This makes sense since the binder will in any case insist on seeing
3131 consistent use, so any unit not conforming to any restrictions that are
3132 anywhere in the partition will be rejected, and you might as well find
3133 that out at compile time rather than at bind time.
3134
3135 For restrictions that do not require partition-wide consistency, e.g.
3136 SPARK or No_Implementation_Attributes, in general the restriction applies
3137 only to the unit in which the pragma appears, and not to any other units.
3138
3139 The exception is No_Elaboration_Code which always applies to the entire
3140 object file from a compilation, i.e. to the body, spec, and all subunits.
3141 This restriction can be specified in a configuration pragma file, or it
3142 can be on the body and/or the spec (in eithe case it applies to all the
3143 relevant units). It can appear on a subunit only if it has previously
3144 appeared in the body of spec.
3145
3146 @node The Configuration Pragmas Files,,Handling of Configuration Pragmas,Configuration Pragmas
3147 @anchor{gnat_ugn/the_gnat_compilation_model the-configuration-pragmas-files}@anchor{79}@anchor{gnat_ugn/the_gnat_compilation_model id30}@anchor{7a}
3148 @subsection The Configuration Pragmas Files
3149
3150
3151 @geindex gnat.adc
3152
3153 In GNAT a compilation environment is defined by the current
3154 directory at the time that a compile command is given. This current
3155 directory is searched for a file whose name is @code{gnat.adc}. If
3156 this file is present, it is expected to contain one or more
3157 configuration pragmas that will be applied to the current compilation.
3158 However, if the switch @code{-gnatA} is used, @code{gnat.adc} is not
3159 considered. When taken into account, @code{gnat.adc} is added to the
3160 dependencies, so that if @code{gnat.adc} is modified later, an invocation of
3161 @code{gnatmake} will recompile the source.
3162
3163 Configuration pragmas may be entered into the @code{gnat.adc} file
3164 either by running @code{gnatchop} on a source file that consists only of
3165 configuration pragmas, or more conveniently by direct editing of the
3166 @code{gnat.adc} file, which is a standard format source file.
3167
3168 Besides @code{gnat.adc}, additional files containing configuration
3169 pragmas may be applied to the current compilation using the switch
3170 @code{-gnatec=@emph{path}} where @code{path} must designate an existing file that
3171 contains only configuration pragmas. These configuration pragmas are
3172 in addition to those found in @code{gnat.adc} (provided @code{gnat.adc}
3173 is present and switch @code{-gnatA} is not used).
3174
3175 It is allowable to specify several switches @code{-gnatec=}, all of which
3176 will be taken into account.
3177
3178 Files containing configuration pragmas specified with switches
3179 @code{-gnatec=} are added to the dependencies, unless they are
3180 temporary files. A file is considered temporary if its name ends in
3181 @code{.tmp} or @code{.TMP}. Certain tools follow this naming
3182 convention because they pass information to @code{gcc} via
3183 temporary files that are immediately deleted; it doesn't make sense to
3184 depend on a file that no longer exists. Such tools include
3185 @code{gprbuild}, @code{gnatmake}, and @code{gnatcheck}.
3186
3187 If you are using project file, a separate mechanism is provided using
3188 project attributes.
3189
3190 @c --Comment
3191 @c See :ref:`Specifying_Configuration_Pragmas` for more details.
3192
3193 @node Generating Object Files,Source Dependencies,Configuration Pragmas,The GNAT Compilation Model
3194 @anchor{gnat_ugn/the_gnat_compilation_model generating-object-files}@anchor{40}@anchor{gnat_ugn/the_gnat_compilation_model id31}@anchor{7b}
3195 @section Generating Object Files
3196
3197
3198 An Ada program consists of a set of source files, and the first step in
3199 compiling the program is to generate the corresponding object files.
3200 These are generated by compiling a subset of these source files.
3201 The files you need to compile are the following:
3202
3203
3204 @itemize *
3205
3206 @item
3207 If a package spec has no body, compile the package spec to produce the
3208 object file for the package.
3209
3210 @item
3211 If a package has both a spec and a body, compile the body to produce the
3212 object file for the package. The source file for the package spec need
3213 not be compiled in this case because there is only one object file, which
3214 contains the code for both the spec and body of the package.
3215
3216 @item
3217 For a subprogram, compile the subprogram body to produce the object file
3218 for the subprogram. The spec, if one is present, is as usual in a
3219 separate file, and need not be compiled.
3220 @end itemize
3221
3222 @geindex Subunits
3223
3224
3225 @itemize *
3226
3227 @item
3228 In the case of subunits, only compile the parent unit. A single object
3229 file is generated for the entire subunit tree, which includes all the
3230 subunits.
3231
3232 @item
3233 Compile child units independently of their parent units
3234 (though, of course, the spec of all the ancestor unit must be present in order
3235 to compile a child unit).
3236
3237 @geindex Generics
3238
3239 @item
3240 Compile generic units in the same manner as any other units. The object
3241 files in this case are small dummy files that contain at most the
3242 flag used for elaboration checking. This is because GNAT always handles generic
3243 instantiation by means of macro expansion. However, it is still necessary to
3244 compile generic units, for dependency checking and elaboration purposes.
3245 @end itemize
3246
3247 The preceding rules describe the set of files that must be compiled to
3248 generate the object files for a program. Each object file has the same
3249 name as the corresponding source file, except that the extension is
3250 @code{.o} as usual.
3251
3252 You may wish to compile other files for the purpose of checking their
3253 syntactic and semantic correctness. For example, in the case where a
3254 package has a separate spec and body, you would not normally compile the
3255 spec. However, it is convenient in practice to compile the spec to make
3256 sure it is error-free before compiling clients of this spec, because such
3257 compilations will fail if there is an error in the spec.
3258
3259 GNAT provides an option for compiling such files purely for the
3260 purposes of checking correctness; such compilations are not required as
3261 part of the process of building a program. To compile a file in this
3262 checking mode, use the @code{-gnatc} switch.
3263
3264 @node Source Dependencies,The Ada Library Information Files,Generating Object Files,The GNAT Compilation Model
3265 @anchor{gnat_ugn/the_gnat_compilation_model id32}@anchor{7c}@anchor{gnat_ugn/the_gnat_compilation_model source-dependencies}@anchor{41}
3266 @section Source Dependencies
3267
3268
3269 A given object file clearly depends on the source file which is compiled
3270 to produce it. Here we are using "depends" in the sense of a typical
3271 @code{make} utility; in other words, an object file depends on a source
3272 file if changes to the source file require the object file to be
3273 recompiled.
3274 In addition to this basic dependency, a given object may depend on
3275 additional source files as follows:
3276
3277
3278 @itemize *
3279
3280 @item
3281 If a file being compiled @emph{with}s a unit @code{X}, the object file
3282 depends on the file containing the spec of unit @code{X}. This includes
3283 files that are @emph{with}ed implicitly either because they are parents
3284 of @emph{with}ed child units or they are run-time units required by the
3285 language constructs used in a particular unit.
3286
3287 @item
3288 If a file being compiled instantiates a library level generic unit, the
3289 object file depends on both the spec and body files for this generic
3290 unit.
3291
3292 @item
3293 If a file being compiled instantiates a generic unit defined within a
3294 package, the object file depends on the body file for the package as
3295 well as the spec file.
3296 @end itemize
3297
3298 @geindex Inline
3299
3300 @geindex -gnatn switch
3301
3302
3303 @itemize *
3304
3305 @item
3306 If a file being compiled contains a call to a subprogram for which
3307 pragma @code{Inline} applies and inlining is activated with the
3308 @code{-gnatn} switch, the object file depends on the file containing the
3309 body of this subprogram as well as on the file containing the spec. Note
3310 that for inlining to actually occur as a result of the use of this switch,
3311 it is necessary to compile in optimizing mode.
3312
3313 @geindex -gnatN switch
3314
3315 The use of @code{-gnatN} activates inlining optimization
3316 that is performed by the front end of the compiler. This inlining does
3317 not require that the code generation be optimized. Like @code{-gnatn},
3318 the use of this switch generates additional dependencies.
3319
3320 When using a gcc-based back end (in practice this means using any version
3321 of GNAT other than for the JVM, .NET or GNAAMP platforms), then the use of
3322 @code{-gnatN} is deprecated, and the use of @code{-gnatn} is preferred.
3323 Historically front end inlining was more extensive than the gcc back end
3324 inlining, but that is no longer the case.
3325
3326 @item
3327 If an object file @code{O} depends on the proper body of a subunit through
3328 inlining or instantiation, it depends on the parent unit of the subunit.
3329 This means that any modification of the parent unit or one of its subunits
3330 affects the compilation of @code{O}.
3331
3332 @item
3333 The object file for a parent unit depends on all its subunit body files.
3334
3335 @item
3336 The previous two rules meant that for purposes of computing dependencies and
3337 recompilation, a body and all its subunits are treated as an indivisible whole.
3338
3339 These rules are applied transitively: if unit @code{A} @emph{with}s
3340 unit @code{B}, whose elaboration calls an inlined procedure in package
3341 @code{C}, the object file for unit @code{A} will depend on the body of
3342 @code{C}, in file @code{c.adb}.
3343
3344 The set of dependent files described by these rules includes all the
3345 files on which the unit is semantically dependent, as dictated by the
3346 Ada language standard. However, it is a superset of what the
3347 standard describes, because it includes generic, inline, and subunit
3348 dependencies.
3349
3350 An object file must be recreated by recompiling the corresponding source
3351 file if any of the source files on which it depends are modified. For
3352 example, if the @code{make} utility is used to control compilation,
3353 the rule for an Ada object file must mention all the source files on
3354 which the object file depends, according to the above definition.
3355 The determination of the necessary
3356 recompilations is done automatically when one uses @code{gnatmake}.
3357 @end itemize
3358
3359 @node The Ada Library Information Files,Binding an Ada Program,Source Dependencies,The GNAT Compilation Model
3360 @anchor{gnat_ugn/the_gnat_compilation_model id33}@anchor{7d}@anchor{gnat_ugn/the_gnat_compilation_model the-ada-library-information-files}@anchor{42}
3361 @section The Ada Library Information Files
3362
3363
3364 @geindex Ada Library Information files
3365
3366 @geindex ALI files
3367
3368 Each compilation actually generates two output files. The first of these
3369 is the normal object file that has a @code{.o} extension. The second is a
3370 text file containing full dependency information. It has the same
3371 name as the source file, but an @code{.ali} extension.
3372 This file is known as the Ada Library Information (@code{ALI}) file.
3373 The following information is contained in the @code{ALI} file.
3374
3375
3376 @itemize *
3377
3378 @item
3379 Version information (indicates which version of GNAT was used to compile
3380 the unit(s) in question)
3381
3382 @item
3383 Main program information (including priority and time slice settings,
3384 as well as the wide character encoding used during compilation).
3385
3386 @item
3387 List of arguments used in the @code{gcc} command for the compilation
3388
3389 @item
3390 Attributes of the unit, including configuration pragmas used, an indication
3391 of whether the compilation was successful, exception model used etc.
3392
3393 @item
3394 A list of relevant restrictions applying to the unit (used for consistency)
3395 checking.
3396
3397 @item
3398 Categorization information (e.g., use of pragma @code{Pure}).
3399
3400 @item
3401 Information on all @emph{with}ed units, including presence of
3402 @code{Elaborate} or @code{Elaborate_All} pragmas.
3403
3404 @item
3405 Information from any @code{Linker_Options} pragmas used in the unit
3406
3407 @item
3408 Information on the use of @code{Body_Version} or @code{Version}
3409 attributes in the unit.
3410
3411 @item
3412 Dependency information. This is a list of files, together with
3413 time stamp and checksum information. These are files on which
3414 the unit depends in the sense that recompilation is required
3415 if any of these units are modified.
3416
3417 @item
3418 Cross-reference data. Contains information on all entities referenced
3419 in the unit. Used by tools like @code{gnatxref} and @code{gnatfind} to
3420 provide cross-reference information.
3421 @end itemize
3422
3423 For a full detailed description of the format of the @code{ALI} file,
3424 see the source of the body of unit @code{Lib.Writ}, contained in file
3425 @code{lib-writ.adb} in the GNAT compiler sources.
3426
3427 @node Binding an Ada Program,GNAT and Libraries,The Ada Library Information Files,The GNAT Compilation Model
3428 @anchor{gnat_ugn/the_gnat_compilation_model id34}@anchor{7e}@anchor{gnat_ugn/the_gnat_compilation_model binding-an-ada-program}@anchor{43}
3429 @section Binding an Ada Program
3430
3431
3432 When using languages such as C and C++, once the source files have been
3433 compiled the only remaining step in building an executable program
3434 is linking the object modules together. This means that it is possible to
3435 link an inconsistent version of a program, in which two units have
3436 included different versions of the same header.
3437
3438 The rules of Ada do not permit such an inconsistent program to be built.
3439 For example, if two clients have different versions of the same package,
3440 it is illegal to build a program containing these two clients.
3441 These rules are enforced by the GNAT binder, which also determines an
3442 elaboration order consistent with the Ada rules.
3443
3444 The GNAT binder is run after all the object files for a program have
3445 been created. It is given the name of the main program unit, and from
3446 this it determines the set of units required by the program, by reading the
3447 corresponding ALI files. It generates error messages if the program is
3448 inconsistent or if no valid order of elaboration exists.
3449
3450 If no errors are detected, the binder produces a main program, in Ada by
3451 default, that contains calls to the elaboration procedures of those
3452 compilation unit that require them, followed by
3453 a call to the main program. This Ada program is compiled to generate the
3454 object file for the main program. The name of
3455 the Ada file is @code{b~xxx}.adb` (with the corresponding spec
3456 @code{b~xxx}.ads`) where @code{xxx} is the name of the
3457 main program unit.
3458
3459 Finally, the linker is used to build the resulting executable program,
3460 using the object from the main program from the bind step as well as the
3461 object files for the Ada units of the program.
3462
3463 @node GNAT and Libraries,Conditional Compilation,Binding an Ada Program,The GNAT Compilation Model
3464 @anchor{gnat_ugn/the_gnat_compilation_model gnat-and-libraries}@anchor{15}@anchor{gnat_ugn/the_gnat_compilation_model id35}@anchor{7f}
3465 @section GNAT and Libraries
3466
3467
3468 @geindex Library building and using
3469
3470 This section describes how to build and use libraries with GNAT, and also shows
3471 how to recompile the GNAT run-time library. You should be familiar with the
3472 Project Manager facility (see the @emph{GNAT_Project_Manager} chapter of the
3473 @emph{GPRbuild User's Guide}) before reading this chapter.
3474
3475 @menu
3476 * Introduction to Libraries in GNAT::
3477 * General Ada Libraries::
3478 * Stand-alone Ada Libraries::
3479 * Rebuilding the GNAT Run-Time Library::
3480
3481 @end menu
3482
3483 @node Introduction to Libraries in GNAT,General Ada Libraries,,GNAT and Libraries
3484 @anchor{gnat_ugn/the_gnat_compilation_model introduction-to-libraries-in-gnat}@anchor{80}@anchor{gnat_ugn/the_gnat_compilation_model id36}@anchor{81}
3485 @subsection Introduction to Libraries in GNAT
3486
3487
3488 A library is, conceptually, a collection of objects which does not have its
3489 own main thread of execution, but rather provides certain services to the
3490 applications that use it. A library can be either statically linked with the
3491 application, in which case its code is directly included in the application,
3492 or, on platforms that support it, be dynamically linked, in which case
3493 its code is shared by all applications making use of this library.
3494
3495 GNAT supports both types of libraries.
3496 In the static case, the compiled code can be provided in different ways. The
3497 simplest approach is to provide directly the set of objects resulting from
3498 compilation of the library source files. Alternatively, you can group the
3499 objects into an archive using whatever commands are provided by the operating
3500 system. For the latter case, the objects are grouped into a shared library.
3501
3502 In the GNAT environment, a library has three types of components:
3503
3504
3505 @itemize *
3506
3507 @item
3508 Source files,
3509
3510 @item
3511 @code{ALI} files (see @ref{42,,The Ada Library Information Files}), and
3512
3513 @item
3514 Object files, an archive or a shared library.
3515 @end itemize
3516
3517 A GNAT library may expose all its source files, which is useful for
3518 documentation purposes. Alternatively, it may expose only the units needed by
3519 an external user to make use of the library. That is to say, the specs
3520 reflecting the library services along with all the units needed to compile
3521 those specs, which can include generic bodies or any body implementing an
3522 inlined routine. In the case of @emph{stand-alone libraries} those exposed
3523 units are called @emph{interface units} (@ref{82,,Stand-alone Ada Libraries}).
3524
3525 All compilation units comprising an application, including those in a library,
3526 need to be elaborated in an order partially defined by Ada's semantics. GNAT
3527 computes the elaboration order from the @code{ALI} files and this is why they
3528 constitute a mandatory part of GNAT libraries.
3529 @emph{Stand-alone libraries} are the exception to this rule because a specific
3530 library elaboration routine is produced independently of the application(s)
3531 using the library.
3532
3533 @node General Ada Libraries,Stand-alone Ada Libraries,Introduction to Libraries in GNAT,GNAT and Libraries
3534 @anchor{gnat_ugn/the_gnat_compilation_model general-ada-libraries}@anchor{83}@anchor{gnat_ugn/the_gnat_compilation_model id37}@anchor{84}
3535 @subsection General Ada Libraries
3536
3537
3538 @menu
3539 * Building a library::
3540 * Installing a library::
3541 * Using a library::
3542
3543 @end menu
3544
3545 @node Building a library,Installing a library,,General Ada Libraries
3546 @anchor{gnat_ugn/the_gnat_compilation_model building-a-library}@anchor{85}@anchor{gnat_ugn/the_gnat_compilation_model id38}@anchor{86}
3547 @subsubsection Building a library
3548
3549
3550 The easiest way to build a library is to use the Project Manager,
3551 which supports a special type of project called a @emph{Library Project}
3552 (see the @emph{Library Projects} section in the @emph{GNAT Project Manager}
3553 chapter of the @emph{GPRbuild User's Guide}).
3554
3555 A project is considered a library project, when two project-level attributes
3556 are defined in it: @code{Library_Name} and @code{Library_Dir}. In order to
3557 control different aspects of library configuration, additional optional
3558 project-level attributes can be specified:
3559
3560
3561 @itemize *
3562
3563 @item
3564
3565 @table @asis
3566
3567 @item @code{Library_Kind}
3568
3569 This attribute controls whether the library is to be static or dynamic
3570 @end table
3571
3572 @item
3573
3574 @table @asis
3575
3576 @item @code{Library_Version}
3577
3578 This attribute specifies the library version; this value is used
3579 during dynamic linking of shared libraries to determine if the currently
3580 installed versions of the binaries are compatible.
3581 @end table
3582
3583 @item
3584 @code{Library_Options}
3585
3586 @item
3587
3588 @table @asis
3589
3590 @item @code{Library_GCC}
3591
3592 These attributes specify additional low-level options to be used during
3593 library generation, and redefine the actual application used to generate
3594 library.
3595 @end table
3596 @end itemize
3597
3598 The GNAT Project Manager takes full care of the library maintenance task,
3599 including recompilation of the source files for which objects do not exist
3600 or are not up to date, assembly of the library archive, and installation of
3601 the library (i.e., copying associated source, object and @code{ALI} files
3602 to the specified location).
3603
3604 Here is a simple library project file:
3605
3606 @example
3607 project My_Lib is
3608 for Source_Dirs use ("src1", "src2");
3609 for Object_Dir use "obj";
3610 for Library_Name use "mylib";
3611 for Library_Dir use "lib";
3612 for Library_Kind use "dynamic";
3613 end My_lib;
3614 @end example
3615
3616 and the compilation command to build and install the library:
3617
3618 @example
3619 $ gnatmake -Pmy_lib
3620 @end example
3621
3622 It is not entirely trivial to perform manually all the steps required to
3623 produce a library. We recommend that you use the GNAT Project Manager
3624 for this task. In special cases where this is not desired, the necessary
3625 steps are discussed below.
3626
3627 There are various possibilities for compiling the units that make up the
3628 library: for example with a Makefile (@ref{1f,,Using the GNU make Utility}) or
3629 with a conventional script. For simple libraries, it is also possible to create
3630 a dummy main program which depends upon all the packages that comprise the
3631 interface of the library. This dummy main program can then be given to
3632 @code{gnatmake}, which will ensure that all necessary objects are built.
3633
3634 After this task is accomplished, you should follow the standard procedure
3635 of the underlying operating system to produce the static or shared library.
3636
3637 Here is an example of such a dummy program:
3638
3639 @example
3640 with My_Lib.Service1;
3641 with My_Lib.Service2;
3642 with My_Lib.Service3;
3643 procedure My_Lib_Dummy is
3644 begin
3645 null;
3646 end;
3647 @end example
3648
3649 Here are the generic commands that will build an archive or a shared library.
3650
3651 @example
3652 # compiling the library
3653 $ gnatmake -c my_lib_dummy.adb
3654
3655 # we don't need the dummy object itself
3656 $ rm my_lib_dummy.o my_lib_dummy.ali
3657
3658 # create an archive with the remaining objects
3659 $ ar rc libmy_lib.a *.o
3660 # some systems may require "ranlib" to be run as well
3661
3662 # or create a shared library
3663 $ gcc -shared -o libmy_lib.so *.o
3664 # some systems may require the code to have been compiled with -fPIC
3665
3666 # remove the object files that are now in the library
3667 $ rm *.o
3668
3669 # Make the ALI files read-only so that gnatmake will not try to
3670 # regenerate the objects that are in the library
3671 $ chmod -w *.ali
3672 @end example
3673
3674 Please note that the library must have a name of the form @code{lib@emph{xxx}.a}
3675 or @code{lib@emph{xxx}.so} (or @code{lib@emph{xxx}.dll} on Windows) in order to
3676 be accessed by the directive @code{-l@emph{xxx}} at link time.
3677
3678 @node Installing a library,Using a library,Building a library,General Ada Libraries
3679 @anchor{gnat_ugn/the_gnat_compilation_model installing-a-library}@anchor{87}@anchor{gnat_ugn/the_gnat_compilation_model id39}@anchor{88}
3680 @subsubsection Installing a library
3681
3682
3683 @geindex ADA_PROJECT_PATH
3684
3685 @geindex GPR_PROJECT_PATH
3686
3687 If you use project files, library installation is part of the library build
3688 process (see the @emph{Installing a Library with Project Files} section of the
3689 @emph{GNAT Project Manager} chapter of the @emph{GPRbuild User's Guide}).
3690
3691 When project files are not an option, it is also possible, but not recommended,
3692 to install the library so that the sources needed to use the library are on the
3693 Ada source path and the ALI files & libraries be on the Ada Object path (see
3694 @ref{89,,Search Paths and the Run-Time Library (RTL)}. Alternatively, the system
3695 administrator can place general-purpose libraries in the default compiler
3696 paths, by specifying the libraries' location in the configuration files
3697 @code{ada_source_path} and @code{ada_object_path}. These configuration files
3698 must be located in the GNAT installation tree at the same place as the gcc spec
3699 file. The location of the gcc spec file can be determined as follows:
3700
3701 @example
3702 $ gcc -v
3703 @end example
3704
3705 The configuration files mentioned above have a simple format: each line
3706 must contain one unique directory name.
3707 Those names are added to the corresponding path
3708 in their order of appearance in the file. The names can be either absolute
3709 or relative; in the latter case, they are relative to where theses files
3710 are located.
3711
3712 The files @code{ada_source_path} and @code{ada_object_path} might not be
3713 present in a
3714 GNAT installation, in which case, GNAT will look for its run-time library in
3715 the directories @code{adainclude} (for the sources) and @code{adalib} (for the
3716 objects and @code{ALI} files). When the files exist, the compiler does not
3717 look in @code{adainclude} and @code{adalib}, and thus the
3718 @code{ada_source_path} file
3719 must contain the location for the GNAT run-time sources (which can simply
3720 be @code{adainclude}). In the same way, the @code{ada_object_path} file must
3721 contain the location for the GNAT run-time objects (which can simply
3722 be @code{adalib}).
3723
3724 You can also specify a new default path to the run-time library at compilation
3725 time with the switch @code{--RTS=rts-path}. You can thus choose / change
3726 the run-time library you want your program to be compiled with. This switch is
3727 recognized by @code{gcc}, @code{gnatmake}, @code{gnatbind},
3728 @code{gnatls}, @code{gnatfind} and @code{gnatxref}.
3729
3730 It is possible to install a library before or after the standard GNAT
3731 library, by reordering the lines in the configuration files. In general, a
3732 library must be installed before the GNAT library if it redefines
3733 any part of it.
3734
3735 @node Using a library,,Installing a library,General Ada Libraries
3736 @anchor{gnat_ugn/the_gnat_compilation_model using-a-library}@anchor{8a}@anchor{gnat_ugn/the_gnat_compilation_model id40}@anchor{8b}
3737 @subsubsection Using a library
3738
3739
3740 Once again, the project facility greatly simplifies the use of
3741 libraries. In this context, using a library is just a matter of adding a
3742 @emph{with} clause in the user project. For instance, to make use of the
3743 library @code{My_Lib} shown in examples in earlier sections, you can
3744 write:
3745
3746 @example
3747 with "my_lib";
3748 project My_Proj is
3749 ...
3750 end My_Proj;
3751 @end example
3752
3753 Even if you have a third-party, non-Ada library, you can still use GNAT's
3754 Project Manager facility to provide a wrapper for it. For example, the
3755 following project, when @emph{with}ed by your main project, will link with the
3756 third-party library @code{liba.a}:
3757
3758 @example
3759 project Liba is
3760 for Externally_Built use "true";
3761 for Source_Files use ();
3762 for Library_Dir use "lib";
3763 for Library_Name use "a";
3764 for Library_Kind use "static";
3765 end Liba;
3766 @end example
3767
3768 This is an alternative to the use of @code{pragma Linker_Options}. It is
3769 especially interesting in the context of systems with several interdependent
3770 static libraries where finding a proper linker order is not easy and best be
3771 left to the tools having visibility over project dependence information.
3772
3773 In order to use an Ada library manually, you need to make sure that this
3774 library is on both your source and object path
3775 (see @ref{89,,Search Paths and the Run-Time Library (RTL)}
3776 and @ref{8c,,Search Paths for gnatbind}). Furthermore, when the objects are grouped
3777 in an archive or a shared library, you need to specify the desired
3778 library at link time.
3779
3780 For example, you can use the library @code{mylib} installed in
3781 @code{/dir/my_lib_src} and @code{/dir/my_lib_obj} with the following commands:
3782
3783 @example
3784 $ gnatmake -aI/dir/my_lib_src -aO/dir/my_lib_obj my_appl \\
3785 -largs -lmy_lib
3786 @end example
3787
3788 This can be expressed more simply:
3789
3790 @example
3791 $ gnatmake my_appl
3792 @end example
3793
3794 when the following conditions are met:
3795
3796
3797 @itemize *
3798
3799 @item
3800 @code{/dir/my_lib_src} has been added by the user to the environment
3801 variable
3802 @geindex ADA_INCLUDE_PATH
3803 @geindex environment variable; ADA_INCLUDE_PATH
3804 @code{ADA_INCLUDE_PATH}, or by the administrator to the file
3805 @code{ada_source_path}
3806
3807 @item
3808 @code{/dir/my_lib_obj} has been added by the user to the environment
3809 variable
3810 @geindex ADA_OBJECTS_PATH
3811 @geindex environment variable; ADA_OBJECTS_PATH
3812 @code{ADA_OBJECTS_PATH}, or by the administrator to the file
3813 @code{ada_object_path}
3814
3815 @item
3816 a pragma @code{Linker_Options} has been added to one of the sources.
3817 For example:
3818
3819 @example
3820 pragma Linker_Options ("-lmy_lib");
3821 @end example
3822 @end itemize
3823
3824 Note that you may also load a library dynamically at
3825 run time given its filename, as illustrated in the GNAT @code{plugins} example
3826 in the directory @code{share/examples/gnat/plugins} within the GNAT
3827 install area.
3828
3829 @node Stand-alone Ada Libraries,Rebuilding the GNAT Run-Time Library,General Ada Libraries,GNAT and Libraries
3830 @anchor{gnat_ugn/the_gnat_compilation_model stand-alone-ada-libraries}@anchor{82}@anchor{gnat_ugn/the_gnat_compilation_model id41}@anchor{8d}
3831 @subsection Stand-alone Ada Libraries
3832
3833
3834 @geindex Stand-alone libraries
3835
3836 @menu
3837 * Introduction to Stand-alone Libraries::
3838 * Building a Stand-alone Library::
3839 * Creating a Stand-alone Library to be used in a non-Ada context::
3840 * Restrictions in Stand-alone Libraries::
3841
3842 @end menu
3843
3844 @node Introduction to Stand-alone Libraries,Building a Stand-alone Library,,Stand-alone Ada Libraries
3845 @anchor{gnat_ugn/the_gnat_compilation_model introduction-to-stand-alone-libraries}@anchor{8e}@anchor{gnat_ugn/the_gnat_compilation_model id42}@anchor{8f}
3846 @subsubsection Introduction to Stand-alone Libraries
3847
3848
3849 A Stand-alone Library (abbreviated 'SAL') is a library that contains the
3850 necessary code to
3851 elaborate the Ada units that are included in the library. In contrast with
3852 an ordinary library, which consists of all sources, objects and @code{ALI}
3853 files of the
3854 library, a SAL may specify a restricted subset of compilation units
3855 to serve as a library interface. In this case, the fully
3856 self-sufficient set of files will normally consist of an objects
3857 archive, the sources of interface units' specs, and the @code{ALI}
3858 files of interface units.
3859 If an interface spec contains a generic unit or an inlined subprogram,
3860 the body's
3861 source must also be provided; if the units that must be provided in the source
3862 form depend on other units, the source and @code{ALI} files of those must
3863 also be provided.
3864
3865 The main purpose of a SAL is to minimize the recompilation overhead of client
3866 applications when a new version of the library is installed. Specifically,
3867 if the interface sources have not changed, client applications do not need to
3868 be recompiled. If, furthermore, a SAL is provided in the shared form and its
3869 version, controlled by @code{Library_Version} attribute, is not changed,
3870 then the clients do not need to be relinked.
3871
3872 SALs also allow the library providers to minimize the amount of library source
3873 text exposed to the clients. Such 'information hiding' might be useful or
3874 necessary for various reasons.
3875
3876 Stand-alone libraries are also well suited to be used in an executable whose
3877 main routine is not written in Ada.
3878
3879 @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
3880 @anchor{gnat_ugn/the_gnat_compilation_model id43}@anchor{90}@anchor{gnat_ugn/the_gnat_compilation_model building-a-stand-alone-library}@anchor{91}
3881 @subsubsection Building a Stand-alone Library
3882
3883
3884 GNAT's Project facility provides a simple way of building and installing
3885 stand-alone libraries; see the @emph{Stand-alone Library Projects} section
3886 in the @emph{GNAT Project Manager} chapter of the @emph{GPRbuild User's Guide}.
3887 To be a Stand-alone Library Project, in addition to the two attributes
3888 that make a project a Library Project (@code{Library_Name} and
3889 @code{Library_Dir}; see the @emph{Library Projects} section in the
3890 @emph{GNAT Project Manager} chapter of the @emph{GPRbuild User's Guide}),
3891 the attribute @code{Library_Interface} must be defined. For example:
3892
3893 @example
3894 for Library_Dir use "lib_dir";
3895 for Library_Name use "dummy";
3896 for Library_Interface use ("int1", "int1.child");
3897 @end example
3898
3899 Attribute @code{Library_Interface} has a non-empty string list value,
3900 each string in the list designating a unit contained in an immediate source
3901 of the project file.
3902
3903 When a Stand-alone Library is built, first the binder is invoked to build
3904 a package whose name depends on the library name
3905 (@code{b~dummy.ads/b} in the example above).
3906 This binder-generated package includes initialization and
3907 finalization procedures whose
3908 names depend on the library name (@code{dummyinit} and @code{dummyfinal}
3909 in the example
3910 above). The object corresponding to this package is included in the library.
3911
3912 You must ensure timely (e.g., prior to any use of interfaces in the SAL)
3913 calling of these procedures if a static SAL is built, or if a shared SAL
3914 is built
3915 with the project-level attribute @code{Library_Auto_Init} set to
3916 @code{"false"}.
3917
3918 For a Stand-Alone Library, only the @code{ALI} files of the Interface Units
3919 (those that are listed in attribute @code{Library_Interface}) are copied to
3920 the Library Directory. As a consequence, only the Interface Units may be
3921 imported from Ada units outside of the library. If other units are imported,
3922 the binding phase will fail.
3923
3924 It is also possible to build an encapsulated library where not only
3925 the code to elaborate and finalize the library is embedded but also
3926 ensuring that the library is linked only against static
3927 libraries. So an encapsulated library only depends on system
3928 libraries, all other code, including the GNAT runtime, is embedded. To
3929 build an encapsulated library the attribute
3930 @code{Library_Standalone} must be set to @code{encapsulated}:
3931
3932 @example
3933 for Library_Dir use "lib_dir";
3934 for Library_Name use "dummy";
3935 for Library_Kind use "dynamic";
3936 for Library_Interface use ("int1", "int1.child");
3937 for Library_Standalone use "encapsulated";
3938 @end example
3939
3940 The default value for this attribute is @code{standard} in which case
3941 a stand-alone library is built.
3942
3943 The attribute @code{Library_Src_Dir} may be specified for a
3944 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
3945 single string value. Its value must be the path (absolute or relative to the
3946 project directory) of an existing directory. This directory cannot be the
3947 object directory or one of the source directories, but it can be the same as
3948 the library directory. The sources of the Interface
3949 Units of the library that are needed by an Ada client of the library will be
3950 copied to the designated directory, called the Interface Copy directory.
3951 These sources include the specs of the Interface Units, but they may also
3952 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
3953 are used, or when there is a generic unit in the spec. Before the sources
3954 are copied to the Interface Copy directory, an attempt is made to delete all
3955 files in the Interface Copy directory.
3956
3957 Building stand-alone libraries by hand is somewhat tedious, but for those
3958 occasions when it is necessary here are the steps that you need to perform:
3959
3960
3961 @itemize *
3962
3963 @item
3964 Compile all library sources.
3965
3966 @item
3967 Invoke the binder with the switch @code{-n} (No Ada main program),
3968 with all the @code{ALI} files of the interfaces, and
3969 with the switch @code{-L} to give specific names to the @code{init}
3970 and @code{final} procedures. For example:
3971
3972 @example
3973 $ gnatbind -n int1.ali int2.ali -Lsal1
3974 @end example
3975
3976 @item
3977 Compile the binder generated file:
3978
3979 @example
3980 $ gcc -c b~int2.adb
3981 @end example
3982
3983 @item
3984 Link the dynamic library with all the necessary object files,
3985 indicating to the linker the names of the @code{init} (and possibly
3986 @code{final}) procedures for automatic initialization (and finalization).
3987 The built library should be placed in a directory different from
3988 the object directory.
3989
3990 @item
3991 Copy the @code{ALI} files of the interface to the library directory,
3992 add in this copy an indication that it is an interface to a SAL
3993 (i.e., add a word @code{SL} on the line in the @code{ALI} file that starts
3994 with letter 'P') and make the modified copy of the @code{ALI} file
3995 read-only.
3996 @end itemize
3997
3998 Using SALs is not different from using other libraries
3999 (see @ref{8a,,Using a library}).
4000
4001 @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
4002 @anchor{gnat_ugn/the_gnat_compilation_model creating-a-stand-alone-library-to-be-used-in-a-non-ada-context}@anchor{92}@anchor{gnat_ugn/the_gnat_compilation_model id44}@anchor{93}
4003 @subsubsection Creating a Stand-alone Library to be used in a non-Ada context
4004
4005
4006 It is easy to adapt the SAL build procedure discussed above for use of a SAL in
4007 a non-Ada context.
4008
4009 The only extra step required is to ensure that library interface subprograms
4010 are compatible with the main program, by means of @code{pragma Export}
4011 or @code{pragma Convention}.
4012
4013 Here is an example of simple library interface for use with C main program:
4014
4015 @example
4016 package My_Package is
4017
4018 procedure Do_Something;
4019 pragma Export (C, Do_Something, "do_something");
4020
4021 procedure Do_Something_Else;
4022 pragma Export (C, Do_Something_Else, "do_something_else");
4023
4024 end My_Package;
4025 @end example
4026
4027 On the foreign language side, you must provide a 'foreign' view of the
4028 library interface; remember that it should contain elaboration routines in
4029 addition to interface subprograms.
4030
4031 The example below shows the content of @code{mylib_interface.h} (note
4032 that there is no rule for the naming of this file, any name can be used)
4033
4034 @example
4035 /* the library elaboration procedure */
4036 extern void mylibinit (void);
4037
4038 /* the library finalization procedure */
4039 extern void mylibfinal (void);
4040
4041 /* the interface exported by the library */
4042 extern void do_something (void);
4043 extern void do_something_else (void);
4044 @end example
4045
4046 Libraries built as explained above can be used from any program, provided
4047 that the elaboration procedures (named @code{mylibinit} in the previous
4048 example) are called before the library services are used. Any number of
4049 libraries can be used simultaneously, as long as the elaboration
4050 procedure of each library is called.
4051
4052 Below is an example of a C program that uses the @code{mylib} library.
4053
4054 @example
4055 #include "mylib_interface.h"
4056
4057 int
4058 main (void)
4059 @{
4060 /* First, elaborate the library before using it */
4061 mylibinit ();
4062
4063 /* Main program, using the library exported entities */
4064 do_something ();
4065 do_something_else ();
4066
4067 /* Library finalization at the end of the program */
4068 mylibfinal ();
4069 return 0;
4070 @}
4071 @end example
4072
4073 Note that invoking any library finalization procedure generated by
4074 @code{gnatbind} shuts down the Ada run-time environment.
4075 Consequently, the
4076 finalization of all Ada libraries must be performed at the end of the program.
4077 No call to these libraries or to the Ada run-time library should be made
4078 after the finalization phase.
4079
4080 Note also that special care must be taken with multi-tasks
4081 applications. The initialization and finalization routines are not
4082 protected against concurrent access. If such requirement is needed it
4083 must be ensured at the application level using a specific operating
4084 system services like a mutex or a critical-section.
4085
4086 @node Restrictions in Stand-alone Libraries,,Creating a Stand-alone Library to be used in a non-Ada context,Stand-alone Ada Libraries
4087 @anchor{gnat_ugn/the_gnat_compilation_model id45}@anchor{94}@anchor{gnat_ugn/the_gnat_compilation_model restrictions-in-stand-alone-libraries}@anchor{95}
4088 @subsubsection Restrictions in Stand-alone Libraries
4089
4090
4091 The pragmas listed below should be used with caution inside libraries,
4092 as they can create incompatibilities with other Ada libraries:
4093
4094
4095 @itemize *
4096
4097 @item
4098 pragma @code{Locking_Policy}
4099
4100 @item
4101 pragma @code{Partition_Elaboration_Policy}
4102
4103 @item
4104 pragma @code{Queuing_Policy}
4105
4106 @item
4107 pragma @code{Task_Dispatching_Policy}
4108
4109 @item
4110 pragma @code{Unreserve_All_Interrupts}
4111 @end itemize
4112
4113 When using a library that contains such pragmas, the user must make sure
4114 that all libraries use the same pragmas with the same values. Otherwise,
4115 @code{Program_Error} will
4116 be raised during the elaboration of the conflicting
4117 libraries. The usage of these pragmas and its consequences for the user
4118 should therefore be well documented.
4119
4120 Similarly, the traceback in the exception occurrence mechanism should be
4121 enabled or disabled in a consistent manner across all libraries.
4122 Otherwise, Program_Error will be raised during the elaboration of the
4123 conflicting libraries.
4124
4125 If the @code{Version} or @code{Body_Version}
4126 attributes are used inside a library, then you need to
4127 perform a @code{gnatbind} step that specifies all @code{ALI} files in all
4128 libraries, so that version identifiers can be properly computed.
4129 In practice these attributes are rarely used, so this is unlikely
4130 to be a consideration.
4131
4132 @node Rebuilding the GNAT Run-Time Library,,Stand-alone Ada Libraries,GNAT and Libraries
4133 @anchor{gnat_ugn/the_gnat_compilation_model id46}@anchor{96}@anchor{gnat_ugn/the_gnat_compilation_model rebuilding-the-gnat-run-time-library}@anchor{97}
4134 @subsection Rebuilding the GNAT Run-Time Library
4135
4136
4137 @geindex GNAT Run-Time Library
4138 @geindex rebuilding
4139
4140 @geindex Building the GNAT Run-Time Library
4141
4142 @geindex Rebuilding the GNAT Run-Time Library
4143
4144 @geindex Run-Time Library
4145 @geindex rebuilding
4146
4147 It may be useful to recompile the GNAT library in various debugging or
4148 experimentation contexts. A project file called
4149 @code{libada.gpr} is provided to that effect and can be found in
4150 the directory containing the GNAT library. The location of this
4151 directory depends on the way the GNAT environment has been installed and can
4152 be determined by means of the command:
4153
4154 @example
4155 $ gnatls -v
4156 @end example
4157
4158 The last entry in the source search path usually contains the
4159 gnat library (the @code{adainclude} directory). This project file contains its
4160 own documentation and in particular the set of instructions needed to rebuild a
4161 new library and to use it.
4162
4163 Note that rebuilding the GNAT Run-Time is only recommended for temporary
4164 experiments or debugging, and is not supported.
4165
4166 @geindex Conditional compilation
4167
4168 @node Conditional Compilation,Mixed Language Programming,GNAT and Libraries,The GNAT Compilation Model
4169 @anchor{gnat_ugn/the_gnat_compilation_model id47}@anchor{98}@anchor{gnat_ugn/the_gnat_compilation_model conditional-compilation}@anchor{16}
4170 @section Conditional Compilation
4171
4172
4173 This section presents some guidelines for modeling conditional compilation in Ada and describes the
4174 gnatprep preprocessor utility.
4175
4176 @geindex Conditional compilation
4177
4178 @menu
4179 * Modeling Conditional Compilation in Ada::
4180 * Preprocessing with gnatprep::
4181 * Integrated Preprocessing::
4182
4183 @end menu
4184
4185 @node Modeling Conditional Compilation in Ada,Preprocessing with gnatprep,,Conditional Compilation
4186 @anchor{gnat_ugn/the_gnat_compilation_model modeling-conditional-compilation-in-ada}@anchor{99}@anchor{gnat_ugn/the_gnat_compilation_model id48}@anchor{9a}
4187 @subsection Modeling Conditional Compilation in Ada
4188
4189
4190 It is often necessary to arrange for a single source program
4191 to serve multiple purposes, where it is compiled in different
4192 ways to achieve these different goals. Some examples of the
4193 need for this feature are
4194
4195
4196 @itemize *
4197
4198 @item
4199 Adapting a program to a different hardware environment
4200
4201 @item
4202 Adapting a program to a different target architecture
4203
4204 @item
4205 Turning debugging features on and off
4206
4207 @item
4208 Arranging for a program to compile with different compilers
4209 @end itemize
4210
4211 In C, or C++, the typical approach would be to use the preprocessor
4212 that is defined as part of the language. The Ada language does not
4213 contain such a feature. This is not an oversight, but rather a very
4214 deliberate design decision, based on the experience that overuse of
4215 the preprocessing features in C and C++ can result in programs that
4216 are extremely difficult to maintain. For example, if we have ten
4217 switches that can be on or off, this means that there are a thousand
4218 separate programs, any one of which might not even be syntactically
4219 correct, and even if syntactically correct, the resulting program
4220 might not work correctly. Testing all combinations can quickly become
4221 impossible.
4222
4223 Nevertheless, the need to tailor programs certainly exists, and in
4224 this section we will discuss how this can
4225 be achieved using Ada in general, and GNAT in particular.
4226
4227 @menu
4228 * Use of Boolean Constants::
4229 * Debugging - A Special Case::
4230 * Conditionalizing Declarations::
4231 * Use of Alternative Implementations::
4232 * Preprocessing::
4233
4234 @end menu
4235
4236 @node Use of Boolean Constants,Debugging - A Special Case,,Modeling Conditional Compilation in Ada
4237 @anchor{gnat_ugn/the_gnat_compilation_model id49}@anchor{9b}@anchor{gnat_ugn/the_gnat_compilation_model use-of-boolean-constants}@anchor{9c}
4238 @subsubsection Use of Boolean Constants
4239
4240
4241 In the case where the difference is simply which code
4242 sequence is executed, the cleanest solution is to use Boolean
4243 constants to control which code is executed.
4244
4245 @example
4246 FP_Initialize_Required : constant Boolean := True;
4247 ...
4248 if FP_Initialize_Required then
4249 ...
4250 end if;
4251 @end example
4252
4253 Not only will the code inside the @code{if} statement not be executed if
4254 the constant Boolean is @code{False}, but it will also be completely
4255 deleted from the program.
4256 However, the code is only deleted after the @code{if} statement
4257 has been checked for syntactic and semantic correctness.
4258 (In contrast, with preprocessors the code is deleted before the
4259 compiler ever gets to see it, so it is not checked until the switch
4260 is turned on.)
4261
4262 @geindex Preprocessors (contrasted with conditional compilation)
4263
4264 Typically the Boolean constants will be in a separate package,
4265 something like:
4266
4267 @example
4268 package Config is
4269 FP_Initialize_Required : constant Boolean := True;
4270 Reset_Available : constant Boolean := False;
4271 ...
4272 end Config;
4273 @end example
4274
4275 The @code{Config} package exists in multiple forms for the various targets,
4276 with an appropriate script selecting the version of @code{Config} needed.
4277 Then any other unit requiring conditional compilation can do a @emph{with}
4278 of @code{Config} to make the constants visible.
4279
4280 @node Debugging - A Special Case,Conditionalizing Declarations,Use of Boolean Constants,Modeling Conditional Compilation in Ada
4281 @anchor{gnat_ugn/the_gnat_compilation_model debugging-a-special-case}@anchor{9d}@anchor{gnat_ugn/the_gnat_compilation_model id50}@anchor{9e}
4282 @subsubsection Debugging - A Special Case
4283
4284
4285 A common use of conditional code is to execute statements (for example
4286 dynamic checks, or output of intermediate results) under control of a
4287 debug switch, so that the debugging behavior can be turned on and off.
4288 This can be done using a Boolean constant to control whether the code
4289 is active:
4290
4291 @example
4292 if Debugging then
4293 Put_Line ("got to the first stage!");
4294 end if;
4295 @end example
4296
4297 or
4298
4299 @example
4300 if Debugging and then Temperature > 999.0 then
4301 raise Temperature_Crazy;
4302 end if;
4303 @end example
4304
4305 @geindex pragma Assert
4306
4307 Since this is a common case, there are special features to deal with
4308 this in a convenient manner. For the case of tests, Ada 2005 has added
4309 a pragma @code{Assert} that can be used for such tests. This pragma is modeled
4310 on the @code{Assert} pragma that has always been available in GNAT, so this
4311 feature may be used with GNAT even if you are not using Ada 2005 features.
4312 The use of pragma @code{Assert} is described in the
4313 @cite{GNAT_Reference_Manual}, but as an
4314 example, the last test could be written:
4315
4316 @example
4317 pragma Assert (Temperature <= 999.0, "Temperature Crazy");
4318 @end example
4319
4320 or simply
4321
4322 @example
4323 pragma Assert (Temperature <= 999.0);
4324 @end example
4325
4326 In both cases, if assertions are active and the temperature is excessive,
4327 the exception @code{Assert_Failure} will be raised, with the given string in
4328 the first case or a string indicating the location of the pragma in the second
4329 case used as the exception message.
4330
4331 @geindex pragma Assertion_Policy
4332
4333 You can turn assertions on and off by using the @code{Assertion_Policy}
4334 pragma.
4335
4336 @geindex -gnata switch
4337
4338 This is an Ada 2005 pragma which is implemented in all modes by
4339 GNAT. Alternatively, you can use the @code{-gnata} switch
4340 to enable assertions from the command line, which applies to
4341 all versions of Ada.
4342
4343 @geindex pragma Debug
4344
4345 For the example above with the @code{Put_Line}, the GNAT-specific pragma
4346 @code{Debug} can be used:
4347
4348 @example
4349 pragma Debug (Put_Line ("got to the first stage!"));
4350 @end example
4351
4352 If debug pragmas are enabled, the argument, which must be of the form of
4353 a procedure call, is executed (in this case, @code{Put_Line} will be called).
4354 Only one call can be present, but of course a special debugging procedure
4355 containing any code you like can be included in the program and then
4356 called in a pragma @code{Debug} argument as needed.
4357
4358 One advantage of pragma @code{Debug} over the @code{if Debugging then}
4359 construct is that pragma @code{Debug} can appear in declarative contexts,
4360 such as at the very beginning of a procedure, before local declarations have
4361 been elaborated.
4362
4363 @geindex pragma Debug_Policy
4364
4365 Debug pragmas are enabled using either the @code{-gnata} switch that also
4366 controls assertions, or with a separate Debug_Policy pragma.
4367
4368 The latter pragma is new in the Ada 2005 versions of GNAT (but it can be used
4369 in Ada 95 and Ada 83 programs as well), and is analogous to
4370 pragma @code{Assertion_Policy} to control assertions.
4371
4372 @code{Assertion_Policy} and @code{Debug_Policy} are configuration pragmas,
4373 and thus they can appear in @code{gnat.adc} if you are not using a
4374 project file, or in the file designated to contain configuration pragmas
4375 in a project file.
4376 They then apply to all subsequent compilations. In practice the use of
4377 the @code{-gnata} switch is often the most convenient method of controlling
4378 the status of these pragmas.
4379
4380 Note that a pragma is not a statement, so in contexts where a statement
4381 sequence is required, you can't just write a pragma on its own. You have
4382 to add a @code{null} statement.
4383
4384 @example
4385 if ... then
4386 ... -- some statements
4387 else
4388 pragma Assert (Num_Cases < 10);
4389 null;
4390 end if;
4391 @end example
4392
4393 @node Conditionalizing Declarations,Use of Alternative Implementations,Debugging - A Special Case,Modeling Conditional Compilation in Ada
4394 @anchor{gnat_ugn/the_gnat_compilation_model conditionalizing-declarations}@anchor{9f}@anchor{gnat_ugn/the_gnat_compilation_model id51}@anchor{a0}
4395 @subsubsection Conditionalizing Declarations
4396
4397
4398 In some cases it may be necessary to conditionalize declarations to meet
4399 different requirements. For example we might want a bit string whose length
4400 is set to meet some hardware message requirement.
4401
4402 This may be possible using declare blocks controlled
4403 by conditional constants:
4404
4405 @example
4406 if Small_Machine then
4407 declare
4408 X : Bit_String (1 .. 10);
4409 begin
4410 ...
4411 end;
4412 else
4413 declare
4414 X : Large_Bit_String (1 .. 1000);
4415 begin
4416 ...
4417 end;
4418 end if;
4419 @end example
4420
4421 Note that in this approach, both declarations are analyzed by the
4422 compiler so this can only be used where both declarations are legal,
4423 even though one of them will not be used.
4424
4425 Another approach is to define integer constants, e.g., @code{Bits_Per_Word},
4426 or Boolean constants, e.g., @code{Little_Endian}, and then write declarations
4427 that are parameterized by these constants. For example
4428
4429 @example
4430 for Rec use
4431 Field1 at 0 range Boolean'Pos (Little_Endian) * 10 .. Bits_Per_Word;
4432 end record;
4433 @end example
4434
4435 If @code{Bits_Per_Word} is set to 32, this generates either
4436
4437 @example
4438 for Rec use
4439 Field1 at 0 range 0 .. 32;
4440 end record;
4441 @end example
4442
4443 for the big endian case, or
4444
4445 @example
4446 for Rec use record
4447 Field1 at 0 range 10 .. 32;
4448 end record;
4449 @end example
4450
4451 for the little endian case. Since a powerful subset of Ada expression
4452 notation is usable for creating static constants, clever use of this
4453 feature can often solve quite difficult problems in conditionalizing
4454 compilation (note incidentally that in Ada 95, the little endian
4455 constant was introduced as @code{System.Default_Bit_Order}, so you do not
4456 need to define this one yourself).
4457
4458 @node Use of Alternative Implementations,Preprocessing,Conditionalizing Declarations,Modeling Conditional Compilation in Ada
4459 @anchor{gnat_ugn/the_gnat_compilation_model use-of-alternative-implementations}@anchor{a1}@anchor{gnat_ugn/the_gnat_compilation_model id52}@anchor{a2}
4460 @subsubsection Use of Alternative Implementations
4461
4462
4463 In some cases, none of the approaches described above are adequate. This
4464 can occur for example if the set of declarations required is radically
4465 different for two different configurations.
4466
4467 In this situation, the official Ada way of dealing with conditionalizing
4468 such code is to write separate units for the different cases. As long as
4469 this does not result in excessive duplication of code, this can be done
4470 without creating maintenance problems. The approach is to share common
4471 code as far as possible, and then isolate the code and declarations
4472 that are different. Subunits are often a convenient method for breaking
4473 out a piece of a unit that is to be conditionalized, with separate files
4474 for different versions of the subunit for different targets, where the
4475 build script selects the right one to give to the compiler.
4476
4477 @geindex Subunits (and conditional compilation)
4478
4479 As an example, consider a situation where a new feature in Ada 2005
4480 allows something to be done in a really nice way. But your code must be able
4481 to compile with an Ada 95 compiler. Conceptually you want to say:
4482
4483 @example
4484 if Ada_2005 then
4485 ... neat Ada 2005 code
4486 else
4487 ... not quite as neat Ada 95 code
4488 end if;
4489 @end example
4490
4491 where @code{Ada_2005} is a Boolean constant.
4492
4493 But this won't work when @code{Ada_2005} is set to @code{False},
4494 since the @code{then} clause will be illegal for an Ada 95 compiler.
4495 (Recall that although such unreachable code would eventually be deleted
4496 by the compiler, it still needs to be legal. If it uses features
4497 introduced in Ada 2005, it will be illegal in Ada 95.)
4498
4499 So instead we write
4500
4501 @example
4502 procedure Insert is separate;
4503 @end example
4504
4505 Then we have two files for the subunit @code{Insert}, with the two sets of
4506 code.
4507 If the package containing this is called @code{File_Queries}, then we might
4508 have two files
4509
4510
4511 @itemize *
4512
4513 @item
4514 @code{file_queries-insert-2005.adb}
4515
4516 @item
4517 @code{file_queries-insert-95.adb}
4518 @end itemize
4519
4520 and the build script renames the appropriate file to @code{file_queries-insert.adb} and then carries out the compilation.
4521
4522 This can also be done with project files' naming schemes. For example:
4523
4524 @example
4525 for body ("File_Queries.Insert") use "file_queries-insert-2005.ada";
4526 @end example
4527
4528 Note also that with project files it is desirable to use a different extension
4529 than @code{ads} / @code{adb} for alternative versions. Otherwise a naming
4530 conflict may arise through another commonly used feature: to declare as part
4531 of the project a set of directories containing all the sources obeying the
4532 default naming scheme.
4533
4534 The use of alternative units is certainly feasible in all situations,
4535 and for example the Ada part of the GNAT run-time is conditionalized
4536 based on the target architecture using this approach. As a specific example,
4537 consider the implementation of the AST feature in VMS. There is one
4538 spec: @code{s-asthan.ads} which is the same for all architectures, and three
4539 bodies:
4540
4541
4542 @itemize *
4543
4544 @item
4545
4546 @table @asis
4547
4548 @item @code{s-asthan.adb}
4549
4550 used for all non-VMS operating systems
4551 @end table
4552
4553 @item
4554
4555 @table @asis
4556
4557 @item @code{s-asthan-vms-alpha.adb}
4558
4559 used for VMS on the Alpha
4560 @end table
4561
4562 @item
4563
4564 @table @asis
4565
4566 @item @code{s-asthan-vms-ia64.adb}
4567
4568 used for VMS on the ia64
4569 @end table
4570 @end itemize
4571
4572 The dummy version @code{s-asthan.adb} simply raises exceptions noting that
4573 this operating system feature is not available, and the two remaining
4574 versions interface with the corresponding versions of VMS to provide
4575 VMS-compatible AST handling. The GNAT build script knows the architecture
4576 and operating system, and automatically selects the right version,
4577 renaming it if necessary to @code{s-asthan.adb} before the run-time build.
4578
4579 Another style for arranging alternative implementations is through Ada's
4580 access-to-subprogram facility.
4581 In case some functionality is to be conditionally included,
4582 you can declare an access-to-procedure variable @code{Ref} that is initialized
4583 to designate a 'do nothing' procedure, and then invoke @code{Ref.all}
4584 when appropriate.
4585 In some library package, set @code{Ref} to @code{Proc'Access} for some
4586 procedure @code{Proc} that performs the relevant processing.
4587 The initialization only occurs if the library package is included in the
4588 program.
4589 The same idea can also be implemented using tagged types and dispatching
4590 calls.
4591
4592 @node Preprocessing,,Use of Alternative Implementations,Modeling Conditional Compilation in Ada
4593 @anchor{gnat_ugn/the_gnat_compilation_model preprocessing}@anchor{a3}@anchor{gnat_ugn/the_gnat_compilation_model id53}@anchor{a4}
4594 @subsubsection Preprocessing
4595
4596
4597 @geindex Preprocessing
4598
4599 Although it is quite possible to conditionalize code without the use of
4600 C-style preprocessing, as described earlier in this section, it is
4601 nevertheless convenient in some cases to use the C approach. Moreover,
4602 older Ada compilers have often provided some preprocessing capability,
4603 so legacy code may depend on this approach, even though it is not
4604 standard.
4605
4606 To accommodate such use, GNAT provides a preprocessor (modeled to a large
4607 extent on the various preprocessors that have been used
4608 with legacy code on other compilers, to enable easier transition).
4609
4610 @geindex gnatprep
4611
4612 The preprocessor may be used in two separate modes. It can be used quite
4613 separately from the compiler, to generate a separate output source file
4614 that is then fed to the compiler as a separate step. This is the
4615 @code{gnatprep} utility, whose use is fully described in
4616 @ref{17,,Preprocessing with gnatprep}.
4617
4618 The preprocessing language allows such constructs as
4619
4620 @example
4621 #if DEBUG or else (PRIORITY > 4) then
4622 sequence of declarations
4623 #else
4624 completely different sequence of declarations
4625 #end if;
4626 @end example
4627
4628 The values of the symbols @code{DEBUG} and @code{PRIORITY} can be
4629 defined either on the command line or in a separate file.
4630
4631 The other way of running the preprocessor is even closer to the C style and
4632 often more convenient. In this approach the preprocessing is integrated into
4633 the compilation process. The compiler is given the preprocessor input which
4634 includes @code{#if} lines etc, and then the compiler carries out the
4635 preprocessing internally and processes the resulting output.
4636 For more details on this approach, see @ref{18,,Integrated Preprocessing}.
4637
4638 @node Preprocessing with gnatprep,Integrated Preprocessing,Modeling Conditional Compilation in Ada,Conditional Compilation
4639 @anchor{gnat_ugn/the_gnat_compilation_model id54}@anchor{a5}@anchor{gnat_ugn/the_gnat_compilation_model preprocessing-with-gnatprep}@anchor{17}
4640 @subsection Preprocessing with @code{gnatprep}
4641
4642
4643 @geindex gnatprep
4644
4645 @geindex Preprocessing (gnatprep)
4646
4647 This section discusses how to use GNAT's @code{gnatprep} utility for simple
4648 preprocessing.
4649 Although designed for use with GNAT, @code{gnatprep} does not depend on any
4650 special GNAT features.
4651 For further discussion of conditional compilation in general, see
4652 @ref{16,,Conditional Compilation}.
4653
4654 @menu
4655 * Preprocessing Symbols::
4656 * Using gnatprep::
4657 * Switches for gnatprep::
4658 * Form of Definitions File::
4659 * Form of Input Text for gnatprep::
4660
4661 @end menu
4662
4663 @node Preprocessing Symbols,Using gnatprep,,Preprocessing with gnatprep
4664 @anchor{gnat_ugn/the_gnat_compilation_model id55}@anchor{a6}@anchor{gnat_ugn/the_gnat_compilation_model preprocessing-symbols}@anchor{a7}
4665 @subsubsection Preprocessing Symbols
4666
4667
4668 Preprocessing symbols are defined in @emph{definition files} and referenced in the
4669 sources to be preprocessed. A preprocessing symbol is an identifier, following
4670 normal Ada (case-insensitive) rules for its syntax, with the restriction that
4671 all characters need to be in the ASCII set (no accented letters).
4672
4673 @node Using gnatprep,Switches for gnatprep,Preprocessing Symbols,Preprocessing with gnatprep
4674 @anchor{gnat_ugn/the_gnat_compilation_model using-gnatprep}@anchor{a8}@anchor{gnat_ugn/the_gnat_compilation_model id56}@anchor{a9}
4675 @subsubsection Using @code{gnatprep}
4676
4677
4678 To call @code{gnatprep} use:
4679
4680 @example
4681 $ gnatprep [ switches ] infile outfile [ deffile ]
4682 @end example
4683
4684 where
4685
4686
4687 @itemize *
4688
4689 @item
4690
4691 @table @asis
4692
4693 @item @emph{switches}
4694
4695 is an optional sequence of switches as described in the next section.
4696 @end table
4697
4698 @item
4699
4700 @table @asis
4701
4702 @item @emph{infile}
4703
4704 is the full name of the input file, which is an Ada source
4705 file containing preprocessor directives.
4706 @end table
4707
4708 @item
4709
4710 @table @asis
4711
4712 @item @emph{outfile}
4713
4714 is the full name of the output file, which is an Ada source
4715 in standard Ada form. When used with GNAT, this file name will
4716 normally have an @code{ads} or @code{adb} suffix.
4717 @end table
4718
4719 @item
4720
4721 @table @asis
4722
4723 @item @code{deffile}
4724
4725 is the full name of a text file containing definitions of
4726 preprocessing symbols to be referenced by the preprocessor. This argument is
4727 optional, and can be replaced by the use of the @code{-D} switch.
4728 @end table
4729 @end itemize
4730
4731 @node Switches for gnatprep,Form of Definitions File,Using gnatprep,Preprocessing with gnatprep
4732 @anchor{gnat_ugn/the_gnat_compilation_model switches-for-gnatprep}@anchor{aa}@anchor{gnat_ugn/the_gnat_compilation_model id57}@anchor{ab}
4733 @subsubsection Switches for @code{gnatprep}
4734
4735
4736 @geindex --version (gnatprep)
4737
4738
4739 @table @asis
4740
4741 @item @code{--version}
4742
4743 Display Copyright and version, then exit disregarding all other options.
4744 @end table
4745
4746 @geindex --help (gnatprep)
4747
4748
4749 @table @asis
4750
4751 @item @code{--help}
4752
4753 If @code{--version} was not used, display usage and then exit disregarding
4754 all other options.
4755 @end table
4756
4757 @geindex -b (gnatprep)
4758
4759
4760 @table @asis
4761
4762 @item @code{-b}
4763
4764 Causes both preprocessor lines and the lines deleted by
4765 preprocessing to be replaced by blank lines in the output source file,
4766 preserving line numbers in the output file.
4767 @end table
4768
4769 @geindex -c (gnatprep)
4770
4771
4772 @table @asis
4773
4774 @item @code{-c}
4775
4776 Causes both preprocessor lines and the lines deleted
4777 by preprocessing to be retained in the output source as comments marked
4778 with the special string @code{"--! "}. This option will result in line numbers
4779 being preserved in the output file.
4780 @end table
4781
4782 @geindex -C (gnatprep)
4783
4784
4785 @table @asis
4786
4787 @item @code{-C}
4788
4789 Causes comments to be scanned. Normally comments are ignored by gnatprep.
4790 If this option is specified, then comments are scanned and any $symbol
4791 substitutions performed as in program text. This is particularly useful
4792 when structured comments are used (e.g., for programs written in a
4793 pre-2014 version of the SPARK Ada subset). Note that this switch is not
4794 available when doing integrated preprocessing (it would be useless in
4795 this context since comments are ignored by the compiler in any case).
4796 @end table
4797
4798 @geindex -D (gnatprep)
4799
4800
4801 @table @asis
4802
4803 @item @code{-D@emph{symbol}[=@emph{value}]}
4804
4805 Defines a new preprocessing symbol with the specified value. If no value is given
4806 on the command line, then symbol is considered to be @code{True}. This switch
4807 can be used in place of a definition file.
4808 @end table
4809
4810 @geindex -r (gnatprep)
4811
4812
4813 @table @asis
4814
4815 @item @code{-r}
4816
4817 Causes a @code{Source_Reference} pragma to be generated that
4818 references the original input file, so that error messages will use
4819 the file name of this original file. The use of this switch implies
4820 that preprocessor lines are not to be removed from the file, so its
4821 use will force @code{-b} mode if @code{-c}
4822 has not been specified explicitly.
4823
4824 Note that if the file to be preprocessed contains multiple units, then
4825 it will be necessary to @code{gnatchop} the output file from
4826 @code{gnatprep}. If a @code{Source_Reference} pragma is present
4827 in the preprocessed file, it will be respected by
4828 @code{gnatchop -r}
4829 so that the final chopped files will correctly refer to the original
4830 input source file for @code{gnatprep}.
4831 @end table
4832
4833 @geindex -s (gnatprep)
4834
4835
4836 @table @asis
4837
4838 @item @code{-s}
4839
4840 Causes a sorted list of symbol names and values to be
4841 listed on the standard output file.
4842 @end table
4843
4844 @geindex -T (gnatprep)
4845
4846
4847 @table @asis
4848
4849 @item @code{-T}
4850
4851 Use LF as line terminators when writing files. By default the line terminator
4852 of the host (LF under unix, CR/LF under Windows) is used.
4853 @end table
4854
4855 @geindex -u (gnatprep)
4856
4857
4858 @table @asis
4859
4860 @item @code{-u}
4861
4862 Causes undefined symbols to be treated as having the value FALSE in the context
4863 of a preprocessor test. In the absence of this option, an undefined symbol in
4864 a @code{#if} or @code{#elsif} test will be treated as an error.
4865 @end table
4866
4867 @geindex -v (gnatprep)
4868
4869
4870 @table @asis
4871
4872 @item @code{-v}
4873
4874 Verbose mode: generates more output about work done.
4875 @end table
4876
4877 Note: if neither @code{-b} nor @code{-c} is present,
4878 then preprocessor lines and
4879 deleted lines are completely removed from the output, unless -r is
4880 specified, in which case -b is assumed.
4881
4882 @node Form of Definitions File,Form of Input Text for gnatprep,Switches for gnatprep,Preprocessing with gnatprep
4883 @anchor{gnat_ugn/the_gnat_compilation_model form-of-definitions-file}@anchor{ac}@anchor{gnat_ugn/the_gnat_compilation_model id58}@anchor{ad}
4884 @subsubsection Form of Definitions File
4885
4886
4887 The definitions file contains lines of the form:
4888
4889 @example
4890 symbol := value
4891 @end example
4892
4893 where @code{symbol} is a preprocessing symbol, and @code{value} is one of the following:
4894
4895
4896 @itemize *
4897
4898 @item
4899 Empty, corresponding to a null substitution,
4900
4901 @item
4902 A string literal using normal Ada syntax, or
4903
4904 @item
4905 Any sequence of characters from the set @{letters, digits, period, underline@}.
4906 @end itemize
4907
4908 Comment lines may also appear in the definitions file, starting with
4909 the usual @code{--},
4910 and comments may be added to the definitions lines.
4911
4912 @node Form of Input Text for gnatprep,,Form of Definitions File,Preprocessing with gnatprep
4913 @anchor{gnat_ugn/the_gnat_compilation_model id59}@anchor{ae}@anchor{gnat_ugn/the_gnat_compilation_model form-of-input-text-for-gnatprep}@anchor{af}
4914 @subsubsection Form of Input Text for @code{gnatprep}
4915
4916
4917 The input text may contain preprocessor conditional inclusion lines,
4918 as well as general symbol substitution sequences.
4919
4920 The preprocessor conditional inclusion commands have the form:
4921
4922 @example
4923 #if <expression> [then]
4924 lines
4925 #elsif <expression> [then]
4926 lines
4927 #elsif <expression> [then]
4928 lines
4929 ...
4930 #else
4931 lines
4932 #end if;
4933 @end example
4934
4935 In this example, <expression> is defined by the following grammar:
4936
4937 @example
4938 <expression> ::= <symbol>
4939 <expression> ::= <symbol> = "<value>"
4940 <expression> ::= <symbol> = <symbol>
4941 <expression> ::= <symbol> = <integer>
4942 <expression> ::= <symbol> > <integer>
4943 <expression> ::= <symbol> >= <integer>
4944 <expression> ::= <symbol> < <integer>
4945 <expression> ::= <symbol> <= <integer>
4946 <expression> ::= <symbol> 'Defined
4947 <expression> ::= not <expression>
4948 <expression> ::= <expression> and <expression>
4949 <expression> ::= <expression> or <expression>
4950 <expression> ::= <expression> and then <expression>
4951 <expression> ::= <expression> or else <expression>
4952 <expression> ::= ( <expression> )
4953 @end example
4954
4955 Note the following restriction: it is not allowed to have "and" or "or"
4956 following "not" in the same expression without parentheses. For example, this
4957 is not allowed:
4958
4959 @example
4960 not X or Y
4961 @end example
4962
4963 This can be expressed instead as one of the following forms:
4964
4965 @example
4966 (not X) or Y
4967 not (X or Y)
4968 @end example
4969
4970 For the first test (<expression> ::= <symbol>) the symbol must have
4971 either the value true or false, that is to say the right-hand of the
4972 symbol definition must be one of the (case-insensitive) literals
4973 @code{True} or @code{False}. If the value is true, then the
4974 corresponding lines are included, and if the value is false, they are
4975 excluded.
4976
4977 When comparing a symbol to an integer, the integer is any non negative
4978 literal integer as defined in the Ada Reference Manual, such as 3, 16#FF# or
4979 2#11#. The symbol value must also be a non negative integer. Integer values
4980 in the range 0 .. 2**31-1 are supported.
4981
4982 The test (<expression> ::= <symbol>'Defined) is true only if
4983 the symbol has been defined in the definition file or by a @code{-D}
4984 switch on the command line. Otherwise, the test is false.
4985
4986 The equality tests are case insensitive, as are all the preprocessor lines.
4987
4988 If the symbol referenced is not defined in the symbol definitions file,
4989 then the effect depends on whether or not switch @code{-u}
4990 is specified. If so, then the symbol is treated as if it had the value
4991 false and the test fails. If this switch is not specified, then
4992 it is an error to reference an undefined symbol. It is also an error to
4993 reference a symbol that is defined with a value other than @code{True}
4994 or @code{False}.
4995
4996 The use of the @code{not} operator inverts the sense of this logical test.
4997 The @code{not} operator cannot be combined with the @code{or} or @code{and}
4998 operators, without parentheses. For example, "if not X or Y then" is not
4999 allowed, but "if (not X) or Y then" and "if not (X or Y) then" are.
5000
5001 The @code{then} keyword is optional as shown
5002
5003 The @code{#} must be the first non-blank character on a line, but
5004 otherwise the format is free form. Spaces or tabs may appear between
5005 the @code{#} and the keyword. The keywords and the symbols are case
5006 insensitive as in normal Ada code. Comments may be used on a
5007 preprocessor line, but other than that, no other tokens may appear on a
5008 preprocessor line. Any number of @code{elsif} clauses can be present,
5009 including none at all. The @code{else} is optional, as in Ada.
5010
5011 The @code{#} marking the start of a preprocessor line must be the first
5012 non-blank character on the line, i.e., it must be preceded only by
5013 spaces or horizontal tabs.
5014
5015 Symbol substitution outside of preprocessor lines is obtained by using
5016 the sequence:
5017
5018 @example
5019 $symbol
5020 @end example
5021
5022 anywhere within a source line, except in a comment or within a
5023 string literal. The identifier
5024 following the @code{$} must match one of the symbols defined in the symbol
5025 definition file, and the result is to substitute the value of the
5026 symbol in place of @code{$symbol} in the output file.
5027
5028 Note that although the substitution of strings within a string literal
5029 is not possible, it is possible to have a symbol whose defined value is
5030 a string literal. So instead of setting XYZ to @code{hello} and writing:
5031
5032 @example
5033 Header : String := "$XYZ";
5034 @end example
5035
5036 you should set XYZ to @code{"hello"} and write:
5037
5038 @example
5039 Header : String := $XYZ;
5040 @end example
5041
5042 and then the substitution will occur as desired.
5043
5044 @node Integrated Preprocessing,,Preprocessing with gnatprep,Conditional Compilation
5045 @anchor{gnat_ugn/the_gnat_compilation_model id60}@anchor{b0}@anchor{gnat_ugn/the_gnat_compilation_model integrated-preprocessing}@anchor{18}
5046 @subsection Integrated Preprocessing
5047
5048
5049 As noted above, a file to be preprocessed consists of Ada source code
5050 in which preprocessing lines have been inserted. However,
5051 instead of using @code{gnatprep} to explicitly preprocess a file as a separate
5052 step before compilation, you can carry out the preprocessing implicitly
5053 as part of compilation. Such @emph{integrated preprocessing}, which is the common
5054 style with C, is performed when either or both of the following switches
5055 are passed to the compiler:
5056
5057 @quotation
5058
5059
5060 @itemize *
5061
5062 @item
5063 @code{-gnatep}, which specifies the @emph{preprocessor data file}.
5064 This file dictates how the source files will be preprocessed (e.g., which
5065 symbol definition files apply to which sources).
5066
5067 @item
5068 @code{-gnateD}, which defines values for preprocessing symbols.
5069 @end itemize
5070 @end quotation
5071
5072 Integrated preprocessing applies only to Ada source files, it is
5073 not available for configuration pragma files.
5074
5075 With integrated preprocessing, the output from the preprocessor is not,
5076 by default, written to any external file. Instead it is passed
5077 internally to the compiler. To preserve the result of
5078 preprocessing in a file, either run @code{gnatprep}
5079 in standalone mode or else supply the @code{-gnateG} switch
5080 (described below) to the compiler.
5081
5082 When using project files:
5083
5084 @quotation
5085
5086
5087 @itemize *
5088
5089 @item
5090 the builder switch @code{-x} should be used if any Ada source is
5091 compiled with @code{gnatep=}, so that the compiler finds the
5092 @emph{preprocessor data file}.
5093
5094 @item
5095 the preprocessing data file and the symbol definition files should be
5096 located in the source directories of the project.
5097 @end itemize
5098 @end quotation
5099
5100 Note that the @code{gnatmake} switch @code{-m} will almost
5101 always trigger recompilation for sources that are preprocessed,
5102 because @code{gnatmake} cannot compute the checksum of the source after
5103 preprocessing.
5104
5105 The actual preprocessing function is described in detail in
5106 @ref{17,,Preprocessing with gnatprep}. This section explains the switches
5107 that relate to integrated preprocessing.
5108
5109 @geindex -gnatep (gcc)
5110
5111
5112 @table @asis
5113
5114 @item @code{-gnatep=@emph{preprocessor_data_file}}
5115
5116 This switch specifies the file name (without directory
5117 information) of the preprocessor data file. Either place this file
5118 in one of the source directories, or, when using project
5119 files, reference the project file's directory via the
5120 @code{project_name'Project_Dir} project attribute; e.g:
5121
5122 @quotation
5123
5124 @example
5125 project Prj is
5126 package Compiler is
5127 for Switches ("Ada") use
5128 ("-gnatep=" & Prj'Project_Dir & "prep.def");
5129 end Compiler;
5130 end Prj;
5131 @end example
5132 @end quotation
5133
5134 A preprocessor data file is a text file that contains @emph{preprocessor
5135 control lines}. A preprocessor control line directs the preprocessing of
5136 either a particular source file, or, analogous to @code{others} in Ada,
5137 all sources not specified elsewhere in the preprocessor data file.
5138 A preprocessor control line
5139 can optionally identify a @emph{definition file} that assigns values to
5140 preprocessor symbols, as well as a list of switches that relate to
5141 preprocessing.
5142 Empty lines and comments (using Ada syntax) are also permitted, with no
5143 semantic effect.
5144
5145 Here's an example of a preprocessor data file:
5146
5147 @quotation
5148
5149 @example
5150 "toto.adb" "prep.def" -u
5151 -- Preprocess toto.adb, using definition file prep.def
5152 -- Undefined symbols are treated as False
5153
5154 * -c -DVERSION=V101
5155 -- Preprocess all other sources without using a definition file
5156 -- Suppressed lined are commented
5157 -- Symbol VERSION has the value V101
5158
5159 "tata.adb" "prep2.def" -s
5160 -- Preprocess tata.adb, using definition file prep2.def
5161 -- List all symbols with their values
5162 @end example
5163 @end quotation
5164
5165 A preprocessor control line has the following syntax:
5166
5167 @quotation
5168
5169 @example
5170 <preprocessor_control_line> ::=
5171 <preprocessor_input> [ <definition_file_name> ] @{ <switch> @}
5172
5173 <preprocessor_input> ::= <source_file_name> | '*'
5174
5175 <definition_file_name> ::= <string_literal>
5176
5177 <source_file_name> := <string_literal>
5178
5179 <switch> := (See below for list)
5180 @end example
5181 @end quotation
5182
5183 Thus each preprocessor control line starts with either a literal string or
5184 the character '*':
5185
5186
5187 @itemize *
5188
5189 @item
5190 A literal string is the file name (without directory information) of the source
5191 file that will be input to the preprocessor.
5192
5193 @item
5194 The character '*' is a wild-card indicator; the additional parameters on the line
5195 indicate the preprocessing for all the sources
5196 that are not specified explicitly on other lines (the order of the lines is not
5197 significant).
5198 @end itemize
5199
5200 It is an error to have two lines with the same file name or two
5201 lines starting with the character '*'.
5202
5203 After the file name or '*', an optional literal string specifies the name of
5204 the definition file to be used for preprocessing
5205 (@ref{ac,,Form of Definitions File}). The definition files are found by the
5206 compiler in one of the source directories. In some cases, when compiling
5207 a source in a directory other than the current directory, if the definition
5208 file is in the current directory, it may be necessary to add the current
5209 directory as a source directory through the @code{-I} switch; otherwise
5210 the compiler would not find the definition file.
5211
5212 Finally, switches similar to those of @code{gnatprep} may optionally appear:
5213
5214
5215 @table @asis
5216
5217 @item @code{-b}
5218
5219 Causes both preprocessor lines and the lines deleted by
5220 preprocessing to be replaced by blank lines, preserving the line number.
5221 This switch is always implied; however, if specified after @code{-c}
5222 it cancels the effect of @code{-c}.
5223
5224 @item @code{-c}
5225
5226 Causes both preprocessor lines and the lines deleted
5227 by preprocessing to be retained as comments marked
5228 with the special string '@cite{--!}'.
5229
5230 @item @code{-D@emph{symbol}=@emph{new_value}}
5231
5232 Define or redefine @code{symbol} to have @code{new_value} as its value.
5233 The permitted form for @code{symbol} is either an Ada identifier, or any Ada reserved word
5234 aside from @code{if},
5235 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
5236 The permitted form for @code{new_value} is a literal string, an Ada identifier or any Ada reserved
5237 word. A symbol declared with this switch replaces a symbol with the
5238 same name defined in a definition file.
5239
5240 @item @code{-s}
5241
5242 Causes a sorted list of symbol names and values to be
5243 listed on the standard output file.
5244
5245 @item @code{-u}
5246
5247 Causes undefined symbols to be treated as having the value @code{FALSE}
5248 in the context
5249 of a preprocessor test. In the absence of this option, an undefined symbol in
5250 a @code{#if} or @code{#elsif} test will be treated as an error.
5251 @end table
5252 @end table
5253
5254 @geindex -gnateD (gcc)
5255
5256
5257 @table @asis
5258
5259 @item @code{-gnateD@emph{symbol}[=@emph{new_value}]}
5260
5261 Define or redefine @code{symbol} to have @code{new_value} as its value. If no value
5262 is supplied, then the value of @code{symbol} is @code{True}.
5263 The form of @code{symbol} is an identifier, following normal Ada (case-insensitive)
5264 rules for its syntax, and @code{new_value} is either an arbitrary string between double
5265 quotes or any sequence (including an empty sequence) of characters from the
5266 set (letters, digits, period, underline).
5267 Ada reserved words may be used as symbols, with the exceptions of @code{if},
5268 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
5269
5270 Examples:
5271
5272 @quotation
5273
5274 @example
5275 -gnateDToto=Tata
5276 -gnateDFoo
5277 -gnateDFoo=\"Foo-Bar\"
5278 @end example
5279 @end quotation
5280
5281 A symbol declared with this switch on the command line replaces a
5282 symbol with the same name either in a definition file or specified with a
5283 switch @code{-D} in the preprocessor data file.
5284
5285 This switch is similar to switch @code{-D} of @code{gnatprep}.
5286
5287 @item @code{-gnateG}
5288
5289 When integrated preprocessing is performed on source file @code{filename.extension},
5290 create or overwrite @code{filename.extension.prep} to contain
5291 the result of the preprocessing.
5292 For example if the source file is @code{foo.adb} then
5293 the output file will be @code{foo.adb.prep}.
5294 @end table
5295
5296 @node Mixed Language Programming,GNAT and Other Compilation Models,Conditional Compilation,The GNAT Compilation Model
5297 @anchor{gnat_ugn/the_gnat_compilation_model mixed-language-programming}@anchor{44}@anchor{gnat_ugn/the_gnat_compilation_model id61}@anchor{b1}
5298 @section Mixed Language Programming
5299
5300
5301 @geindex Mixed Language Programming
5302
5303 This section describes how to develop a mixed-language program,
5304 with a focus on combining Ada with C or C++.
5305
5306 @menu
5307 * Interfacing to C::
5308 * Calling Conventions::
5309 * Building Mixed Ada and C++ Programs::
5310 * Generating Ada Bindings for C and C++ headers::
5311 * Generating C Headers for Ada Specifications::
5312
5313 @end menu
5314
5315 @node Interfacing to C,Calling Conventions,,Mixed Language Programming
5316 @anchor{gnat_ugn/the_gnat_compilation_model interfacing-to-c}@anchor{b2}@anchor{gnat_ugn/the_gnat_compilation_model id62}@anchor{b3}
5317 @subsection Interfacing to C
5318
5319
5320 Interfacing Ada with a foreign language such as C involves using
5321 compiler directives to import and/or export entity definitions in each
5322 language -- using @code{extern} statements in C, for instance, and the
5323 @code{Import}, @code{Export}, and @code{Convention} pragmas in Ada.
5324 A full treatment of these topics is provided in Appendix B, section 1
5325 of the Ada Reference Manual.
5326
5327 There are two ways to build a program using GNAT that contains some Ada
5328 sources and some foreign language sources, depending on whether or not
5329 the main subprogram is written in Ada. Here is a source example with
5330 the main subprogram in Ada:
5331
5332 @example
5333 /* file1.c */
5334 #include <stdio.h>
5335
5336 void print_num (int num)
5337 @{
5338 printf ("num is %d.\\n", num);
5339 return;
5340 @}
5341 @end example
5342
5343 @example
5344 /* file2.c */
5345
5346 /* num_from_Ada is declared in my_main.adb */
5347 extern int num_from_Ada;
5348
5349 int get_num (void)
5350 @{
5351 return num_from_Ada;
5352 @}
5353 @end example
5354
5355 @example
5356 -- my_main.adb
5357 procedure My_Main is
5358
5359 -- Declare then export an Integer entity called num_from_Ada
5360 My_Num : Integer := 10;
5361 pragma Export (C, My_Num, "num_from_Ada");
5362
5363 -- Declare an Ada function spec for Get_Num, then use
5364 -- C function get_num for the implementation.
5365 function Get_Num return Integer;
5366 pragma Import (C, Get_Num, "get_num");
5367
5368 -- Declare an Ada procedure spec for Print_Num, then use
5369 -- C function print_num for the implementation.
5370 procedure Print_Num (Num : Integer);
5371 pragma Import (C, Print_Num, "print_num");
5372
5373 begin
5374 Print_Num (Get_Num);
5375 end My_Main;
5376 @end example
5377
5378 To build this example:
5379
5380
5381 @itemize *
5382
5383 @item
5384 First compile the foreign language files to
5385 generate object files:
5386
5387 @example
5388 $ gcc -c file1.c
5389 $ gcc -c file2.c
5390 @end example
5391
5392 @item
5393 Then, compile the Ada units to produce a set of object files and ALI
5394 files:
5395
5396 @example
5397 $ gnatmake -c my_main.adb
5398 @end example
5399
5400 @item
5401 Run the Ada binder on the Ada main program:
5402
5403 @example
5404 $ gnatbind my_main.ali
5405 @end example
5406
5407 @item
5408 Link the Ada main program, the Ada objects and the other language
5409 objects:
5410
5411 @example
5412 $ gnatlink my_main.ali file1.o file2.o
5413 @end example
5414 @end itemize
5415
5416 The last three steps can be grouped in a single command:
5417
5418 @example
5419 $ gnatmake my_main.adb -largs file1.o file2.o
5420 @end example
5421
5422 @geindex Binder output file
5423
5424 If the main program is in a language other than Ada, then you may have
5425 more than one entry point into the Ada subsystem. You must use a special
5426 binder option to generate callable routines that initialize and
5427 finalize the Ada units (@ref{b4,,Binding with Non-Ada Main Programs}).
5428 Calls to the initialization and finalization routines must be inserted
5429 in the main program, or some other appropriate point in the code. The
5430 call to initialize the Ada units must occur before the first Ada
5431 subprogram is called, and the call to finalize the Ada units must occur
5432 after the last Ada subprogram returns. The binder will place the
5433 initialization and finalization subprograms into the
5434 @code{b~xxx.adb} file where they can be accessed by your C
5435 sources. To illustrate, we have the following example:
5436
5437 @example
5438 /* main.c */
5439 extern void adainit (void);
5440 extern void adafinal (void);
5441 extern int add (int, int);
5442 extern int sub (int, int);
5443
5444 int main (int argc, char *argv[])
5445 @{
5446 int a = 21, b = 7;
5447
5448 adainit();
5449
5450 /* Should print "21 + 7 = 28" */
5451 printf ("%d + %d = %d\\n", a, b, add (a, b));
5452
5453 /* Should print "21 - 7 = 14" */
5454 printf ("%d - %d = %d\\n", a, b, sub (a, b));
5455
5456 adafinal();
5457 @}
5458 @end example
5459
5460 @example
5461 -- unit1.ads
5462 package Unit1 is
5463 function Add (A, B : Integer) return Integer;
5464 pragma Export (C, Add, "add");
5465 end Unit1;
5466 @end example
5467
5468 @example
5469 -- unit1.adb
5470 package body Unit1 is
5471 function Add (A, B : Integer) return Integer is
5472 begin
5473 return A + B;
5474 end Add;
5475 end Unit1;
5476 @end example
5477
5478 @example
5479 -- unit2.ads
5480 package Unit2 is
5481 function Sub (A, B : Integer) return Integer;
5482 pragma Export (C, Sub, "sub");
5483 end Unit2;
5484 @end example
5485
5486 @example
5487 -- unit2.adb
5488 package body Unit2 is
5489 function Sub (A, B : Integer) return Integer is
5490 begin
5491 return A - B;
5492 end Sub;
5493 end Unit2;
5494 @end example
5495
5496 The build procedure for this application is similar to the last
5497 example's:
5498
5499
5500 @itemize *
5501
5502 @item
5503 First, compile the foreign language files to generate object files:
5504
5505 @example
5506 $ gcc -c main.c
5507 @end example
5508
5509 @item
5510 Next, compile the Ada units to produce a set of object files and ALI
5511 files:
5512
5513 @example
5514 $ gnatmake -c unit1.adb
5515 $ gnatmake -c unit2.adb
5516 @end example
5517
5518 @item
5519 Run the Ada binder on every generated ALI file. Make sure to use the
5520 @code{-n} option to specify a foreign main program:
5521
5522 @example
5523 $ gnatbind -n unit1.ali unit2.ali
5524 @end example
5525
5526 @item
5527 Link the Ada main program, the Ada objects and the foreign language
5528 objects. You need only list the last ALI file here:
5529
5530 @example
5531 $ gnatlink unit2.ali main.o -o exec_file
5532 @end example
5533
5534 This procedure yields a binary executable called @code{exec_file}.
5535 @end itemize
5536
5537 Depending on the circumstances (for example when your non-Ada main object
5538 does not provide symbol @code{main}), you may also need to instruct the
5539 GNAT linker not to include the standard startup objects by passing the
5540 @code{-nostartfiles} switch to @code{gnatlink}.
5541
5542 @node Calling Conventions,Building Mixed Ada and C++ Programs,Interfacing to C,Mixed Language Programming
5543 @anchor{gnat_ugn/the_gnat_compilation_model calling-conventions}@anchor{b5}@anchor{gnat_ugn/the_gnat_compilation_model id63}@anchor{b6}
5544 @subsection Calling Conventions
5545
5546
5547 @geindex Foreign Languages
5548
5549 @geindex Calling Conventions
5550
5551 GNAT follows standard calling sequence conventions and will thus interface
5552 to any other language that also follows these conventions. The following
5553 Convention identifiers are recognized by GNAT:
5554
5555 @geindex Interfacing to Ada
5556
5557 @geindex Other Ada compilers
5558
5559 @geindex Convention Ada
5560
5561
5562 @table @asis
5563
5564 @item @code{Ada}
5565
5566 This indicates that the standard Ada calling sequence will be
5567 used and all Ada data items may be passed without any limitations in the
5568 case where GNAT is used to generate both the caller and callee. It is also
5569 possible to mix GNAT generated code and code generated by another Ada
5570 compiler. In this case, the data types should be restricted to simple
5571 cases, including primitive types. Whether complex data types can be passed
5572 depends on the situation. Probably it is safe to pass simple arrays, such
5573 as arrays of integers or floats. Records may or may not work, depending
5574 on whether both compilers lay them out identically. Complex structures
5575 involving variant records, access parameters, tasks, or protected types,
5576 are unlikely to be able to be passed.
5577
5578 Note that in the case of GNAT running
5579 on a platform that supports HP Ada 83, a higher degree of compatibility
5580 can be guaranteed, and in particular records are laid out in an identical
5581 manner in the two compilers. Note also that if output from two different
5582 compilers is mixed, the program is responsible for dealing with elaboration
5583 issues. Probably the safest approach is to write the main program in the
5584 version of Ada other than GNAT, so that it takes care of its own elaboration
5585 requirements, and then call the GNAT-generated adainit procedure to ensure
5586 elaboration of the GNAT components. Consult the documentation of the other
5587 Ada compiler for further details on elaboration.
5588
5589 However, it is not possible to mix the tasking run time of GNAT and
5590 HP Ada 83, All the tasking operations must either be entirely within
5591 GNAT compiled sections of the program, or entirely within HP Ada 83
5592 compiled sections of the program.
5593 @end table
5594
5595 @geindex Interfacing to Assembly
5596
5597 @geindex Convention Assembler
5598
5599
5600 @table @asis
5601
5602 @item @code{Assembler}
5603
5604 Specifies assembler as the convention. In practice this has the
5605 same effect as convention Ada (but is not equivalent in the sense of being
5606 considered the same convention).
5607 @end table
5608
5609 @geindex Convention Asm
5610
5611 @geindex Asm
5612
5613
5614 @table @asis
5615
5616 @item @code{Asm}
5617
5618 Equivalent to Assembler.
5619
5620 @geindex Interfacing to COBOL
5621
5622 @geindex Convention COBOL
5623 @end table
5624
5625 @geindex COBOL
5626
5627
5628 @table @asis
5629
5630 @item @code{COBOL}
5631
5632 Data will be passed according to the conventions described
5633 in section B.4 of the Ada Reference Manual.
5634 @end table
5635
5636 @geindex C
5637
5638 @geindex Interfacing to C
5639
5640 @geindex Convention C
5641
5642
5643 @table @asis
5644
5645 @item @code{C}
5646
5647 Data will be passed according to the conventions described
5648 in section B.3 of the Ada Reference Manual.
5649
5650 A note on interfacing to a C 'varargs' function:
5651
5652 @quotation
5653
5654 @geindex C varargs function
5655
5656 @geindex Interfacing to C varargs function
5657
5658 @geindex varargs function interfaces
5659
5660 In C, @code{varargs} allows a function to take a variable number of
5661 arguments. There is no direct equivalent in this to Ada. One
5662 approach that can be used is to create a C wrapper for each
5663 different profile and then interface to this C wrapper. For
5664 example, to print an @code{int} value using @code{printf},
5665 create a C function @code{printfi} that takes two arguments, a
5666 pointer to a string and an int, and calls @code{printf}.
5667 Then in the Ada program, use pragma @code{Import} to
5668 interface to @code{printfi}.
5669
5670 It may work on some platforms to directly interface to
5671 a @code{varargs} function by providing a specific Ada profile
5672 for a particular call. However, this does not work on
5673 all platforms, since there is no guarantee that the
5674 calling sequence for a two argument normal C function
5675 is the same as for calling a @code{varargs} C function with
5676 the same two arguments.
5677 @end quotation
5678 @end table
5679
5680 @geindex Convention Default
5681
5682 @geindex Default
5683
5684
5685 @table @asis
5686
5687 @item @code{Default}
5688
5689 Equivalent to C.
5690 @end table
5691
5692 @geindex Convention External
5693
5694 @geindex External
5695
5696
5697 @table @asis
5698
5699 @item @code{External}
5700
5701 Equivalent to C.
5702 @end table
5703
5704 @geindex C++
5705
5706 @geindex Interfacing to C++
5707
5708 @geindex Convention C++
5709
5710
5711 @table @asis
5712
5713 @item @code{C_Plus_Plus} (or @code{CPP})
5714
5715 This stands for C++. For most purposes this is identical to C.
5716 See the separate description of the specialized GNAT pragmas relating to
5717 C++ interfacing for further details.
5718 @end table
5719
5720 @geindex Fortran
5721
5722 @geindex Interfacing to Fortran
5723
5724 @geindex Convention Fortran
5725
5726
5727 @table @asis
5728
5729 @item @code{Fortran}
5730
5731 Data will be passed according to the conventions described
5732 in section B.5 of the Ada Reference Manual.
5733
5734 @item @code{Intrinsic}
5735
5736 This applies to an intrinsic operation, as defined in the Ada
5737 Reference Manual. If a pragma Import (Intrinsic) applies to a subprogram,
5738 this means that the body of the subprogram is provided by the compiler itself,
5739 usually by means of an efficient code sequence, and that the user does not
5740 supply an explicit body for it. In an application program, the pragma may
5741 be applied to the following sets of names:
5742
5743
5744 @itemize *
5745
5746 @item
5747 Rotate_Left, Rotate_Right, Shift_Left, Shift_Right, Shift_Right_Arithmetic.
5748 The corresponding subprogram declaration must have
5749 two formal parameters. The
5750 first one must be a signed integer type or a modular type with a binary
5751 modulus, and the second parameter must be of type Natural.
5752 The return type must be the same as the type of the first argument. The size
5753 of this type can only be 8, 16, 32, or 64.
5754
5755 @item
5756 Binary arithmetic operators: '+', '-', '*', '/'.
5757 The corresponding operator declaration must have parameters and result type
5758 that have the same root numeric type (for example, all three are long_float
5759 types). This simplifies the definition of operations that use type checking
5760 to perform dimensional checks:
5761 @end itemize
5762
5763 @example
5764 type Distance is new Long_Float;
5765 type Time is new Long_Float;
5766 type Velocity is new Long_Float;
5767 function "/" (D : Distance; T : Time)
5768 return Velocity;
5769 pragma Import (Intrinsic, "/");
5770
5771 This common idiom is often programmed with a generic definition and an
5772 explicit body. The pragma makes it simpler to introduce such declarations.
5773 It incurs no overhead in compilation time or code size, because it is
5774 implemented as a single machine instruction.
5775 @end example
5776
5777
5778 @itemize *
5779
5780 @item
5781 General subprogram entities. This is used to bind an Ada subprogram
5782 declaration to
5783 a compiler builtin by name with back-ends where such interfaces are
5784 available. A typical example is the set of @code{__builtin} functions
5785 exposed by the GCC back-end, as in the following example:
5786
5787 @example
5788 function builtin_sqrt (F : Float) return Float;
5789 pragma Import (Intrinsic, builtin_sqrt, "__builtin_sqrtf");
5790 @end example
5791
5792 Most of the GCC builtins are accessible this way, and as for other
5793 import conventions (e.g. C), it is the user's responsibility to ensure
5794 that the Ada subprogram profile matches the underlying builtin
5795 expectations.
5796 @end itemize
5797 @end table
5798
5799 @geindex Stdcall
5800
5801 @geindex Convention Stdcall
5802
5803
5804 @table @asis
5805
5806 @item @code{Stdcall}
5807
5808 This is relevant only to Windows implementations of GNAT,
5809 and specifies that the @code{Stdcall} calling sequence will be used,
5810 as defined by the NT API. Nevertheless, to ease building
5811 cross-platform bindings this convention will be handled as a @code{C} calling
5812 convention on non-Windows platforms.
5813 @end table
5814
5815 @geindex DLL
5816
5817 @geindex Convention DLL
5818
5819
5820 @table @asis
5821
5822 @item @code{DLL}
5823
5824 This is equivalent to @code{Stdcall}.
5825 @end table
5826
5827 @geindex Win32
5828
5829 @geindex Convention Win32
5830
5831
5832 @table @asis
5833
5834 @item @code{Win32}
5835
5836 This is equivalent to @code{Stdcall}.
5837 @end table
5838
5839 @geindex Stubbed
5840
5841 @geindex Convention Stubbed
5842
5843
5844 @table @asis
5845
5846 @item @code{Stubbed}
5847
5848 This is a special convention that indicates that the compiler
5849 should provide a stub body that raises @code{Program_Error}.
5850 @end table
5851
5852 GNAT additionally provides a useful pragma @code{Convention_Identifier}
5853 that can be used to parameterize conventions and allow additional synonyms
5854 to be specified. For example if you have legacy code in which the convention
5855 identifier Fortran77 was used for Fortran, you can use the configuration
5856 pragma:
5857
5858 @example
5859 pragma Convention_Identifier (Fortran77, Fortran);
5860 @end example
5861
5862 And from now on the identifier Fortran77 may be used as a convention
5863 identifier (for example in an @code{Import} pragma) with the same
5864 meaning as Fortran.
5865
5866 @node Building Mixed Ada and C++ Programs,Generating Ada Bindings for C and C++ headers,Calling Conventions,Mixed Language Programming
5867 @anchor{gnat_ugn/the_gnat_compilation_model id64}@anchor{b7}@anchor{gnat_ugn/the_gnat_compilation_model building-mixed-ada-and-c-programs}@anchor{b8}
5868 @subsection Building Mixed Ada and C++ Programs
5869
5870
5871 A programmer inexperienced with mixed-language development may find that
5872 building an application containing both Ada and C++ code can be a
5873 challenge. This section gives a few hints that should make this task easier.
5874
5875 @menu
5876 * Interfacing to C++::
5877 * Linking a Mixed C++ & Ada Program::
5878 * A Simple Example::
5879 * Interfacing with C++ constructors::
5880 * Interfacing with C++ at the Class Level::
5881
5882 @end menu
5883
5884 @node Interfacing to C++,Linking a Mixed C++ & Ada Program,,Building Mixed Ada and C++ Programs
5885 @anchor{gnat_ugn/the_gnat_compilation_model id65}@anchor{b9}@anchor{gnat_ugn/the_gnat_compilation_model id66}@anchor{ba}
5886 @subsubsection Interfacing to C++
5887
5888
5889 GNAT supports interfacing with the G++ compiler (or any C++ compiler
5890 generating code that is compatible with the G++ Application Binary
5891 Interface ---see @indicateurl{http://www.codesourcery.com/archives/cxx-abi}).
5892
5893 Interfacing can be done at 3 levels: simple data, subprograms, and
5894 classes. In the first two cases, GNAT offers a specific @code{Convention C_Plus_Plus}
5895 (or @code{CPP}) that behaves exactly like @code{Convention C}.
5896 Usually, C++ mangles the names of subprograms. To generate proper mangled
5897 names automatically, see @ref{19,,Generating Ada Bindings for C and C++ headers}).
5898 This problem can also be addressed manually in two ways:
5899
5900
5901 @itemize *
5902
5903 @item
5904 by modifying the C++ code in order to force a C convention using
5905 the @code{extern "C"} syntax.
5906
5907 @item
5908 by figuring out the mangled name (using e.g. @code{nm}) and using it as the
5909 Link_Name argument of the pragma import.
5910 @end itemize
5911
5912 Interfacing at the class level can be achieved by using the GNAT specific
5913 pragmas such as @code{CPP_Constructor}. See the @cite{GNAT_Reference_Manual} for additional information.
5914
5915 @node Linking a Mixed C++ & Ada Program,A Simple Example,Interfacing to C++,Building Mixed Ada and C++ Programs
5916 @anchor{gnat_ugn/the_gnat_compilation_model linking-a-mixed-c-ada-program}@anchor{bb}@anchor{gnat_ugn/the_gnat_compilation_model linking-a-mixed-c-and-ada-program}@anchor{bc}
5917 @subsubsection Linking a Mixed C++ & Ada Program
5918
5919
5920 Usually the linker of the C++ development system must be used to link
5921 mixed applications because most C++ systems will resolve elaboration
5922 issues (such as calling constructors on global class instances)
5923 transparently during the link phase. GNAT has been adapted to ease the
5924 use of a foreign linker for the last phase. Three cases can be
5925 considered:
5926
5927
5928 @itemize *
5929
5930 @item
5931 Using GNAT and G++ (GNU C++ compiler) from the same GCC installation:
5932 The C++ linker can simply be called by using the C++ specific driver
5933 called @code{g++}.
5934
5935 Note that if the C++ code uses inline functions, you will need to
5936 compile your C++ code with the @code{-fkeep-inline-functions} switch in
5937 order to provide an existing function implementation that the Ada code can
5938 link with.
5939
5940 @example
5941 $ g++ -c -fkeep-inline-functions file1.C
5942 $ g++ -c -fkeep-inline-functions file2.C
5943 $ gnatmake ada_unit -largs file1.o file2.o --LINK=g++
5944 @end example
5945
5946 @item
5947 Using GNAT and G++ from two different GCC installations: If both
5948 compilers are on the :envvar`PATH`, the previous method may be used. It is
5949 important to note that environment variables such as
5950 @geindex C_INCLUDE_PATH
5951 @geindex environment variable; C_INCLUDE_PATH
5952 @code{C_INCLUDE_PATH},
5953 @geindex GCC_EXEC_PREFIX
5954 @geindex environment variable; GCC_EXEC_PREFIX
5955 @code{GCC_EXEC_PREFIX},
5956 @geindex BINUTILS_ROOT
5957 @geindex environment variable; BINUTILS_ROOT
5958 @code{BINUTILS_ROOT}, and
5959 @geindex GCC_ROOT
5960 @geindex environment variable; GCC_ROOT
5961 @code{GCC_ROOT} will affect both compilers
5962 at the same time and may make one of the two compilers operate
5963 improperly if set during invocation of the wrong compiler. It is also
5964 very important that the linker uses the proper @code{libgcc.a} GCC
5965 library -- that is, the one from the C++ compiler installation. The
5966 implicit link command as suggested in the @code{gnatmake} command
5967 from the former example can be replaced by an explicit link command with
5968 the full-verbosity option in order to verify which library is used:
5969
5970 @example
5971 $ gnatbind ada_unit
5972 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=c++
5973 @end example
5974
5975 If there is a problem due to interfering environment variables, it can
5976 be worked around by using an intermediate script. The following example
5977 shows the proper script to use when GNAT has not been installed at its
5978 default location and g++ has been installed at its default location:
5979
5980 @example
5981 $ cat ./my_script
5982 #!/bin/sh
5983 unset BINUTILS_ROOT
5984 unset GCC_ROOT
5985 c++ $*
5986 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=./my_script
5987 @end example
5988
5989 @item
5990 Using a non-GNU C++ compiler: The commands previously described can be
5991 used to insure that the C++ linker is used. Nonetheless, you need to add
5992 a few more parameters to the link command line, depending on the exception
5993 mechanism used.
5994
5995 If the @code{setjmp} / @code{longjmp} exception mechanism is used, only the paths
5996 to the @code{libgcc} libraries are required:
5997
5998 @example
5999 $ cat ./my_script
6000 #!/bin/sh
6001 CC $* gcc -print-file-name=libgcc.a gcc -print-file-name=libgcc_eh.a
6002 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
6003 @end example
6004
6005 where CC is the name of the non-GNU C++ compiler.
6006
6007 If the "zero cost" exception mechanism is used, and the platform
6008 supports automatic registration of exception tables (e.g., Solaris),
6009 paths to more objects are required:
6010
6011 @example
6012 $ cat ./my_script
6013 #!/bin/sh
6014 CC gcc -print-file-name=crtbegin.o $* \\
6015 gcc -print-file-name=libgcc.a gcc -print-file-name=libgcc_eh.a \\
6016 gcc -print-file-name=crtend.o
6017 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
6018 @end example
6019
6020 If the "zero cost exception" mechanism is used, and the platform
6021 doesn't support automatic registration of exception tables (e.g., HP-UX
6022 or AIX), the simple approach described above will not work and
6023 a pre-linking phase using GNAT will be necessary.
6024 @end itemize
6025
6026 Another alternative is to use the @code{gprbuild} multi-language builder
6027 which has a large knowledge base and knows how to link Ada and C++ code
6028 together automatically in most cases.
6029
6030 @node A Simple Example,Interfacing with C++ constructors,Linking a Mixed C++ & Ada Program,Building Mixed Ada and C++ Programs
6031 @anchor{gnat_ugn/the_gnat_compilation_model id67}@anchor{bd}@anchor{gnat_ugn/the_gnat_compilation_model a-simple-example}@anchor{be}
6032 @subsubsection A Simple Example
6033
6034
6035 The following example, provided as part of the GNAT examples, shows how
6036 to achieve procedural interfacing between Ada and C++ in both
6037 directions. The C++ class A has two methods. The first method is exported
6038 to Ada by the means of an extern C wrapper function. The second method
6039 calls an Ada subprogram. On the Ada side, the C++ calls are modelled by
6040 a limited record with a layout comparable to the C++ class. The Ada
6041 subprogram, in turn, calls the C++ method. So, starting from the C++
6042 main program, the process passes back and forth between the two
6043 languages.
6044
6045 Here are the compilation commands:
6046
6047 @example
6048 $ gnatmake -c simple_cpp_interface
6049 $ g++ -c cpp_main.C
6050 $ g++ -c ex7.C
6051 $ gnatbind -n simple_cpp_interface
6052 $ gnatlink simple_cpp_interface -o cpp_main --LINK=g++ -lstdc++ ex7.o cpp_main.o
6053 @end example
6054
6055 Here are the corresponding sources:
6056
6057 @example
6058 //cpp_main.C
6059
6060 #include "ex7.h"
6061
6062 extern "C" @{
6063 void adainit (void);
6064 void adafinal (void);
6065 void method1 (A *t);
6066 @}
6067
6068 void method1 (A *t)
6069 @{
6070 t->method1 ();
6071 @}
6072
6073 int main ()
6074 @{
6075 A obj;
6076 adainit ();
6077 obj.method2 (3030);
6078 adafinal ();
6079 @}
6080 @end example
6081
6082 @example
6083 //ex7.h
6084
6085 class Origin @{
6086 public:
6087 int o_value;
6088 @};
6089 class A : public Origin @{
6090 public:
6091 void method1 (void);
6092 void method2 (int v);
6093 A();
6094 int a_value;
6095 @};
6096 @end example
6097
6098 @example
6099 //ex7.C
6100
6101 #include "ex7.h"
6102 #include <stdio.h>
6103
6104 extern "C" @{ void ada_method2 (A *t, int v);@}
6105
6106 void A::method1 (void)
6107 @{
6108 a_value = 2020;
6109 printf ("in A::method1, a_value = %d \\n",a_value);
6110 @}
6111
6112 void A::method2 (int v)
6113 @{
6114 ada_method2 (this, v);
6115 printf ("in A::method2, a_value = %d \\n",a_value);
6116 @}
6117
6118 A::A(void)
6119 @{
6120 a_value = 1010;
6121 printf ("in A::A, a_value = %d \\n",a_value);
6122 @}
6123 @end example
6124
6125 @example
6126 -- simple_cpp_interface.ads
6127 with System;
6128 package Simple_Cpp_Interface is
6129 type A is limited
6130 record
6131 Vptr : System.Address;
6132 O_Value : Integer;
6133 A_Value : Integer;
6134 end record;
6135 pragma Convention (C, A);
6136
6137 procedure Method1 (This : in out A);
6138 pragma Import (C, Method1);
6139
6140 procedure Ada_Method2 (This : in out A; V : Integer);
6141 pragma Export (C, Ada_Method2);
6142
6143 end Simple_Cpp_Interface;
6144 @end example
6145
6146 @example
6147 -- simple_cpp_interface.adb
6148 package body Simple_Cpp_Interface is
6149
6150 procedure Ada_Method2 (This : in out A; V : Integer) is
6151 begin
6152 Method1 (This);
6153 This.A_Value := V;
6154 end Ada_Method2;
6155
6156 end Simple_Cpp_Interface;
6157 @end example
6158
6159 @node Interfacing with C++ constructors,Interfacing with C++ at the Class Level,A Simple Example,Building Mixed Ada and C++ Programs
6160 @anchor{gnat_ugn/the_gnat_compilation_model id68}@anchor{bf}@anchor{gnat_ugn/the_gnat_compilation_model interfacing-with-c-constructors}@anchor{c0}
6161 @subsubsection Interfacing with C++ constructors
6162
6163
6164 In order to interface with C++ constructors GNAT provides the
6165 @code{pragma CPP_Constructor} (see the @cite{GNAT_Reference_Manual}
6166 for additional information).
6167 In this section we present some common uses of C++ constructors
6168 in mixed-languages programs in GNAT.
6169
6170 Let us assume that we need to interface with the following
6171 C++ class:
6172
6173 @example
6174 class Root @{
6175 public:
6176 int a_value;
6177 int b_value;
6178 virtual int Get_Value ();
6179 Root(); // Default constructor
6180 Root(int v); // 1st non-default constructor
6181 Root(int v, int w); // 2nd non-default constructor
6182 @};
6183 @end example
6184
6185 For this purpose we can write the following package spec (further
6186 information on how to build this spec is available in
6187 @ref{c1,,Interfacing with C++ at the Class Level} and
6188 @ref{19,,Generating Ada Bindings for C and C++ headers}).
6189
6190 @example
6191 with Interfaces.C; use Interfaces.C;
6192 package Pkg_Root is
6193 type Root is tagged limited record
6194 A_Value : int;
6195 B_Value : int;
6196 end record;
6197 pragma Import (CPP, Root);
6198
6199 function Get_Value (Obj : Root) return int;
6200 pragma Import (CPP, Get_Value);
6201
6202 function Constructor return Root;
6203 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ev");
6204
6205 function Constructor (v : Integer) return Root;
6206 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ei");
6207
6208 function Constructor (v, w : Integer) return Root;
6209 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Eii");
6210 end Pkg_Root;
6211 @end example
6212
6213 On the Ada side the constructor is represented by a function (whose
6214 name is arbitrary) that returns the classwide type corresponding to
6215 the imported C++ class. Although the constructor is described as a
6216 function, it is typically a procedure with an extra implicit argument
6217 (the object being initialized) at the implementation level. GNAT
6218 issues the appropriate call, whatever it is, to get the object
6219 properly initialized.
6220
6221 Constructors can only appear in the following contexts:
6222
6223
6224 @itemize *
6225
6226 @item
6227 On the right side of an initialization of an object of type @code{T}.
6228
6229 @item
6230 On the right side of an initialization of a record component of type @code{T}.
6231
6232 @item
6233 In an Ada 2005 limited aggregate.
6234
6235 @item
6236 In an Ada 2005 nested limited aggregate.
6237
6238 @item
6239 In an Ada 2005 limited aggregate that initializes an object built in
6240 place by an extended return statement.
6241 @end itemize
6242
6243 In a declaration of an object whose type is a class imported from C++,
6244 either the default C++ constructor is implicitly called by GNAT, or
6245 else the required C++ constructor must be explicitly called in the
6246 expression that initializes the object. For example:
6247
6248 @example
6249 Obj1 : Root;
6250 Obj2 : Root := Constructor;
6251 Obj3 : Root := Constructor (v => 10);
6252 Obj4 : Root := Constructor (30, 40);
6253 @end example
6254
6255 The first two declarations are equivalent: in both cases the default C++
6256 constructor is invoked (in the former case the call to the constructor is
6257 implicit, and in the latter case the call is explicit in the object
6258 declaration). @code{Obj3} is initialized by the C++ non-default constructor
6259 that takes an integer argument, and @code{Obj4} is initialized by the
6260 non-default C++ constructor that takes two integers.
6261
6262 Let us derive the imported C++ class in the Ada side. For example:
6263
6264 @example
6265 type DT is new Root with record
6266 C_Value : Natural := 2009;
6267 end record;
6268 @end example
6269
6270 In this case the components DT inherited from the C++ side must be
6271 initialized by a C++ constructor, and the additional Ada components
6272 of type DT are initialized by GNAT. The initialization of such an
6273 object is done either by default, or by means of a function returning
6274 an aggregate of type DT, or by means of an extension aggregate.
6275
6276 @example
6277 Obj5 : DT;
6278 Obj6 : DT := Function_Returning_DT (50);
6279 Obj7 : DT := (Constructor (30,40) with C_Value => 50);
6280 @end example
6281
6282 The declaration of @code{Obj5} invokes the default constructors: the
6283 C++ default constructor of the parent type takes care of the initialization
6284 of the components inherited from Root, and GNAT takes care of the default
6285 initialization of the additional Ada components of type DT (that is,
6286 @code{C_Value} is initialized to value 2009). The order of invocation of
6287 the constructors is consistent with the order of elaboration required by
6288 Ada and C++. That is, the constructor of the parent type is always called
6289 before the constructor of the derived type.
6290
6291 Let us now consider a record that has components whose type is imported
6292 from C++. For example:
6293
6294 @example
6295 type Rec1 is limited record
6296 Data1 : Root := Constructor (10);
6297 Value : Natural := 1000;
6298 end record;
6299
6300 type Rec2 (D : Integer := 20) is limited record
6301 Rec : Rec1;
6302 Data2 : Root := Constructor (D, 30);
6303 end record;
6304 @end example
6305
6306 The initialization of an object of type @code{Rec2} will call the
6307 non-default C++ constructors specified for the imported components.
6308 For example:
6309
6310 @example
6311 Obj8 : Rec2 (40);
6312 @end example
6313
6314 Using Ada 2005 we can use limited aggregates to initialize an object
6315 invoking C++ constructors that differ from those specified in the type
6316 declarations. For example:
6317
6318 @example
6319 Obj9 : Rec2 := (Rec => (Data1 => Constructor (15, 16),
6320 others => <>),
6321 others => <>);
6322 @end example
6323
6324 The above declaration uses an Ada 2005 limited aggregate to
6325 initialize @code{Obj9}, and the C++ constructor that has two integer
6326 arguments is invoked to initialize the @code{Data1} component instead
6327 of the constructor specified in the declaration of type @code{Rec1}. In
6328 Ada 2005 the box in the aggregate indicates that unspecified components
6329 are initialized using the expression (if any) available in the component
6330 declaration. That is, in this case discriminant @code{D} is initialized
6331 to value @code{20}, @code{Value} is initialized to value 1000, and the
6332 non-default C++ constructor that handles two integers takes care of
6333 initializing component @code{Data2} with values @code{20,30}.
6334
6335 In Ada 2005 we can use the extended return statement to build the Ada
6336 equivalent to C++ non-default constructors. For example:
6337
6338 @example
6339 function Constructor (V : Integer) return Rec2 is
6340 begin
6341 return Obj : Rec2 := (Rec => (Data1 => Constructor (V, 20),
6342 others => <>),
6343 others => <>) do
6344 -- Further actions required for construction of
6345 -- objects of type Rec2
6346 ...
6347 end record;
6348 end Constructor;
6349 @end example
6350
6351 In this example the extended return statement construct is used to
6352 build in place the returned object whose components are initialized
6353 by means of a limited aggregate. Any further action associated with
6354 the constructor can be placed inside the construct.
6355
6356 @node Interfacing with C++ at the Class Level,,Interfacing with C++ constructors,Building Mixed Ada and C++ Programs
6357 @anchor{gnat_ugn/the_gnat_compilation_model interfacing-with-c-at-the-class-level}@anchor{c1}@anchor{gnat_ugn/the_gnat_compilation_model id69}@anchor{c2}
6358 @subsubsection Interfacing with C++ at the Class Level
6359
6360
6361 In this section we demonstrate the GNAT features for interfacing with
6362 C++ by means of an example making use of Ada 2005 abstract interface
6363 types. This example consists of a classification of animals; classes
6364 have been used to model our main classification of animals, and
6365 interfaces provide support for the management of secondary
6366 classifications. We first demonstrate a case in which the types and
6367 constructors are defined on the C++ side and imported from the Ada
6368 side, and latter the reverse case.
6369
6370 The root of our derivation will be the @code{Animal} class, with a
6371 single private attribute (the @code{Age} of the animal), a constructor,
6372 and two public primitives to set and get the value of this attribute.
6373
6374 @example
6375 class Animal @{
6376 public:
6377 virtual void Set_Age (int New_Age);
6378 virtual int Age ();
6379 Animal() @{Age_Count = 0;@};
6380 private:
6381 int Age_Count;
6382 @};
6383 @end example
6384
6385 Abstract interface types are defined in C++ by means of classes with pure
6386 virtual functions and no data members. In our example we will use two
6387 interfaces that provide support for the common management of @code{Carnivore}
6388 and @code{Domestic} animals:
6389
6390 @example
6391 class Carnivore @{
6392 public:
6393 virtual int Number_Of_Teeth () = 0;
6394 @};
6395
6396 class Domestic @{
6397 public:
6398 virtual void Set_Owner (char* Name) = 0;
6399 @};
6400 @end example
6401
6402 Using these declarations, we can now say that a @code{Dog} is an animal that is
6403 both Carnivore and Domestic, that is:
6404
6405 @example
6406 class Dog : Animal, Carnivore, Domestic @{
6407 public:
6408 virtual int Number_Of_Teeth ();
6409 virtual void Set_Owner (char* Name);
6410
6411 Dog(); // Constructor
6412 private:
6413 int Tooth_Count;
6414 char *Owner;
6415 @};
6416 @end example
6417
6418 In the following examples we will assume that the previous declarations are
6419 located in a file named @code{animals.h}. The following package demonstrates
6420 how to import these C++ declarations from the Ada side:
6421
6422 @example
6423 with Interfaces.C.Strings; use Interfaces.C.Strings;
6424 package Animals is
6425 type Carnivore is limited interface;
6426 pragma Convention (C_Plus_Plus, Carnivore);
6427 function Number_Of_Teeth (X : Carnivore)
6428 return Natural is abstract;
6429
6430 type Domestic is limited interface;
6431 pragma Convention (C_Plus_Plus, Domestic);
6432 procedure Set_Owner
6433 (X : in out Domestic;
6434 Name : Chars_Ptr) is abstract;
6435
6436 type Animal is tagged limited record
6437 Age : Natural;
6438 end record;
6439 pragma Import (C_Plus_Plus, Animal);
6440
6441 procedure Set_Age (X : in out Animal; Age : Integer);
6442 pragma Import (C_Plus_Plus, Set_Age);
6443
6444 function Age (X : Animal) return Integer;
6445 pragma Import (C_Plus_Plus, Age);
6446
6447 function New_Animal return Animal;
6448 pragma CPP_Constructor (New_Animal);
6449 pragma Import (CPP, New_Animal, "_ZN6AnimalC1Ev");
6450
6451 type Dog is new Animal and Carnivore and Domestic with record
6452 Tooth_Count : Natural;
6453 Owner : Chars_Ptr;
6454 end record;
6455 pragma Import (C_Plus_Plus, Dog);
6456
6457 function Number_Of_Teeth (A : Dog) return Natural;
6458 pragma Import (C_Plus_Plus, Number_Of_Teeth);
6459
6460 procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
6461 pragma Import (C_Plus_Plus, Set_Owner);
6462
6463 function New_Dog return Dog;
6464 pragma CPP_Constructor (New_Dog);
6465 pragma Import (CPP, New_Dog, "_ZN3DogC2Ev");
6466 end Animals;
6467 @end example
6468
6469 Thanks to the compatibility between GNAT run-time structures and the C++ ABI,
6470 interfacing with these C++ classes is easy. The only requirement is that all
6471 the primitives and components must be declared exactly in the same order in
6472 the two languages.
6473
6474 Regarding the abstract interfaces, we must indicate to the GNAT compiler by
6475 means of a @code{pragma Convention (C_Plus_Plus)}, the convention used to pass
6476 the arguments to the called primitives will be the same as for C++. For the
6477 imported classes we use @code{pragma Import} with convention @code{C_Plus_Plus}
6478 to indicate that they have been defined on the C++ side; this is required
6479 because the dispatch table associated with these tagged types will be built
6480 in the C++ side and therefore will not contain the predefined Ada primitives
6481 which Ada would otherwise expect.
6482
6483 As the reader can see there is no need to indicate the C++ mangled names
6484 associated with each subprogram because it is assumed that all the calls to
6485 these primitives will be dispatching calls. The only exception is the
6486 constructor, which must be registered with the compiler by means of
6487 @code{pragma CPP_Constructor} and needs to provide its associated C++
6488 mangled name because the Ada compiler generates direct calls to it.
6489
6490 With the above packages we can now declare objects of type Dog on the Ada side
6491 and dispatch calls to the corresponding subprograms on the C++ side. We can
6492 also extend the tagged type Dog with further fields and primitives, and
6493 override some of its C++ primitives on the Ada side. For example, here we have
6494 a type derivation defined on the Ada side that inherits all the dispatching
6495 primitives of the ancestor from the C++ side.
6496
6497 @example
6498 with Animals; use Animals;
6499 package Vaccinated_Animals is
6500 type Vaccinated_Dog is new Dog with null record;
6501 function Vaccination_Expired (A : Vaccinated_Dog) return Boolean;
6502 end Vaccinated_Animals;
6503 @end example
6504
6505 It is important to note that, because of the ABI compatibility, the programmer
6506 does not need to add any further information to indicate either the object
6507 layout or the dispatch table entry associated with each dispatching operation.
6508
6509 Now let us define all the types and constructors on the Ada side and export
6510 them to C++, using the same hierarchy of our previous example:
6511
6512 @example
6513 with Interfaces.C.Strings;
6514 use Interfaces.C.Strings;
6515 package Animals is
6516 type Carnivore is limited interface;
6517 pragma Convention (C_Plus_Plus, Carnivore);
6518 function Number_Of_Teeth (X : Carnivore)
6519 return Natural is abstract;
6520
6521 type Domestic is limited interface;
6522 pragma Convention (C_Plus_Plus, Domestic);
6523 procedure Set_Owner
6524 (X : in out Domestic;
6525 Name : Chars_Ptr) is abstract;
6526
6527 type Animal is tagged record
6528 Age : Natural;
6529 end record;
6530 pragma Convention (C_Plus_Plus, Animal);
6531
6532 procedure Set_Age (X : in out Animal; Age : Integer);
6533 pragma Export (C_Plus_Plus, Set_Age);
6534
6535 function Age (X : Animal) return Integer;
6536 pragma Export (C_Plus_Plus, Age);
6537
6538 function New_Animal return Animal'Class;
6539 pragma Export (C_Plus_Plus, New_Animal);
6540
6541 type Dog is new Animal and Carnivore and Domestic with record
6542 Tooth_Count : Natural;
6543 Owner : String (1 .. 30);
6544 end record;
6545 pragma Convention (C_Plus_Plus, Dog);
6546
6547 function Number_Of_Teeth (A : Dog) return Natural;
6548 pragma Export (C_Plus_Plus, Number_Of_Teeth);
6549
6550 procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
6551 pragma Export (C_Plus_Plus, Set_Owner);
6552
6553 function New_Dog return Dog'Class;
6554 pragma Export (C_Plus_Plus, New_Dog);
6555 end Animals;
6556 @end example
6557
6558 Compared with our previous example the only differences are the use of
6559 @code{pragma Convention} (instead of @code{pragma Import}), and the use of
6560 @code{pragma Export} to indicate to the GNAT compiler that the primitives will
6561 be available to C++. Thanks to the ABI compatibility, on the C++ side there is
6562 nothing else to be done; as explained above, the only requirement is that all
6563 the primitives and components are declared in exactly the same order.
6564
6565 For completeness, let us see a brief C++ main program that uses the
6566 declarations available in @code{animals.h} (presented in our first example) to
6567 import and use the declarations from the Ada side, properly initializing and
6568 finalizing the Ada run-time system along the way:
6569
6570 @example
6571 #include "animals.h"
6572 #include <iostream>
6573 using namespace std;
6574
6575 void Check_Carnivore (Carnivore *obj) @{...@}
6576 void Check_Domestic (Domestic *obj) @{...@}
6577 void Check_Animal (Animal *obj) @{...@}
6578 void Check_Dog (Dog *obj) @{...@}
6579
6580 extern "C" @{
6581 void adainit (void);
6582 void adafinal (void);
6583 Dog* new_dog ();
6584 @}
6585
6586 void test ()
6587 @{
6588 Dog *obj = new_dog(); // Ada constructor
6589 Check_Carnivore (obj); // Check secondary DT
6590 Check_Domestic (obj); // Check secondary DT
6591 Check_Animal (obj); // Check primary DT
6592 Check_Dog (obj); // Check primary DT
6593 @}
6594
6595 int main ()
6596 @{
6597 adainit (); test(); adafinal ();
6598 return 0;
6599 @}
6600 @end example
6601
6602 @node Generating Ada Bindings for C and C++ headers,Generating C Headers for Ada Specifications,Building Mixed Ada and C++ Programs,Mixed Language Programming
6603 @anchor{gnat_ugn/the_gnat_compilation_model id70}@anchor{c3}@anchor{gnat_ugn/the_gnat_compilation_model generating-ada-bindings-for-c-and-c-headers}@anchor{19}
6604 @subsection Generating Ada Bindings for C and C++ headers
6605
6606
6607 @geindex Binding generation (for C and C++ headers)
6608
6609 @geindex C headers (binding generation)
6610
6611 @geindex C++ headers (binding generation)
6612
6613 GNAT includes a binding generator for C and C++ headers which is
6614 intended to do 95% of the tedious work of generating Ada specs from C
6615 or C++ header files.
6616
6617 Note that this capability is not intended to generate 100% correct Ada specs,
6618 and will is some cases require manual adjustments, although it can often
6619 be used out of the box in practice.
6620
6621 Some of the known limitations include:
6622
6623
6624 @itemize *
6625
6626 @item
6627 only very simple character constant macros are translated into Ada
6628 constants. Function macros (macros with arguments) are partially translated
6629 as comments, to be completed manually if needed.
6630
6631 @item
6632 some extensions (e.g. vector types) are not supported
6633
6634 @item
6635 pointers to pointers or complex structures are mapped to System.Address
6636
6637 @item
6638 identifiers with identical name (except casing) will generate compilation
6639 errors (e.g. @code{shm_get} vs @code{SHM_GET}).
6640 @end itemize
6641
6642 The code is generated using Ada 2012 syntax, which makes it easier to interface
6643 with other languages. In most cases you can still use the generated binding
6644 even if your code is compiled using earlier versions of Ada (e.g. @code{-gnat95}).
6645
6646 @menu
6647 * Running the Binding Generator::
6648 * Generating Bindings for C++ Headers::
6649 * Switches::
6650
6651 @end menu
6652
6653 @node Running the Binding Generator,Generating Bindings for C++ Headers,,Generating Ada Bindings for C and C++ headers
6654 @anchor{gnat_ugn/the_gnat_compilation_model id71}@anchor{c4}@anchor{gnat_ugn/the_gnat_compilation_model running-the-binding-generator}@anchor{c5}
6655 @subsubsection Running the Binding Generator
6656
6657
6658 The binding generator is part of the @code{gcc} compiler and can be
6659 invoked via the @code{-fdump-ada-spec} switch, which will generate Ada
6660 spec files for the header files specified on the command line, and all
6661 header files needed by these files transitively. For example:
6662
6663 @example
6664 $ g++ -c -fdump-ada-spec -C /usr/include/time.h
6665 $ gcc -c *.ads
6666 @end example
6667
6668 will generate, under GNU/Linux, the following files: @code{time_h.ads},
6669 @code{bits_time_h.ads}, @code{stddef_h.ads}, @code{bits_types_h.ads} which
6670 correspond to the files @code{/usr/include/time.h},
6671 @code{/usr/include/bits/time.h}, etc..., and will then compile these Ada specs
6672 in Ada 2005 mode.
6673
6674 The @code{-C} switch tells @code{gcc} to extract comments from headers,
6675 and will attempt to generate corresponding Ada comments.
6676
6677 If you want to generate a single Ada file and not the transitive closure, you
6678 can use instead the @code{-fdump-ada-spec-slim} switch.
6679
6680 You can optionally specify a parent unit, of which all generated units will
6681 be children, using @code{-fada-spec-parent=@emph{unit}}.
6682
6683 Note that we recommend when possible to use the @emph{g++} driver to
6684 generate bindings, even for most C headers, since this will in general
6685 generate better Ada specs. For generating bindings for C++ headers, it is
6686 mandatory to use the @emph{g++} command, or @emph{gcc -x c++} which
6687 is equivalent in this case. If @emph{g++} cannot work on your C headers
6688 because of incompatibilities between C and C++, then you can fallback to
6689 @code{gcc} instead.
6690
6691 For an example of better bindings generated from the C++ front-end,
6692 the name of the parameters (when available) are actually ignored by the C
6693 front-end. Consider the following C header:
6694
6695 @example
6696 extern void foo (int variable);
6697 @end example
6698
6699 with the C front-end, @code{variable} is ignored, and the above is handled as:
6700
6701 @example
6702 extern void foo (int);
6703 @end example
6704
6705 generating a generic:
6706
6707 @example
6708 procedure foo (param1 : int);
6709 @end example
6710
6711 with the C++ front-end, the name is available, and we generate:
6712
6713 @example
6714 procedure foo (variable : int);
6715 @end example
6716
6717 In some cases, the generated bindings will be more complete or more meaningful
6718 when defining some macros, which you can do via the @code{-D} switch. This
6719 is for example the case with @code{Xlib.h} under GNU/Linux:
6720
6721 @example
6722 $ g++ -c -fdump-ada-spec -DXLIB_ILLEGAL_ACCESS -C /usr/include/X11/Xlib.h
6723 @end example
6724
6725 The above will generate more complete bindings than a straight call without
6726 the @code{-DXLIB_ILLEGAL_ACCESS} switch.
6727
6728 In other cases, it is not possible to parse a header file in a stand-alone
6729 manner, because other include files need to be included first. In this
6730 case, the solution is to create a small header file including the needed
6731 @code{#include} and possible @code{#define} directives. For example, to
6732 generate Ada bindings for @code{readline/readline.h}, you need to first
6733 include @code{stdio.h}, so you can create a file with the following two
6734 lines in e.g. @code{readline1.h}:
6735
6736 @example
6737 #include <stdio.h>
6738 #include <readline/readline.h>
6739 @end example
6740
6741 and then generate Ada bindings from this file:
6742
6743 @example
6744 $ g++ -c -fdump-ada-spec readline1.h
6745 @end example
6746
6747 @node Generating Bindings for C++ Headers,Switches,Running the Binding Generator,Generating Ada Bindings for C and C++ headers
6748 @anchor{gnat_ugn/the_gnat_compilation_model id72}@anchor{c6}@anchor{gnat_ugn/the_gnat_compilation_model generating-bindings-for-c-headers}@anchor{c7}
6749 @subsubsection Generating Bindings for C++ Headers
6750
6751
6752 Generating bindings for C++ headers is done using the same options, always
6753 with the @emph{g++} compiler. Note that generating Ada spec from C++ headers is a
6754 much more complex job and support for C++ headers is much more limited that
6755 support for C headers. As a result, you will need to modify the resulting
6756 bindings by hand more extensively when using C++ headers.
6757
6758 In this mode, C++ classes will be mapped to Ada tagged types, constructors
6759 will be mapped using the @code{CPP_Constructor} pragma, and when possible,
6760 multiple inheritance of abstract classes will be mapped to Ada interfaces
6761 (see the @emph{Interfacing to C++} section in the @cite{GNAT Reference Manual}
6762 for additional information on interfacing to C++).
6763
6764 For example, given the following C++ header file:
6765
6766 @example
6767 class Carnivore @{
6768 public:
6769 virtual int Number_Of_Teeth () = 0;
6770 @};
6771
6772 class Domestic @{
6773 public:
6774 virtual void Set_Owner (char* Name) = 0;
6775 @};
6776
6777 class Animal @{
6778 public:
6779 int Age_Count;
6780 virtual void Set_Age (int New_Age);
6781 @};
6782
6783 class Dog : Animal, Carnivore, Domestic @{
6784 public:
6785 int Tooth_Count;
6786 char *Owner;
6787
6788 virtual int Number_Of_Teeth ();
6789 virtual void Set_Owner (char* Name);
6790
6791 Dog();
6792 @};
6793 @end example
6794
6795 The corresponding Ada code is generated:
6796
6797 @example
6798 package Class_Carnivore is
6799 type Carnivore is limited interface;
6800 pragma Import (CPP, Carnivore);
6801
6802 function Number_Of_Teeth (this : access Carnivore) return int is abstract;
6803 end;
6804 use Class_Carnivore;
6805
6806 package Class_Domestic is
6807 type Domestic is limited interface;
6808 pragma Import (CPP, Domestic);
6809
6810 procedure Set_Owner
6811 (this : access Domestic;
6812 Name : Interfaces.C.Strings.chars_ptr) is abstract;
6813 end;
6814 use Class_Domestic;
6815
6816 package Class_Animal is
6817 type Animal is tagged limited record
6818 Age_Count : aliased int;
6819 end record;
6820 pragma Import (CPP, Animal);
6821
6822 procedure Set_Age (this : access Animal; New_Age : int);
6823 pragma Import (CPP, Set_Age, "_ZN6Animal7Set_AgeEi");
6824 end;
6825 use Class_Animal;
6826
6827 package Class_Dog is
6828 type Dog is new Animal and Carnivore and Domestic with record
6829 Tooth_Count : aliased int;
6830 Owner : Interfaces.C.Strings.chars_ptr;
6831 end record;
6832 pragma Import (CPP, Dog);
6833
6834 function Number_Of_Teeth (this : access Dog) return int;
6835 pragma Import (CPP, Number_Of_Teeth, "_ZN3Dog15Number_Of_TeethEv");
6836
6837 procedure Set_Owner
6838 (this : access Dog; Name : Interfaces.C.Strings.chars_ptr);
6839 pragma Import (CPP, Set_Owner, "_ZN3Dog9Set_OwnerEPc");
6840
6841 function New_Dog return Dog;
6842 pragma CPP_Constructor (New_Dog);
6843 pragma Import (CPP, New_Dog, "_ZN3DogC1Ev");
6844 end;
6845 use Class_Dog;
6846 @end example
6847
6848 @node Switches,,Generating Bindings for C++ Headers,Generating Ada Bindings for C and C++ headers
6849 @anchor{gnat_ugn/the_gnat_compilation_model switches}@anchor{c8}@anchor{gnat_ugn/the_gnat_compilation_model switches-for-ada-binding-generation}@anchor{c9}
6850 @subsubsection Switches
6851
6852
6853 @geindex -fdump-ada-spec (gcc)
6854
6855
6856 @table @asis
6857
6858 @item @code{-fdump-ada-spec}
6859
6860 Generate Ada spec files for the given header files transitively (including
6861 all header files that these headers depend upon).
6862 @end table
6863
6864 @geindex -fdump-ada-spec-slim (gcc)
6865
6866
6867 @table @asis
6868
6869 @item @code{-fdump-ada-spec-slim}
6870
6871 Generate Ada spec files for the header files specified on the command line
6872 only.
6873 @end table
6874
6875 @geindex -fada-spec-parent (gcc)
6876
6877
6878 @table @asis
6879
6880 @item @code{-fada-spec-parent=@emph{unit}}
6881
6882 Specifies that all files generated by @code{-fdump-ada-spec} are
6883 to be child units of the specified parent unit.
6884 @end table
6885
6886 @geindex -C (gcc)
6887
6888
6889 @table @asis
6890
6891 @item @code{-C}
6892
6893 Extract comments from headers and generate Ada comments in the Ada spec files.
6894 @end table
6895
6896 @node Generating C Headers for Ada Specifications,,Generating Ada Bindings for C and C++ headers,Mixed Language Programming
6897 @anchor{gnat_ugn/the_gnat_compilation_model generating-c-headers-for-ada-specifications}@anchor{ca}@anchor{gnat_ugn/the_gnat_compilation_model id73}@anchor{cb}
6898 @subsection Generating C Headers for Ada Specifications
6899
6900
6901 @geindex Binding generation (for Ada specs)
6902
6903 @geindex C headers (binding generation)
6904
6905 GNAT includes a C header generator for Ada specifications which supports
6906 Ada types that have a direct mapping to C types. This includes in particular
6907 support for:
6908
6909
6910 @itemize *
6911
6912 @item
6913 Scalar types
6914
6915 @item
6916 Constrained arrays
6917
6918 @item
6919 Records (untagged)
6920
6921 @item
6922 Composition of the above types
6923
6924 @item
6925 Constant declarations
6926
6927 @item
6928 Object declarations
6929
6930 @item
6931 Subprogram declarations
6932 @end itemize
6933
6934 @menu
6935 * Running the C Header Generator::
6936
6937 @end menu
6938
6939 @node Running the C Header Generator,,,Generating C Headers for Ada Specifications
6940 @anchor{gnat_ugn/the_gnat_compilation_model running-the-c-header-generator}@anchor{cc}
6941 @subsubsection Running the C Header Generator
6942
6943
6944 The C header generator is part of the GNAT compiler and can be invoked via
6945 the @code{-gnatceg} combination of switches, which will generate a @code{.h}
6946 file corresponding to the given input file (Ada spec or body). Note that
6947 only spec files are processed in any case, so giving a spec or a body file
6948 as input is equivalent. For example:
6949
6950 @example
6951 $ gcc -c -gnatceg pack1.ads
6952 @end example
6953
6954 will generate a self-contained file called @code{pack1.h} including
6955 common definitions from the Ada Standard package, followed by the
6956 definitions included in @code{pack1.ads}, as well as all the other units
6957 withed by this file.
6958
6959 For instance, given the following Ada files:
6960
6961 @example
6962 package Pack2 is
6963 type Int is range 1 .. 10;
6964 end Pack2;
6965 @end example
6966
6967 @example
6968 with Pack2;
6969
6970 package Pack1 is
6971 type Rec is record
6972 Field1, Field2 : Pack2.Int;
6973 end record;
6974
6975 Global : Rec := (1, 2);
6976
6977 procedure Proc1 (R : Rec);
6978 procedure Proc2 (R : in out Rec);
6979 end Pack1;
6980 @end example
6981
6982 The above @code{gcc} command will generate the following @code{pack1.h} file:
6983
6984 @example
6985 /* Standard definitions skipped */
6986 #ifndef PACK2_ADS
6987 #define PACK2_ADS
6988 typedef short_short_integer pack2__TintB;
6989 typedef pack2__TintB pack2__int;
6990 #endif /* PACK2_ADS */
6991
6992 #ifndef PACK1_ADS
6993 #define PACK1_ADS
6994 typedef struct _pack1__rec @{
6995 pack2__int field1;
6996 pack2__int field2;
6997 @} pack1__rec;
6998 extern pack1__rec pack1__global;
6999 extern void pack1__proc1(const pack1__rec r);
7000 extern void pack1__proc2(pack1__rec *r);
7001 #endif /* PACK1_ADS */
7002 @end example
7003
7004 You can then @code{include} @code{pack1.h} from a C source file and use the types,
7005 call subprograms, reference objects, and constants.
7006
7007 @node GNAT and Other Compilation Models,Using GNAT Files with External Tools,Mixed Language Programming,The GNAT Compilation Model
7008 @anchor{gnat_ugn/the_gnat_compilation_model id74}@anchor{cd}@anchor{gnat_ugn/the_gnat_compilation_model gnat-and-other-compilation-models}@anchor{45}
7009 @section GNAT and Other Compilation Models
7010
7011
7012 This section compares the GNAT model with the approaches taken in
7013 other environents, first the C/C++ model and then the mechanism that
7014 has been used in other Ada systems, in particular those traditionally
7015 used for Ada 83.
7016
7017 @menu
7018 * Comparison between GNAT and C/C++ Compilation Models::
7019 * Comparison between GNAT and Conventional Ada Library Models::
7020
7021 @end menu
7022
7023 @node Comparison between GNAT and C/C++ Compilation Models,Comparison between GNAT and Conventional Ada Library Models,,GNAT and Other Compilation Models
7024 @anchor{gnat_ugn/the_gnat_compilation_model comparison-between-gnat-and-c-c-compilation-models}@anchor{ce}@anchor{gnat_ugn/the_gnat_compilation_model id75}@anchor{cf}
7025 @subsection Comparison between GNAT and C/C++ Compilation Models
7026
7027
7028 The GNAT model of compilation is close to the C and C++ models. You can
7029 think of Ada specs as corresponding to header files in C. As in C, you
7030 don't need to compile specs; they are compiled when they are used. The
7031 Ada @emph{with} is similar in effect to the @code{#include} of a C
7032 header.
7033
7034 One notable difference is that, in Ada, you may compile specs separately
7035 to check them for semantic and syntactic accuracy. This is not always
7036 possible with C headers because they are fragments of programs that have
7037 less specific syntactic or semantic rules.
7038
7039 The other major difference is the requirement for running the binder,
7040 which performs two important functions. First, it checks for
7041 consistency. In C or C++, the only defense against assembling
7042 inconsistent programs lies outside the compiler, in a makefile, for
7043 example. The binder satisfies the Ada requirement that it be impossible
7044 to construct an inconsistent program when the compiler is used in normal
7045 mode.
7046
7047 @geindex Elaboration order control
7048
7049 The other important function of the binder is to deal with elaboration
7050 issues. There are also elaboration issues in C++ that are handled
7051 automatically. This automatic handling has the advantage of being
7052 simpler to use, but the C++ programmer has no control over elaboration.
7053 Where @code{gnatbind} might complain there was no valid order of
7054 elaboration, a C++ compiler would simply construct a program that
7055 malfunctioned at run time.
7056
7057 @node Comparison between GNAT and Conventional Ada Library Models,,Comparison between GNAT and C/C++ Compilation Models,GNAT and Other Compilation Models
7058 @anchor{gnat_ugn/the_gnat_compilation_model comparison-between-gnat-and-conventional-ada-library-models}@anchor{d0}@anchor{gnat_ugn/the_gnat_compilation_model id76}@anchor{d1}
7059 @subsection Comparison between GNAT and Conventional Ada Library Models
7060
7061
7062 This section is intended for Ada programmers who have
7063 used an Ada compiler implementing the traditional Ada library
7064 model, as described in the Ada Reference Manual.
7065
7066 @geindex GNAT library
7067
7068 In GNAT, there is no 'library' in the normal sense. Instead, the set of
7069 source files themselves acts as the library. Compiling Ada programs does
7070 not generate any centralized information, but rather an object file and
7071 a ALI file, which are of interest only to the binder and linker.
7072 In a traditional system, the compiler reads information not only from
7073 the source file being compiled, but also from the centralized library.
7074 This means that the effect of a compilation depends on what has been
7075 previously compiled. In particular:
7076
7077
7078 @itemize *
7079
7080 @item
7081 When a unit is @emph{with}ed, the unit seen by the compiler corresponds
7082 to the version of the unit most recently compiled into the library.
7083
7084 @item
7085 Inlining is effective only if the necessary body has already been
7086 compiled into the library.
7087
7088 @item
7089 Compiling a unit may obsolete other units in the library.
7090 @end itemize
7091
7092 In GNAT, compiling one unit never affects the compilation of any other
7093 units because the compiler reads only source files. Only changes to source
7094 files can affect the results of a compilation. In particular:
7095
7096
7097 @itemize *
7098
7099 @item
7100 When a unit is @emph{with}ed, the unit seen by the compiler corresponds
7101 to the source version of the unit that is currently accessible to the
7102 compiler.
7103
7104 @geindex Inlining
7105
7106 @item
7107 Inlining requires the appropriate source files for the package or
7108 subprogram bodies to be available to the compiler. Inlining is always
7109 effective, independent of the order in which units are compiled.
7110
7111 @item
7112 Compiling a unit never affects any other compilations. The editing of
7113 sources may cause previous compilations to be out of date if they
7114 depended on the source file being modified.
7115 @end itemize
7116
7117 The most important result of these differences is that order of compilation
7118 is never significant in GNAT. There is no situation in which one is
7119 required to do one compilation before another. What shows up as order of
7120 compilation requirements in the traditional Ada library becomes, in
7121 GNAT, simple source dependencies; in other words, there is only a set
7122 of rules saying what source files must be present when a file is
7123 compiled.
7124
7125 @node Using GNAT Files with External Tools,,GNAT and Other Compilation Models,The GNAT Compilation Model
7126 @anchor{gnat_ugn/the_gnat_compilation_model using-gnat-files-with-external-tools}@anchor{1a}@anchor{gnat_ugn/the_gnat_compilation_model id77}@anchor{d2}
7127 @section Using GNAT Files with External Tools
7128
7129
7130 This section explains how files that are produced by GNAT may be
7131 used with tools designed for other languages.
7132
7133 @menu
7134 * Using Other Utility Programs with GNAT::
7135 * The External Symbol Naming Scheme of GNAT::
7136
7137 @end menu
7138
7139 @node Using Other Utility Programs with GNAT,The External Symbol Naming Scheme of GNAT,,Using GNAT Files with External Tools
7140 @anchor{gnat_ugn/the_gnat_compilation_model using-other-utility-programs-with-gnat}@anchor{d3}@anchor{gnat_ugn/the_gnat_compilation_model id78}@anchor{d4}
7141 @subsection Using Other Utility Programs with GNAT
7142
7143
7144 The object files generated by GNAT are in standard system format and in
7145 particular the debugging information uses this format. This means
7146 programs generated by GNAT can be used with existing utilities that
7147 depend on these formats.
7148
7149 In general, any utility program that works with C will also often work with
7150 Ada programs generated by GNAT. This includes software utilities such as
7151 gprof (a profiling program), gdb (the FSF debugger), and utilities such
7152 as Purify.
7153
7154 @node The External Symbol Naming Scheme of GNAT,,Using Other Utility Programs with GNAT,Using GNAT Files with External Tools
7155 @anchor{gnat_ugn/the_gnat_compilation_model the-external-symbol-naming-scheme-of-gnat}@anchor{d5}@anchor{gnat_ugn/the_gnat_compilation_model id79}@anchor{d6}
7156 @subsection The External Symbol Naming Scheme of GNAT
7157
7158
7159 In order to interpret the output from GNAT, when using tools that are
7160 originally intended for use with other languages, it is useful to
7161 understand the conventions used to generate link names from the Ada
7162 entity names.
7163
7164 All link names are in all lowercase letters. With the exception of library
7165 procedure names, the mechanism used is simply to use the full expanded
7166 Ada name with dots replaced by double underscores. For example, suppose
7167 we have the following package spec:
7168
7169 @example
7170 package QRS is
7171 MN : Integer;
7172 end QRS;
7173 @end example
7174
7175 @geindex pragma Export
7176
7177 The variable @code{MN} has a full expanded Ada name of @code{QRS.MN}, so
7178 the corresponding link name is @code{qrs__mn}.
7179 Of course if a @code{pragma Export} is used this may be overridden:
7180
7181 @example
7182 package Exports is
7183 Var1 : Integer;
7184 pragma Export (Var1, C, External_Name => "var1_name");
7185 Var2 : Integer;
7186 pragma Export (Var2, C, Link_Name => "var2_link_name");
7187 end Exports;
7188 @end example
7189
7190 In this case, the link name for @code{Var1} is whatever link name the
7191 C compiler would assign for the C function @code{var1_name}. This typically
7192 would be either @code{var1_name} or @code{_var1_name}, depending on operating
7193 system conventions, but other possibilities exist. The link name for
7194 @code{Var2} is @code{var2_link_name}, and this is not operating system
7195 dependent.
7196
7197 One exception occurs for library level procedures. A potential ambiguity
7198 arises between the required name @code{_main} for the C main program,
7199 and the name we would otherwise assign to an Ada library level procedure
7200 called @code{Main} (which might well not be the main program).
7201
7202 To avoid this ambiguity, we attach the prefix @code{_ada_} to such
7203 names. So if we have a library level procedure such as:
7204
7205 @example
7206 procedure Hello (S : String);
7207 @end example
7208
7209 the external name of this procedure will be @code{_ada_hello}.
7210
7211 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
7212
7213 @node Building Executable Programs with GNAT,GNAT Utility Programs,The GNAT Compilation Model,Top
7214 @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{d7}@anchor{gnat_ugn/building_executable_programs_with_gnat id1}@anchor{d8}
7215 @chapter Building Executable Programs with GNAT
7216
7217
7218 This chapter describes first the gnatmake tool
7219 (@ref{1b,,Building with gnatmake}),
7220 which automatically determines the set of sources
7221 needed by an Ada compilation unit and executes the necessary
7222 (re)compilations, binding and linking.
7223 It also explains how to use each tool individually: the
7224 compiler (gcc, see @ref{1c,,Compiling with gcc}),
7225 binder (gnatbind, see @ref{1d,,Binding with gnatbind}),
7226 and linker (gnatlink, see @ref{1e,,Linking with gnatlink})
7227 to build executable programs.
7228 Finally, this chapter provides examples of
7229 how to make use of the general GNU make mechanism
7230 in a GNAT context (see @ref{1f,,Using the GNU make Utility}).
7231
7232
7233 @menu
7234 * Building with gnatmake::
7235 * Compiling with gcc::
7236 * Compiler Switches::
7237 * Linker Switches::
7238 * Binding with gnatbind::
7239 * Linking with gnatlink::
7240 * Using the GNU make Utility::
7241
7242 @end menu
7243
7244 @node Building with gnatmake,Compiling with gcc,,Building Executable Programs with GNAT
7245 @anchor{gnat_ugn/building_executable_programs_with_gnat the-gnat-make-program-gnatmake}@anchor{1b}@anchor{gnat_ugn/building_executable_programs_with_gnat building-with-gnatmake}@anchor{d9}
7246 @section Building with @code{gnatmake}
7247
7248
7249 @geindex gnatmake
7250
7251 A typical development cycle when working on an Ada program consists of
7252 the following steps:
7253
7254
7255 @enumerate
7256
7257 @item
7258 Edit some sources to fix bugs;
7259
7260 @item
7261 Add enhancements;
7262
7263 @item
7264 Compile all sources affected;
7265
7266 @item
7267 Rebind and relink; and
7268
7269 @item
7270 Test.
7271 @end enumerate
7272
7273 @geindex Dependency rules (compilation)
7274
7275 The third step in particular can be tricky, because not only do the modified
7276 files have to be compiled, but any files depending on these files must also be
7277 recompiled. The dependency rules in Ada can be quite complex, especially
7278 in the presence of overloading, @code{use} clauses, generics and inlined
7279 subprograms.
7280
7281 @code{gnatmake} automatically takes care of the third and fourth steps
7282 of this process. It determines which sources need to be compiled,
7283 compiles them, and binds and links the resulting object files.
7284
7285 Unlike some other Ada make programs, the dependencies are always
7286 accurately recomputed from the new sources. The source based approach of
7287 the GNAT compilation model makes this possible. This means that if
7288 changes to the source program cause corresponding changes in
7289 dependencies, they will always be tracked exactly correctly by
7290 @code{gnatmake}.
7291
7292 Note that for advanced forms of project structure, we recommend creating
7293 a project file as explained in the @emph{GNAT_Project_Manager} chapter in the
7294 @emph{GPRbuild User's Guide}, and using the
7295 @code{gprbuild} tool which supports building with project files and works similarly
7296 to @code{gnatmake}.
7297
7298 @menu
7299 * Running gnatmake::
7300 * Switches for gnatmake::
7301 * Mode Switches for gnatmake::
7302 * Notes on the Command Line::
7303 * How gnatmake Works::
7304 * Examples of gnatmake Usage::
7305
7306 @end menu
7307
7308 @node Running gnatmake,Switches for gnatmake,,Building with gnatmake
7309 @anchor{gnat_ugn/building_executable_programs_with_gnat running-gnatmake}@anchor{da}@anchor{gnat_ugn/building_executable_programs_with_gnat id2}@anchor{db}
7310 @subsection Running @code{gnatmake}
7311
7312
7313 The usual form of the @code{gnatmake} command is
7314
7315 @example
7316 $ gnatmake [<switches>] <file_name> [<file_names>] [<mode_switches>]
7317 @end example
7318
7319 The only required argument is one @code{file_name}, which specifies
7320 a compilation unit that is a main program. Several @code{file_names} can be
7321 specified: this will result in several executables being built.
7322 If @code{switches} are present, they can be placed before the first
7323 @code{file_name}, between @code{file_names} or after the last @code{file_name}.
7324 If @code{mode_switches} are present, they must always be placed after
7325 the last @code{file_name} and all @code{switches}.
7326
7327 If you are using standard file extensions (@code{.adb} and
7328 @code{.ads}), then the
7329 extension may be omitted from the @code{file_name} arguments. However, if
7330 you are using non-standard extensions, then it is required that the
7331 extension be given. A relative or absolute directory path can be
7332 specified in a @code{file_name}, in which case, the input source file will
7333 be searched for in the specified directory only. Otherwise, the input
7334 source file will first be searched in the directory where
7335 @code{gnatmake} was invoked and if it is not found, it will be search on
7336 the source path of the compiler as described in
7337 @ref{89,,Search Paths and the Run-Time Library (RTL)}.
7338
7339 All @code{gnatmake} output (except when you specify @code{-M}) is sent to
7340 @code{stderr}. The output produced by the
7341 @code{-M} switch is sent to @code{stdout}.
7342
7343 @node Switches for gnatmake,Mode Switches for gnatmake,Running gnatmake,Building with gnatmake
7344 @anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gnatmake}@anchor{dc}@anchor{gnat_ugn/building_executable_programs_with_gnat id3}@anchor{dd}
7345 @subsection Switches for @code{gnatmake}
7346
7347
7348 You may specify any of the following switches to @code{gnatmake}:
7349
7350 @geindex --version (gnatmake)
7351
7352
7353 @table @asis
7354
7355 @item @code{--version}
7356
7357 Display Copyright and version, then exit disregarding all other options.
7358 @end table
7359
7360 @geindex --help (gnatmake)
7361
7362
7363 @table @asis
7364
7365 @item @code{--help}
7366
7367 If @code{--version} was not used, display usage, then exit disregarding
7368 all other options.
7369 @end table
7370
7371 @geindex --GCC=compiler_name (gnatmake)
7372
7373
7374 @table @asis
7375
7376 @item @code{--GCC=@emph{compiler_name}}
7377
7378 Program used for compiling. The default is @code{gcc}. You need to use
7379 quotes around @code{compiler_name} if @code{compiler_name} contains
7380 spaces or other separator characters.
7381 As an example @code{--GCC="foo -x -y"}
7382 will instruct @code{gnatmake} to use @code{foo -x -y} as your
7383 compiler. A limitation of this syntax is that the name and path name of
7384 the executable itself must not include any embedded spaces. Note that
7385 switch @code{-c} is always inserted after your command name. Thus in the
7386 above example the compiler command that will be used by @code{gnatmake}
7387 will be @code{foo -c -x -y}. If several @code{--GCC=compiler_name} are
7388 used, only the last @code{compiler_name} is taken into account. However,
7389 all the additional switches are also taken into account. Thus,
7390 @code{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
7391 @code{--GCC="bar -x -y -z -t"}.
7392 @end table
7393
7394 @geindex --GNATBIND=binder_name (gnatmake)
7395
7396
7397 @table @asis
7398
7399 @item @code{--GNATBIND=@emph{binder_name}}
7400
7401 Program used for binding. The default is @code{gnatbind}. You need to
7402 use quotes around @code{binder_name} if @code{binder_name} contains spaces
7403 or other separator characters.
7404 As an example @code{--GNATBIND="bar -x -y"}
7405 will instruct @code{gnatmake} to use @code{bar -x -y} as your
7406 binder. Binder switches that are normally appended by @code{gnatmake}
7407 to @code{gnatbind} are now appended to the end of @code{bar -x -y}.
7408 A limitation of this syntax is that the name and path name of the executable
7409 itself must not include any embedded spaces.
7410 @end table
7411
7412 @geindex --GNATLINK=linker_name (gnatmake)
7413
7414
7415 @table @asis
7416
7417 @item @code{--GNATLINK=@emph{linker_name}}
7418
7419 Program used for linking. The default is @code{gnatlink}. You need to
7420 use quotes around @code{linker_name} if @code{linker_name} contains spaces
7421 or other separator characters.
7422 As an example @code{--GNATLINK="lan -x -y"}
7423 will instruct @code{gnatmake} to use @code{lan -x -y} as your
7424 linker. Linker switches that are normally appended by @code{gnatmake} to
7425 @code{gnatlink} are now appended to the end of @code{lan -x -y}.
7426 A limitation of this syntax is that the name and path name of the executable
7427 itself must not include any embedded spaces.
7428
7429 @item @code{--create-map-file}
7430
7431 When linking an executable, create a map file. The name of the map file
7432 has the same name as the executable with extension ".map".
7433
7434 @item @code{--create-map-file=@emph{mapfile}}
7435
7436 When linking an executable, create a map file with the specified name.
7437 @end table
7438
7439 @geindex --create-missing-dirs (gnatmake)
7440
7441
7442 @table @asis
7443
7444 @item @code{--create-missing-dirs}
7445
7446 When using project files (@code{-P@emph{project}}), automatically create
7447 missing object directories, library directories and exec
7448 directories.
7449
7450 @item @code{--single-compile-per-obj-dir}
7451
7452 Disallow simultaneous compilations in the same object directory when
7453 project files are used.
7454
7455 @item @code{--subdirs=@emph{subdir}}
7456
7457 Actual object directory of each project file is the subdirectory subdir of the
7458 object directory specified or defaulted in the project file.
7459
7460 @item @code{--unchecked-shared-lib-imports}
7461
7462 By default, shared library projects are not allowed to import static library
7463 projects. When this switch is used on the command line, this restriction is
7464 relaxed.
7465
7466 @item @code{--source-info=@emph{source info file}}
7467
7468 Specify a source info file. This switch is active only when project files
7469 are used. If the source info file is specified as a relative path, then it is
7470 relative to the object directory of the main project. If the source info file
7471 does not exist, then after the Project Manager has successfully parsed and
7472 processed the project files and found the sources, it creates the source info
7473 file. If the source info file already exists and can be read successfully,
7474 then the Project Manager will get all the needed information about the sources
7475 from the source info file and will not look for them. This reduces the time
7476 to process the project files, especially when looking for sources that take a
7477 long time. If the source info file exists but cannot be parsed successfully,
7478 the Project Manager will attempt to recreate it. If the Project Manager fails
7479 to create the source info file, a message is issued, but gnatmake does not
7480 fail. @code{gnatmake} "trusts" the source info file. This means that
7481 if the source files have changed (addition, deletion, moving to a different
7482 source directory), then the source info file need to be deleted and recreated.
7483 @end table
7484
7485 @geindex -a (gnatmake)
7486
7487
7488 @table @asis
7489
7490 @item @code{-a}
7491
7492 Consider all files in the make process, even the GNAT internal system
7493 files (for example, the predefined Ada library files), as well as any
7494 locked files. Locked files are files whose ALI file is write-protected.
7495 By default,
7496 @code{gnatmake} does not check these files,
7497 because the assumption is that the GNAT internal files are properly up
7498 to date, and also that any write protected ALI files have been properly
7499 installed. Note that if there is an installation problem, such that one
7500 of these files is not up to date, it will be properly caught by the
7501 binder.
7502 You may have to specify this switch if you are working on GNAT
7503 itself. The switch @code{-a} is also useful
7504 in conjunction with @code{-f}
7505 if you need to recompile an entire application,
7506 including run-time files, using special configuration pragmas,
7507 such as a @code{Normalize_Scalars} pragma.
7508
7509 By default
7510 @code{gnatmake -a} compiles all GNAT
7511 internal files with
7512 @code{gcc -c -gnatpg} rather than @code{gcc -c}.
7513 @end table
7514
7515 @geindex -b (gnatmake)
7516
7517
7518 @table @asis
7519
7520 @item @code{-b}
7521
7522 Bind only. Can be combined with @code{-c} to do
7523 compilation and binding, but no link.
7524 Can be combined with @code{-l}
7525 to do binding and linking. When not combined with
7526 @code{-c}
7527 all the units in the closure of the main program must have been previously
7528 compiled and must be up to date. The root unit specified by @code{file_name}
7529 may be given without extension, with the source extension or, if no GNAT
7530 Project File is specified, with the ALI file extension.
7531 @end table
7532
7533 @geindex -c (gnatmake)
7534
7535
7536 @table @asis
7537
7538 @item @code{-c}
7539
7540 Compile only. Do not perform binding, except when @code{-b}
7541 is also specified. Do not perform linking, except if both
7542 @code{-b} and
7543 @code{-l} are also specified.
7544 If the root unit specified by @code{file_name} is not a main unit, this is the
7545 default. Otherwise @code{gnatmake} will attempt binding and linking
7546 unless all objects are up to date and the executable is more recent than
7547 the objects.
7548 @end table
7549
7550 @geindex -C (gnatmake)
7551
7552
7553 @table @asis
7554
7555 @item @code{-C}
7556
7557 Use a temporary mapping file. A mapping file is a way to communicate
7558 to the compiler two mappings: from unit names to file names (without
7559 any directory information) and from file names to path names (with
7560 full directory information). A mapping file can make the compiler's
7561 file searches faster, especially if there are many source directories,
7562 or the sources are read over a slow network connection. If
7563 @code{-P} is used, a mapping file is always used, so
7564 @code{-C} is unnecessary; in this case the mapping file
7565 is initially populated based on the project file. If
7566 @code{-C} is used without
7567 @code{-P},
7568 the mapping file is initially empty. Each invocation of the compiler
7569 will add any newly accessed sources to the mapping file.
7570 @end table
7571
7572 @geindex -C= (gnatmake)
7573
7574
7575 @table @asis
7576
7577 @item @code{-C=@emph{file}}
7578
7579 Use a specific mapping file. The file, specified as a path name (absolute or
7580 relative) by this switch, should already exist, otherwise the switch is
7581 ineffective. The specified mapping file will be communicated to the compiler.
7582 This switch is not compatible with a project file
7583 (-P`file`) or with multiple compiling processes
7584 (-jnnn, when nnn is greater than 1).
7585 @end table
7586
7587 @geindex -d (gnatmake)
7588
7589
7590 @table @asis
7591
7592 @item @code{-d}
7593
7594 Display progress for each source, up to date or not, as a single line:
7595
7596 @example
7597 completed x out of y (zz%)
7598 @end example
7599
7600 If the file needs to be compiled this is displayed after the invocation of
7601 the compiler. These lines are displayed even in quiet output mode.
7602 @end table
7603
7604 @geindex -D (gnatmake)
7605
7606
7607 @table @asis
7608
7609 @item @code{-D @emph{dir}}
7610
7611 Put all object files and ALI file in directory @code{dir}.
7612 If the @code{-D} switch is not used, all object files
7613 and ALI files go in the current working directory.
7614
7615 This switch cannot be used when using a project file.
7616 @end table
7617
7618 @geindex -eI (gnatmake)
7619
7620
7621 @table @asis
7622
7623 @item @code{-eI@emph{nnn}}
7624
7625 Indicates that the main source is a multi-unit source and the rank of the unit
7626 in the source file is nnn. nnn needs to be a positive number and a valid
7627 index in the source. This switch cannot be used when @code{gnatmake} is
7628 invoked for several mains.
7629 @end table
7630
7631 @geindex -eL (gnatmake)
7632
7633 @geindex symbolic links
7634
7635
7636 @table @asis
7637
7638 @item @code{-eL}
7639
7640 Follow all symbolic links when processing project files.
7641 This should be used if your project uses symbolic links for files or
7642 directories, but is not needed in other cases.
7643
7644 @geindex naming scheme
7645
7646 This also assumes that no directory matches the naming scheme for files (for
7647 instance that you do not have a directory called "sources.ads" when using the
7648 default GNAT naming scheme).
7649
7650 When you do not have to use this switch (i.e., by default), gnatmake is able to
7651 save a lot of system calls (several per source file and object file), which
7652 can result in a significant speed up to load and manipulate a project file,
7653 especially when using source files from a remote system.
7654 @end table
7655
7656 @geindex -eS (gnatmake)
7657
7658
7659 @table @asis
7660
7661 @item @code{-eS}
7662
7663 Output the commands for the compiler, the binder and the linker
7664 on standard output,
7665 instead of standard error.
7666 @end table
7667
7668 @geindex -f (gnatmake)
7669
7670
7671 @table @asis
7672
7673 @item @code{-f}
7674
7675 Force recompilations. Recompile all sources, even though some object
7676 files may be up to date, but don't recompile predefined or GNAT internal
7677 files or locked files (files with a write-protected ALI file),
7678 unless the @code{-a} switch is also specified.
7679 @end table
7680
7681 @geindex -F (gnatmake)
7682
7683
7684 @table @asis
7685
7686 @item @code{-F}
7687
7688 When using project files, if some errors or warnings are detected during
7689 parsing and verbose mode is not in effect (no use of switch
7690 -v), then error lines start with the full path name of the project
7691 file, rather than its simple file name.
7692 @end table
7693
7694 @geindex -g (gnatmake)
7695
7696
7697 @table @asis
7698
7699 @item @code{-g}
7700
7701 Enable debugging. This switch is simply passed to the compiler and to the
7702 linker.
7703 @end table
7704
7705 @geindex -i (gnatmake)
7706
7707
7708 @table @asis
7709
7710 @item @code{-i}
7711
7712 In normal mode, @code{gnatmake} compiles all object files and ALI files
7713 into the current directory. If the @code{-i} switch is used,
7714 then instead object files and ALI files that already exist are overwritten
7715 in place. This means that once a large project is organized into separate
7716 directories in the desired manner, then @code{gnatmake} will automatically
7717 maintain and update this organization. If no ALI files are found on the
7718 Ada object path (see @ref{89,,Search Paths and the Run-Time Library (RTL)}),
7719 the new object and ALI files are created in the
7720 directory containing the source being compiled. If another organization
7721 is desired, where objects and sources are kept in different directories,
7722 a useful technique is to create dummy ALI files in the desired directories.
7723 When detecting such a dummy file, @code{gnatmake} will be forced to
7724 recompile the corresponding source file, and it will be put the resulting
7725 object and ALI files in the directory where it found the dummy file.
7726 @end table
7727
7728 @geindex -j (gnatmake)
7729
7730 @geindex Parallel make
7731
7732
7733 @table @asis
7734
7735 @item @code{-j@emph{n}}
7736
7737 Use @code{n} processes to carry out the (re)compilations. On a multiprocessor
7738 machine compilations will occur in parallel. If @code{n} is 0, then the
7739 maximum number of parallel compilations is the number of core processors
7740 on the platform. In the event of compilation errors, messages from various
7741 compilations might get interspersed (but @code{gnatmake} will give you the
7742 full ordered list of failing compiles at the end). If this is problematic,
7743 rerun the make process with n set to 1 to get a clean list of messages.
7744 @end table
7745
7746 @geindex -k (gnatmake)
7747
7748
7749 @table @asis
7750
7751 @item @code{-k}
7752
7753 Keep going. Continue as much as possible after a compilation error. To
7754 ease the programmer's task in case of compilation errors, the list of
7755 sources for which the compile fails is given when @code{gnatmake}
7756 terminates.
7757
7758 If @code{gnatmake} is invoked with several @code{file_names} and with this
7759 switch, if there are compilation errors when building an executable,
7760 @code{gnatmake} will not attempt to build the following executables.
7761 @end table
7762
7763 @geindex -l (gnatmake)
7764
7765
7766 @table @asis
7767
7768 @item @code{-l}
7769
7770 Link only. Can be combined with @code{-b} to binding
7771 and linking. Linking will not be performed if combined with
7772 @code{-c}
7773 but not with @code{-b}.
7774 When not combined with @code{-b}
7775 all the units in the closure of the main program must have been previously
7776 compiled and must be up to date, and the main program needs to have been bound.
7777 The root unit specified by @code{file_name}
7778 may be given without extension, with the source extension or, if no GNAT
7779 Project File is specified, with the ALI file extension.
7780 @end table
7781
7782 @geindex -m (gnatmake)
7783
7784
7785 @table @asis
7786
7787 @item @code{-m}
7788
7789 Specify that the minimum necessary amount of recompilations
7790 be performed. In this mode @code{gnatmake} ignores time
7791 stamp differences when the only
7792 modifications to a source file consist in adding/removing comments,
7793 empty lines, spaces or tabs. This means that if you have changed the
7794 comments in a source file or have simply reformatted it, using this
7795 switch will tell @code{gnatmake} not to recompile files that depend on it
7796 (provided other sources on which these files depend have undergone no
7797 semantic modifications). Note that the debugging information may be
7798 out of date with respect to the sources if the @code{-m} switch causes
7799 a compilation to be switched, so the use of this switch represents a
7800 trade-off between compilation time and accurate debugging information.
7801 @end table
7802
7803 @geindex Dependencies
7804 @geindex producing list
7805
7806 @geindex -M (gnatmake)
7807
7808
7809 @table @asis
7810
7811 @item @code{-M}
7812
7813 Check if all objects are up to date. If they are, output the object
7814 dependences to @code{stdout} in a form that can be directly exploited in
7815 a @code{Makefile}. By default, each source file is prefixed with its
7816 (relative or absolute) directory name. This name is whatever you
7817 specified in the various @code{-aI}
7818 and @code{-I} switches. If you use
7819 @code{gnatmake -M} @code{-q}
7820 (see below), only the source file names,
7821 without relative paths, are output. If you just specify the @code{-M}
7822 switch, dependencies of the GNAT internal system files are omitted. This
7823 is typically what you want. If you also specify
7824 the @code{-a} switch,
7825 dependencies of the GNAT internal files are also listed. Note that
7826 dependencies of the objects in external Ada libraries (see
7827 switch @code{-aL@emph{dir}} in the following list)
7828 are never reported.
7829 @end table
7830
7831 @geindex -n (gnatmake)
7832
7833
7834 @table @asis
7835
7836 @item @code{-n}
7837
7838 Don't compile, bind, or link. Checks if all objects are up to date.
7839 If they are not, the full name of the first file that needs to be
7840 recompiled is printed.
7841 Repeated use of this option, followed by compiling the indicated source
7842 file, will eventually result in recompiling all required units.
7843 @end table
7844
7845 @geindex -o (gnatmake)
7846
7847
7848 @table @asis
7849
7850 @item @code{-o @emph{exec_name}}
7851
7852 Output executable name. The name of the final executable program will be
7853 @code{exec_name}. If the @code{-o} switch is omitted the default
7854 name for the executable will be the name of the input file in appropriate form
7855 for an executable file on the host system.
7856
7857 This switch cannot be used when invoking @code{gnatmake} with several
7858 @code{file_names}.
7859 @end table
7860
7861 @geindex -p (gnatmake)
7862
7863
7864 @table @asis
7865
7866 @item @code{-p}
7867
7868 Same as @code{--create-missing-dirs}
7869 @end table
7870
7871 @geindex -P (gnatmake)
7872
7873
7874 @table @asis
7875
7876 @item @code{-P@emph{project}}
7877
7878 Use project file @code{project}. Only one such switch can be used.
7879 @end table
7880
7881 @c -- Comment:
7882 @c :ref:`gnatmake_and_Project_Files`.
7883
7884 @geindex -q (gnatmake)
7885
7886
7887 @table @asis
7888
7889 @item @code{-q}
7890
7891 Quiet. When this flag is not set, the commands carried out by
7892 @code{gnatmake} are displayed.
7893 @end table
7894
7895 @geindex -s (gnatmake)
7896
7897
7898 @table @asis
7899
7900 @item @code{-s}
7901
7902 Recompile if compiler switches have changed since last compilation.
7903 All compiler switches but -I and -o are taken into account in the
7904 following way:
7905 orders between different 'first letter' switches are ignored, but
7906 orders between same switches are taken into account. For example,
7907 @code{-O -O2} is different than @code{-O2 -O}, but @code{-g -O}
7908 is equivalent to @code{-O -g}.
7909
7910 This switch is recommended when Integrated Preprocessing is used.
7911 @end table
7912
7913 @geindex -u (gnatmake)
7914
7915
7916 @table @asis
7917
7918 @item @code{-u}
7919
7920 Unique. Recompile at most the main files. It implies -c. Combined with
7921 -f, it is equivalent to calling the compiler directly. Note that using
7922 -u with a project file and no main has a special meaning.
7923 @end table
7924
7925 @c --Comment
7926 @c (See :ref:`Project_Files_and_Main_Subprograms`.)
7927
7928 @geindex -U (gnatmake)
7929
7930
7931 @table @asis
7932
7933 @item @code{-U}
7934
7935 When used without a project file or with one or several mains on the command
7936 line, is equivalent to -u. When used with a project file and no main
7937 on the command line, all sources of all project files are checked and compiled
7938 if not up to date, and libraries are rebuilt, if necessary.
7939 @end table
7940
7941 @geindex -v (gnatmake)
7942
7943
7944 @table @asis
7945
7946 @item @code{-v}
7947
7948 Verbose. Display the reason for all recompilations @code{gnatmake}
7949 decides are necessary, with the highest verbosity level.
7950 @end table
7951
7952 @geindex -vl (gnatmake)
7953
7954
7955 @table @asis
7956
7957 @item @code{-vl}
7958
7959 Verbosity level Low. Display fewer lines than in verbosity Medium.
7960 @end table
7961
7962 @geindex -vm (gnatmake)
7963
7964
7965 @table @asis
7966
7967 @item @code{-vm}
7968
7969 Verbosity level Medium. Potentially display fewer lines than in verbosity High.
7970 @end table
7971
7972 @geindex -vm (gnatmake)
7973
7974
7975 @table @asis
7976
7977 @item @code{-vh}
7978
7979 Verbosity level High. Equivalent to -v.
7980
7981 @item @code{-vP@emph{x}}
7982
7983 Indicate the verbosity of the parsing of GNAT project files.
7984 See @ref{de,,Switches Related to Project Files}.
7985 @end table
7986
7987 @geindex -x (gnatmake)
7988
7989
7990 @table @asis
7991
7992 @item @code{-x}
7993
7994 Indicate that sources that are not part of any Project File may be compiled.
7995 Normally, when using Project Files, only sources that are part of a Project
7996 File may be compile. When this switch is used, a source outside of all Project
7997 Files may be compiled. The ALI file and the object file will be put in the
7998 object directory of the main Project. The compilation switches used will only
7999 be those specified on the command line. Even when
8000 @code{-x} is used, mains specified on the
8001 command line need to be sources of a project file.
8002
8003 @item @code{-X@emph{name}=@emph{value}}
8004
8005 Indicate that external variable @code{name} has the value @code{value}.
8006 The Project Manager will use this value for occurrences of
8007 @code{external(name)} when parsing the project file.
8008 @ref{de,,Switches Related to Project Files}.
8009 @end table
8010
8011 @geindex -z (gnatmake)
8012
8013
8014 @table @asis
8015
8016 @item @code{-z}
8017
8018 No main subprogram. Bind and link the program even if the unit name
8019 given on the command line is a package name. The resulting executable
8020 will execute the elaboration routines of the package and its closure,
8021 then the finalization routines.
8022 @end table
8023
8024 @subsubheading GCC switches
8025
8026
8027 Any uppercase or multi-character switch that is not a @code{gnatmake} switch
8028 is passed to @code{gcc} (e.g., @code{-O}, @code{-gnato,} etc.)
8029
8030 @subsubheading Source and library search path switches
8031
8032
8033 @geindex -aI (gnatmake)
8034
8035
8036 @table @asis
8037
8038 @item @code{-aI@emph{dir}}
8039
8040 When looking for source files also look in directory @code{dir}.
8041 The order in which source files search is undertaken is
8042 described in @ref{89,,Search Paths and the Run-Time Library (RTL)}.
8043 @end table
8044
8045 @geindex -aL (gnatmake)
8046
8047
8048 @table @asis
8049
8050 @item @code{-aL@emph{dir}}
8051
8052 Consider @code{dir} as being an externally provided Ada library.
8053 Instructs @code{gnatmake} to skip compilation units whose @code{.ALI}
8054 files have been located in directory @code{dir}. This allows you to have
8055 missing bodies for the units in @code{dir} and to ignore out of date bodies
8056 for the same units. You still need to specify
8057 the location of the specs for these units by using the switches
8058 @code{-aI@emph{dir}} or @code{-I@emph{dir}}.
8059 Note: this switch is provided for compatibility with previous versions
8060 of @code{gnatmake}. The easier method of causing standard libraries
8061 to be excluded from consideration is to write-protect the corresponding
8062 ALI files.
8063 @end table
8064
8065 @geindex -aO (gnatmake)
8066
8067
8068 @table @asis
8069
8070 @item @code{-aO@emph{dir}}
8071
8072 When searching for library and object files, look in directory
8073 @code{dir}. The order in which library files are searched is described in
8074 @ref{8c,,Search Paths for gnatbind}.
8075 @end table
8076
8077 @geindex Search paths
8078 @geindex for gnatmake
8079
8080 @geindex -A (gnatmake)
8081
8082
8083 @table @asis
8084
8085 @item @code{-A@emph{dir}}
8086
8087 Equivalent to @code{-aL@emph{dir}} @code{-aI@emph{dir}}.
8088
8089 @geindex -I (gnatmake)
8090
8091 @item @code{-I@emph{dir}}
8092
8093 Equivalent to @code{-aO@emph{dir} -aI@emph{dir}}.
8094 @end table
8095
8096 @geindex -I- (gnatmake)
8097
8098 @geindex Source files
8099 @geindex suppressing search
8100
8101
8102 @table @asis
8103
8104 @item @code{-I-}
8105
8106 Do not look for source files in the directory containing the source
8107 file named in the command line.
8108 Do not look for ALI or object files in the directory
8109 where @code{gnatmake} was invoked.
8110 @end table
8111
8112 @geindex -L (gnatmake)
8113
8114 @geindex Linker libraries
8115
8116
8117 @table @asis
8118
8119 @item @code{-L@emph{dir}}
8120
8121 Add directory @code{dir} to the list of directories in which the linker
8122 will search for libraries. This is equivalent to
8123 @code{-largs} @code{-L@emph{dir}}.
8124 Furthermore, under Windows, the sources pointed to by the libraries path
8125 set in the registry are not searched for.
8126 @end table
8127
8128 @geindex -nostdinc (gnatmake)
8129
8130
8131 @table @asis
8132
8133 @item @code{-nostdinc}
8134
8135 Do not look for source files in the system default directory.
8136 @end table
8137
8138 @geindex -nostdlib (gnatmake)
8139
8140
8141 @table @asis
8142
8143 @item @code{-nostdlib}
8144
8145 Do not look for library files in the system default directory.
8146 @end table
8147
8148 @geindex --RTS (gnatmake)
8149
8150
8151 @table @asis
8152
8153 @item @code{--RTS=@emph{rts-path}}
8154
8155 Specifies the default location of the run-time library. GNAT looks for the
8156 run-time
8157 in the following directories, and stops as soon as a valid run-time is found
8158 (@code{adainclude} or @code{ada_source_path}, and @code{adalib} or
8159 @code{ada_object_path} present):
8160
8161
8162 @itemize *
8163
8164 @item
8165 @emph{<current directory>/$rts_path}
8166
8167 @item
8168 @emph{<default-search-dir>/$rts_path}
8169
8170 @item
8171 @emph{<default-search-dir>/rts-$rts_path}
8172
8173 @item
8174 The selected path is handled like a normal RTS path.
8175 @end itemize
8176 @end table
8177
8178 @node Mode Switches for gnatmake,Notes on the Command Line,Switches for gnatmake,Building with gnatmake
8179 @anchor{gnat_ugn/building_executable_programs_with_gnat id4}@anchor{df}@anchor{gnat_ugn/building_executable_programs_with_gnat mode-switches-for-gnatmake}@anchor{e0}
8180 @subsection Mode Switches for @code{gnatmake}
8181
8182
8183 The mode switches (referred to as @code{mode_switches}) allow the
8184 inclusion of switches that are to be passed to the compiler itself, the
8185 binder or the linker. The effect of a mode switch is to cause all
8186 subsequent switches up to the end of the switch list, or up to the next
8187 mode switch, to be interpreted as switches to be passed on to the
8188 designated component of GNAT.
8189
8190 @geindex -cargs (gnatmake)
8191
8192
8193 @table @asis
8194
8195 @item @code{-cargs @emph{switches}}
8196
8197 Compiler switches. Here @code{switches} is a list of switches
8198 that are valid switches for @code{gcc}. They will be passed on to
8199 all compile steps performed by @code{gnatmake}.
8200 @end table
8201
8202 @geindex -bargs (gnatmake)
8203
8204
8205 @table @asis
8206
8207 @item @code{-bargs @emph{switches}}
8208
8209 Binder switches. Here @code{switches} is a list of switches
8210 that are valid switches for @code{gnatbind}. They will be passed on to
8211 all bind steps performed by @code{gnatmake}.
8212 @end table
8213
8214 @geindex -largs (gnatmake)
8215
8216
8217 @table @asis
8218
8219 @item @code{-largs @emph{switches}}
8220
8221 Linker switches. Here @code{switches} is a list of switches
8222 that are valid switches for @code{gnatlink}. They will be passed on to
8223 all link steps performed by @code{gnatmake}.
8224 @end table
8225
8226 @geindex -margs (gnatmake)
8227
8228
8229 @table @asis
8230
8231 @item @code{-margs @emph{switches}}
8232
8233 Make switches. The switches are directly interpreted by @code{gnatmake},
8234 regardless of any previous occurrence of @code{-cargs}, @code{-bargs}
8235 or @code{-largs}.
8236 @end table
8237
8238 @node Notes on the Command Line,How gnatmake Works,Mode Switches for gnatmake,Building with gnatmake
8239 @anchor{gnat_ugn/building_executable_programs_with_gnat id5}@anchor{e1}@anchor{gnat_ugn/building_executable_programs_with_gnat notes-on-the-command-line}@anchor{e2}
8240 @subsection Notes on the Command Line
8241
8242
8243 This section contains some additional useful notes on the operation
8244 of the @code{gnatmake} command.
8245
8246 @geindex Recompilation (by gnatmake)
8247
8248
8249 @itemize *
8250
8251 @item
8252 If @code{gnatmake} finds no ALI files, it recompiles the main program
8253 and all other units required by the main program.
8254 This means that @code{gnatmake}
8255 can be used for the initial compile, as well as during subsequent steps of
8256 the development cycle.
8257
8258 @item
8259 If you enter @code{gnatmake foo.adb}, where @code{foo}
8260 is a subunit or body of a generic unit, @code{gnatmake} recompiles
8261 @code{foo.adb} (because it finds no ALI) and stops, issuing a
8262 warning.
8263
8264 @item
8265 In @code{gnatmake} the switch @code{-I}
8266 is used to specify both source and
8267 library file paths. Use @code{-aI}
8268 instead if you just want to specify
8269 source paths only and @code{-aO}
8270 if you want to specify library paths
8271 only.
8272
8273 @item
8274 @code{gnatmake} will ignore any files whose ALI file is write-protected.
8275 This may conveniently be used to exclude standard libraries from
8276 consideration and in particular it means that the use of the
8277 @code{-f} switch will not recompile these files
8278 unless @code{-a} is also specified.
8279
8280 @item
8281 @code{gnatmake} has been designed to make the use of Ada libraries
8282 particularly convenient. Assume you have an Ada library organized
8283 as follows: @emph{obj-dir} contains the objects and ALI files for
8284 of your Ada compilation units,
8285 whereas @emph{include-dir} contains the
8286 specs of these units, but no bodies. Then to compile a unit
8287 stored in @code{main.adb}, which uses this Ada library you would just type:
8288
8289 @example
8290 $ gnatmake -aI`include-dir` -aL`obj-dir` main
8291 @end example
8292
8293 @item
8294 Using @code{gnatmake} along with the @code{-m (minimal recompilation)}
8295 switch provides a mechanism for avoiding unnecessary recompilations. Using
8296 this switch,
8297 you can update the comments/format of your
8298 source files without having to recompile everything. Note, however, that
8299 adding or deleting lines in a source files may render its debugging
8300 info obsolete. If the file in question is a spec, the impact is rather
8301 limited, as that debugging info will only be useful during the
8302 elaboration phase of your program. For bodies the impact can be more
8303 significant. In all events, your debugger will warn you if a source file
8304 is more recent than the corresponding object, and alert you to the fact
8305 that the debugging information may be out of date.
8306 @end itemize
8307
8308 @node How gnatmake Works,Examples of gnatmake Usage,Notes on the Command Line,Building with gnatmake
8309 @anchor{gnat_ugn/building_executable_programs_with_gnat id6}@anchor{e3}@anchor{gnat_ugn/building_executable_programs_with_gnat how-gnatmake-works}@anchor{e4}
8310 @subsection How @code{gnatmake} Works
8311
8312
8313 Generally @code{gnatmake} automatically performs all necessary
8314 recompilations and you don't need to worry about how it works. However,
8315 it may be useful to have some basic understanding of the @code{gnatmake}
8316 approach and in particular to understand how it uses the results of
8317 previous compilations without incorrectly depending on them.
8318
8319 First a definition: an object file is considered @emph{up to date} if the
8320 corresponding ALI file exists and if all the source files listed in the
8321 dependency section of this ALI file have time stamps matching those in
8322 the ALI file. This means that neither the source file itself nor any
8323 files that it depends on have been modified, and hence there is no need
8324 to recompile this file.
8325
8326 @code{gnatmake} works by first checking if the specified main unit is up
8327 to date. If so, no compilations are required for the main unit. If not,
8328 @code{gnatmake} compiles the main program to build a new ALI file that
8329 reflects the latest sources. Then the ALI file of the main unit is
8330 examined to find all the source files on which the main program depends,
8331 and @code{gnatmake} recursively applies the above procedure on all these
8332 files.
8333
8334 This process ensures that @code{gnatmake} only trusts the dependencies
8335 in an existing ALI file if they are known to be correct. Otherwise it
8336 always recompiles to determine a new, guaranteed accurate set of
8337 dependencies. As a result the program is compiled 'upside down' from what may
8338 be more familiar as the required order of compilation in some other Ada
8339 systems. In particular, clients are compiled before the units on which
8340 they depend. The ability of GNAT to compile in any order is critical in
8341 allowing an order of compilation to be chosen that guarantees that
8342 @code{gnatmake} will recompute a correct set of new dependencies if
8343 necessary.
8344
8345 When invoking @code{gnatmake} with several @code{file_names}, if a unit is
8346 imported by several of the executables, it will be recompiled at most once.
8347
8348 Note: when using non-standard naming conventions
8349 (@ref{35,,Using Other File Names}), changing through a configuration pragmas
8350 file the version of a source and invoking @code{gnatmake} to recompile may
8351 have no effect, if the previous version of the source is still accessible
8352 by @code{gnatmake}. It may be necessary to use the switch
8353 -f.
8354
8355 @node Examples of gnatmake Usage,,How gnatmake Works,Building with gnatmake
8356 @anchor{gnat_ugn/building_executable_programs_with_gnat examples-of-gnatmake-usage}@anchor{e5}@anchor{gnat_ugn/building_executable_programs_with_gnat id7}@anchor{e6}
8357 @subsection Examples of @code{gnatmake} Usage
8358
8359
8360
8361 @table @asis
8362
8363 @item @emph{gnatmake hello.adb}
8364
8365 Compile all files necessary to bind and link the main program
8366 @code{hello.adb} (containing unit @code{Hello}) and bind and link the
8367 resulting object files to generate an executable file @code{hello}.
8368
8369 @item @emph{gnatmake main1 main2 main3}
8370
8371 Compile all files necessary to bind and link the main programs
8372 @code{main1.adb} (containing unit @code{Main1}), @code{main2.adb}
8373 (containing unit @code{Main2}) and @code{main3.adb}
8374 (containing unit @code{Main3}) and bind and link the resulting object files
8375 to generate three executable files @code{main1},
8376 @code{main2} and @code{main3}.
8377
8378 @item @emph{gnatmake -q Main_Unit -cargs -O2 -bargs -l}
8379
8380 Compile all files necessary to bind and link the main program unit
8381 @code{Main_Unit} (from file @code{main_unit.adb}). All compilations will
8382 be done with optimization level 2 and the order of elaboration will be
8383 listed by the binder. @code{gnatmake} will operate in quiet mode, not
8384 displaying commands it is executing.
8385 @end table
8386
8387 @node Compiling with gcc,Compiler Switches,Building with gnatmake,Building Executable Programs with GNAT
8388 @anchor{gnat_ugn/building_executable_programs_with_gnat compiling-with-gcc}@anchor{1c}@anchor{gnat_ugn/building_executable_programs_with_gnat id8}@anchor{e7}
8389 @section Compiling with @code{gcc}
8390
8391
8392 This section discusses how to compile Ada programs using the @code{gcc}
8393 command. It also describes the set of switches
8394 that can be used to control the behavior of the compiler.
8395
8396 @menu
8397 * Compiling Programs::
8398 * Search Paths and the Run-Time Library (RTL): Search Paths and the Run-Time Library RTL.
8399 * Order of Compilation Issues::
8400 * Examples::
8401
8402 @end menu
8403
8404 @node Compiling Programs,Search Paths and the Run-Time Library RTL,,Compiling with gcc
8405 @anchor{gnat_ugn/building_executable_programs_with_gnat compiling-programs}@anchor{e8}@anchor{gnat_ugn/building_executable_programs_with_gnat id9}@anchor{e9}
8406 @subsection Compiling Programs
8407
8408
8409 The first step in creating an executable program is to compile the units
8410 of the program using the @code{gcc} command. You must compile the
8411 following files:
8412
8413
8414 @itemize *
8415
8416 @item
8417 the body file (@code{.adb}) for a library level subprogram or generic
8418 subprogram
8419
8420 @item
8421 the spec file (@code{.ads}) for a library level package or generic
8422 package that has no body
8423
8424 @item
8425 the body file (@code{.adb}) for a library level package
8426 or generic package that has a body
8427 @end itemize
8428
8429 You need @emph{not} compile the following files
8430
8431
8432 @itemize *
8433
8434 @item
8435 the spec of a library unit which has a body
8436
8437 @item
8438 subunits
8439 @end itemize
8440
8441 because they are compiled as part of compiling related units. GNAT
8442 package specs
8443 when the corresponding body is compiled, and subunits when the parent is
8444 compiled.
8445
8446 @geindex cannot generate code
8447
8448 If you attempt to compile any of these files, you will get one of the
8449 following error messages (where @code{fff} is the name of the file you
8450 compiled):
8451
8452 @quotation
8453
8454 @example
8455 cannot generate code for file `@w{`}fff`@w{`} (package spec)
8456 to check package spec, use -gnatc
8457
8458 cannot generate code for file `@w{`}fff`@w{`} (missing subunits)
8459 to check parent unit, use -gnatc
8460
8461 cannot generate code for file `@w{`}fff`@w{`} (subprogram spec)
8462 to check subprogram spec, use -gnatc
8463
8464 cannot generate code for file `@w{`}fff`@w{`} (subunit)
8465 to check subunit, use -gnatc
8466 @end example
8467 @end quotation
8468
8469 As indicated by the above error messages, if you want to submit
8470 one of these files to the compiler to check for correct semantics
8471 without generating code, then use the @code{-gnatc} switch.
8472
8473 The basic command for compiling a file containing an Ada unit is:
8474
8475 @example
8476 $ gcc -c [switches] <file name>
8477 @end example
8478
8479 where @code{file name} is the name of the Ada file (usually
8480 having an extension @code{.ads} for a spec or @code{.adb} for a body).
8481 You specify the
8482 @code{-c} switch to tell @code{gcc} to compile, but not link, the file.
8483 The result of a successful compilation is an object file, which has the
8484 same name as the source file but an extension of @code{.o} and an Ada
8485 Library Information (ALI) file, which also has the same name as the
8486 source file, but with @code{.ali} as the extension. GNAT creates these
8487 two output files in the current directory, but you may specify a source
8488 file in any directory using an absolute or relative path specification
8489 containing the directory information.
8490
8491 TESTING: the @code{--foobar@emph{NN}} switch
8492
8493 @geindex gnat1
8494
8495 @code{gcc} is actually a driver program that looks at the extensions of
8496 the file arguments and loads the appropriate compiler. For example, the
8497 GNU C compiler is @code{cc1}, and the Ada compiler is @code{gnat1}.
8498 These programs are in directories known to the driver program (in some
8499 configurations via environment variables you set), but need not be in
8500 your path. The @code{gcc} driver also calls the assembler and any other
8501 utilities needed to complete the generation of the required object
8502 files.
8503
8504 It is possible to supply several file names on the same @code{gcc}
8505 command. This causes @code{gcc} to call the appropriate compiler for
8506 each file. For example, the following command lists two separate
8507 files to be compiled:
8508
8509 @example
8510 $ gcc -c x.adb y.adb
8511 @end example
8512
8513 calls @code{gnat1} (the Ada compiler) twice to compile @code{x.adb} and
8514 @code{y.adb}.
8515 The compiler generates two object files @code{x.o} and @code{y.o}
8516 and the two ALI files @code{x.ali} and @code{y.ali}.
8517
8518 Any switches apply to all the files listed, see @ref{ea,,Compiler Switches} for a
8519 list of available @code{gcc} switches.
8520
8521 @node Search Paths and the Run-Time Library RTL,Order of Compilation Issues,Compiling Programs,Compiling with gcc
8522 @anchor{gnat_ugn/building_executable_programs_with_gnat id10}@anchor{eb}@anchor{gnat_ugn/building_executable_programs_with_gnat search-paths-and-the-run-time-library-rtl}@anchor{89}
8523 @subsection Search Paths and the Run-Time Library (RTL)
8524
8525
8526 With the GNAT source-based library system, the compiler must be able to
8527 find source files for units that are needed by the unit being compiled.
8528 Search paths are used to guide this process.
8529
8530 The compiler compiles one source file whose name must be given
8531 explicitly on the command line. In other words, no searching is done
8532 for this file. To find all other source files that are needed (the most
8533 common being the specs of units), the compiler examines the following
8534 directories, in the following order:
8535
8536
8537 @itemize *
8538
8539 @item
8540 The directory containing the source file of the main unit being compiled
8541 (the file name on the command line).
8542
8543 @item
8544 Each directory named by an @code{-I} switch given on the @code{gcc}
8545 command line, in the order given.
8546
8547 @geindex ADA_PRJ_INCLUDE_FILE
8548
8549 @item
8550 Each of the directories listed in the text file whose name is given
8551 by the
8552 @geindex ADA_PRJ_INCLUDE_FILE
8553 @geindex environment variable; ADA_PRJ_INCLUDE_FILE
8554 @code{ADA_PRJ_INCLUDE_FILE} environment variable.
8555 @geindex ADA_PRJ_INCLUDE_FILE
8556 @geindex environment variable; ADA_PRJ_INCLUDE_FILE
8557 @code{ADA_PRJ_INCLUDE_FILE} is normally set by gnatmake or by the gnat
8558 driver when project files are used. It should not normally be set
8559 by other means.
8560
8561 @geindex ADA_INCLUDE_PATH
8562
8563 @item
8564 Each of the directories listed in the value of the
8565 @geindex ADA_INCLUDE_PATH
8566 @geindex environment variable; ADA_INCLUDE_PATH
8567 @code{ADA_INCLUDE_PATH} environment variable.
8568 Construct this value
8569 exactly as the
8570 @geindex PATH
8571 @geindex environment variable; PATH
8572 @code{PATH} environment variable: a list of directory
8573 names separated by colons (semicolons when working with the NT version).
8574
8575 @item
8576 The content of the @code{ada_source_path} file which is part of the GNAT
8577 installation tree and is used to store standard libraries such as the
8578 GNAT Run Time Library (RTL) source files.
8579 @ref{87,,Installing a library}
8580 @end itemize
8581
8582 Specifying the switch @code{-I-}
8583 inhibits the use of the directory
8584 containing the source file named in the command line. You can still
8585 have this directory on your search path, but in this case it must be
8586 explicitly requested with a @code{-I} switch.
8587
8588 Specifying the switch @code{-nostdinc}
8589 inhibits the search of the default location for the GNAT Run Time
8590 Library (RTL) source files.
8591
8592 The compiler outputs its object files and ALI files in the current
8593 working directory.
8594 Caution: The object file can be redirected with the @code{-o} switch;
8595 however, @code{gcc} and @code{gnat1} have not been coordinated on this
8596 so the @code{ALI} file will not go to the right place. Therefore, you should
8597 avoid using the @code{-o} switch.
8598
8599 @geindex System.IO
8600
8601 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
8602 children make up the GNAT RTL, together with the simple @code{System.IO}
8603 package used in the @code{"Hello World"} example. The sources for these units
8604 are needed by the compiler and are kept together in one directory. Not
8605 all of the bodies are needed, but all of the sources are kept together
8606 anyway. In a normal installation, you need not specify these directory
8607 names when compiling or binding. Either the environment variables or
8608 the built-in defaults cause these files to be found.
8609
8610 In addition to the language-defined hierarchies (@code{System}, @code{Ada} and
8611 @code{Interfaces}), the GNAT distribution provides a fourth hierarchy,
8612 consisting of child units of @code{GNAT}. This is a collection of generally
8613 useful types, subprograms, etc. See the @cite{GNAT_Reference_Manual}
8614 for further details.
8615
8616 Besides simplifying access to the RTL, a major use of search paths is
8617 in compiling sources from multiple directories. This can make
8618 development environments much more flexible.
8619
8620 @node Order of Compilation Issues,Examples,Search Paths and the Run-Time Library RTL,Compiling with gcc
8621 @anchor{gnat_ugn/building_executable_programs_with_gnat id11}@anchor{ec}@anchor{gnat_ugn/building_executable_programs_with_gnat order-of-compilation-issues}@anchor{ed}
8622 @subsection Order of Compilation Issues
8623
8624
8625 If, in our earlier example, there was a spec for the @code{hello}
8626 procedure, it would be contained in the file @code{hello.ads}; yet this
8627 file would not have to be explicitly compiled. This is the result of the
8628 model we chose to implement library management. Some of the consequences
8629 of this model are as follows:
8630
8631
8632 @itemize *
8633
8634 @item
8635 There is no point in compiling specs (except for package
8636 specs with no bodies) because these are compiled as needed by clients. If
8637 you attempt a useless compilation, you will receive an error message.
8638 It is also useless to compile subunits because they are compiled as needed
8639 by the parent.
8640
8641 @item
8642 There are no order of compilation requirements: performing a
8643 compilation never obsoletes anything. The only way you can obsolete
8644 something and require recompilations is to modify one of the
8645 source files on which it depends.
8646
8647 @item
8648 There is no library as such, apart from the ALI files
8649 (@ref{42,,The Ada Library Information Files}, for information on the format
8650 of these files). For now we find it convenient to create separate ALI files,
8651 but eventually the information therein may be incorporated into the object
8652 file directly.
8653
8654 @item
8655 When you compile a unit, the source files for the specs of all units
8656 that it @emph{with}s, all its subunits, and the bodies of any generics it
8657 instantiates must be available (reachable by the search-paths mechanism
8658 described above), or you will receive a fatal error message.
8659 @end itemize
8660
8661 @node Examples,,Order of Compilation Issues,Compiling with gcc
8662 @anchor{gnat_ugn/building_executable_programs_with_gnat id12}@anchor{ee}@anchor{gnat_ugn/building_executable_programs_with_gnat examples}@anchor{ef}
8663 @subsection Examples
8664
8665
8666 The following are some typical Ada compilation command line examples:
8667
8668 @example
8669 $ gcc -c xyz.adb
8670 @end example
8671
8672 Compile body in file @code{xyz.adb} with all default options.
8673
8674 @example
8675 $ gcc -c -O2 -gnata xyz-def.adb
8676 @end example
8677
8678 Compile the child unit package in file @code{xyz-def.adb} with extensive
8679 optimizations, and pragma @code{Assert}/@cite{Debug} statements
8680 enabled.
8681
8682 @example
8683 $ gcc -c -gnatc abc-def.adb
8684 @end example
8685
8686 Compile the subunit in file @code{abc-def.adb} in semantic-checking-only
8687 mode.
8688
8689 @node Compiler Switches,Linker Switches,Compiling with gcc,Building Executable Programs with GNAT
8690 @anchor{gnat_ugn/building_executable_programs_with_gnat compiler-switches}@anchor{f0}@anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gcc}@anchor{ea}
8691 @section Compiler Switches
8692
8693
8694 The @code{gcc} command accepts switches that control the
8695 compilation process. These switches are fully described in this section:
8696 first an alphabetical listing of all switches with a brief description,
8697 and then functionally grouped sets of switches with more detailed
8698 information.
8699
8700 More switches exist for GCC than those documented here, especially
8701 for specific targets. However, their use is not recommended as
8702 they may change code generation in ways that are incompatible with
8703 the Ada run-time library, or can cause inconsistencies between
8704 compilation units.
8705
8706 @menu
8707 * Alphabetical List of All Switches::
8708 * Output and Error Message Control::
8709 * Warning Message Control::
8710 * Debugging and Assertion Control::
8711 * Validity Checking::
8712 * Style Checking::
8713 * Run-Time Checks::
8714 * Using gcc for Syntax Checking::
8715 * Using gcc for Semantic Checking::
8716 * Compiling Different Versions of Ada::
8717 * Character Set Control::
8718 * File Naming Control::
8719 * Subprogram Inlining Control::
8720 * Auxiliary Output Control::
8721 * Debugging Control::
8722 * Exception Handling Control::
8723 * Units to Sources Mapping Files::
8724 * Code Generation Control::
8725
8726 @end menu
8727
8728 @node Alphabetical List of All Switches,Output and Error Message Control,,Compiler Switches
8729 @anchor{gnat_ugn/building_executable_programs_with_gnat id13}@anchor{f1}@anchor{gnat_ugn/building_executable_programs_with_gnat alphabetical-list-of-all-switches}@anchor{f2}
8730 @subsection Alphabetical List of All Switches
8731
8732
8733 @geindex -b (gcc)
8734
8735
8736 @table @asis
8737
8738 @item @code{-b @emph{target}}
8739
8740 Compile your program to run on @code{target}, which is the name of a
8741 system configuration. You must have a GNAT cross-compiler built if
8742 @code{target} is not the same as your host system.
8743 @end table
8744
8745 @geindex -B (gcc)
8746
8747
8748 @table @asis
8749
8750 @item @code{-B@emph{dir}}
8751
8752 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
8753 from @code{dir} instead of the default location. Only use this switch
8754 when multiple versions of the GNAT compiler are available.
8755 See the "Options for Directory Search" section in the
8756 @cite{Using the GNU Compiler Collection (GCC)} manual for further details.
8757 You would normally use the @code{-b} or @code{-V} switch instead.
8758 @end table
8759
8760 @geindex -c (gcc)
8761
8762
8763 @table @asis
8764
8765 @item @code{-c}
8766
8767 Compile. Always use this switch when compiling Ada programs.
8768
8769 Note: for some other languages when using @code{gcc}, notably in
8770 the case of C and C++, it is possible to use
8771 use @code{gcc} without a @code{-c} switch to
8772 compile and link in one step. In the case of GNAT, you
8773 cannot use this approach, because the binder must be run
8774 and @code{gcc} cannot be used to run the GNAT binder.
8775 @end table
8776
8777 @geindex -fcallgraph-info (gcc)
8778
8779
8780 @table @asis
8781
8782 @item @code{-fcallgraph-info[=su,da]}
8783
8784 Makes the compiler output callgraph information for the program, on a
8785 per-file basis. The information is generated in the VCG format. It can
8786 be decorated with additional, per-node and/or per-edge information, if a
8787 list of comma-separated markers is additionally specified. When the
8788 @code{su} marker is specified, the callgraph is decorated with stack usage
8789 information; it is equivalent to @code{-fstack-usage}. When the @code{da}
8790 marker is specified, the callgraph is decorated with information about
8791 dynamically allocated objects.
8792 @end table
8793
8794 @geindex -fdump-scos (gcc)
8795
8796
8797 @table @asis
8798
8799 @item @code{-fdump-scos}
8800
8801 Generates SCO (Source Coverage Obligation) information in the ALI file.
8802 This information is used by advanced coverage tools. See unit @code{SCOs}
8803 in the compiler sources for details in files @code{scos.ads} and
8804 @code{scos.adb}.
8805 @end table
8806
8807 @geindex -fgnat-encodings (gcc)
8808
8809
8810 @table @asis
8811
8812 @item @code{-fgnat-encodings=[all|gdb|minimal]}
8813
8814 This switch controls the balance between GNAT encodings and standard DWARF
8815 emitted in the debug information.
8816 @end table
8817
8818 @geindex -flto (gcc)
8819
8820
8821 @table @asis
8822
8823 @item @code{-flto[=@emph{n}]}
8824
8825 Enables Link Time Optimization. This switch must be used in conjunction
8826 with the @code{-Ox} switches (but not with the @code{-gnatn} switch
8827 since it is a full replacement for the latter) and instructs the compiler
8828 to defer most optimizations until the link stage. The advantage of this
8829 approach is that the compiler can do a whole-program analysis and choose
8830 the best interprocedural optimization strategy based on a complete view
8831 of the program, instead of a fragmentary view with the usual approach.
8832 This can also speed up the compilation of big programs and reduce the
8833 size of the executable, compared with a traditional per-unit compilation
8834 with inlining across units enabled by the @code{-gnatn} switch.
8835 The drawback of this approach is that it may require more memory and that
8836 the debugging information generated by -g with it might be hardly usable.
8837 The switch, as well as the accompanying @code{-Ox} switches, must be
8838 specified both for the compilation and the link phases.
8839 If the @code{n} parameter is specified, the optimization and final code
8840 generation at link time are executed using @code{n} parallel jobs by
8841 means of an installed @code{make} program.
8842 @end table
8843
8844 @geindex -fno-inline (gcc)
8845
8846
8847 @table @asis
8848
8849 @item @code{-fno-inline}
8850
8851 Suppresses all inlining, unless requested with pragma @code{Inline_Always}. The
8852 effect is enforced regardless of other optimization or inlining switches.
8853 Note that inlining can also be suppressed on a finer-grained basis with
8854 pragma @code{No_Inline}.
8855 @end table
8856
8857 @geindex -fno-inline-functions (gcc)
8858
8859
8860 @table @asis
8861
8862 @item @code{-fno-inline-functions}
8863
8864 Suppresses automatic inlining of subprograms, which is enabled
8865 if @code{-O3} is used.
8866 @end table
8867
8868 @geindex -fno-inline-small-functions (gcc)
8869
8870
8871 @table @asis
8872
8873 @item @code{-fno-inline-small-functions}
8874
8875 Suppresses automatic inlining of small subprograms, which is enabled
8876 if @code{-O2} is used.
8877 @end table
8878
8879 @geindex -fno-inline-functions-called-once (gcc)
8880
8881
8882 @table @asis
8883
8884 @item @code{-fno-inline-functions-called-once}
8885
8886 Suppresses inlining of subprograms local to the unit and called once
8887 from within it, which is enabled if @code{-O1} is used.
8888 @end table
8889
8890 @geindex -fno-ivopts (gcc)
8891
8892
8893 @table @asis
8894
8895 @item @code{-fno-ivopts}
8896
8897 Suppresses high-level loop induction variable optimizations, which are
8898 enabled if @code{-O1} is used. These optimizations are generally
8899 profitable but, for some specific cases of loops with numerous uses
8900 of the iteration variable that follow a common pattern, they may end
8901 up destroying the regularity that could be exploited at a lower level
8902 and thus producing inferior code.
8903 @end table
8904
8905 @geindex -fno-strict-aliasing (gcc)
8906
8907
8908 @table @asis
8909
8910 @item @code{-fno-strict-aliasing}
8911
8912 Causes the compiler to avoid assumptions regarding non-aliasing
8913 of objects of different types. See
8914 @ref{f3,,Optimization and Strict Aliasing} for details.
8915 @end table
8916
8917 @geindex -fno-strict-overflow (gcc)
8918
8919
8920 @table @asis
8921
8922 @item @code{-fno-strict-overflow}
8923
8924 Causes the compiler to avoid assumptions regarding the rules of signed
8925 integer overflow. These rules specify that signed integer overflow will
8926 result in a Constraint_Error exception at run time and are enforced in
8927 default mode by the compiler, so this switch should not be necessary in
8928 normal operating mode. It might be useful in conjunction with @code{-gnato0}
8929 for very peculiar cases of low-level programming.
8930 @end table
8931
8932 @geindex -fstack-check (gcc)
8933
8934
8935 @table @asis
8936
8937 @item @code{-fstack-check}
8938
8939 Activates stack checking.
8940 See @ref{f4,,Stack Overflow Checking} for details.
8941 @end table
8942
8943 @geindex -fstack-usage (gcc)
8944
8945
8946 @table @asis
8947
8948 @item @code{-fstack-usage}
8949
8950 Makes the compiler output stack usage information for the program, on a
8951 per-subprogram basis. See @ref{f5,,Static Stack Usage Analysis} for details.
8952 @end table
8953
8954 @geindex -g (gcc)
8955
8956
8957 @table @asis
8958
8959 @item @code{-g}
8960
8961 Generate debugging information. This information is stored in the object
8962 file and copied from there to the final executable file by the linker,
8963 where it can be read by the debugger. You must use the
8964 @code{-g} switch if you plan on using the debugger.
8965 @end table
8966
8967 @geindex -gnat05 (gcc)
8968
8969
8970 @table @asis
8971
8972 @item @code{-gnat05}
8973
8974 Allow full Ada 2005 features.
8975 @end table
8976
8977 @geindex -gnat12 (gcc)
8978
8979
8980 @table @asis
8981
8982 @item @code{-gnat12}
8983
8984 Allow full Ada 2012 features.
8985 @end table
8986
8987 @geindex -gnat83 (gcc)
8988
8989 @geindex -gnat2005 (gcc)
8990
8991
8992 @table @asis
8993
8994 @item @code{-gnat2005}
8995
8996 Allow full Ada 2005 features (same as @code{-gnat05})
8997 @end table
8998
8999 @geindex -gnat2012 (gcc)
9000
9001
9002 @table @asis
9003
9004 @item @code{-gnat2012}
9005
9006 Allow full Ada 2012 features (same as @code{-gnat12})
9007
9008 @item @code{-gnat83}
9009
9010 Enforce Ada 83 restrictions.
9011 @end table
9012
9013 @geindex -gnat95 (gcc)
9014
9015
9016 @table @asis
9017
9018 @item @code{-gnat95}
9019
9020 Enforce Ada 95 restrictions.
9021
9022 Note: for compatibility with some Ada 95 compilers which support only
9023 the @code{overriding} keyword of Ada 2005, the @code{-gnatd.D} switch can
9024 be used along with @code{-gnat95} to achieve a similar effect with GNAT.
9025
9026 @code{-gnatd.D} instructs GNAT to consider @code{overriding} as a keyword
9027 and handle its associated semantic checks, even in Ada 95 mode.
9028 @end table
9029
9030 @geindex -gnata (gcc)
9031
9032
9033 @table @asis
9034
9035 @item @code{-gnata}
9036
9037 Assertions enabled. @code{Pragma Assert} and @code{pragma Debug} to be
9038 activated. Note that these pragmas can also be controlled using the
9039 configuration pragmas @code{Assertion_Policy} and @code{Debug_Policy}.
9040 It also activates pragmas @code{Check}, @code{Precondition}, and
9041 @code{Postcondition}. Note that these pragmas can also be controlled
9042 using the configuration pragma @code{Check_Policy}. In Ada 2012, it
9043 also activates all assertions defined in the RM as aspects: preconditions,
9044 postconditions, type invariants and (sub)type predicates. In all Ada modes,
9045 corresponding pragmas for type invariants and (sub)type predicates are
9046 also activated. The default is that all these assertions are disabled,
9047 and have no effect, other than being checked for syntactic validity, and
9048 in the case of subtype predicates, constructions such as membership tests
9049 still test predicates even if assertions are turned off.
9050 @end table
9051
9052 @geindex -gnatA (gcc)
9053
9054
9055 @table @asis
9056
9057 @item @code{-gnatA}
9058
9059 Avoid processing @code{gnat.adc}. If a @code{gnat.adc} file is present,
9060 it will be ignored.
9061 @end table
9062
9063 @geindex -gnatb (gcc)
9064
9065
9066 @table @asis
9067
9068 @item @code{-gnatb}
9069
9070 Generate brief messages to @code{stderr} even if verbose mode set.
9071 @end table
9072
9073 @geindex -gnatB (gcc)
9074
9075
9076 @table @asis
9077
9078 @item @code{-gnatB}
9079
9080 Assume no invalid (bad) values except for 'Valid attribute use
9081 (@ref{f6,,Validity Checking}).
9082 @end table
9083
9084 @geindex -gnatc (gcc)
9085
9086
9087 @table @asis
9088
9089 @item @code{-gnatc}
9090
9091 Check syntax and semantics only (no code generation attempted). When the
9092 compiler is invoked by @code{gnatmake}, if the switch @code{-gnatc} is
9093 only given to the compiler (after @code{-cargs} or in package Compiler of
9094 the project file, @code{gnatmake} will fail because it will not find the
9095 object file after compilation. If @code{gnatmake} is called with
9096 @code{-gnatc} as a builder switch (before @code{-cargs} or in package
9097 Builder of the project file) then @code{gnatmake} will not fail because
9098 it will not look for the object files after compilation, and it will not try
9099 to build and link.
9100 @end table
9101
9102 @geindex -gnatC (gcc)
9103
9104
9105 @table @asis
9106
9107 @item @code{-gnatC}
9108
9109 Generate CodePeer intermediate format (no code generation attempted).
9110 This switch will generate an intermediate representation suitable for
9111 use by CodePeer (@code{.scil} files). This switch is not compatible with
9112 code generation (it will, among other things, disable some switches such
9113 as -gnatn, and enable others such as -gnata).
9114 @end table
9115
9116 @geindex -gnatd (gcc)
9117
9118
9119 @table @asis
9120
9121 @item @code{-gnatd}
9122
9123 Specify debug options for the compiler. The string of characters after
9124 the @code{-gnatd} specify the specific debug options. The possible
9125 characters are 0-9, a-z, A-Z, optionally preceded by a dot. See
9126 compiler source file @code{debug.adb} for details of the implemented
9127 debug options. Certain debug options are relevant to applications
9128 programmers, and these are documented at appropriate points in this
9129 users guide.
9130 @end table
9131
9132 @geindex -gnatD[nn] (gcc)
9133
9134
9135 @table @asis
9136
9137 @item @code{-gnatD}
9138
9139 Create expanded source files for source level debugging. This switch
9140 also suppresses generation of cross-reference information
9141 (see @code{-gnatx}). Note that this switch is not allowed if a previous
9142 -gnatR switch has been given, since these two switches are not compatible.
9143 @end table
9144
9145 @geindex -gnateA (gcc)
9146
9147
9148 @table @asis
9149
9150 @item @code{-gnateA}
9151
9152 Check that the actual parameters of a subprogram call are not aliases of one
9153 another. To qualify as aliasing, the actuals must denote objects of a composite
9154 type, their memory locations must be identical or overlapping, and at least one
9155 of the corresponding formal parameters must be of mode OUT or IN OUT.
9156
9157 @example
9158 type Rec_Typ is record
9159 Data : Integer := 0;
9160 end record;
9161
9162 function Self (Val : Rec_Typ) return Rec_Typ is
9163 begin
9164 return Val;
9165 end Self;
9166
9167 procedure Detect_Aliasing (Val_1 : in out Rec_Typ; Val_2 : Rec_Typ) is
9168 begin
9169 null;
9170 end Detect_Aliasing;
9171
9172 Obj : Rec_Typ;
9173
9174 Detect_Aliasing (Obj, Obj);
9175 Detect_Aliasing (Obj, Self (Obj));
9176 @end example
9177
9178 In the example above, the first call to @code{Detect_Aliasing} fails with a
9179 @code{Program_Error} at run time because the actuals for @code{Val_1} and
9180 @code{Val_2} denote the same object. The second call executes without raising
9181 an exception because @code{Self(Obj)} produces an anonymous object which does
9182 not share the memory location of @code{Obj}.
9183 @end table
9184
9185 @geindex -gnatec (gcc)
9186
9187
9188 @table @asis
9189
9190 @item @code{-gnatec=@emph{path}}
9191
9192 Specify a configuration pragma file
9193 (the equal sign is optional)
9194 (@ref{79,,The Configuration Pragmas Files}).
9195 @end table
9196
9197 @geindex -gnateC (gcc)
9198
9199
9200 @table @asis
9201
9202 @item @code{-gnateC}
9203
9204 Generate CodePeer messages in a compiler-like format. This switch is only
9205 effective if @code{-gnatcC} is also specified and requires an installation
9206 of CodePeer.
9207 @end table
9208
9209 @geindex -gnated (gcc)
9210
9211
9212 @table @asis
9213
9214 @item @code{-gnated}
9215
9216 Disable atomic synchronization
9217 @end table
9218
9219 @geindex -gnateD (gcc)
9220
9221
9222 @table @asis
9223
9224 @item @code{-gnateDsymbol[=@emph{value}]}
9225
9226 Defines a symbol, associated with @code{value}, for preprocessing.
9227 (@ref{18,,Integrated Preprocessing}).
9228 @end table
9229
9230 @geindex -gnateE (gcc)
9231
9232
9233 @table @asis
9234
9235 @item @code{-gnateE}
9236
9237 Generate extra information in exception messages. In particular, display
9238 extra column information and the value and range associated with index and
9239 range check failures, and extra column information for access checks.
9240 In cases where the compiler is able to determine at compile time that
9241 a check will fail, it gives a warning, and the extra information is not
9242 produced at run time.
9243 @end table
9244
9245 @geindex -gnatef (gcc)
9246
9247
9248 @table @asis
9249
9250 @item @code{-gnatef}
9251
9252 Display full source path name in brief error messages.
9253 @end table
9254
9255 @geindex -gnateF (gcc)
9256
9257
9258 @table @asis
9259
9260 @item @code{-gnateF}
9261
9262 Check for overflow on all floating-point operations, including those
9263 for unconstrained predefined types. See description of pragma
9264 @code{Check_Float_Overflow} in GNAT RM.
9265 @end table
9266
9267 @geindex -gnateg (gcc)
9268
9269 @code{-gnateg}
9270 @code{-gnatceg}
9271
9272 @quotation
9273
9274 The @code{-gnatc} switch must always be specified before this switch, e.g.
9275 @code{-gnatceg}. Generate a C header from the Ada input file. See
9276 @ref{ca,,Generating C Headers for Ada Specifications} for more
9277 information.
9278 @end quotation
9279
9280 @geindex -gnateG (gcc)
9281
9282
9283 @table @asis
9284
9285 @item @code{-gnateG}
9286
9287 Save result of preprocessing in a text file.
9288 @end table
9289
9290 @geindex -gnatei (gcc)
9291
9292
9293 @table @asis
9294
9295 @item @code{-gnatei@emph{nnn}}
9296
9297 Set maximum number of instantiations during compilation of a single unit to
9298 @code{nnn}. This may be useful in increasing the default maximum of 8000 for
9299 the rare case when a single unit legitimately exceeds this limit.
9300 @end table
9301
9302 @geindex -gnateI (gcc)
9303
9304
9305 @table @asis
9306
9307 @item @code{-gnateI@emph{nnn}}
9308
9309 Indicates that the source is a multi-unit source and that the index of the
9310 unit to compile is @code{nnn}. @code{nnn} needs to be a positive number and need
9311 to be a valid index in the multi-unit source.
9312 @end table
9313
9314 @geindex -gnatel (gcc)
9315
9316
9317 @table @asis
9318
9319 @item @code{-gnatel}
9320
9321 This switch can be used with the static elaboration model to issue info
9322 messages showing
9323 where implicit @code{pragma Elaborate} and @code{pragma Elaborate_All}
9324 are generated. This is useful in diagnosing elaboration circularities
9325 caused by these implicit pragmas when using the static elaboration
9326 model. See See the section in this guide on elaboration checking for
9327 further details. These messages are not generated by default, and are
9328 intended only for temporary use when debugging circularity problems.
9329 @end table
9330
9331 @geindex -gnatel (gcc)
9332
9333
9334 @table @asis
9335
9336 @item @code{-gnateL}
9337
9338 This switch turns off the info messages about implicit elaboration pragmas.
9339 @end table
9340
9341 @geindex -gnatem (gcc)
9342
9343
9344 @table @asis
9345
9346 @item @code{-gnatem=@emph{path}}
9347
9348 Specify a mapping file
9349 (the equal sign is optional)
9350 (@ref{f7,,Units to Sources Mapping Files}).
9351 @end table
9352
9353 @geindex -gnatep (gcc)
9354
9355
9356 @table @asis
9357
9358 @item @code{-gnatep=@emph{file}}
9359
9360 Specify a preprocessing data file
9361 (the equal sign is optional)
9362 (@ref{18,,Integrated Preprocessing}).
9363 @end table
9364
9365 @geindex -gnateP (gcc)
9366
9367
9368 @table @asis
9369
9370 @item @code{-gnateP}
9371
9372 Turn categorization dependency errors into warnings.
9373 Ada requires that units that WITH one another have compatible categories, for
9374 example a Pure unit cannot WITH a Preelaborate unit. If this switch is used,
9375 these errors become warnings (which can be ignored, or suppressed in the usual
9376 manner). This can be useful in some specialized circumstances such as the
9377 temporary use of special test software.
9378 @end table
9379
9380 @geindex -gnateS (gcc)
9381
9382
9383 @table @asis
9384
9385 @item @code{-gnateS}
9386
9387 Synonym of @code{-fdump-scos}, kept for backwards compatibility.
9388 @end table
9389
9390 @geindex -gnatet=file (gcc)
9391
9392
9393 @table @asis
9394
9395 @item @code{-gnatet=@emph{path}}
9396
9397 Generate target dependent information. The format of the output file is
9398 described in the section about switch @code{-gnateT}.
9399 @end table
9400
9401 @geindex -gnateT (gcc)
9402
9403
9404 @table @asis
9405
9406 @item @code{-gnateT=@emph{path}}
9407
9408 Read target dependent information, such as endianness or sizes and alignments
9409 of base type. If this switch is passed, the default target dependent
9410 information of the compiler is replaced by the one read from the input file.
9411 This is used by tools other than the compiler, e.g. to do
9412 semantic analysis of programs that will run on some other target than
9413 the machine on which the tool is run.
9414
9415 The following target dependent values should be defined,
9416 where @code{Nat} denotes a natural integer value, @code{Pos} denotes a
9417 positive integer value, and fields marked with a question mark are
9418 boolean fields, where a value of 0 is False, and a value of 1 is True:
9419
9420 @example
9421 Bits_BE : Nat; -- Bits stored big-endian?
9422 Bits_Per_Unit : Pos; -- Bits in a storage unit
9423 Bits_Per_Word : Pos; -- Bits in a word
9424 Bytes_BE : Nat; -- Bytes stored big-endian?
9425 Char_Size : Pos; -- Standard.Character'Size
9426 Double_Float_Alignment : Nat; -- Alignment of double float
9427 Double_Scalar_Alignment : Nat; -- Alignment of double length scalar
9428 Double_Size : Pos; -- Standard.Long_Float'Size
9429 Float_Size : Pos; -- Standard.Float'Size
9430 Float_Words_BE : Nat; -- Float words stored big-endian?
9431 Int_Size : Pos; -- Standard.Integer'Size
9432 Long_Double_Size : Pos; -- Standard.Long_Long_Float'Size
9433 Long_Long_Size : Pos; -- Standard.Long_Long_Integer'Size
9434 Long_Size : Pos; -- Standard.Long_Integer'Size
9435 Maximum_Alignment : Pos; -- Maximum permitted alignment
9436 Max_Unaligned_Field : Pos; -- Maximum size for unaligned bit field
9437 Pointer_Size : Pos; -- System.Address'Size
9438 Short_Enums : Nat; -- Foreign enums use short size?
9439 Short_Size : Pos; -- Standard.Short_Integer'Size
9440 Strict_Alignment : Nat; -- Strict alignment?
9441 System_Allocator_Alignment : Nat; -- Alignment for malloc calls
9442 Wchar_T_Size : Pos; -- Interfaces.C.wchar_t'Size
9443 Words_BE : Nat; -- Words stored big-endian?
9444 @end example
9445
9446 @code{Bits_Per_Unit} is the number of bits in a storage unit, the equivalent of
9447 GCC macro @code{BITS_PER_UNIT} documented as follows: @cite{Define this macro to be the number of bits in an addressable storage unit (byte); normally 8.}
9448
9449 @code{Bits_Per_Word} is the number of bits in a machine word, the equivalent of
9450 GCC macro @code{BITS_PER_WORD} documented as follows: @cite{Number of bits in a word; normally 32.}
9451
9452 @code{Double_Float_Alignment}, if not zero, is the maximum alignment that the
9453 compiler can choose by default for a 64-bit floating-point type or object.
9454
9455 @code{Double_Scalar_Alignment}, if not zero, is the maximum alignment that the
9456 compiler can choose by default for a 64-bit or larger scalar type or object.
9457
9458 @code{Maximum_Alignment} is the maximum alignment that the compiler can choose
9459 by default for a type or object, which is also the maximum alignment that can
9460 be specified in GNAT. It is computed for GCC backends as @code{BIGGEST_ALIGNMENT
9461 / BITS_PER_UNIT} where GCC macro @code{BIGGEST_ALIGNMENT} is documented as
9462 follows: @cite{Biggest alignment that any data type can require on this machine@comma{} in bits.}
9463
9464 @code{Max_Unaligned_Field} is the maximum size for unaligned bit field, which is
9465 64 for the majority of GCC targets (but can be different on some targets like
9466 AAMP).
9467
9468 @code{Strict_Alignment} is the equivalent of GCC macro @code{STRICT_ALIGNMENT}
9469 documented as follows: @cite{Define this macro to be the value 1 if instructions will fail to work if given data not on the nominal alignment. If instructions will merely go slower in that case@comma{} define this macro as 0.}
9470
9471 @code{System_Allocator_Alignment} is the guaranteed alignment of data returned
9472 by calls to @code{malloc}.
9473
9474 The format of the input file is as follows. First come the values of
9475 the variables defined above, with one line per value:
9476
9477 @example
9478 name value
9479 @end example
9480
9481 where @code{name} is the name of the parameter, spelled out in full,
9482 and cased as in the above list, and @code{value} is an unsigned decimal
9483 integer. Two or more blanks separates the name from the value.
9484
9485 All the variables must be present, in alphabetical order (i.e. the
9486 same order as the list above).
9487
9488 Then there is a blank line to separate the two parts of the file. Then
9489 come the lines showing the floating-point types to be registered, with
9490 one line per registered mode:
9491
9492 @example
9493 name digs float_rep size alignment
9494 @end example
9495
9496 where @code{name} is the string name of the type (which can have
9497 single spaces embedded in the name (e.g. long double), @code{digs} is
9498 the number of digits for the floating-point type, @code{float_rep} is
9499 the float representation (I/V/A for IEEE-754-Binary, Vax_Native,
9500 AAMP), @code{size} is the size in bits, @code{alignment} is the
9501 alignment in bits. The name is followed by at least two blanks, fields
9502 are separated by at least one blank, and a LF character immediately
9503 follows the alignment field.
9504
9505 Here is an example of a target parameterization file:
9506
9507 @example
9508 Bits_BE 0
9509 Bits_Per_Unit 8
9510 Bits_Per_Word 64
9511 Bytes_BE 0
9512 Char_Size 8
9513 Double_Float_Alignment 0
9514 Double_Scalar_Alignment 0
9515 Double_Size 64
9516 Float_Size 32
9517 Float_Words_BE 0
9518 Int_Size 64
9519 Long_Double_Size 128
9520 Long_Long_Size 64
9521 Long_Size 64
9522 Maximum_Alignment 16
9523 Max_Unaligned_Field 64
9524 Pointer_Size 64
9525 Short_Size 16
9526 Strict_Alignment 0
9527 System_Allocator_Alignment 16
9528 Wchar_T_Size 32
9529 Words_BE 0
9530
9531 float 15 I 64 64
9532 double 15 I 64 64
9533 long double 18 I 80 128
9534 TF 33 I 128 128
9535 @end example
9536 @end table
9537
9538 @geindex -gnateu (gcc)
9539
9540
9541 @table @asis
9542
9543 @item @code{-gnateu}
9544
9545 Ignore unrecognized validity, warning, and style switches that
9546 appear after this switch is given. This may be useful when
9547 compiling sources developed on a later version of the compiler
9548 with an earlier version. Of course the earlier version must
9549 support this switch.
9550 @end table
9551
9552 @geindex -gnateV (gcc)
9553
9554
9555 @table @asis
9556
9557 @item @code{-gnateV}
9558
9559 Check that all actual parameters of a subprogram call are valid according to
9560 the rules of validity checking (@ref{f6,,Validity Checking}).
9561 @end table
9562
9563 @geindex -gnateY (gcc)
9564
9565
9566 @table @asis
9567
9568 @item @code{-gnateY}
9569
9570 Ignore all STYLE_CHECKS pragmas. Full legality checks
9571 are still carried out, but the pragmas have no effect
9572 on what style checks are active. This allows all style
9573 checking options to be controlled from the command line.
9574 @end table
9575
9576 @geindex -gnatE (gcc)
9577
9578
9579 @table @asis
9580
9581 @item @code{-gnatE}
9582
9583 Dynamic elaboration checking mode enabled. For further details see
9584 @ref{f,,Elaboration Order Handling in GNAT}.
9585 @end table
9586
9587 @geindex -gnatf (gcc)
9588
9589
9590 @table @asis
9591
9592 @item @code{-gnatf}
9593
9594 Full errors. Multiple errors per line, all undefined references, do not
9595 attempt to suppress cascaded errors.
9596 @end table
9597
9598 @geindex -gnatF (gcc)
9599
9600
9601 @table @asis
9602
9603 @item @code{-gnatF}
9604
9605 Externals names are folded to all uppercase.
9606 @end table
9607
9608 @geindex -gnatg (gcc)
9609
9610
9611 @table @asis
9612
9613 @item @code{-gnatg}
9614
9615 Internal GNAT implementation mode. This should not be used for applications
9616 programs, it is intended only for use by the compiler and its run-time
9617 library. For documentation, see the GNAT sources. Note that @code{-gnatg}
9618 implies @code{-gnatw.ge} and @code{-gnatyg} so that all standard
9619 warnings and all standard style options are turned on. All warnings and style
9620 messages are treated as errors.
9621 @end table
9622
9623 @geindex -gnatG[nn] (gcc)
9624
9625
9626 @table @asis
9627
9628 @item @code{-gnatG=nn}
9629
9630 List generated expanded code in source form.
9631 @end table
9632
9633 @geindex -gnath (gcc)
9634
9635
9636 @table @asis
9637
9638 @item @code{-gnath}
9639
9640 Output usage information. The output is written to @code{stdout}.
9641 @end table
9642
9643 @geindex -gnatH (gcc)
9644
9645
9646 @table @asis
9647
9648 @item @code{-gnatH}
9649
9650 Legacy elaboration-checking mode enabled. When this switch is in effect,
9651 the pre-18.x access-before-elaboration model becomes the de facto model.
9652 For further details see @ref{f,,Elaboration Order Handling in GNAT}.
9653 @end table
9654
9655 @geindex -gnati (gcc)
9656
9657
9658 @table @asis
9659
9660 @item @code{-gnati@emph{c}}
9661
9662 Identifier character set (@code{c} = 1/2/3/4/8/9/p/f/n/w).
9663 For details of the possible selections for @code{c},
9664 see @ref{48,,Character Set Control}.
9665 @end table
9666
9667 @geindex -gnatI (gcc)
9668
9669
9670 @table @asis
9671
9672 @item @code{-gnatI}
9673
9674 Ignore representation clauses. When this switch is used,
9675 representation clauses are treated as comments. This is useful
9676 when initially porting code where you want to ignore rep clause
9677 problems, and also for compiling foreign code (particularly
9678 for use with ASIS). The representation clauses that are ignored
9679 are: enumeration_representation_clause, record_representation_clause,
9680 and attribute_definition_clause for the following attributes:
9681 Address, Alignment, Bit_Order, Component_Size, Machine_Radix,
9682 Object_Size, Scalar_Storage_Order, Size, Small, Stream_Size,
9683 and Value_Size. Pragma Default_Scalar_Storage_Order is also ignored.
9684 Note that this option should be used only for compiling -- the
9685 code is likely to malfunction at run time.
9686 @end table
9687
9688 @geindex -gnatjnn (gcc)
9689
9690
9691 @table @asis
9692
9693 @item @code{-gnatj@emph{nn}}
9694
9695 Reformat error messages to fit on @code{nn} character lines
9696 @end table
9697
9698 @geindex -gnatJ (gcc)
9699
9700
9701 @table @asis
9702
9703 @item @code{-gnatJ}
9704
9705 Permissive elaboration-checking mode enabled. When this switch is in effect,
9706 the post-18.x access-before-elaboration model ignores potential issues with:
9707
9708
9709 @itemize -
9710
9711 @item
9712 Accept statements
9713
9714 @item
9715 Activations of tasks defined in instances
9716
9717 @item
9718 Assertion pragmas
9719
9720 @item
9721 Calls from within an instance to its enclosing context
9722
9723 @item
9724 Calls through generic formal parameters
9725
9726 @item
9727 Calls to subprograms defined in instances
9728
9729 @item
9730 Entry calls
9731
9732 @item
9733 Indirect calls using 'Access
9734
9735 @item
9736 Requeue statements
9737
9738 @item
9739 Select statements
9740
9741 @item
9742 Synchronous task suspension
9743 @end itemize
9744
9745 and does not emit compile-time diagnostics or run-time checks. For further
9746 details see @ref{f,,Elaboration Order Handling in GNAT}.
9747 @end table
9748
9749 @geindex -gnatk (gcc)
9750
9751
9752 @table @asis
9753
9754 @item @code{-gnatk=@emph{n}}
9755
9756 Limit file names to @code{n} (1-999) characters (@code{k} = krunch).
9757 @end table
9758
9759 @geindex -gnatl (gcc)
9760
9761
9762 @table @asis
9763
9764 @item @code{-gnatl}
9765
9766 Output full source listing with embedded error messages.
9767 @end table
9768
9769 @geindex -gnatL (gcc)
9770
9771
9772 @table @asis
9773
9774 @item @code{-gnatL}
9775
9776 Used in conjunction with -gnatG or -gnatD to intersperse original
9777 source lines (as comment lines with line numbers) in the expanded
9778 source output.
9779 @end table
9780
9781 @geindex -gnatm (gcc)
9782
9783
9784 @table @asis
9785
9786 @item @code{-gnatm=@emph{n}}
9787
9788 Limit number of detected error or warning messages to @code{n}
9789 where @code{n} is in the range 1..999999. The default setting if
9790 no switch is given is 9999. If the number of warnings reaches this
9791 limit, then a message is output and further warnings are suppressed,
9792 but the compilation is continued. If the number of error messages
9793 reaches this limit, then a message is output and the compilation
9794 is abandoned. The equal sign here is optional. A value of zero
9795 means that no limit applies.
9796 @end table
9797
9798 @geindex -gnatn (gcc)
9799
9800
9801 @table @asis
9802
9803 @item @code{-gnatn[12]}
9804
9805 Activate inlining across units for subprograms for which pragma @code{Inline}
9806 is specified. This inlining is performed by the GCC back-end. An optional
9807 digit sets the inlining level: 1 for moderate inlining across units
9808 or 2 for full inlining across units. If no inlining level is specified,
9809 the compiler will pick it based on the optimization level.
9810 @end table
9811
9812 @geindex -gnatN (gcc)
9813
9814
9815 @table @asis
9816
9817 @item @code{-gnatN}
9818
9819 Activate front end inlining for subprograms for which
9820 pragma @code{Inline} is specified. This inlining is performed
9821 by the front end and will be visible in the
9822 @code{-gnatG} output.
9823
9824 When using a gcc-based back end (in practice this means using any version
9825 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
9826 @code{-gnatN} is deprecated, and the use of @code{-gnatn} is preferred.
9827 Historically front end inlining was more extensive than the gcc back end
9828 inlining, but that is no longer the case.
9829 @end table
9830
9831 @geindex -gnato0 (gcc)
9832
9833
9834 @table @asis
9835
9836 @item @code{-gnato0}
9837
9838 Suppresses overflow checking. This causes the behavior of the compiler to
9839 match the default for older versions where overflow checking was suppressed
9840 by default. This is equivalent to having
9841 @code{pragma Suppress (Overflow_Check)} in a configuration pragma file.
9842 @end table
9843
9844 @geindex -gnato?? (gcc)
9845
9846
9847 @table @asis
9848
9849 @item @code{-gnato??}
9850
9851 Set default mode for handling generation of code to avoid intermediate
9852 arithmetic overflow. Here @code{??} is two digits, a
9853 single digit, or nothing. Each digit is one of the digits @code{1}
9854 through @code{3}:
9855
9856
9857 @multitable {xxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
9858 @item
9859
9860 Digit
9861
9862 @tab
9863
9864 Interpretation
9865
9866 @item
9867
9868 @emph{1}
9869
9870 @tab
9871
9872 All intermediate overflows checked against base type (@code{STRICT})
9873
9874 @item
9875
9876 @emph{2}
9877
9878 @tab
9879
9880 Minimize intermediate overflows (@code{MINIMIZED})
9881
9882 @item
9883
9884 @emph{3}
9885
9886 @tab
9887
9888 Eliminate intermediate overflows (@code{ELIMINATED})
9889
9890 @end multitable
9891
9892
9893 If only one digit appears, then it applies to all
9894 cases; if two digits are given, then the first applies outside
9895 assertions, pre/postconditions, and type invariants, and the second
9896 applies within assertions, pre/postconditions, and type invariants.
9897
9898 If no digits follow the @code{-gnato}, then it is equivalent to
9899 @code{-gnato11},
9900 causing all intermediate overflows to be handled in strict
9901 mode.
9902
9903 This switch also causes arithmetic overflow checking to be performed
9904 (as though @code{pragma Unsuppress (Overflow_Check)} had been specified).
9905
9906 The default if no option @code{-gnato} is given is that overflow handling
9907 is in @code{STRICT} mode (computations done using the base type), and that
9908 overflow checking is enabled.
9909
9910 Note that division by zero is a separate check that is not
9911 controlled by this switch (divide-by-zero checking is on by default).
9912
9913 See also @ref{f8,,Specifying the Desired Mode}.
9914 @end table
9915
9916 @geindex -gnatp (gcc)
9917
9918
9919 @table @asis
9920
9921 @item @code{-gnatp}
9922
9923 Suppress all checks. See @ref{f9,,Run-Time Checks} for details. This switch
9924 has no effect if cancelled by a subsequent @code{-gnat-p} switch.
9925 @end table
9926
9927 @geindex -gnat-p (gcc)
9928
9929
9930 @table @asis
9931
9932 @item @code{-gnat-p}
9933
9934 Cancel effect of previous @code{-gnatp} switch.
9935 @end table
9936
9937 @geindex -gnatP (gcc)
9938
9939
9940 @table @asis
9941
9942 @item @code{-gnatP}
9943
9944 Enable polling. This is required on some systems (notably Windows NT) to
9945 obtain asynchronous abort and asynchronous transfer of control capability.
9946 See @code{Pragma_Polling} in the @cite{GNAT_Reference_Manual} for full
9947 details.
9948 @end table
9949
9950 @geindex -gnatq (gcc)
9951
9952
9953 @table @asis
9954
9955 @item @code{-gnatq}
9956
9957 Don't quit. Try semantics, even if parse errors.
9958 @end table
9959
9960 @geindex -gnatQ (gcc)
9961
9962
9963 @table @asis
9964
9965 @item @code{-gnatQ}
9966
9967 Don't quit. Generate @code{ALI} and tree files even if illegalities.
9968 Note that code generation is still suppressed in the presence of any
9969 errors, so even with @code{-gnatQ} no object file is generated.
9970 @end table
9971
9972 @geindex -gnatr (gcc)
9973
9974
9975 @table @asis
9976
9977 @item @code{-gnatr}
9978
9979 Treat pragma Restrictions as Restriction_Warnings.
9980 @end table
9981
9982 @geindex -gnatR (gcc)
9983
9984
9985 @table @asis
9986
9987 @item @code{-gnatR[0|1|2|3|4][e][j][m][s]}
9988
9989 Output representation information for declared types, objects and
9990 subprograms. Note that this switch is not allowed if a previous
9991 @code{-gnatD} switch has been given, since these two switches
9992 are not compatible.
9993 @end table
9994
9995 @geindex -gnats (gcc)
9996
9997
9998 @table @asis
9999
10000 @item @code{-gnats}
10001
10002 Syntax check only.
10003 @end table
10004
10005 @geindex -gnatS (gcc)
10006
10007
10008 @table @asis
10009
10010 @item @code{-gnatS}
10011
10012 Print package Standard.
10013 @end table
10014
10015 @geindex -gnatT (gcc)
10016
10017
10018 @table @asis
10019
10020 @item @code{-gnatT@emph{nnn}}
10021
10022 All compiler tables start at @code{nnn} times usual starting size.
10023 @end table
10024
10025 @geindex -gnatu (gcc)
10026
10027
10028 @table @asis
10029
10030 @item @code{-gnatu}
10031
10032 List units for this compilation.
10033 @end table
10034
10035 @geindex -gnatU (gcc)
10036
10037
10038 @table @asis
10039
10040 @item @code{-gnatU}
10041
10042 Tag all error messages with the unique string 'error:'
10043 @end table
10044
10045 @geindex -gnatv (gcc)
10046
10047
10048 @table @asis
10049
10050 @item @code{-gnatv}
10051
10052 Verbose mode. Full error output with source lines to @code{stdout}.
10053 @end table
10054
10055 @geindex -gnatV (gcc)
10056
10057
10058 @table @asis
10059
10060 @item @code{-gnatV}
10061
10062 Control level of validity checking (@ref{f6,,Validity Checking}).
10063 @end table
10064
10065 @geindex -gnatw (gcc)
10066
10067
10068 @table @asis
10069
10070 @item @code{-gnatw@emph{xxx}}
10071
10072 Warning mode where
10073 @code{xxx} is a string of option letters that denotes
10074 the exact warnings that
10075 are enabled or disabled (@ref{fa,,Warning Message Control}).
10076 @end table
10077
10078 @geindex -gnatW (gcc)
10079
10080
10081 @table @asis
10082
10083 @item @code{-gnatW@emph{e}}
10084
10085 Wide character encoding method
10086 (@code{e}=n/h/u/s/e/8).
10087 @end table
10088
10089 @geindex -gnatx (gcc)
10090
10091
10092 @table @asis
10093
10094 @item @code{-gnatx}
10095
10096 Suppress generation of cross-reference information.
10097 @end table
10098
10099 @geindex -gnatX (gcc)
10100
10101
10102 @table @asis
10103
10104 @item @code{-gnatX}
10105
10106 Enable GNAT implementation extensions and latest Ada version.
10107 @end table
10108
10109 @geindex -gnaty (gcc)
10110
10111
10112 @table @asis
10113
10114 @item @code{-gnaty}
10115
10116 Enable built-in style checks (@ref{fb,,Style Checking}).
10117 @end table
10118
10119 @geindex -gnatz (gcc)
10120
10121
10122 @table @asis
10123
10124 @item @code{-gnatz@emph{m}}
10125
10126 Distribution stub generation and compilation
10127 (@code{m}=r/c for receiver/caller stubs).
10128 @end table
10129
10130 @geindex -I (gcc)
10131
10132
10133 @table @asis
10134
10135 @item @code{-I@emph{dir}}
10136
10137 @geindex RTL
10138
10139 Direct GNAT to search the @code{dir} directory for source files needed by
10140 the current compilation
10141 (see @ref{89,,Search Paths and the Run-Time Library (RTL)}).
10142 @end table
10143
10144 @geindex -I- (gcc)
10145
10146
10147 @table @asis
10148
10149 @item @code{-I-}
10150
10151 @geindex RTL
10152
10153 Except for the source file named in the command line, do not look for source
10154 files in the directory containing the source file named in the command line
10155 (see @ref{89,,Search Paths and the Run-Time Library (RTL)}).
10156 @end table
10157
10158 @geindex -o (gcc)
10159
10160
10161 @table @asis
10162
10163 @item @code{-o @emph{file}}
10164
10165 This switch is used in @code{gcc} to redirect the generated object file
10166 and its associated ALI file. Beware of this switch with GNAT, because it may
10167 cause the object file and ALI file to have different names which in turn
10168 may confuse the binder and the linker.
10169 @end table
10170
10171 @geindex -nostdinc (gcc)
10172
10173
10174 @table @asis
10175
10176 @item @code{-nostdinc}
10177
10178 Inhibit the search of the default location for the GNAT Run Time
10179 Library (RTL) source files.
10180 @end table
10181
10182 @geindex -nostdlib (gcc)
10183
10184
10185 @table @asis
10186
10187 @item @code{-nostdlib}
10188
10189 Inhibit the search of the default location for the GNAT Run Time
10190 Library (RTL) ALI files.
10191 @end table
10192
10193 @geindex -O (gcc)
10194
10195
10196 @table @asis
10197
10198 @item @code{-O[@emph{n}]}
10199
10200 @code{n} controls the optimization level:
10201
10202
10203 @multitable {xxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
10204 @item
10205
10206 @emph{n}
10207
10208 @tab
10209
10210 Effect
10211
10212 @item
10213
10214 @emph{0}
10215
10216 @tab
10217
10218 No optimization, the default setting if no @code{-O} appears
10219
10220 @item
10221
10222 @emph{1}
10223
10224 @tab
10225
10226 Normal optimization, the default if you specify @code{-O} without an
10227 operand. A good compromise between code quality and compilation
10228 time.
10229
10230 @item
10231
10232 @emph{2}
10233
10234 @tab
10235
10236 Extensive optimization, may improve execution time, possibly at
10237 the cost of substantially increased compilation time.
10238
10239 @item
10240
10241 @emph{3}
10242
10243 @tab
10244
10245 Same as @code{-O2}, and also includes inline expansion for small
10246 subprograms in the same unit.
10247
10248 @item
10249
10250 @emph{s}
10251
10252 @tab
10253
10254 Optimize space usage
10255
10256 @end multitable
10257
10258
10259 See also @ref{fc,,Optimization Levels}.
10260 @end table
10261
10262 @geindex -pass-exit-codes (gcc)
10263
10264
10265 @table @asis
10266
10267 @item @code{-pass-exit-codes}
10268
10269 Catch exit codes from the compiler and use the most meaningful as
10270 exit status.
10271 @end table
10272
10273 @geindex --RTS (gcc)
10274
10275
10276 @table @asis
10277
10278 @item @code{--RTS=@emph{rts-path}}
10279
10280 Specifies the default location of the run-time library. Same meaning as the
10281 equivalent @code{gnatmake} flag (@ref{dc,,Switches for gnatmake}).
10282 @end table
10283
10284 @geindex -S (gcc)
10285
10286
10287 @table @asis
10288
10289 @item @code{-S}
10290
10291 Used in place of @code{-c} to
10292 cause the assembler source file to be
10293 generated, using @code{.s} as the extension,
10294 instead of the object file.
10295 This may be useful if you need to examine the generated assembly code.
10296 @end table
10297
10298 @geindex -fverbose-asm (gcc)
10299
10300
10301 @table @asis
10302
10303 @item @code{-fverbose-asm}
10304
10305 Used in conjunction with @code{-S}
10306 to cause the generated assembly code file to be annotated with variable
10307 names, making it significantly easier to follow.
10308 @end table
10309
10310 @geindex -v (gcc)
10311
10312
10313 @table @asis
10314
10315 @item @code{-v}
10316
10317 Show commands generated by the @code{gcc} driver. Normally used only for
10318 debugging purposes or if you need to be sure what version of the
10319 compiler you are executing.
10320 @end table
10321
10322 @geindex -V (gcc)
10323
10324
10325 @table @asis
10326
10327 @item @code{-V @emph{ver}}
10328
10329 Execute @code{ver} version of the compiler. This is the @code{gcc}
10330 version, not the GNAT version.
10331 @end table
10332
10333 @geindex -w (gcc)
10334
10335
10336 @table @asis
10337
10338 @item @code{-w}
10339
10340 Turn off warnings generated by the back end of the compiler. Use of
10341 this switch also causes the default for front end warnings to be set
10342 to suppress (as though @code{-gnatws} had appeared at the start of
10343 the options).
10344 @end table
10345
10346 @geindex Combining GNAT switches
10347
10348 You may combine a sequence of GNAT switches into a single switch. For
10349 example, the combined switch
10350
10351 @quotation
10352
10353 @example
10354 -gnatofi3
10355 @end example
10356 @end quotation
10357
10358 is equivalent to specifying the following sequence of switches:
10359
10360 @quotation
10361
10362 @example
10363 -gnato -gnatf -gnati3
10364 @end example
10365 @end quotation
10366
10367 The following restrictions apply to the combination of switches
10368 in this manner:
10369
10370
10371 @itemize *
10372
10373 @item
10374 The switch @code{-gnatc} if combined with other switches must come
10375 first in the string.
10376
10377 @item
10378 The switch @code{-gnats} if combined with other switches must come
10379 first in the string.
10380
10381 @item
10382 The switches
10383 @code{-gnatzc} and @code{-gnatzr} may not be combined with any other
10384 switches, and only one of them may appear in the command line.
10385
10386 @item
10387 The switch @code{-gnat-p} may not be combined with any other switch.
10388
10389 @item
10390 Once a 'y' appears in the string (that is a use of the @code{-gnaty}
10391 switch), then all further characters in the switch are interpreted
10392 as style modifiers (see description of @code{-gnaty}).
10393
10394 @item
10395 Once a 'd' appears in the string (that is a use of the @code{-gnatd}
10396 switch), then all further characters in the switch are interpreted
10397 as debug flags (see description of @code{-gnatd}).
10398
10399 @item
10400 Once a 'w' appears in the string (that is a use of the @code{-gnatw}
10401 switch), then all further characters in the switch are interpreted
10402 as warning mode modifiers (see description of @code{-gnatw}).
10403
10404 @item
10405 Once a 'V' appears in the string (that is a use of the @code{-gnatV}
10406 switch), then all further characters in the switch are interpreted
10407 as validity checking options (@ref{f6,,Validity Checking}).
10408
10409 @item
10410 Option 'em', 'ec', 'ep', 'l=' and 'R' must be the last options in
10411 a combined list of options.
10412 @end itemize
10413
10414 @node Output and Error Message Control,Warning Message Control,Alphabetical List of All Switches,Compiler Switches
10415 @anchor{gnat_ugn/building_executable_programs_with_gnat id14}@anchor{fd}@anchor{gnat_ugn/building_executable_programs_with_gnat output-and-error-message-control}@anchor{fe}
10416 @subsection Output and Error Message Control
10417
10418
10419 @geindex stderr
10420
10421 The standard default format for error messages is called 'brief format'.
10422 Brief format messages are written to @code{stderr} (the standard error
10423 file) and have the following form:
10424
10425 @example
10426 e.adb:3:04: Incorrect spelling of keyword "function"
10427 e.adb:4:20: ";" should be "is"
10428 @end example
10429
10430 The first integer after the file name is the line number in the file,
10431 and the second integer is the column number within the line.
10432 @code{GNAT Studio} can parse the error messages
10433 and point to the referenced character.
10434 The following switches provide control over the error message
10435 format:
10436
10437 @geindex -gnatv (gcc)
10438
10439
10440 @table @asis
10441
10442 @item @code{-gnatv}
10443
10444 The @code{v} stands for verbose.
10445 The effect of this setting is to write long-format error
10446 messages to @code{stdout} (the standard output file.
10447 The same program compiled with the
10448 @code{-gnatv} switch would generate:
10449
10450 @example
10451 3. funcion X (Q : Integer)
10452 |
10453 >>> Incorrect spelling of keyword "function"
10454 4. return Integer;
10455 |
10456 >>> ";" should be "is"
10457 @end example
10458
10459 The vertical bar indicates the location of the error, and the @code{>>>}
10460 prefix can be used to search for error messages. When this switch is
10461 used the only source lines output are those with errors.
10462 @end table
10463
10464 @geindex -gnatl (gcc)
10465
10466
10467 @table @asis
10468
10469 @item @code{-gnatl}
10470
10471 The @code{l} stands for list.
10472 This switch causes a full listing of
10473 the file to be generated. In the case where a body is
10474 compiled, the corresponding spec is also listed, along
10475 with any subunits. Typical output from compiling a package
10476 body @code{p.adb} might look like:
10477
10478 @example
10479 Compiling: p.adb
10480
10481 1. package body p is
10482 2. procedure a;
10483 3. procedure a is separate;
10484 4. begin
10485 5. null
10486 |
10487 >>> missing ";"
10488
10489 6. end;
10490
10491 Compiling: p.ads
10492
10493 1. package p is
10494 2. pragma Elaborate_Body
10495 |
10496 >>> missing ";"
10497
10498 3. end p;
10499
10500 Compiling: p-a.adb
10501
10502 1. separate p
10503 |
10504 >>> missing "("
10505
10506 2. procedure a is
10507 3. begin
10508 4. null
10509 |
10510 >>> missing ";"
10511
10512 5. end;
10513 @end example
10514
10515 When you specify the @code{-gnatv} or @code{-gnatl} switches and
10516 standard output is redirected, a brief summary is written to
10517 @code{stderr} (standard error) giving the number of error messages and
10518 warning messages generated.
10519 @end table
10520
10521 @geindex -gnatl=fname (gcc)
10522
10523
10524 @table @asis
10525
10526 @item @code{-gnatl=@emph{fname}}
10527
10528 This has the same effect as @code{-gnatl} except that the output is
10529 written to a file instead of to standard output. If the given name
10530 @code{fname} does not start with a period, then it is the full name
10531 of the file to be written. If @code{fname} is an extension, it is
10532 appended to the name of the file being compiled. For example, if
10533 file @code{xyz.adb} is compiled with @code{-gnatl=.lst},
10534 then the output is written to file xyz.adb.lst.
10535 @end table
10536
10537 @geindex -gnatU (gcc)
10538
10539
10540 @table @asis
10541
10542 @item @code{-gnatU}
10543
10544 This switch forces all error messages to be preceded by the unique
10545 string 'error:'. This means that error messages take a few more
10546 characters in space, but allows easy searching for and identification
10547 of error messages.
10548 @end table
10549
10550 @geindex -gnatb (gcc)
10551
10552
10553 @table @asis
10554
10555 @item @code{-gnatb}
10556
10557 The @code{b} stands for brief.
10558 This switch causes GNAT to generate the
10559 brief format error messages to @code{stderr} (the standard error
10560 file) as well as the verbose
10561 format message or full listing (which as usual is written to
10562 @code{stdout} (the standard output file).
10563 @end table
10564
10565 @geindex -gnatm (gcc)
10566
10567
10568 @table @asis
10569
10570 @item @code{-gnatm=@emph{n}}
10571
10572 The @code{m} stands for maximum.
10573 @code{n} is a decimal integer in the
10574 range of 1 to 999999 and limits the number of error or warning
10575 messages to be generated. For example, using
10576 @code{-gnatm2} might yield
10577
10578 @example
10579 e.adb:3:04: Incorrect spelling of keyword "function"
10580 e.adb:5:35: missing ".."
10581 fatal error: maximum number of errors detected
10582 compilation abandoned
10583 @end example
10584
10585 The default setting if
10586 no switch is given is 9999. If the number of warnings reaches this
10587 limit, then a message is output and further warnings are suppressed,
10588 but the compilation is continued. If the number of error messages
10589 reaches this limit, then a message is output and the compilation
10590 is abandoned. A value of zero means that no limit applies.
10591
10592 Note that the equal sign is optional, so the switches
10593 @code{-gnatm2} and @code{-gnatm=2} are equivalent.
10594 @end table
10595
10596 @geindex -gnatf (gcc)
10597
10598
10599 @table @asis
10600
10601 @item @code{-gnatf}
10602
10603 @geindex Error messages
10604 @geindex suppressing
10605
10606 The @code{f} stands for full.
10607 Normally, the compiler suppresses error messages that are likely to be
10608 redundant. This switch causes all error
10609 messages to be generated. In particular, in the case of
10610 references to undefined variables. If a given variable is referenced
10611 several times, the normal format of messages is
10612
10613 @example
10614 e.adb:7:07: "V" is undefined (more references follow)
10615 @end example
10616
10617 where the parenthetical comment warns that there are additional
10618 references to the variable @code{V}. Compiling the same program with the
10619 @code{-gnatf} switch yields
10620
10621 @example
10622 e.adb:7:07: "V" is undefined
10623 e.adb:8:07: "V" is undefined
10624 e.adb:8:12: "V" is undefined
10625 e.adb:8:16: "V" is undefined
10626 e.adb:9:07: "V" is undefined
10627 e.adb:9:12: "V" is undefined
10628 @end example
10629
10630 The @code{-gnatf} switch also generates additional information for
10631 some error messages. Some examples are:
10632
10633
10634 @itemize *
10635
10636 @item
10637 Details on possibly non-portable unchecked conversion
10638
10639 @item
10640 List possible interpretations for ambiguous calls
10641
10642 @item
10643 Additional details on incorrect parameters
10644 @end itemize
10645 @end table
10646
10647 @geindex -gnatjnn (gcc)
10648
10649
10650 @table @asis
10651
10652 @item @code{-gnatjnn}
10653
10654 In normal operation mode (or if @code{-gnatj0} is used), then error messages
10655 with continuation lines are treated as though the continuation lines were
10656 separate messages (and so a warning with two continuation lines counts as
10657 three warnings, and is listed as three separate messages).
10658
10659 If the @code{-gnatjnn} switch is used with a positive value for nn, then
10660 messages are output in a different manner. A message and all its continuation
10661 lines are treated as a unit, and count as only one warning or message in the
10662 statistics totals. Furthermore, the message is reformatted so that no line
10663 is longer than nn characters.
10664 @end table
10665
10666 @geindex -gnatq (gcc)
10667
10668
10669 @table @asis
10670
10671 @item @code{-gnatq}
10672
10673 The @code{q} stands for quit (really 'don't quit').
10674 In normal operation mode, the compiler first parses the program and
10675 determines if there are any syntax errors. If there are, appropriate
10676 error messages are generated and compilation is immediately terminated.
10677 This switch tells
10678 GNAT to continue with semantic analysis even if syntax errors have been
10679 found. This may enable the detection of more errors in a single run. On
10680 the other hand, the semantic analyzer is more likely to encounter some
10681 internal fatal error when given a syntactically invalid tree.
10682 @end table
10683
10684 @geindex -gnatQ (gcc)
10685
10686
10687 @table @asis
10688
10689 @item @code{-gnatQ}
10690
10691 In normal operation mode, the @code{ALI} file is not generated if any
10692 illegalities are detected in the program. The use of @code{-gnatQ} forces
10693 generation of the @code{ALI} file. This file is marked as being in
10694 error, so it cannot be used for binding purposes, but it does contain
10695 reasonably complete cross-reference information, and thus may be useful
10696 for use by tools (e.g., semantic browsing tools or integrated development
10697 environments) that are driven from the @code{ALI} file. This switch
10698 implies @code{-gnatq}, since the semantic phase must be run to get a
10699 meaningful ALI file.
10700
10701 When @code{-gnatQ} is used and the generated @code{ALI} file is marked as
10702 being in error, @code{gnatmake} will attempt to recompile the source when it
10703 finds such an @code{ALI} file, including with switch @code{-gnatc}.
10704
10705 Note that @code{-gnatQ} has no effect if @code{-gnats} is specified,
10706 since ALI files are never generated if @code{-gnats} is set.
10707 @end table
10708
10709 @node Warning Message Control,Debugging and Assertion Control,Output and Error Message Control,Compiler Switches
10710 @anchor{gnat_ugn/building_executable_programs_with_gnat warning-message-control}@anchor{fa}@anchor{gnat_ugn/building_executable_programs_with_gnat id15}@anchor{ff}
10711 @subsection Warning Message Control
10712
10713
10714 @geindex Warning messages
10715
10716 In addition to error messages, which correspond to illegalities as defined
10717 in the Ada Reference Manual, the compiler detects two kinds of warning
10718 situations.
10719
10720 First, the compiler considers some constructs suspicious and generates a
10721 warning message to alert you to a possible error. Second, if the
10722 compiler detects a situation that is sure to raise an exception at
10723 run time, it generates a warning message. The following shows an example
10724 of warning messages:
10725
10726 @example
10727 e.adb:4:24: warning: creation of object may raise Storage_Error
10728 e.adb:10:17: warning: static value out of range
10729 e.adb:10:17: warning: "Constraint_Error" will be raised at run time
10730 @end example
10731
10732 GNAT considers a large number of situations as appropriate
10733 for the generation of warning messages. As always, warnings are not
10734 definite indications of errors. For example, if you do an out-of-range
10735 assignment with the deliberate intention of raising a
10736 @code{Constraint_Error} exception, then the warning that may be
10737 issued does not indicate an error. Some of the situations for which GNAT
10738 issues warnings (at least some of the time) are given in the following
10739 list. This list is not complete, and new warnings are often added to
10740 subsequent versions of GNAT. The list is intended to give a general idea
10741 of the kinds of warnings that are generated.
10742
10743
10744 @itemize *
10745
10746 @item
10747 Possible infinitely recursive calls
10748
10749 @item
10750 Out-of-range values being assigned
10751
10752 @item
10753 Possible order of elaboration problems
10754
10755 @item
10756 Size not a multiple of alignment for a record type
10757
10758 @item
10759 Assertions (pragma Assert) that are sure to fail
10760
10761 @item
10762 Unreachable code
10763
10764 @item
10765 Address clauses with possibly unaligned values, or where an attempt is
10766 made to overlay a smaller variable with a larger one.
10767
10768 @item
10769 Fixed-point type declarations with a null range
10770
10771 @item
10772 Direct_IO or Sequential_IO instantiated with a type that has access values
10773
10774 @item
10775 Variables that are never assigned a value
10776
10777 @item
10778 Variables that are referenced before being initialized
10779
10780 @item
10781 Task entries with no corresponding @code{accept} statement
10782
10783 @item
10784 Duplicate accepts for the same task entry in a @code{select}
10785
10786 @item
10787 Objects that take too much storage
10788
10789 @item
10790 Unchecked conversion between types of differing sizes
10791
10792 @item
10793 Missing @code{return} statement along some execution path in a function
10794
10795 @item
10796 Incorrect (unrecognized) pragmas
10797
10798 @item
10799 Incorrect external names
10800
10801 @item
10802 Allocation from empty storage pool
10803
10804 @item
10805 Potentially blocking operation in protected type
10806
10807 @item
10808 Suspicious parenthesization of expressions
10809
10810 @item
10811 Mismatching bounds in an aggregate
10812
10813 @item
10814 Attempt to return local value by reference
10815
10816 @item
10817 Premature instantiation of a generic body
10818
10819 @item
10820 Attempt to pack aliased components
10821
10822 @item
10823 Out of bounds array subscripts
10824
10825 @item
10826 Wrong length on string assignment
10827
10828 @item
10829 Violations of style rules if style checking is enabled
10830
10831 @item
10832 Unused @emph{with} clauses
10833
10834 @item
10835 @code{Bit_Order} usage that does not have any effect
10836
10837 @item
10838 @code{Standard.Duration} used to resolve universal fixed expression
10839
10840 @item
10841 Dereference of possibly null value
10842
10843 @item
10844 Declaration that is likely to cause storage error
10845
10846 @item
10847 Internal GNAT unit @emph{with}ed by application unit
10848
10849 @item
10850 Values known to be out of range at compile time
10851
10852 @item
10853 Unreferenced or unmodified variables. Note that a special
10854 exemption applies to variables which contain any of the substrings
10855 @code{DISCARD, DUMMY, IGNORE, JUNK, UNUSED}, in any casing. Such variables
10856 are considered likely to be intentionally used in a situation where
10857 otherwise a warning would be given, so warnings of this kind are
10858 always suppressed for such variables.
10859
10860 @item
10861 Address overlays that could clobber memory
10862
10863 @item
10864 Unexpected initialization when address clause present
10865
10866 @item
10867 Bad alignment for address clause
10868
10869 @item
10870 Useless type conversions
10871
10872 @item
10873 Redundant assignment statements and other redundant constructs
10874
10875 @item
10876 Useless exception handlers
10877
10878 @item
10879 Accidental hiding of name by child unit
10880
10881 @item
10882 Access before elaboration detected at compile time
10883
10884 @item
10885 A range in a @code{for} loop that is known to be null or might be null
10886 @end itemize
10887
10888 The following section lists compiler switches that are available
10889 to control the handling of warning messages. It is also possible
10890 to exercise much finer control over what warnings are issued and
10891 suppressed using the GNAT pragma Warnings (see the description
10892 of the pragma in the @cite{GNAT_Reference_manual}).
10893
10894 @geindex -gnatwa (gcc)
10895
10896
10897 @table @asis
10898
10899 @item @code{-gnatwa}
10900
10901 @emph{Activate most optional warnings.}
10902
10903 This switch activates most optional warning messages. See the remaining list
10904 in this section for details on optional warning messages that can be
10905 individually controlled. The warnings that are not turned on by this
10906 switch are:
10907
10908
10909 @itemize *
10910
10911 @item
10912 @code{-gnatwd} (implicit dereferencing)
10913
10914 @item
10915 @code{-gnatw.d} (tag warnings with -gnatw switch)
10916
10917 @item
10918 @code{-gnatwh} (hiding)
10919
10920 @item
10921 @code{-gnatw.h} (holes in record layouts)
10922
10923 @item
10924 @code{-gnatw.j} (late primitives of tagged types)
10925
10926 @item
10927 @code{-gnatw.k} (redefinition of names in standard)
10928
10929 @item
10930 @code{-gnatwl} (elaboration warnings)
10931
10932 @item
10933 @code{-gnatw.l} (inherited aspects)
10934
10935 @item
10936 @code{-gnatw.n} (atomic synchronization)
10937
10938 @item
10939 @code{-gnatwo} (address clause overlay)
10940
10941 @item
10942 @code{-gnatw.o} (values set by out parameters ignored)
10943
10944 @item
10945 @code{-gnatw.q} (questionable layout of record types)
10946
10947 @item
10948 @code{-gnatw_r} (out-of-order record representation clauses)
10949
10950 @item
10951 @code{-gnatw.s} (overridden size clause)
10952
10953 @item
10954 @code{-gnatwt} (tracking of deleted conditional code)
10955
10956 @item
10957 @code{-gnatw.u} (unordered enumeration)
10958
10959 @item
10960 @code{-gnatw.w} (use of Warnings Off)
10961
10962 @item
10963 @code{-gnatw.y} (reasons for package needing body)
10964 @end itemize
10965
10966 All other optional warnings are turned on.
10967 @end table
10968
10969 @geindex -gnatwA (gcc)
10970
10971
10972 @table @asis
10973
10974 @item @code{-gnatwA}
10975
10976 @emph{Suppress all optional errors.}
10977
10978 This switch suppresses all optional warning messages, see remaining list
10979 in this section for details on optional warning messages that can be
10980 individually controlled. Note that unlike switch @code{-gnatws}, the
10981 use of switch @code{-gnatwA} does not suppress warnings that are
10982 normally given unconditionally and cannot be individually controlled
10983 (for example, the warning about a missing exit path in a function).
10984 Also, again unlike switch @code{-gnatws}, warnings suppressed by
10985 the use of switch @code{-gnatwA} can be individually turned back
10986 on. For example the use of switch @code{-gnatwA} followed by
10987 switch @code{-gnatwd} will suppress all optional warnings except
10988 the warnings for implicit dereferencing.
10989 @end table
10990
10991 @geindex -gnatw.a (gcc)
10992
10993
10994 @table @asis
10995
10996 @item @code{-gnatw.a}
10997
10998 @emph{Activate warnings on failing assertions.}
10999
11000 @geindex Assert failures
11001
11002 This switch activates warnings for assertions where the compiler can tell at
11003 compile time that the assertion will fail. Note that this warning is given
11004 even if assertions are disabled. The default is that such warnings are
11005 generated.
11006 @end table
11007
11008 @geindex -gnatw.A (gcc)
11009
11010
11011 @table @asis
11012
11013 @item @code{-gnatw.A}
11014
11015 @emph{Suppress warnings on failing assertions.}
11016
11017 @geindex Assert failures
11018
11019 This switch suppresses warnings for assertions where the compiler can tell at
11020 compile time that the assertion will fail.
11021 @end table
11022
11023 @geindex -gnatw_a
11024
11025
11026 @table @asis
11027
11028 @item @code{-gnatw_a}
11029
11030 @emph{Activate warnings on anonymous allocators.}
11031
11032 @geindex Anonymous allocators
11033
11034 This switch activates warnings for allocators of anonymous access types,
11035 which can involve run-time accessibility checks and lead to unexpected
11036 accessibility violations. For more details on the rules involved, see
11037 RM 3.10.2 (14).
11038 @end table
11039
11040 @geindex -gnatw_A
11041
11042
11043 @table @asis
11044
11045 @item @code{-gnatw_A}
11046
11047 @emph{Supress warnings on anonymous allocators.}
11048
11049 @geindex Anonymous allocators
11050
11051 This switch suppresses warnings for anonymous access type allocators.
11052 @end table
11053
11054 @geindex -gnatwb (gcc)
11055
11056
11057 @table @asis
11058
11059 @item @code{-gnatwb}
11060
11061 @emph{Activate warnings on bad fixed values.}
11062
11063 @geindex Bad fixed values
11064
11065 @geindex Fixed-point Small value
11066
11067 @geindex Small value
11068
11069 This switch activates warnings for static fixed-point expressions whose
11070 value is not an exact multiple of Small. Such values are implementation
11071 dependent, since an implementation is free to choose either of the multiples
11072 that surround the value. GNAT always chooses the closer one, but this is not
11073 required behavior, and it is better to specify a value that is an exact
11074 multiple, ensuring predictable execution. The default is that such warnings
11075 are not generated.
11076 @end table
11077
11078 @geindex -gnatwB (gcc)
11079
11080
11081 @table @asis
11082
11083 @item @code{-gnatwB}
11084
11085 @emph{Suppress warnings on bad fixed values.}
11086
11087 This switch suppresses warnings for static fixed-point expressions whose
11088 value is not an exact multiple of Small.
11089 @end table
11090
11091 @geindex -gnatw.b (gcc)
11092
11093
11094 @table @asis
11095
11096 @item @code{-gnatw.b}
11097
11098 @emph{Activate warnings on biased representation.}
11099
11100 @geindex Biased representation
11101
11102 This switch activates warnings when a size clause, value size clause, component
11103 clause, or component size clause forces the use of biased representation for an
11104 integer type (e.g. representing a range of 10..11 in a single bit by using 0/1
11105 to represent 10/11). The default is that such warnings are generated.
11106 @end table
11107
11108 @geindex -gnatwB (gcc)
11109
11110
11111 @table @asis
11112
11113 @item @code{-gnatw.B}
11114
11115 @emph{Suppress warnings on biased representation.}
11116
11117 This switch suppresses warnings for representation clauses that force the use
11118 of biased representation.
11119 @end table
11120
11121 @geindex -gnatwc (gcc)
11122
11123
11124 @table @asis
11125
11126 @item @code{-gnatwc}
11127
11128 @emph{Activate warnings on conditionals.}
11129
11130 @geindex Conditionals
11131 @geindex constant
11132
11133 This switch activates warnings for conditional expressions used in
11134 tests that are known to be True or False at compile time. The default
11135 is that such warnings are not generated.
11136 Note that this warning does
11137 not get issued for the use of boolean variables or constants whose
11138 values are known at compile time, since this is a standard technique
11139 for conditional compilation in Ada, and this would generate too many
11140 false positive warnings.
11141
11142 This warning option also activates a special test for comparisons using
11143 the operators '>=' and' <='.
11144 If the compiler can tell that only the equality condition is possible,
11145 then it will warn that the '>' or '<' part of the test
11146 is useless and that the operator could be replaced by '='.
11147 An example would be comparing a @code{Natural} variable <= 0.
11148
11149 This warning option also generates warnings if
11150 one or both tests is optimized away in a membership test for integer
11151 values if the result can be determined at compile time. Range tests on
11152 enumeration types are not included, since it is common for such tests
11153 to include an end point.
11154
11155 This warning can also be turned on using @code{-gnatwa}.
11156 @end table
11157
11158 @geindex -gnatwC (gcc)
11159
11160
11161 @table @asis
11162
11163 @item @code{-gnatwC}
11164
11165 @emph{Suppress warnings on conditionals.}
11166
11167 This switch suppresses warnings for conditional expressions used in
11168 tests that are known to be True or False at compile time.
11169 @end table
11170
11171 @geindex -gnatw.c (gcc)
11172
11173
11174 @table @asis
11175
11176 @item @code{-gnatw.c}
11177
11178 @emph{Activate warnings on missing component clauses.}
11179
11180 @geindex Component clause
11181 @geindex missing
11182
11183 This switch activates warnings for record components where a record
11184 representation clause is present and has component clauses for the
11185 majority, but not all, of the components. A warning is given for each
11186 component for which no component clause is present.
11187 @end table
11188
11189 @geindex -gnatw.C (gcc)
11190
11191
11192 @table @asis
11193
11194 @item @code{-gnatw.C}
11195
11196 @emph{Suppress warnings on missing component clauses.}
11197
11198 This switch suppresses warnings for record components that are
11199 missing a component clause in the situation described above.
11200 @end table
11201
11202 @geindex -gnatw_c (gcc)
11203
11204
11205 @table @asis
11206
11207 @item @code{-gnatw_c}
11208
11209 @emph{Activate warnings on unknown condition in Compile_Time_Warning.}
11210
11211 @geindex Compile_Time_Warning
11212
11213 @geindex Compile_Time_Error
11214
11215 This switch activates warnings on a pragma Compile_Time_Warning
11216 or Compile_Time_Error whose condition has a value that is not
11217 known at compile time.
11218 The default is that such warnings are generated.
11219 @end table
11220
11221 @geindex -gnatw_C (gcc)
11222
11223
11224 @table @asis
11225
11226 @item @code{-gnatw_C}
11227
11228 @emph{Suppress warnings on unknown condition in Compile_Time_Warning.}
11229
11230 This switch supresses warnings on a pragma Compile_Time_Warning
11231 or Compile_Time_Error whose condition has a value that is not
11232 known at compile time.
11233 @end table
11234
11235 @geindex -gnatwd (gcc)
11236
11237
11238 @table @asis
11239
11240 @item @code{-gnatwd}
11241
11242 @emph{Activate warnings on implicit dereferencing.}
11243
11244 If this switch is set, then the use of a prefix of an access type
11245 in an indexed component, slice, or selected component without an
11246 explicit @code{.all} will generate a warning. With this warning
11247 enabled, access checks occur only at points where an explicit
11248 @code{.all} appears in the source code (assuming no warnings are
11249 generated as a result of this switch). The default is that such
11250 warnings are not generated.
11251 @end table
11252
11253 @geindex -gnatwD (gcc)
11254
11255
11256 @table @asis
11257
11258 @item @code{-gnatwD}
11259
11260 @emph{Suppress warnings on implicit dereferencing.}
11261
11262 @geindex Implicit dereferencing
11263
11264 @geindex Dereferencing
11265 @geindex implicit
11266
11267 This switch suppresses warnings for implicit dereferences in
11268 indexed components, slices, and selected components.
11269 @end table
11270
11271 @geindex -gnatw.d (gcc)
11272
11273
11274 @table @asis
11275
11276 @item @code{-gnatw.d}
11277
11278 @emph{Activate tagging of warning and info messages.}
11279
11280 If this switch is set, then warning messages are tagged, with one of the
11281 following strings:
11282
11283 @quotation
11284
11285
11286 @itemize -
11287
11288 @item
11289 @emph{[-gnatw?]}
11290 Used to tag warnings controlled by the switch @code{-gnatwx} where x
11291 is a letter a-z.
11292
11293 @item
11294 @emph{[-gnatw.?]}
11295 Used to tag warnings controlled by the switch @code{-gnatw.x} where x
11296 is a letter a-z.
11297
11298 @item
11299 @emph{[-gnatel]}
11300 Used to tag elaboration information (info) messages generated when the
11301 static model of elaboration is used and the @code{-gnatel} switch is set.
11302
11303 @item
11304 @emph{[restriction warning]}
11305 Used to tag warning messages for restriction violations, activated by use
11306 of the pragma @code{Restriction_Warnings}.
11307
11308 @item
11309 @emph{[warning-as-error]}
11310 Used to tag warning messages that have been converted to error messages by
11311 use of the pragma Warning_As_Error. Note that such warnings are prefixed by
11312 the string "error: " rather than "warning: ".
11313
11314 @item
11315 @emph{[enabled by default]}
11316 Used to tag all other warnings that are always given by default, unless
11317 warnings are completely suppressed using pragma @emph{Warnings(Off)} or
11318 the switch @code{-gnatws}.
11319 @end itemize
11320 @end quotation
11321 @end table
11322
11323 @geindex -gnatw.d (gcc)
11324
11325
11326 @table @asis
11327
11328 @item @code{-gnatw.D}
11329
11330 @emph{Deactivate tagging of warning and info messages messages.}
11331
11332 If this switch is set, then warning messages return to the default
11333 mode in which warnings and info messages are not tagged as described above for
11334 @code{-gnatw.d}.
11335 @end table
11336
11337 @geindex -gnatwe (gcc)
11338
11339 @geindex Warnings
11340 @geindex treat as error
11341
11342
11343 @table @asis
11344
11345 @item @code{-gnatwe}
11346
11347 @emph{Treat warnings and style checks as errors.}
11348
11349 This switch causes warning messages and style check messages to be
11350 treated as errors.
11351 The warning string still appears, but the warning messages are counted
11352 as errors, and prevent the generation of an object file. Note that this
11353 is the only -gnatw switch that affects the handling of style check messages.
11354 Note also that this switch has no effect on info (information) messages, which
11355 are not treated as errors if this switch is present.
11356 @end table
11357
11358 @geindex -gnatw.e (gcc)
11359
11360
11361 @table @asis
11362
11363 @item @code{-gnatw.e}
11364
11365 @emph{Activate every optional warning.}
11366
11367 @geindex Warnings
11368 @geindex activate every optional warning
11369
11370 This switch activates all optional warnings, including those which
11371 are not activated by @code{-gnatwa}. The use of this switch is not
11372 recommended for normal use. If you turn this switch on, it is almost
11373 certain that you will get large numbers of useless warnings. The
11374 warnings that are excluded from @code{-gnatwa} are typically highly
11375 specialized warnings that are suitable for use only in code that has
11376 been specifically designed according to specialized coding rules.
11377 @end table
11378
11379 @geindex -gnatwE (gcc)
11380
11381 @geindex Warnings
11382 @geindex treat as error
11383
11384
11385 @table @asis
11386
11387 @item @code{-gnatwE}
11388
11389 @emph{Treat all run-time exception warnings as errors.}
11390
11391 This switch causes warning messages regarding errors that will be raised
11392 during run-time execution to be treated as errors.
11393 @end table
11394
11395 @geindex -gnatwf (gcc)
11396
11397
11398 @table @asis
11399
11400 @item @code{-gnatwf}
11401
11402 @emph{Activate warnings on unreferenced formals.}
11403
11404 @geindex Formals
11405 @geindex unreferenced
11406
11407 This switch causes a warning to be generated if a formal parameter
11408 is not referenced in the body of the subprogram. This warning can
11409 also be turned on using @code{-gnatwu}. The
11410 default is that these warnings are not generated.
11411 @end table
11412
11413 @geindex -gnatwF (gcc)
11414
11415
11416 @table @asis
11417
11418 @item @code{-gnatwF}
11419
11420 @emph{Suppress warnings on unreferenced formals.}
11421
11422 This switch suppresses warnings for unreferenced formal
11423 parameters. Note that the
11424 combination @code{-gnatwu} followed by @code{-gnatwF} has the
11425 effect of warning on unreferenced entities other than subprogram
11426 formals.
11427 @end table
11428
11429 @geindex -gnatwg (gcc)
11430
11431
11432 @table @asis
11433
11434 @item @code{-gnatwg}
11435
11436 @emph{Activate warnings on unrecognized pragmas.}
11437
11438 @geindex Pragmas
11439 @geindex unrecognized
11440
11441 This switch causes a warning to be generated if an unrecognized
11442 pragma is encountered. Apart from issuing this warning, the
11443 pragma is ignored and has no effect. The default
11444 is that such warnings are issued (satisfying the Ada Reference
11445 Manual requirement that such warnings appear).
11446 @end table
11447
11448 @geindex -gnatwG (gcc)
11449
11450
11451 @table @asis
11452
11453 @item @code{-gnatwG}
11454
11455 @emph{Suppress warnings on unrecognized pragmas.}
11456
11457 This switch suppresses warnings for unrecognized pragmas.
11458 @end table
11459
11460 @geindex -gnatw.g (gcc)
11461
11462
11463 @table @asis
11464
11465 @item @code{-gnatw.g}
11466
11467 @emph{Warnings used for GNAT sources.}
11468
11469 This switch sets the warning categories that are used by the standard
11470 GNAT style. Currently this is equivalent to
11471 @code{-gnatwAao.q.s.CI.V.X.Z}
11472 but more warnings may be added in the future without advanced notice.
11473 @end table
11474
11475 @geindex -gnatwh (gcc)
11476
11477
11478 @table @asis
11479
11480 @item @code{-gnatwh}
11481
11482 @emph{Activate warnings on hiding.}
11483
11484 @geindex Hiding of Declarations
11485
11486 This switch activates warnings on hiding declarations that are considered
11487 potentially confusing. Not all cases of hiding cause warnings; for example an
11488 overriding declaration hides an implicit declaration, which is just normal
11489 code. The default is that warnings on hiding are not generated.
11490 @end table
11491
11492 @geindex -gnatwH (gcc)
11493
11494
11495 @table @asis
11496
11497 @item @code{-gnatwH}
11498
11499 @emph{Suppress warnings on hiding.}
11500
11501 This switch suppresses warnings on hiding declarations.
11502 @end table
11503
11504 @geindex -gnatw.h (gcc)
11505
11506
11507 @table @asis
11508
11509 @item @code{-gnatw.h}
11510
11511 @emph{Activate warnings on holes/gaps in records.}
11512
11513 @geindex Record Representation (gaps)
11514
11515 This switch activates warnings on component clauses in record
11516 representation clauses that leave holes (gaps) in the record layout.
11517 If this warning option is active, then record representation clauses
11518 should specify a contiguous layout, adding unused fill fields if needed.
11519 @end table
11520
11521 @geindex -gnatw.H (gcc)
11522
11523
11524 @table @asis
11525
11526 @item @code{-gnatw.H}
11527
11528 @emph{Suppress warnings on holes/gaps in records.}
11529
11530 This switch suppresses warnings on component clauses in record
11531 representation clauses that leave holes (haps) in the record layout.
11532 @end table
11533
11534 @geindex -gnatwi (gcc)
11535
11536
11537 @table @asis
11538
11539 @item @code{-gnatwi}
11540
11541 @emph{Activate warnings on implementation units.}
11542
11543 This switch activates warnings for a @emph{with} of an internal GNAT
11544 implementation unit, defined as any unit from the @code{Ada},
11545 @code{Interfaces}, @code{GNAT},
11546 or @code{System}
11547 hierarchies that is not
11548 documented in either the Ada Reference Manual or the GNAT
11549 Programmer's Reference Manual. Such units are intended only
11550 for internal implementation purposes and should not be @emph{with}ed
11551 by user programs. The default is that such warnings are generated
11552 @end table
11553
11554 @geindex -gnatwI (gcc)
11555
11556
11557 @table @asis
11558
11559 @item @code{-gnatwI}
11560
11561 @emph{Disable warnings on implementation units.}
11562
11563 This switch disables warnings for a @emph{with} of an internal GNAT
11564 implementation unit.
11565 @end table
11566
11567 @geindex -gnatw.i (gcc)
11568
11569
11570 @table @asis
11571
11572 @item @code{-gnatw.i}
11573
11574 @emph{Activate warnings on overlapping actuals.}
11575
11576 This switch enables a warning on statically detectable overlapping actuals in
11577 a subprogram call, when one of the actuals is an in-out parameter, and the
11578 types of the actuals are not by-copy types. This warning is off by default.
11579 @end table
11580
11581 @geindex -gnatw.I (gcc)
11582
11583
11584 @table @asis
11585
11586 @item @code{-gnatw.I}
11587
11588 @emph{Disable warnings on overlapping actuals.}
11589
11590 This switch disables warnings on overlapping actuals in a call..
11591 @end table
11592
11593 @geindex -gnatwj (gcc)
11594
11595
11596 @table @asis
11597
11598 @item @code{-gnatwj}
11599
11600 @emph{Activate warnings on obsolescent features (Annex J).}
11601
11602 @geindex Features
11603 @geindex obsolescent
11604
11605 @geindex Obsolescent features
11606
11607 If this warning option is activated, then warnings are generated for
11608 calls to subprograms marked with @code{pragma Obsolescent} and
11609 for use of features in Annex J of the Ada Reference Manual. In the
11610 case of Annex J, not all features are flagged. In particular use
11611 of the renamed packages (like @code{Text_IO}) and use of package
11612 @code{ASCII} are not flagged, since these are very common and
11613 would generate many annoying positive warnings. The default is that
11614 such warnings are not generated.
11615
11616 In addition to the above cases, warnings are also generated for
11617 GNAT features that have been provided in past versions but which
11618 have been superseded (typically by features in the new Ada standard).
11619 For example, @code{pragma Ravenscar} will be flagged since its
11620 function is replaced by @code{pragma Profile(Ravenscar)}, and
11621 @code{pragma Interface_Name} will be flagged since its function
11622 is replaced by @code{pragma Import}.
11623
11624 Note that this warning option functions differently from the
11625 restriction @code{No_Obsolescent_Features} in two respects.
11626 First, the restriction applies only to annex J features.
11627 Second, the restriction does flag uses of package @code{ASCII}.
11628 @end table
11629
11630 @geindex -gnatwJ (gcc)
11631
11632
11633 @table @asis
11634
11635 @item @code{-gnatwJ}
11636
11637 @emph{Suppress warnings on obsolescent features (Annex J).}
11638
11639 This switch disables warnings on use of obsolescent features.
11640 @end table
11641
11642 @geindex -gnatw.j (gcc)
11643
11644
11645 @table @asis
11646
11647 @item @code{-gnatw.j}
11648
11649 @emph{Activate warnings on late declarations of tagged type primitives.}
11650
11651 This switch activates warnings on visible primitives added to a
11652 tagged type after deriving a private extension from it.
11653 @end table
11654
11655 @geindex -gnatw.J (gcc)
11656
11657
11658 @table @asis
11659
11660 @item @code{-gnatw.J}
11661
11662 @emph{Suppress warnings on late declarations of tagged type primitives.}
11663
11664 This switch suppresses warnings on visible primitives added to a
11665 tagged type after deriving a private extension from it.
11666 @end table
11667
11668 @geindex -gnatwk (gcc)
11669
11670
11671 @table @asis
11672
11673 @item @code{-gnatwk}
11674
11675 @emph{Activate warnings on variables that could be constants.}
11676
11677 This switch activates warnings for variables that are initialized but
11678 never modified, and then could be declared constants. The default is that
11679 such warnings are not given.
11680 @end table
11681
11682 @geindex -gnatwK (gcc)
11683
11684
11685 @table @asis
11686
11687 @item @code{-gnatwK}
11688
11689 @emph{Suppress warnings on variables that could be constants.}
11690
11691 This switch disables warnings on variables that could be declared constants.
11692 @end table
11693
11694 @geindex -gnatw.k (gcc)
11695
11696
11697 @table @asis
11698
11699 @item @code{-gnatw.k}
11700
11701 @emph{Activate warnings on redefinition of names in standard.}
11702
11703 This switch activates warnings for declarations that declare a name that
11704 is defined in package Standard. Such declarations can be confusing,
11705 especially since the names in package Standard continue to be directly
11706 visible, meaning that use visibiliy on such redeclared names does not
11707 work as expected. Names of discriminants and components in records are
11708 not included in this check.
11709 @end table
11710
11711 @geindex -gnatwK (gcc)
11712
11713
11714 @table @asis
11715
11716 @item @code{-gnatw.K}
11717
11718 @emph{Suppress warnings on redefinition of names in standard.}
11719
11720 This switch activates warnings for declarations that declare a name that
11721 is defined in package Standard.
11722 @end table
11723
11724 @geindex -gnatwl (gcc)
11725
11726
11727 @table @asis
11728
11729 @item @code{-gnatwl}
11730
11731 @emph{Activate warnings for elaboration pragmas.}
11732
11733 @geindex Elaboration
11734 @geindex warnings
11735
11736 This switch activates warnings for possible elaboration problems,
11737 including suspicious use
11738 of @code{Elaborate} pragmas, when using the static elaboration model, and
11739 possible situations that may raise @code{Program_Error} when using the
11740 dynamic elaboration model.
11741 See the section in this guide on elaboration checking for further details.
11742 The default is that such warnings
11743 are not generated.
11744 @end table
11745
11746 @geindex -gnatwL (gcc)
11747
11748
11749 @table @asis
11750
11751 @item @code{-gnatwL}
11752
11753 @emph{Suppress warnings for elaboration pragmas.}
11754
11755 This switch suppresses warnings for possible elaboration problems.
11756 @end table
11757
11758 @geindex -gnatw.l (gcc)
11759
11760
11761 @table @asis
11762
11763 @item @code{-gnatw.l}
11764
11765 @emph{List inherited aspects.}
11766
11767 This switch causes the compiler to list inherited invariants,
11768 preconditions, and postconditions from Type_Invariant'Class, Invariant'Class,
11769 Pre'Class, and Post'Class aspects. Also list inherited subtype predicates.
11770 @end table
11771
11772 @geindex -gnatw.L (gcc)
11773
11774
11775 @table @asis
11776
11777 @item @code{-gnatw.L}
11778
11779 @emph{Suppress listing of inherited aspects.}
11780
11781 This switch suppresses listing of inherited aspects.
11782 @end table
11783
11784 @geindex -gnatwm (gcc)
11785
11786
11787 @table @asis
11788
11789 @item @code{-gnatwm}
11790
11791 @emph{Activate warnings on modified but unreferenced variables.}
11792
11793 This switch activates warnings for variables that are assigned (using
11794 an initialization value or with one or more assignment statements) but
11795 whose value is never read. The warning is suppressed for volatile
11796 variables and also for variables that are renamings of other variables
11797 or for which an address clause is given.
11798 The default is that these warnings are not given.
11799 @end table
11800
11801 @geindex -gnatwM (gcc)
11802
11803
11804 @table @asis
11805
11806 @item @code{-gnatwM}
11807
11808 @emph{Disable warnings on modified but unreferenced variables.}
11809
11810 This switch disables warnings for variables that are assigned or
11811 initialized, but never read.
11812 @end table
11813
11814 @geindex -gnatw.m (gcc)
11815
11816
11817 @table @asis
11818
11819 @item @code{-gnatw.m}
11820
11821 @emph{Activate warnings on suspicious modulus values.}
11822
11823 This switch activates warnings for modulus values that seem suspicious.
11824 The cases caught are where the size is the same as the modulus (e.g.
11825 a modulus of 7 with a size of 7 bits), and modulus values of 32 or 64
11826 with no size clause. The guess in both cases is that 2**x was intended
11827 rather than x. In addition expressions of the form 2*x for small x
11828 generate a warning (the almost certainly accurate guess being that
11829 2**x was intended). The default is that these warnings are given.
11830 @end table
11831
11832 @geindex -gnatw.M (gcc)
11833
11834
11835 @table @asis
11836
11837 @item @code{-gnatw.M}
11838
11839 @emph{Disable warnings on suspicious modulus values.}
11840
11841 This switch disables warnings for suspicious modulus values.
11842 @end table
11843
11844 @geindex -gnatwn (gcc)
11845
11846
11847 @table @asis
11848
11849 @item @code{-gnatwn}
11850
11851 @emph{Set normal warnings mode.}
11852
11853 This switch sets normal warning mode, in which enabled warnings are
11854 issued and treated as warnings rather than errors. This is the default
11855 mode. the switch @code{-gnatwn} can be used to cancel the effect of
11856 an explicit @code{-gnatws} or
11857 @code{-gnatwe}. It also cancels the effect of the
11858 implicit @code{-gnatwe} that is activated by the
11859 use of @code{-gnatg}.
11860 @end table
11861
11862 @geindex -gnatw.n (gcc)
11863
11864 @geindex Atomic Synchronization
11865 @geindex warnings
11866
11867
11868 @table @asis
11869
11870 @item @code{-gnatw.n}
11871
11872 @emph{Activate warnings on atomic synchronization.}
11873
11874 This switch actives warnings when an access to an atomic variable
11875 requires the generation of atomic synchronization code. These
11876 warnings are off by default.
11877 @end table
11878
11879 @geindex -gnatw.N (gcc)
11880
11881
11882 @table @asis
11883
11884 @item @code{-gnatw.N}
11885
11886 @emph{Suppress warnings on atomic synchronization.}
11887
11888 @geindex Atomic Synchronization
11889 @geindex warnings
11890
11891 This switch suppresses warnings when an access to an atomic variable
11892 requires the generation of atomic synchronization code.
11893 @end table
11894
11895 @geindex -gnatwo (gcc)
11896
11897 @geindex Address Clauses
11898 @geindex warnings
11899
11900
11901 @table @asis
11902
11903 @item @code{-gnatwo}
11904
11905 @emph{Activate warnings on address clause overlays.}
11906
11907 This switch activates warnings for possibly unintended initialization
11908 effects of defining address clauses that cause one variable to overlap
11909 another. The default is that such warnings are generated.
11910 @end table
11911
11912 @geindex -gnatwO (gcc)
11913
11914
11915 @table @asis
11916
11917 @item @code{-gnatwO}
11918
11919 @emph{Suppress warnings on address clause overlays.}
11920
11921 This switch suppresses warnings on possibly unintended initialization
11922 effects of defining address clauses that cause one variable to overlap
11923 another.
11924 @end table
11925
11926 @geindex -gnatw.o (gcc)
11927
11928
11929 @table @asis
11930
11931 @item @code{-gnatw.o}
11932
11933 @emph{Activate warnings on modified but unreferenced out parameters.}
11934
11935 This switch activates warnings for variables that are modified by using
11936 them as actuals for a call to a procedure with an out mode formal, where
11937 the resulting assigned value is never read. It is applicable in the case
11938 where there is more than one out mode formal. If there is only one out
11939 mode formal, the warning is issued by default (controlled by -gnatwu).
11940 The warning is suppressed for volatile
11941 variables and also for variables that are renamings of other variables
11942 or for which an address clause is given.
11943 The default is that these warnings are not given.
11944 @end table
11945
11946 @geindex -gnatw.O (gcc)
11947
11948
11949 @table @asis
11950
11951 @item @code{-gnatw.O}
11952
11953 @emph{Disable warnings on modified but unreferenced out parameters.}
11954
11955 This switch suppresses warnings for variables that are modified by using
11956 them as actuals for a call to a procedure with an out mode formal, where
11957 the resulting assigned value is never read.
11958 @end table
11959
11960 @geindex -gnatwp (gcc)
11961
11962 @geindex Inlining
11963 @geindex warnings
11964
11965
11966 @table @asis
11967
11968 @item @code{-gnatwp}
11969
11970 @emph{Activate warnings on ineffective pragma Inlines.}
11971
11972 This switch activates warnings for failure of front end inlining
11973 (activated by @code{-gnatN}) to inline a particular call. There are
11974 many reasons for not being able to inline a call, including most
11975 commonly that the call is too complex to inline. The default is
11976 that such warnings are not given.
11977 Warnings on ineffective inlining by the gcc back-end can be activated
11978 separately, using the gcc switch -Winline.
11979 @end table
11980
11981 @geindex -gnatwP (gcc)
11982
11983
11984 @table @asis
11985
11986 @item @code{-gnatwP}
11987
11988 @emph{Suppress warnings on ineffective pragma Inlines.}
11989
11990 This switch suppresses warnings on ineffective pragma Inlines. If the
11991 inlining mechanism cannot inline a call, it will simply ignore the
11992 request silently.
11993 @end table
11994
11995 @geindex -gnatw.p (gcc)
11996
11997 @geindex Parameter order
11998 @geindex warnings
11999
12000
12001 @table @asis
12002
12003 @item @code{-gnatw.p}
12004
12005 @emph{Activate warnings on parameter ordering.}
12006
12007 This switch activates warnings for cases of suspicious parameter
12008 ordering when the list of arguments are all simple identifiers that
12009 match the names of the formals, but are in a different order. The
12010 warning is suppressed if any use of named parameter notation is used,
12011 so this is the appropriate way to suppress a false positive (and
12012 serves to emphasize that the "misordering" is deliberate). The
12013 default is that such warnings are not given.
12014 @end table
12015
12016 @geindex -gnatw.P (gcc)
12017
12018
12019 @table @asis
12020
12021 @item @code{-gnatw.P}
12022
12023 @emph{Suppress warnings on parameter ordering.}
12024
12025 This switch suppresses warnings on cases of suspicious parameter
12026 ordering.
12027 @end table
12028
12029 @geindex -gnatwq (gcc)
12030
12031 @geindex Parentheses
12032 @geindex warnings
12033
12034
12035 @table @asis
12036
12037 @item @code{-gnatwq}
12038
12039 @emph{Activate warnings on questionable missing parentheses.}
12040
12041 This switch activates warnings for cases where parentheses are not used and
12042 the result is potential ambiguity from a readers point of view. For example
12043 (not a > b) when a and b are modular means ((not a) > b) and very likely the
12044 programmer intended (not (a > b)). Similarly (-x mod 5) means (-(x mod 5)) and
12045 quite likely ((-x) mod 5) was intended. In such situations it seems best to
12046 follow the rule of always parenthesizing to make the association clear, and
12047 this warning switch warns if such parentheses are not present. The default
12048 is that these warnings are given.
12049 @end table
12050
12051 @geindex -gnatwQ (gcc)
12052
12053
12054 @table @asis
12055
12056 @item @code{-gnatwQ}
12057
12058 @emph{Suppress warnings on questionable missing parentheses.}
12059
12060 This switch suppresses warnings for cases where the association is not
12061 clear and the use of parentheses is preferred.
12062 @end table
12063
12064 @geindex -gnatw.q (gcc)
12065
12066 @geindex Layout
12067 @geindex warnings
12068
12069
12070 @table @asis
12071
12072 @item @code{-gnatw.q}
12073
12074 @emph{Activate warnings on questionable layout of record types.}
12075
12076 This switch activates warnings for cases where the default layout of
12077 a record type, that is to say the layout of its components in textual
12078 order of the source code, would very likely cause inefficiencies in
12079 the code generated by the compiler, both in terms of space and speed
12080 during execution. One warning is issued for each problematic component
12081 without representation clause in the nonvariant part and then in each
12082 variant recursively, if any.
12083
12084 The purpose of these warnings is neither to prescribe an optimal layout
12085 nor to force the use of representation clauses, but rather to get rid of
12086 the most blatant inefficiencies in the layout. Therefore, the default
12087 layout is matched against the following synthetic ordered layout and
12088 the deviations are flagged on a component-by-component basis:
12089
12090
12091 @itemize *
12092
12093 @item
12094 first all components or groups of components whose length is fixed
12095 and a multiple of the storage unit,
12096
12097 @item
12098 then the remaining components whose length is fixed and not a multiple
12099 of the storage unit,
12100
12101 @item
12102 then the remaining components whose length doesn't depend on discriminants
12103 (that is to say, with variable but uniform length for all objects),
12104
12105 @item
12106 then all components whose length depends on discriminants,
12107
12108 @item
12109 finally the variant part (if any),
12110 @end itemize
12111
12112 for the nonvariant part and for each variant recursively, if any.
12113
12114 The exact wording of the warning depends on whether the compiler is allowed
12115 to reorder the components in the record type or precluded from doing it by
12116 means of pragma @code{No_Component_Reordering}.
12117
12118 The default is that these warnings are not given.
12119 @end table
12120
12121 @geindex -gnatw.Q (gcc)
12122
12123
12124 @table @asis
12125
12126 @item @code{-gnatw.Q}
12127
12128 @emph{Suppress warnings on questionable layout of record types.}
12129
12130 This switch suppresses warnings for cases where the default layout of
12131 a record type would very likely cause inefficiencies.
12132 @end table
12133
12134 @geindex -gnatwr (gcc)
12135
12136
12137 @table @asis
12138
12139 @item @code{-gnatwr}
12140
12141 @emph{Activate warnings on redundant constructs.}
12142
12143 This switch activates warnings for redundant constructs. The following
12144 is the current list of constructs regarded as redundant:
12145
12146
12147 @itemize *
12148
12149 @item
12150 Assignment of an item to itself.
12151
12152 @item
12153 Type conversion that converts an expression to its own type.
12154
12155 @item
12156 Use of the attribute @code{Base} where @code{typ'Base} is the same
12157 as @code{typ}.
12158
12159 @item
12160 Use of pragma @code{Pack} when all components are placed by a record
12161 representation clause.
12162
12163 @item
12164 Exception handler containing only a reraise statement (raise with no
12165 operand) which has no effect.
12166
12167 @item
12168 Use of the operator abs on an operand that is known at compile time
12169 to be non-negative
12170
12171 @item
12172 Comparison of an object or (unary or binary) operation of boolean type to
12173 an explicit True value.
12174 @end itemize
12175
12176 The default is that warnings for redundant constructs are not given.
12177 @end table
12178
12179 @geindex -gnatwR (gcc)
12180
12181
12182 @table @asis
12183
12184 @item @code{-gnatwR}
12185
12186 @emph{Suppress warnings on redundant constructs.}
12187
12188 This switch suppresses warnings for redundant constructs.
12189 @end table
12190
12191 @geindex -gnatw.r (gcc)
12192
12193
12194 @table @asis
12195
12196 @item @code{-gnatw.r}
12197
12198 @emph{Activate warnings for object renaming function.}
12199
12200 This switch activates warnings for an object renaming that renames a
12201 function call, which is equivalent to a constant declaration (as
12202 opposed to renaming the function itself). The default is that these
12203 warnings are given.
12204 @end table
12205
12206 @geindex -gnatw.R (gcc)
12207
12208
12209 @table @asis
12210
12211 @item @code{-gnatw.R}
12212
12213 @emph{Suppress warnings for object renaming function.}
12214
12215 This switch suppresses warnings for object renaming function.
12216 @end table
12217
12218 @geindex -gnatw_r (gcc)
12219
12220
12221 @table @asis
12222
12223 @item @code{-gnatw_r}
12224
12225 @emph{Activate warnings for out-of-order record representation clauses.}
12226
12227 This switch activates warnings for record representation clauses,
12228 if the order of component declarations, component clauses,
12229 and bit-level layout do not all agree.
12230 The default is that these warnings are not given.
12231 @end table
12232
12233 @geindex -gnatw_R (gcc)
12234
12235
12236 @table @asis
12237
12238 @item @code{-gnatw_R}
12239
12240 @emph{Suppress warnings for out-of-order record representation clauses.}
12241 @end table
12242
12243 @geindex -gnatws (gcc)
12244
12245
12246 @table @asis
12247
12248 @item @code{-gnatws}
12249
12250 @emph{Suppress all warnings.}
12251
12252 This switch completely suppresses the
12253 output of all warning messages from the GNAT front end, including
12254 both warnings that can be controlled by switches described in this
12255 section, and those that are normally given unconditionally. The
12256 effect of this suppress action can only be cancelled by a subsequent
12257 use of the switch @code{-gnatwn}.
12258
12259 Note that switch @code{-gnatws} does not suppress
12260 warnings from the @code{gcc} back end.
12261 To suppress these back end warnings as well, use the switch @code{-w}
12262 in addition to @code{-gnatws}. Also this switch has no effect on the
12263 handling of style check messages.
12264 @end table
12265
12266 @geindex -gnatw.s (gcc)
12267
12268 @geindex Record Representation (component sizes)
12269
12270
12271 @table @asis
12272
12273 @item @code{-gnatw.s}
12274
12275 @emph{Activate warnings on overridden size clauses.}
12276
12277 This switch activates warnings on component clauses in record
12278 representation clauses where the length given overrides that
12279 specified by an explicit size clause for the component type. A
12280 warning is similarly given in the array case if a specified
12281 component size overrides an explicit size clause for the array
12282 component type.
12283 @end table
12284
12285 @geindex -gnatw.S (gcc)
12286
12287
12288 @table @asis
12289
12290 @item @code{-gnatw.S}
12291
12292 @emph{Suppress warnings on overridden size clauses.}
12293
12294 This switch suppresses warnings on component clauses in record
12295 representation clauses that override size clauses, and similar
12296 warnings when an array component size overrides a size clause.
12297 @end table
12298
12299 @geindex -gnatwt (gcc)
12300
12301 @geindex Deactivated code
12302 @geindex warnings
12303
12304 @geindex Deleted code
12305 @geindex warnings
12306
12307
12308 @table @asis
12309
12310 @item @code{-gnatwt}
12311
12312 @emph{Activate warnings for tracking of deleted conditional code.}
12313
12314 This switch activates warnings for tracking of code in conditionals (IF and
12315 CASE statements) that is detected to be dead code which cannot be executed, and
12316 which is removed by the front end. This warning is off by default. This may be
12317 useful for detecting deactivated code in certified applications.
12318 @end table
12319
12320 @geindex -gnatwT (gcc)
12321
12322
12323 @table @asis
12324
12325 @item @code{-gnatwT}
12326
12327 @emph{Suppress warnings for tracking of deleted conditional code.}
12328
12329 This switch suppresses warnings for tracking of deleted conditional code.
12330 @end table
12331
12332 @geindex -gnatw.t (gcc)
12333
12334
12335 @table @asis
12336
12337 @item @code{-gnatw.t}
12338
12339 @emph{Activate warnings on suspicious contracts.}
12340
12341 This switch activates warnings on suspicious contracts. This includes
12342 warnings on suspicious postconditions (whether a pragma @code{Postcondition} or a
12343 @code{Post} aspect in Ada 2012) and suspicious contract cases (pragma or aspect
12344 @code{Contract_Cases}). A function postcondition or contract case is suspicious
12345 when no postcondition or contract case for this function mentions the result
12346 of the function. A procedure postcondition or contract case is suspicious
12347 when it only refers to the pre-state of the procedure, because in that case
12348 it should rather be expressed as a precondition. This switch also controls
12349 warnings on suspicious cases of expressions typically found in contracts like
12350 quantified expressions and uses of Update attribute. The default is that such
12351 warnings are generated.
12352 @end table
12353
12354 @geindex -gnatw.T (gcc)
12355
12356
12357 @table @asis
12358
12359 @item @code{-gnatw.T}
12360
12361 @emph{Suppress warnings on suspicious contracts.}
12362
12363 This switch suppresses warnings on suspicious contracts.
12364 @end table
12365
12366 @geindex -gnatwu (gcc)
12367
12368
12369 @table @asis
12370
12371 @item @code{-gnatwu}
12372
12373 @emph{Activate warnings on unused entities.}
12374
12375 This switch activates warnings to be generated for entities that
12376 are declared but not referenced, and for units that are @emph{with}ed
12377 and not
12378 referenced. In the case of packages, a warning is also generated if
12379 no entities in the package are referenced. This means that if a with'ed
12380 package is referenced but the only references are in @code{use}
12381 clauses or @code{renames}
12382 declarations, a warning is still generated. A warning is also generated
12383 for a generic package that is @emph{with}ed but never instantiated.
12384 In the case where a package or subprogram body is compiled, and there
12385 is a @emph{with} on the corresponding spec
12386 that is only referenced in the body,
12387 a warning is also generated, noting that the
12388 @emph{with} can be moved to the body. The default is that
12389 such warnings are not generated.
12390 This switch also activates warnings on unreferenced formals
12391 (it includes the effect of @code{-gnatwf}).
12392 @end table
12393
12394 @geindex -gnatwU (gcc)
12395
12396
12397 @table @asis
12398
12399 @item @code{-gnatwU}
12400
12401 @emph{Suppress warnings on unused entities.}
12402
12403 This switch suppresses warnings for unused entities and packages.
12404 It also turns off warnings on unreferenced formals (and thus includes
12405 the effect of @code{-gnatwF}).
12406 @end table
12407
12408 @geindex -gnatw.u (gcc)
12409
12410
12411 @table @asis
12412
12413 @item @code{-gnatw.u}
12414
12415 @emph{Activate warnings on unordered enumeration types.}
12416
12417 This switch causes enumeration types to be considered as conceptually
12418 unordered, unless an explicit pragma @code{Ordered} is given for the type.
12419 The effect is to generate warnings in clients that use explicit comparisons
12420 or subranges, since these constructs both treat objects of the type as
12421 ordered. (A @emph{client} is defined as a unit that is other than the unit in
12422 which the type is declared, or its body or subunits.) Please refer to
12423 the description of pragma @code{Ordered} in the
12424 @cite{GNAT Reference Manual} for further details.
12425 The default is that such warnings are not generated.
12426 @end table
12427
12428 @geindex -gnatw.U (gcc)
12429
12430
12431 @table @asis
12432
12433 @item @code{-gnatw.U}
12434
12435 @emph{Deactivate warnings on unordered enumeration types.}
12436
12437 This switch causes all enumeration types to be considered as ordered, so
12438 that no warnings are given for comparisons or subranges for any type.
12439 @end table
12440
12441 @geindex -gnatwv (gcc)
12442
12443 @geindex Unassigned variable warnings
12444
12445
12446 @table @asis
12447
12448 @item @code{-gnatwv}
12449
12450 @emph{Activate warnings on unassigned variables.}
12451
12452 This switch activates warnings for access to variables which
12453 may not be properly initialized. The default is that
12454 such warnings are generated.
12455 @end table
12456
12457 @geindex -gnatwV (gcc)
12458
12459
12460 @table @asis
12461
12462 @item @code{-gnatwV}
12463
12464 @emph{Suppress warnings on unassigned variables.}
12465
12466 This switch suppresses warnings for access to variables which
12467 may not be properly initialized.
12468 For variables of a composite type, the warning can also be suppressed in
12469 Ada 2005 by using a default initialization with a box. For example, if
12470 Table is an array of records whose components are only partially uninitialized,
12471 then the following code:
12472
12473 @example
12474 Tab : Table := (others => <>);
12475 @end example
12476
12477 will suppress warnings on subsequent statements that access components
12478 of variable Tab.
12479 @end table
12480
12481 @geindex -gnatw.v (gcc)
12482
12483 @geindex bit order warnings
12484
12485
12486 @table @asis
12487
12488 @item @code{-gnatw.v}
12489
12490 @emph{Activate info messages for non-default bit order.}
12491
12492 This switch activates messages (labeled "info", they are not warnings,
12493 just informational messages) about the effects of non-default bit-order
12494 on records to which a component clause is applied. The effect of specifying
12495 non-default bit ordering is a bit subtle (and changed with Ada 2005), so
12496 these messages, which are given by default, are useful in understanding the
12497 exact consequences of using this feature.
12498 @end table
12499
12500 @geindex -gnatw.V (gcc)
12501
12502
12503 @table @asis
12504
12505 @item @code{-gnatw.V}
12506
12507 @emph{Suppress info messages for non-default bit order.}
12508
12509 This switch suppresses information messages for the effects of specifying
12510 non-default bit order on record components with component clauses.
12511 @end table
12512
12513 @geindex -gnatww (gcc)
12514
12515 @geindex String indexing warnings
12516
12517
12518 @table @asis
12519
12520 @item @code{-gnatww}
12521
12522 @emph{Activate warnings on wrong low bound assumption.}
12523
12524 This switch activates warnings for indexing an unconstrained string parameter
12525 with a literal or S'Length. This is a case where the code is assuming that the
12526 low bound is one, which is in general not true (for example when a slice is
12527 passed). The default is that such warnings are generated.
12528 @end table
12529
12530 @geindex -gnatwW (gcc)
12531
12532
12533 @table @asis
12534
12535 @item @code{-gnatwW}
12536
12537 @emph{Suppress warnings on wrong low bound assumption.}
12538
12539 This switch suppresses warnings for indexing an unconstrained string parameter
12540 with a literal or S'Length. Note that this warning can also be suppressed
12541 in a particular case by adding an assertion that the lower bound is 1,
12542 as shown in the following example:
12543
12544 @example
12545 procedure K (S : String) is
12546 pragma Assert (S'First = 1);
12547 ...
12548 @end example
12549 @end table
12550
12551 @geindex -gnatw.w (gcc)
12552
12553 @geindex Warnings Off control
12554
12555
12556 @table @asis
12557
12558 @item @code{-gnatw.w}
12559
12560 @emph{Activate warnings on Warnings Off pragmas.}
12561
12562 This switch activates warnings for use of @code{pragma Warnings (Off, entity)}
12563 where either the pragma is entirely useless (because it suppresses no
12564 warnings), or it could be replaced by @code{pragma Unreferenced} or
12565 @code{pragma Unmodified}.
12566 Also activates warnings for the case of
12567 Warnings (Off, String), where either there is no matching
12568 Warnings (On, String), or the Warnings (Off) did not suppress any warning.
12569 The default is that these warnings are not given.
12570 @end table
12571
12572 @geindex -gnatw.W (gcc)
12573
12574
12575 @table @asis
12576
12577 @item @code{-gnatw.W}
12578
12579 @emph{Suppress warnings on unnecessary Warnings Off pragmas.}
12580
12581 This switch suppresses warnings for use of @code{pragma Warnings (Off, ...)}.
12582 @end table
12583
12584 @geindex -gnatwx (gcc)
12585
12586 @geindex Export/Import pragma warnings
12587
12588
12589 @table @asis
12590
12591 @item @code{-gnatwx}
12592
12593 @emph{Activate warnings on Export/Import pragmas.}
12594
12595 This switch activates warnings on Export/Import pragmas when
12596 the compiler detects a possible conflict between the Ada and
12597 foreign language calling sequences. For example, the use of
12598 default parameters in a convention C procedure is dubious
12599 because the C compiler cannot supply the proper default, so
12600 a warning is issued. The default is that such warnings are
12601 generated.
12602 @end table
12603
12604 @geindex -gnatwX (gcc)
12605
12606
12607 @table @asis
12608
12609 @item @code{-gnatwX}
12610
12611 @emph{Suppress warnings on Export/Import pragmas.}
12612
12613 This switch suppresses warnings on Export/Import pragmas.
12614 The sense of this is that you are telling the compiler that
12615 you know what you are doing in writing the pragma, and it
12616 should not complain at you.
12617 @end table
12618
12619 @geindex -gnatwm (gcc)
12620
12621
12622 @table @asis
12623
12624 @item @code{-gnatw.x}
12625
12626 @emph{Activate warnings for No_Exception_Propagation mode.}
12627
12628 This switch activates warnings for exception usage when pragma Restrictions
12629 (No_Exception_Propagation) is in effect. Warnings are given for implicit or
12630 explicit exception raises which are not covered by a local handler, and for
12631 exception handlers which do not cover a local raise. The default is that
12632 these warnings are given for units that contain exception handlers.
12633
12634 @item @code{-gnatw.X}
12635
12636 @emph{Disable warnings for No_Exception_Propagation mode.}
12637
12638 This switch disables warnings for exception usage when pragma Restrictions
12639 (No_Exception_Propagation) is in effect.
12640 @end table
12641
12642 @geindex -gnatwy (gcc)
12643
12644 @geindex Ada compatibility issues warnings
12645
12646
12647 @table @asis
12648
12649 @item @code{-gnatwy}
12650
12651 @emph{Activate warnings for Ada compatibility issues.}
12652
12653 For the most part, newer versions of Ada are upwards compatible
12654 with older versions. For example, Ada 2005 programs will almost
12655 always work when compiled as Ada 2012.
12656 However there are some exceptions (for example the fact that
12657 @code{some} is now a reserved word in Ada 2012). This
12658 switch activates several warnings to help in identifying
12659 and correcting such incompatibilities. The default is that
12660 these warnings are generated. Note that at one point Ada 2005
12661 was called Ada 0Y, hence the choice of character.
12662 @end table
12663
12664 @geindex -gnatwY (gcc)
12665
12666 @geindex Ada compatibility issues warnings
12667
12668
12669 @table @asis
12670
12671 @item @code{-gnatwY}
12672
12673 @emph{Disable warnings for Ada compatibility issues.}
12674
12675 This switch suppresses the warnings intended to help in identifying
12676 incompatibilities between Ada language versions.
12677 @end table
12678
12679 @geindex -gnatw.y (gcc)
12680
12681 @geindex Package spec needing body
12682
12683
12684 @table @asis
12685
12686 @item @code{-gnatw.y}
12687
12688 @emph{Activate information messages for why package spec needs body.}
12689
12690 There are a number of cases in which a package spec needs a body.
12691 For example, the use of pragma Elaborate_Body, or the declaration
12692 of a procedure specification requiring a completion. This switch
12693 causes information messages to be output showing why a package
12694 specification requires a body. This can be useful in the case of
12695 a large package specification which is unexpectedly requiring a
12696 body. The default is that such information messages are not output.
12697 @end table
12698
12699 @geindex -gnatw.Y (gcc)
12700
12701 @geindex No information messages for why package spec needs body
12702
12703
12704 @table @asis
12705
12706 @item @code{-gnatw.Y}
12707
12708 @emph{Disable information messages for why package spec needs body.}
12709
12710 This switch suppresses the output of information messages showing why
12711 a package specification needs a body.
12712 @end table
12713
12714 @geindex -gnatwz (gcc)
12715
12716 @geindex Unchecked_Conversion warnings
12717
12718
12719 @table @asis
12720
12721 @item @code{-gnatwz}
12722
12723 @emph{Activate warnings on unchecked conversions.}
12724
12725 This switch activates warnings for unchecked conversions
12726 where the types are known at compile time to have different
12727 sizes. The default is that such warnings are generated. Warnings are also
12728 generated for subprogram pointers with different conventions.
12729 @end table
12730
12731 @geindex -gnatwZ (gcc)
12732
12733
12734 @table @asis
12735
12736 @item @code{-gnatwZ}
12737
12738 @emph{Suppress warnings on unchecked conversions.}
12739
12740 This switch suppresses warnings for unchecked conversions
12741 where the types are known at compile time to have different
12742 sizes or conventions.
12743 @end table
12744
12745 @geindex -gnatw.z (gcc)
12746
12747 @geindex Size/Alignment warnings
12748
12749
12750 @table @asis
12751
12752 @item @code{-gnatw.z}
12753
12754 @emph{Activate warnings for size not a multiple of alignment.}
12755
12756 This switch activates warnings for cases of array and record types
12757 with specified @code{Size} and @code{Alignment} attributes where the
12758 size is not a multiple of the alignment, resulting in an object
12759 size that is greater than the specified size. The default
12760 is that such warnings are generated.
12761 @end table
12762
12763 @geindex -gnatw.Z (gcc)
12764
12765 @geindex Size/Alignment warnings
12766
12767
12768 @table @asis
12769
12770 @item @code{-gnatw.Z}
12771
12772 @emph{Suppress warnings for size not a multiple of alignment.}
12773
12774 This switch suppresses warnings for cases of array and record types
12775 with specified @code{Size} and @code{Alignment} attributes where the
12776 size is not a multiple of the alignment, resulting in an object
12777 size that is greater than the specified size. The warning can also
12778 be suppressed by giving an explicit @code{Object_Size} value.
12779 @end table
12780
12781 @geindex -Wunused (gcc)
12782
12783
12784 @table @asis
12785
12786 @item @code{-Wunused}
12787
12788 The warnings controlled by the @code{-gnatw} switch are generated by
12789 the front end of the compiler. The GCC back end can provide
12790 additional warnings and they are controlled by the @code{-W} switch.
12791 For example, @code{-Wunused} activates back end
12792 warnings for entities that are declared but not referenced.
12793 @end table
12794
12795 @geindex -Wuninitialized (gcc)
12796
12797
12798 @table @asis
12799
12800 @item @code{-Wuninitialized}
12801
12802 Similarly, @code{-Wuninitialized} activates
12803 the back end warning for uninitialized variables. This switch must be
12804 used in conjunction with an optimization level greater than zero.
12805 @end table
12806
12807 @geindex -Wstack-usage (gcc)
12808
12809
12810 @table @asis
12811
12812 @item @code{-Wstack-usage=@emph{len}}
12813
12814 Warn if the stack usage of a subprogram might be larger than @code{len} bytes.
12815 See @ref{f5,,Static Stack Usage Analysis} for details.
12816 @end table
12817
12818 @geindex -Wall (gcc)
12819
12820
12821 @table @asis
12822
12823 @item @code{-Wall}
12824
12825 This switch enables most warnings from the GCC back end.
12826 The code generator detects a number of warning situations that are missed
12827 by the GNAT front end, and this switch can be used to activate them.
12828 The use of this switch also sets the default front end warning mode to
12829 @code{-gnatwa}, that is, most front end warnings activated as well.
12830 @end table
12831
12832 @geindex -w (gcc)
12833
12834
12835 @table @asis
12836
12837 @item @code{-w}
12838
12839 Conversely, this switch suppresses warnings from the GCC back end.
12840 The use of this switch also sets the default front end warning mode to
12841 @code{-gnatws}, that is, front end warnings suppressed as well.
12842 @end table
12843
12844 @geindex -Werror (gcc)
12845
12846
12847 @table @asis
12848
12849 @item @code{-Werror}
12850
12851 This switch causes warnings from the GCC back end to be treated as
12852 errors. The warning string still appears, but the warning messages are
12853 counted as errors, and prevent the generation of an object file.
12854 @end table
12855
12856 A string of warning parameters can be used in the same parameter. For example:
12857
12858 @example
12859 -gnatwaGe
12860 @end example
12861
12862 will turn on all optional warnings except for unrecognized pragma warnings,
12863 and also specify that warnings should be treated as errors.
12864
12865 When no switch @code{-gnatw} is used, this is equivalent to:
12866
12867 @quotation
12868
12869
12870 @itemize *
12871
12872 @item
12873 @code{-gnatw.a}
12874
12875 @item
12876 @code{-gnatwB}
12877
12878 @item
12879 @code{-gnatw.b}
12880
12881 @item
12882 @code{-gnatwC}
12883
12884 @item
12885 @code{-gnatw.C}
12886
12887 @item
12888 @code{-gnatwD}
12889
12890 @item
12891 @code{-gnatw.D}
12892
12893 @item
12894 @code{-gnatwF}
12895
12896 @item
12897 @code{-gnatw.F}
12898
12899 @item
12900 @code{-gnatwg}
12901
12902 @item
12903 @code{-gnatwH}
12904
12905 @item
12906 @code{-gnatw.H}
12907
12908 @item
12909 @code{-gnatwi}
12910
12911 @item
12912 @code{-gnatwJ}
12913
12914 @item
12915 @code{-gnatw.J}
12916
12917 @item
12918 @code{-gnatwK}
12919
12920 @item
12921 @code{-gnatw.K}
12922
12923 @item
12924 @code{-gnatwL}
12925
12926 @item
12927 @code{-gnatw.L}
12928
12929 @item
12930 @code{-gnatwM}
12931
12932 @item
12933 @code{-gnatw.m}
12934
12935 @item
12936 @code{-gnatwn}
12937
12938 @item
12939 @code{-gnatw.N}
12940
12941 @item
12942 @code{-gnatwo}
12943
12944 @item
12945 @code{-gnatw.O}
12946
12947 @item
12948 @code{-gnatwP}
12949
12950 @item
12951 @code{-gnatw.P}
12952
12953 @item
12954 @code{-gnatwq}
12955
12956 @item
12957 @code{-gnatw.Q}
12958
12959 @item
12960 @code{-gnatwR}
12961
12962 @item
12963 @code{-gnatw.R}
12964
12965 @item
12966 @code{-gnatw.S}
12967
12968 @item
12969 @code{-gnatwT}
12970
12971 @item
12972 @code{-gnatw.t}
12973
12974 @item
12975 @code{-gnatwU}
12976
12977 @item
12978 @code{-gnatw.U}
12979
12980 @item
12981 @code{-gnatwv}
12982
12983 @item
12984 @code{-gnatw.v}
12985
12986 @item
12987 @code{-gnatww}
12988
12989 @item
12990 @code{-gnatw.W}
12991
12992 @item
12993 @code{-gnatwx}
12994
12995 @item
12996 @code{-gnatw.X}
12997
12998 @item
12999 @code{-gnatwy}
13000
13001 @item
13002 @code{-gnatw.Y}
13003
13004 @item
13005 @code{-gnatwz}
13006
13007 @item
13008 @code{-gnatw.z}
13009 @end itemize
13010 @end quotation
13011
13012 @node Debugging and Assertion Control,Validity Checking,Warning Message Control,Compiler Switches
13013 @anchor{gnat_ugn/building_executable_programs_with_gnat debugging-and-assertion-control}@anchor{100}@anchor{gnat_ugn/building_executable_programs_with_gnat id16}@anchor{101}
13014 @subsection Debugging and Assertion Control
13015
13016
13017 @geindex -gnata (gcc)
13018
13019
13020 @table @asis
13021
13022 @item @code{-gnata}
13023
13024 @geindex Assert
13025
13026 @geindex Debug
13027
13028 @geindex Assertions
13029
13030 @geindex Precondition
13031
13032 @geindex Postcondition
13033
13034 @geindex Type invariants
13035
13036 @geindex Subtype predicates
13037
13038 The @code{-gnata} option is equivalent to the following @code{Assertion_Policy} pragma:
13039
13040 @example
13041 pragma Assertion_Policy (Check);
13042 @end example
13043
13044 Which is a shorthand for:
13045
13046 @example
13047 pragma Assertion_Policy
13048 (Assert => Check,
13049 Static_Predicate => Check,
13050 Dynamic_Predicate => Check,
13051 Pre => Check,
13052 Pre'Class => Check,
13053 Post => Check,
13054 Post'Class => Check,
13055 Type_Invariant => Check,
13056 Type_Invariant'Class => Check);
13057 @end example
13058
13059 The pragmas @code{Assert} and @code{Debug} normally have no effect and
13060 are ignored. This switch, where @code{a} stands for 'assert', causes
13061 pragmas @code{Assert} and @code{Debug} to be activated. This switch also
13062 causes preconditions, postconditions, subtype predicates, and
13063 type invariants to be activated.
13064
13065 The pragmas have the form:
13066
13067 @example
13068 pragma Assert (<Boolean-expression> [, <static-string-expression>])
13069 pragma Debug (<procedure call>)
13070 pragma Type_Invariant (<type-local-name>, <Boolean-expression>)
13071 pragma Predicate (<type-local-name>, <Boolean-expression>)
13072 pragma Precondition (<Boolean-expression>, <string-expression>)
13073 pragma Postcondition (<Boolean-expression>, <string-expression>)
13074 @end example
13075
13076 The aspects have the form:
13077
13078 @example
13079 with [Pre|Post|Type_Invariant|Dynamic_Predicate|Static_Predicate]
13080 => <Boolean-expression>;
13081 @end example
13082
13083 The @code{Assert} pragma causes @code{Boolean-expression} to be tested.
13084 If the result is @code{True}, the pragma has no effect (other than
13085 possible side effects from evaluating the expression). If the result is
13086 @code{False}, the exception @code{Assert_Failure} declared in the package
13087 @code{System.Assertions} is raised (passing @code{static-string-expression}, if
13088 present, as the message associated with the exception). If no string
13089 expression is given, the default is a string containing the file name and
13090 line number of the pragma.
13091
13092 The @code{Debug} pragma causes @code{procedure} to be called. Note that
13093 @code{pragma Debug} may appear within a declaration sequence, allowing
13094 debugging procedures to be called between declarations.
13095
13096 For the aspect specification, the @code{Boolean-expression} is evaluated.
13097 If the result is @code{True}, the aspect has no effect. If the result
13098 is @code{False}, the exception @code{Assert_Failure} is raised.
13099 @end table
13100
13101 @node Validity Checking,Style Checking,Debugging and Assertion Control,Compiler Switches
13102 @anchor{gnat_ugn/building_executable_programs_with_gnat validity-checking}@anchor{f6}@anchor{gnat_ugn/building_executable_programs_with_gnat id17}@anchor{102}
13103 @subsection Validity Checking
13104
13105
13106 @geindex Validity Checking
13107
13108 The Ada Reference Manual defines the concept of invalid values (see
13109 RM 13.9.1). The primary source of invalid values is uninitialized
13110 variables. A scalar variable that is left uninitialized may contain
13111 an invalid value; the concept of invalid does not apply to access or
13112 composite types.
13113
13114 It is an error to read an invalid value, but the RM does not require
13115 run-time checks to detect such errors, except for some minimal
13116 checking to prevent erroneous execution (i.e. unpredictable
13117 behavior). This corresponds to the @code{-gnatVd} switch below,
13118 which is the default. For example, by default, if the expression of a
13119 case statement is invalid, it will raise Constraint_Error rather than
13120 causing a wild jump, and if an array index on the left-hand side of an
13121 assignment is invalid, it will raise Constraint_Error rather than
13122 overwriting an arbitrary memory location.
13123
13124 The @code{-gnatVa} may be used to enable additional validity checks,
13125 which are not required by the RM. These checks are often very
13126 expensive (which is why the RM does not require them). These checks
13127 are useful in tracking down uninitialized variables, but they are
13128 not usually recommended for production builds, and in particular
13129 we do not recommend using these extra validity checking options in
13130 combination with optimization, since this can confuse the optimizer.
13131 If performance is a consideration, leading to the need to optimize,
13132 then the validity checking options should not be used.
13133
13134 The other @code{-gnatV@emph{x}} switches below allow finer-grained
13135 control; you can enable whichever validity checks you desire. However,
13136 for most debugging purposes, @code{-gnatVa} is sufficient, and the
13137 default @code{-gnatVd} (i.e. standard Ada behavior) is usually
13138 sufficient for non-debugging use.
13139
13140 The @code{-gnatB} switch tells the compiler to assume that all
13141 values are valid (that is, within their declared subtype range)
13142 except in the context of a use of the Valid attribute. This means
13143 the compiler can generate more efficient code, since the range
13144 of values is better known at compile time. However, an uninitialized
13145 variable can cause wild jumps and memory corruption in this mode.
13146
13147 The @code{-gnatV@emph{x}} switch allows control over the validity
13148 checking mode as described below.
13149 The @code{x} argument is a string of letters that
13150 indicate validity checks that are performed or not performed in addition
13151 to the default checks required by Ada as described above.
13152
13153 @geindex -gnatVa (gcc)
13154
13155
13156 @table @asis
13157
13158 @item @code{-gnatVa}
13159
13160 @emph{All validity checks.}
13161
13162 All validity checks are turned on.
13163 That is, @code{-gnatVa} is
13164 equivalent to @code{gnatVcdfimorst}.
13165 @end table
13166
13167 @geindex -gnatVc (gcc)
13168
13169
13170 @table @asis
13171
13172 @item @code{-gnatVc}
13173
13174 @emph{Validity checks for copies.}
13175
13176 The right hand side of assignments, and the initializing values of
13177 object declarations are validity checked.
13178 @end table
13179
13180 @geindex -gnatVd (gcc)
13181
13182
13183 @table @asis
13184
13185 @item @code{-gnatVd}
13186
13187 @emph{Default (RM) validity checks.}
13188
13189 Some validity checks are done by default following normal Ada semantics
13190 (RM 13.9.1 (9-11)).
13191 A check is done in case statements that the expression is within the range
13192 of the subtype. If it is not, Constraint_Error is raised.
13193 For assignments to array components, a check is done that the expression used
13194 as index is within the range. If it is not, Constraint_Error is raised.
13195 Both these validity checks may be turned off using switch @code{-gnatVD}.
13196 They are turned on by default. If @code{-gnatVD} is specified, a subsequent
13197 switch @code{-gnatVd} will leave the checks turned on.
13198 Switch @code{-gnatVD} should be used only if you are sure that all such
13199 expressions have valid values. If you use this switch and invalid values
13200 are present, then the program is erroneous, and wild jumps or memory
13201 overwriting may occur.
13202 @end table
13203
13204 @geindex -gnatVe (gcc)
13205
13206
13207 @table @asis
13208
13209 @item @code{-gnatVe}
13210
13211 @emph{Validity checks for elementary components.}
13212
13213 In the absence of this switch, assignments to record or array components are
13214 not validity checked, even if validity checks for assignments generally
13215 (@code{-gnatVc}) are turned on. In Ada, assignment of composite values do not
13216 require valid data, but assignment of individual components does. So for
13217 example, there is a difference between copying the elements of an array with a
13218 slice assignment, compared to assigning element by element in a loop. This
13219 switch allows you to turn off validity checking for components, even when they
13220 are assigned component by component.
13221 @end table
13222
13223 @geindex -gnatVf (gcc)
13224
13225
13226 @table @asis
13227
13228 @item @code{-gnatVf}
13229
13230 @emph{Validity checks for floating-point values.}
13231
13232 In the absence of this switch, validity checking occurs only for discrete
13233 values. If @code{-gnatVf} is specified, then validity checking also applies
13234 for floating-point values, and NaNs and infinities are considered invalid,
13235 as well as out of range values for constrained types. Note that this means
13236 that standard IEEE infinity mode is not allowed. The exact contexts
13237 in which floating-point values are checked depends on the setting of other
13238 options. For example, @code{-gnatVif} or @code{-gnatVfi}
13239 (the order does not matter) specifies that floating-point parameters of mode
13240 @code{in} should be validity checked.
13241 @end table
13242
13243 @geindex -gnatVi (gcc)
13244
13245
13246 @table @asis
13247
13248 @item @code{-gnatVi}
13249
13250 @emph{Validity checks for `@w{`}in`@w{`} mode parameters.}
13251
13252 Arguments for parameters of mode @code{in} are validity checked in function
13253 and procedure calls at the point of call.
13254 @end table
13255
13256 @geindex -gnatVm (gcc)
13257
13258
13259 @table @asis
13260
13261 @item @code{-gnatVm}
13262
13263 @emph{Validity checks for `@w{`}in out`@w{`} mode parameters.}
13264
13265 Arguments for parameters of mode @code{in out} are validity checked in
13266 procedure calls at the point of call. The @code{'m'} here stands for
13267 modify, since this concerns parameters that can be modified by the call.
13268 Note that there is no specific option to test @code{out} parameters,
13269 but any reference within the subprogram will be tested in the usual
13270 manner, and if an invalid value is copied back, any reference to it
13271 will be subject to validity checking.
13272 @end table
13273
13274 @geindex -gnatVn (gcc)
13275
13276
13277 @table @asis
13278
13279 @item @code{-gnatVn}
13280
13281 @emph{No validity checks.}
13282
13283 This switch turns off all validity checking, including the default checking
13284 for case statements and left hand side subscripts. Note that the use of
13285 the switch @code{-gnatp} suppresses all run-time checks, including
13286 validity checks, and thus implies @code{-gnatVn}. When this switch
13287 is used, it cancels any other @code{-gnatV} previously issued.
13288 @end table
13289
13290 @geindex -gnatVo (gcc)
13291
13292
13293 @table @asis
13294
13295 @item @code{-gnatVo}
13296
13297 @emph{Validity checks for operator and attribute operands.}
13298
13299 Arguments for predefined operators and attributes are validity checked.
13300 This includes all operators in package @code{Standard},
13301 the shift operators defined as intrinsic in package @code{Interfaces}
13302 and operands for attributes such as @code{Pos}. Checks are also made
13303 on individual component values for composite comparisons, and on the
13304 expressions in type conversions and qualified expressions. Checks are
13305 also made on explicit ranges using @code{..} (e.g., slices, loops etc).
13306 @end table
13307
13308 @geindex -gnatVp (gcc)
13309
13310
13311 @table @asis
13312
13313 @item @code{-gnatVp}
13314
13315 @emph{Validity checks for parameters.}
13316
13317 This controls the treatment of parameters within a subprogram (as opposed
13318 to @code{-gnatVi} and @code{-gnatVm} which control validity testing
13319 of parameters on a call. If either of these call options is used, then
13320 normally an assumption is made within a subprogram that the input arguments
13321 have been validity checking at the point of call, and do not need checking
13322 again within a subprogram). If @code{-gnatVp} is set, then this assumption
13323 is not made, and parameters are not assumed to be valid, so their validity
13324 will be checked (or rechecked) within the subprogram.
13325 @end table
13326
13327 @geindex -gnatVr (gcc)
13328
13329
13330 @table @asis
13331
13332 @item @code{-gnatVr}
13333
13334 @emph{Validity checks for function returns.}
13335
13336 The expression in @code{return} statements in functions is validity
13337 checked.
13338 @end table
13339
13340 @geindex -gnatVs (gcc)
13341
13342
13343 @table @asis
13344
13345 @item @code{-gnatVs}
13346
13347 @emph{Validity checks for subscripts.}
13348
13349 All subscripts expressions are checked for validity, whether they appear
13350 on the right side or left side (in default mode only left side subscripts
13351 are validity checked).
13352 @end table
13353
13354 @geindex -gnatVt (gcc)
13355
13356
13357 @table @asis
13358
13359 @item @code{-gnatVt}
13360
13361 @emph{Validity checks for tests.}
13362
13363 Expressions used as conditions in @code{if}, @code{while} or @code{exit}
13364 statements are checked, as well as guard expressions in entry calls.
13365 @end table
13366
13367 The @code{-gnatV} switch may be followed by a string of letters
13368 to turn on a series of validity checking options.
13369 For example, @code{-gnatVcr}
13370 specifies that in addition to the default validity checking, copies and
13371 function return expressions are to be validity checked.
13372 In order to make it easier to specify the desired combination of effects,
13373 the upper case letters @code{CDFIMORST} may
13374 be used to turn off the corresponding lower case option.
13375 Thus @code{-gnatVaM} turns on all validity checking options except for
13376 checking of @code{in out} parameters.
13377
13378 The specification of additional validity checking generates extra code (and
13379 in the case of @code{-gnatVa} the code expansion can be substantial).
13380 However, these additional checks can be very useful in detecting
13381 uninitialized variables, incorrect use of unchecked conversion, and other
13382 errors leading to invalid values. The use of pragma @code{Initialize_Scalars}
13383 is useful in conjunction with the extra validity checking, since this
13384 ensures that wherever possible uninitialized variables have invalid values.
13385
13386 See also the pragma @code{Validity_Checks} which allows modification of
13387 the validity checking mode at the program source level, and also allows for
13388 temporary disabling of validity checks.
13389
13390 @node Style Checking,Run-Time Checks,Validity Checking,Compiler Switches
13391 @anchor{gnat_ugn/building_executable_programs_with_gnat id18}@anchor{103}@anchor{gnat_ugn/building_executable_programs_with_gnat style-checking}@anchor{fb}
13392 @subsection Style Checking
13393
13394
13395 @geindex Style checking
13396
13397 @geindex -gnaty (gcc)
13398
13399 The @code{-gnatyx} switch causes the compiler to
13400 enforce specified style rules. A limited set of style rules has been used
13401 in writing the GNAT sources themselves. This switch allows user programs
13402 to activate all or some of these checks. If the source program fails a
13403 specified style check, an appropriate message is given, preceded by
13404 the character sequence '(style)'. This message does not prevent
13405 successful compilation (unless the @code{-gnatwe} switch is used).
13406
13407 Note that this is by no means intended to be a general facility for
13408 checking arbitrary coding standards. It is simply an embedding of the
13409 style rules we have chosen for the GNAT sources. If you are starting
13410 a project which does not have established style standards, you may
13411 find it useful to adopt the entire set of GNAT coding standards, or
13412 some subset of them.
13413
13414
13415 The string @code{x} is a sequence of letters or digits
13416 indicating the particular style
13417 checks to be performed. The following checks are defined:
13418
13419 @geindex -gnaty[0-9] (gcc)
13420
13421
13422 @table @asis
13423
13424 @item @code{-gnaty0}
13425
13426 @emph{Specify indentation level.}
13427
13428 If a digit from 1-9 appears
13429 in the string after @code{-gnaty}
13430 then proper indentation is checked, with the digit indicating the
13431 indentation level required. A value of zero turns off this style check.
13432 The general style of required indentation is as specified by
13433 the examples in the Ada Reference Manual. Full line comments must be
13434 aligned with the @code{--} starting on a column that is a multiple of
13435 the alignment level, or they may be aligned the same way as the following
13436 non-blank line (this is useful when full line comments appear in the middle
13437 of a statement, or they may be aligned with the source line on the previous
13438 non-blank line.
13439 @end table
13440
13441 @geindex -gnatya (gcc)
13442
13443
13444 @table @asis
13445
13446 @item @code{-gnatya}
13447
13448 @emph{Check attribute casing.}
13449
13450 Attribute names, including the case of keywords such as @code{digits}
13451 used as attributes names, must be written in mixed case, that is, the
13452 initial letter and any letter following an underscore must be uppercase.
13453 All other letters must be lowercase.
13454 @end table
13455
13456 @geindex -gnatyA (gcc)
13457
13458
13459 @table @asis
13460
13461 @item @code{-gnatyA}
13462
13463 @emph{Use of array index numbers in array attributes.}
13464
13465 When using the array attributes First, Last, Range,
13466 or Length, the index number must be omitted for one-dimensional arrays
13467 and is required for multi-dimensional arrays.
13468 @end table
13469
13470 @geindex -gnatyb (gcc)
13471
13472
13473 @table @asis
13474
13475 @item @code{-gnatyb}
13476
13477 @emph{Blanks not allowed at statement end.}
13478
13479 Trailing blanks are not allowed at the end of statements. The purpose of this
13480 rule, together with h (no horizontal tabs), is to enforce a canonical format
13481 for the use of blanks to separate source tokens.
13482 @end table
13483
13484 @geindex -gnatyB (gcc)
13485
13486
13487 @table @asis
13488
13489 @item @code{-gnatyB}
13490
13491 @emph{Check Boolean operators.}
13492
13493 The use of AND/OR operators is not permitted except in the cases of modular
13494 operands, array operands, and simple stand-alone boolean variables or
13495 boolean constants. In all other cases @code{and then}/@cite{or else} are
13496 required.
13497 @end table
13498
13499 @geindex -gnatyc (gcc)
13500
13501
13502 @table @asis
13503
13504 @item @code{-gnatyc}
13505
13506 @emph{Check comments, double space.}
13507
13508 Comments must meet the following set of rules:
13509
13510
13511 @itemize *
13512
13513 @item
13514 The @code{--} that starts the column must either start in column one,
13515 or else at least one blank must precede this sequence.
13516
13517 @item
13518 Comments that follow other tokens on a line must have at least one blank
13519 following the @code{--} at the start of the comment.
13520
13521 @item
13522 Full line comments must have at least two blanks following the
13523 @code{--} that starts the comment, with the following exceptions.
13524
13525 @item
13526 A line consisting only of the @code{--} characters, possibly preceded
13527 by blanks is permitted.
13528
13529 @item
13530 A comment starting with @code{--x} where @code{x} is a special character
13531 is permitted.
13532 This allows proper processing of the output from specialized tools
13533 such as @code{gnatprep} (where @code{--!} is used) and in earlier versions of the SPARK
13534 annotation
13535 language (where @code{--#} is used). For the purposes of this rule, a
13536 special character is defined as being in one of the ASCII ranges
13537 @code{16#21#...16#2F#} or @code{16#3A#...16#3F#}.
13538 Note that this usage is not permitted
13539 in GNAT implementation units (i.e., when @code{-gnatg} is used).
13540
13541 @item
13542 A line consisting entirely of minus signs, possibly preceded by blanks, is
13543 permitted. This allows the construction of box comments where lines of minus
13544 signs are used to form the top and bottom of the box.
13545
13546 @item
13547 A comment that starts and ends with @code{--} is permitted as long as at
13548 least one blank follows the initial @code{--}. Together with the preceding
13549 rule, this allows the construction of box comments, as shown in the following
13550 example:
13551
13552 @example
13553 ---------------------------
13554 -- This is a box comment --
13555 -- with two text lines. --
13556 ---------------------------
13557 @end example
13558 @end itemize
13559 @end table
13560
13561 @geindex -gnatyC (gcc)
13562
13563
13564 @table @asis
13565
13566 @item @code{-gnatyC}
13567
13568 @emph{Check comments, single space.}
13569
13570 This is identical to @code{c} except that only one space
13571 is required following the @code{--} of a comment instead of two.
13572 @end table
13573
13574 @geindex -gnatyd (gcc)
13575
13576
13577 @table @asis
13578
13579 @item @code{-gnatyd}
13580
13581 @emph{Check no DOS line terminators present.}
13582
13583 All lines must be terminated by a single ASCII.LF
13584 character (in particular the DOS line terminator sequence CR/LF is not
13585 allowed).
13586 @end table
13587
13588 @geindex -gnatyD (gcc)
13589
13590
13591 @table @asis
13592
13593 @item @code{-gnatyD}
13594
13595 @emph{Check declared identifiers in mixed case.}
13596
13597 Declared identifiers must be in mixed case, as in
13598 This_Is_An_Identifier. Use -gnatyr in addition to ensure
13599 that references match declarations.
13600 @end table
13601
13602 @geindex -gnatye (gcc)
13603
13604
13605 @table @asis
13606
13607 @item @code{-gnatye}
13608
13609 @emph{Check end/exit labels.}
13610
13611 Optional labels on @code{end} statements ending subprograms and on
13612 @code{exit} statements exiting named loops, are required to be present.
13613 @end table
13614
13615 @geindex -gnatyf (gcc)
13616
13617
13618 @table @asis
13619
13620 @item @code{-gnatyf}
13621
13622 @emph{No form feeds or vertical tabs.}
13623
13624 Neither form feeds nor vertical tab characters are permitted
13625 in the source text.
13626 @end table
13627
13628 @geindex -gnatyg (gcc)
13629
13630
13631 @table @asis
13632
13633 @item @code{-gnatyg}
13634
13635 @emph{GNAT style mode.}
13636
13637 The set of style check switches is set to match that used by the GNAT sources.
13638 This may be useful when developing code that is eventually intended to be
13639 incorporated into GNAT. Currently this is equivalent to @code{-gnatyydISux})
13640 but additional style switches may be added to this set in the future without
13641 advance notice.
13642 @end table
13643
13644 @geindex -gnatyh (gcc)
13645
13646
13647 @table @asis
13648
13649 @item @code{-gnatyh}
13650
13651 @emph{No horizontal tabs.}
13652
13653 Horizontal tab characters are not permitted in the source text.
13654 Together with the b (no blanks at end of line) check, this
13655 enforces a canonical form for the use of blanks to separate
13656 source tokens.
13657 @end table
13658
13659 @geindex -gnatyi (gcc)
13660
13661
13662 @table @asis
13663
13664 @item @code{-gnatyi}
13665
13666 @emph{Check if-then layout.}
13667
13668 The keyword @code{then} must appear either on the same
13669 line as corresponding @code{if}, or on a line on its own, lined
13670 up under the @code{if}.
13671 @end table
13672
13673 @geindex -gnatyI (gcc)
13674
13675
13676 @table @asis
13677
13678 @item @code{-gnatyI}
13679
13680 @emph{check mode IN keywords.}
13681
13682 Mode @code{in} (the default mode) is not
13683 allowed to be given explicitly. @code{in out} is fine,
13684 but not @code{in} on its own.
13685 @end table
13686
13687 @geindex -gnatyk (gcc)
13688
13689
13690 @table @asis
13691
13692 @item @code{-gnatyk}
13693
13694 @emph{Check keyword casing.}
13695
13696 All keywords must be in lower case (with the exception of keywords
13697 such as @code{digits} used as attribute names to which this check
13698 does not apply).
13699 @end table
13700
13701 @geindex -gnatyl (gcc)
13702
13703
13704 @table @asis
13705
13706 @item @code{-gnatyl}
13707
13708 @emph{Check layout.}
13709
13710 Layout of statement and declaration constructs must follow the
13711 recommendations in the Ada Reference Manual, as indicated by the
13712 form of the syntax rules. For example an @code{else} keyword must
13713 be lined up with the corresponding @code{if} keyword.
13714
13715 There are two respects in which the style rule enforced by this check
13716 option are more liberal than those in the Ada Reference Manual. First
13717 in the case of record declarations, it is permissible to put the
13718 @code{record} keyword on the same line as the @code{type} keyword, and
13719 then the @code{end} in @code{end record} must line up under @code{type}.
13720 This is also permitted when the type declaration is split on two lines.
13721 For example, any of the following three layouts is acceptable:
13722
13723 @example
13724 type q is record
13725 a : integer;
13726 b : integer;
13727 end record;
13728
13729 type q is
13730 record
13731 a : integer;
13732 b : integer;
13733 end record;
13734
13735 type q is
13736 record
13737 a : integer;
13738 b : integer;
13739 end record;
13740 @end example
13741
13742 Second, in the case of a block statement, a permitted alternative
13743 is to put the block label on the same line as the @code{declare} or
13744 @code{begin} keyword, and then line the @code{end} keyword up under
13745 the block label. For example both the following are permitted:
13746
13747 @example
13748 Block : declare
13749 A : Integer := 3;
13750 begin
13751 Proc (A, A);
13752 end Block;
13753
13754 Block :
13755 declare
13756 A : Integer := 3;
13757 begin
13758 Proc (A, A);
13759 end Block;
13760 @end example
13761
13762 The same alternative format is allowed for loops. For example, both of
13763 the following are permitted:
13764
13765 @example
13766 Clear : while J < 10 loop
13767 A (J) := 0;
13768 end loop Clear;
13769
13770 Clear :
13771 while J < 10 loop
13772 A (J) := 0;
13773 end loop Clear;
13774 @end example
13775 @end table
13776
13777 @geindex -gnatyLnnn (gcc)
13778
13779
13780 @table @asis
13781
13782 @item @code{-gnatyL}
13783
13784 @emph{Set maximum nesting level.}
13785
13786 The maximum level of nesting of constructs (including subprograms, loops,
13787 blocks, packages, and conditionals) may not exceed the given value
13788 @emph{nnn}. A value of zero disconnects this style check.
13789 @end table
13790
13791 @geindex -gnatym (gcc)
13792
13793
13794 @table @asis
13795
13796 @item @code{-gnatym}
13797
13798 @emph{Check maximum line length.}
13799
13800 The length of source lines must not exceed 79 characters, including
13801 any trailing blanks. The value of 79 allows convenient display on an
13802 80 character wide device or window, allowing for possible special
13803 treatment of 80 character lines. Note that this count is of
13804 characters in the source text. This means that a tab character counts
13805 as one character in this count and a wide character sequence counts as
13806 a single character (however many bytes are needed in the encoding).
13807 @end table
13808
13809 @geindex -gnatyMnnn (gcc)
13810
13811
13812 @table @asis
13813
13814 @item @code{-gnatyM}
13815
13816 @emph{Set maximum line length.}
13817
13818 The length of lines must not exceed the
13819 given value @emph{nnn}. The maximum value that can be specified is 32767.
13820 If neither style option for setting the line length is used, then the
13821 default is 255. This also controls the maximum length of lexical elements,
13822 where the only restriction is that they must fit on a single line.
13823 @end table
13824
13825 @geindex -gnatyn (gcc)
13826
13827
13828 @table @asis
13829
13830 @item @code{-gnatyn}
13831
13832 @emph{Check casing of entities in Standard.}
13833
13834 Any identifier from Standard must be cased
13835 to match the presentation in the Ada Reference Manual (for example,
13836 @code{Integer} and @code{ASCII.NUL}).
13837 @end table
13838
13839 @geindex -gnatyN (gcc)
13840
13841
13842 @table @asis
13843
13844 @item @code{-gnatyN}
13845
13846 @emph{Turn off all style checks.}
13847
13848 All style check options are turned off.
13849 @end table
13850
13851 @geindex -gnatyo (gcc)
13852
13853
13854 @table @asis
13855
13856 @item @code{-gnatyo}
13857
13858 @emph{Check order of subprogram bodies.}
13859
13860 All subprogram bodies in a given scope
13861 (e.g., a package body) must be in alphabetical order. The ordering
13862 rule uses normal Ada rules for comparing strings, ignoring casing
13863 of letters, except that if there is a trailing numeric suffix, then
13864 the value of this suffix is used in the ordering (e.g., Junk2 comes
13865 before Junk10).
13866 @end table
13867
13868 @geindex -gnatyO (gcc)
13869
13870
13871 @table @asis
13872
13873 @item @code{-gnatyO}
13874
13875 @emph{Check that overriding subprograms are explicitly marked as such.}
13876
13877 This applies to all subprograms of a derived type that override a primitive
13878 operation of the type, for both tagged and untagged types. In particular,
13879 the declaration of a primitive operation of a type extension that overrides
13880 an inherited operation must carry an overriding indicator. Another case is
13881 the declaration of a function that overrides a predefined operator (such
13882 as an equality operator).
13883 @end table
13884
13885 @geindex -gnatyp (gcc)
13886
13887
13888 @table @asis
13889
13890 @item @code{-gnatyp}
13891
13892 @emph{Check pragma casing.}
13893
13894 Pragma names must be written in mixed case, that is, the
13895 initial letter and any letter following an underscore must be uppercase.
13896 All other letters must be lowercase. An exception is that SPARK_Mode is
13897 allowed as an alternative for Spark_Mode.
13898 @end table
13899
13900 @geindex -gnatyr (gcc)
13901
13902
13903 @table @asis
13904
13905 @item @code{-gnatyr}
13906
13907 @emph{Check references.}
13908
13909 All identifier references must be cased in the same way as the
13910 corresponding declaration. No specific casing style is imposed on
13911 identifiers. The only requirement is for consistency of references
13912 with declarations.
13913 @end table
13914
13915 @geindex -gnatys (gcc)
13916
13917
13918 @table @asis
13919
13920 @item @code{-gnatys}
13921
13922 @emph{Check separate specs.}
13923
13924 Separate declarations ('specs') are required for subprograms (a
13925 body is not allowed to serve as its own declaration). The only
13926 exception is that parameterless library level procedures are
13927 not required to have a separate declaration. This exception covers
13928 the most frequent form of main program procedures.
13929 @end table
13930
13931 @geindex -gnatyS (gcc)
13932
13933
13934 @table @asis
13935
13936 @item @code{-gnatyS}
13937
13938 @emph{Check no statements after then/else.}
13939
13940 No statements are allowed
13941 on the same line as a @code{then} or @code{else} keyword following the
13942 keyword in an @code{if} statement. @code{or else} and @code{and then} are not
13943 affected, and a special exception allows a pragma to appear after @code{else}.
13944 @end table
13945
13946 @geindex -gnatyt (gcc)
13947
13948
13949 @table @asis
13950
13951 @item @code{-gnatyt}
13952
13953 @emph{Check token spacing.}
13954
13955 The following token spacing rules are enforced:
13956
13957
13958 @itemize *
13959
13960 @item
13961 The keywords @code{abs} and @code{not} must be followed by a space.
13962
13963 @item
13964 The token @code{=>} must be surrounded by spaces.
13965
13966 @item
13967 The token @code{<>} must be preceded by a space or a left parenthesis.
13968
13969 @item
13970 Binary operators other than @code{**} must be surrounded by spaces.
13971 There is no restriction on the layout of the @code{**} binary operator.
13972
13973 @item
13974 Colon must be surrounded by spaces.
13975
13976 @item
13977 Colon-equal (assignment, initialization) must be surrounded by spaces.
13978
13979 @item
13980 Comma must be the first non-blank character on the line, or be
13981 immediately preceded by a non-blank character, and must be followed
13982 by a space.
13983
13984 @item
13985 If the token preceding a left parenthesis ends with a letter or digit, then
13986 a space must separate the two tokens.
13987
13988 @item
13989 If the token following a right parenthesis starts with a letter or digit, then
13990 a space must separate the two tokens.
13991
13992 @item
13993 A right parenthesis must either be the first non-blank character on
13994 a line, or it must be preceded by a non-blank character.
13995
13996 @item
13997 A semicolon must not be preceded by a space, and must not be followed by
13998 a non-blank character.
13999
14000 @item
14001 A unary plus or minus may not be followed by a space.
14002
14003 @item
14004 A vertical bar must be surrounded by spaces.
14005 @end itemize
14006
14007 Exactly one blank (and no other white space) must appear between
14008 a @code{not} token and a following @code{in} token.
14009 @end table
14010
14011 @geindex -gnatyu (gcc)
14012
14013
14014 @table @asis
14015
14016 @item @code{-gnatyu}
14017
14018 @emph{Check unnecessary blank lines.}
14019
14020 Unnecessary blank lines are not allowed. A blank line is considered
14021 unnecessary if it appears at the end of the file, or if more than
14022 one blank line occurs in sequence.
14023 @end table
14024
14025 @geindex -gnatyx (gcc)
14026
14027
14028 @table @asis
14029
14030 @item @code{-gnatyx}
14031
14032 @emph{Check extra parentheses.}
14033
14034 Unnecessary extra level of parentheses (C-style) are not allowed
14035 around conditions in @code{if} statements, @code{while} statements and
14036 @code{exit} statements.
14037 @end table
14038
14039 @geindex -gnatyy (gcc)
14040
14041
14042 @table @asis
14043
14044 @item @code{-gnatyy}
14045
14046 @emph{Set all standard style check options.}
14047
14048 This is equivalent to @code{gnaty3aAbcefhiklmnprst}, that is all checking
14049 options enabled with the exception of @code{-gnatyB}, @code{-gnatyd},
14050 @code{-gnatyI}, @code{-gnatyLnnn}, @code{-gnatyo}, @code{-gnatyO},
14051 @code{-gnatyS}, @code{-gnatyu}, and @code{-gnatyx}.
14052 @end table
14053
14054 @geindex -gnaty- (gcc)
14055
14056
14057 @table @asis
14058
14059 @item @code{-gnaty-}
14060
14061 @emph{Remove style check options.}
14062
14063 This causes any subsequent options in the string to act as canceling the
14064 corresponding style check option. To cancel maximum nesting level control,
14065 use the @code{L} parameter without any integer value after that, because any
14066 digit following @emph{-} in the parameter string of the @code{-gnaty}
14067 option will be treated as canceling the indentation check. The same is true
14068 for the @code{M} parameter. @code{y} and @code{N} parameters are not
14069 allowed after @emph{-}.
14070 @end table
14071
14072 @geindex -gnaty+ (gcc)
14073
14074
14075 @table @asis
14076
14077 @item @code{-gnaty+}
14078
14079 @emph{Enable style check options.}
14080
14081 This causes any subsequent options in the string to enable the corresponding
14082 style check option. That is, it cancels the effect of a previous -,
14083 if any.
14084 @end table
14085
14086 @c end of switch description (leave this comment to ease automatic parsing for
14087
14088 @c GNAT Studio
14089
14090 In the above rules, appearing in column one is always permitted, that is,
14091 counts as meeting either a requirement for a required preceding space,
14092 or as meeting a requirement for no preceding space.
14093
14094 Appearing at the end of a line is also always permitted, that is, counts
14095 as meeting either a requirement for a following space, or as meeting
14096 a requirement for no following space.
14097
14098 If any of these style rules is violated, a message is generated giving
14099 details on the violation. The initial characters of such messages are
14100 always '@cite{(style)}'. Note that these messages are treated as warning
14101 messages, so they normally do not prevent the generation of an object
14102 file. The @code{-gnatwe} switch can be used to treat warning messages,
14103 including style messages, as fatal errors.
14104
14105 The switch @code{-gnaty} on its own (that is not
14106 followed by any letters or digits) is equivalent
14107 to the use of @code{-gnatyy} as described above, that is all
14108 built-in standard style check options are enabled.
14109
14110 The switch @code{-gnatyN} clears any previously set style checks.
14111
14112 @node Run-Time Checks,Using gcc for Syntax Checking,Style Checking,Compiler Switches
14113 @anchor{gnat_ugn/building_executable_programs_with_gnat run-time-checks}@anchor{f9}@anchor{gnat_ugn/building_executable_programs_with_gnat id19}@anchor{104}
14114 @subsection Run-Time Checks
14115
14116
14117 @geindex Division by zero
14118
14119 @geindex Access before elaboration
14120
14121 @geindex Checks
14122 @geindex division by zero
14123
14124 @geindex Checks
14125 @geindex access before elaboration
14126
14127 @geindex Checks
14128 @geindex stack overflow checking
14129
14130 By default, the following checks are suppressed: stack overflow
14131 checks, and checks for access before elaboration on subprogram
14132 calls. All other checks, including overflow checks, range checks and
14133 array bounds checks, are turned on by default. The following @code{gcc}
14134 switches refine this default behavior.
14135
14136 @geindex -gnatp (gcc)
14137
14138
14139 @table @asis
14140
14141 @item @code{-gnatp}
14142
14143 @geindex Suppressing checks
14144
14145 @geindex Checks
14146 @geindex suppressing
14147
14148 This switch causes the unit to be compiled
14149 as though @code{pragma Suppress (All_checks)}
14150 had been present in the source. Validity checks are also eliminated (in
14151 other words @code{-gnatp} also implies @code{-gnatVn}.
14152 Use this switch to improve the performance
14153 of the code at the expense of safety in the presence of invalid data or
14154 program bugs.
14155
14156 Note that when checks are suppressed, the compiler is allowed, but not
14157 required, to omit the checking code. If the run-time cost of the
14158 checking code is zero or near-zero, the compiler will generate it even
14159 if checks are suppressed. In particular, if the compiler can prove
14160 that a certain check will necessarily fail, it will generate code to
14161 do an unconditional 'raise', even if checks are suppressed. The
14162 compiler warns in this case. Another case in which checks may not be
14163 eliminated is when they are embedded in certain run-time routines such
14164 as math library routines.
14165
14166 Of course, run-time checks are omitted whenever the compiler can prove
14167 that they will not fail, whether or not checks are suppressed.
14168
14169 Note that if you suppress a check that would have failed, program
14170 execution is erroneous, which means the behavior is totally
14171 unpredictable. The program might crash, or print wrong answers, or
14172 do anything else. It might even do exactly what you wanted it to do
14173 (and then it might start failing mysteriously next week or next
14174 year). The compiler will generate code based on the assumption that
14175 the condition being checked is true, which can result in erroneous
14176 execution if that assumption is wrong.
14177
14178 The checks subject to suppression include all the checks defined by the Ada
14179 standard, the additional implementation defined checks @code{Alignment_Check},
14180 @code{Duplicated_Tag_Check}, @code{Predicate_Check}, @code{Container_Checks}, @code{Tampering_Check},
14181 and @code{Validity_Check}, as well as any checks introduced using @code{pragma Check_Name}.
14182 Note that @code{Atomic_Synchronization} is not automatically suppressed by use of this option.
14183
14184 If the code depends on certain checks being active, you can use
14185 pragma @code{Unsuppress} either as a configuration pragma or as
14186 a local pragma to make sure that a specified check is performed
14187 even if @code{gnatp} is specified.
14188
14189 The @code{-gnatp} switch has no effect if a subsequent
14190 @code{-gnat-p} switch appears.
14191 @end table
14192
14193 @geindex -gnat-p (gcc)
14194
14195 @geindex Suppressing checks
14196
14197 @geindex Checks
14198 @geindex suppressing
14199
14200 @geindex Suppress
14201
14202
14203 @table @asis
14204
14205 @item @code{-gnat-p}
14206
14207 This switch cancels the effect of a previous @code{gnatp} switch.
14208 @end table
14209
14210 @geindex -gnato?? (gcc)
14211
14212 @geindex Overflow checks
14213
14214 @geindex Overflow mode
14215
14216 @geindex Check
14217 @geindex overflow
14218
14219
14220 @table @asis
14221
14222 @item @code{-gnato??}
14223
14224 This switch controls the mode used for computing intermediate
14225 arithmetic integer operations, and also enables overflow checking.
14226 For a full description of overflow mode and checking control, see
14227 the 'Overflow Check Handling in GNAT' appendix in this
14228 User's Guide.
14229
14230 Overflow checks are always enabled by this switch. The argument
14231 controls the mode, using the codes
14232
14233
14234 @table @asis
14235
14236 @item @emph{1 = STRICT}
14237
14238 In STRICT mode, intermediate operations are always done using the
14239 base type, and overflow checking ensures that the result is within
14240 the base type range.
14241
14242 @item @emph{2 = MINIMIZED}
14243
14244 In MINIMIZED mode, overflows in intermediate operations are avoided
14245 where possible by using a larger integer type for the computation
14246 (typically @code{Long_Long_Integer}). Overflow checking ensures that
14247 the result fits in this larger integer type.
14248
14249 @item @emph{3 = ELIMINATED}
14250
14251 In ELIMINATED mode, overflows in intermediate operations are avoided
14252 by using multi-precision arithmetic. In this case, overflow checking
14253 has no effect on intermediate operations (since overflow is impossible).
14254 @end table
14255
14256 If two digits are present after @code{-gnato} then the first digit
14257 sets the mode for expressions outside assertions, and the second digit
14258 sets the mode for expressions within assertions. Here assertions is used
14259 in the technical sense (which includes for example precondition and
14260 postcondition expressions).
14261
14262 If one digit is present, the corresponding mode is applicable to both
14263 expressions within and outside assertion expressions.
14264
14265 If no digits are present, the default is to enable overflow checks
14266 and set STRICT mode for both kinds of expressions. This is compatible
14267 with the use of @code{-gnato} in previous versions of GNAT.
14268
14269 @geindex Machine_Overflows
14270
14271 Note that the @code{-gnato??} switch does not affect the code generated
14272 for any floating-point operations; it applies only to integer semantics.
14273 For floating-point, GNAT has the @code{Machine_Overflows}
14274 attribute set to @code{False} and the normal mode of operation is to
14275 generate IEEE NaN and infinite values on overflow or invalid operations
14276 (such as dividing 0.0 by 0.0).
14277
14278 The reason that we distinguish overflow checking from other kinds of
14279 range constraint checking is that a failure of an overflow check, unlike
14280 for example the failure of a range check, can result in an incorrect
14281 value, but cannot cause random memory destruction (like an out of range
14282 subscript), or a wild jump (from an out of range case value). Overflow
14283 checking is also quite expensive in time and space, since in general it
14284 requires the use of double length arithmetic.
14285
14286 Note again that the default is @code{-gnato11} (equivalent to @code{-gnato1}),
14287 so overflow checking is performed in STRICT mode by default.
14288 @end table
14289
14290 @geindex -gnatE (gcc)
14291
14292 @geindex Elaboration checks
14293
14294 @geindex Check
14295 @geindex elaboration
14296
14297
14298 @table @asis
14299
14300 @item @code{-gnatE}
14301
14302 Enables dynamic checks for access-before-elaboration
14303 on subprogram calls and generic instantiations.
14304 Note that @code{-gnatE} is not necessary for safety, because in the
14305 default mode, GNAT ensures statically that the checks would not fail.
14306 For full details of the effect and use of this switch,
14307 @ref{1c,,Compiling with gcc}.
14308 @end table
14309
14310 @geindex -fstack-check (gcc)
14311
14312 @geindex Stack Overflow Checking
14313
14314 @geindex Checks
14315 @geindex stack overflow checking
14316
14317
14318 @table @asis
14319
14320 @item @code{-fstack-check}
14321
14322 Activates stack overflow checking. For full details of the effect and use of
14323 this switch see @ref{f4,,Stack Overflow Checking}.
14324 @end table
14325
14326 @geindex Unsuppress
14327
14328 The setting of these switches only controls the default setting of the
14329 checks. You may modify them using either @code{Suppress} (to remove
14330 checks) or @code{Unsuppress} (to add back suppressed checks) pragmas in
14331 the program source.
14332
14333 @node Using gcc for Syntax Checking,Using gcc for Semantic Checking,Run-Time Checks,Compiler Switches
14334 @anchor{gnat_ugn/building_executable_programs_with_gnat id20}@anchor{105}@anchor{gnat_ugn/building_executable_programs_with_gnat using-gcc-for-syntax-checking}@anchor{106}
14335 @subsection Using @code{gcc} for Syntax Checking
14336
14337
14338 @geindex -gnats (gcc)
14339
14340
14341 @table @asis
14342
14343 @item @code{-gnats}
14344
14345 The @code{s} stands for 'syntax'.
14346
14347 Run GNAT in syntax checking only mode. For
14348 example, the command
14349
14350 @example
14351 $ gcc -c -gnats x.adb
14352 @end example
14353
14354 compiles file @code{x.adb} in syntax-check-only mode. You can check a
14355 series of files in a single command
14356 , and can use wildcards to specify such a group of files.
14357 Note that you must specify the @code{-c} (compile
14358 only) flag in addition to the @code{-gnats} flag.
14359
14360 You may use other switches in conjunction with @code{-gnats}. In
14361 particular, @code{-gnatl} and @code{-gnatv} are useful to control the
14362 format of any generated error messages.
14363
14364 When the source file is empty or contains only empty lines and/or comments,
14365 the output is a warning:
14366
14367 @example
14368 $ gcc -c -gnats -x ada toto.txt
14369 toto.txt:1:01: warning: empty file, contains no compilation units
14370 $
14371 @end example
14372
14373 Otherwise, the output is simply the error messages, if any. No object file or
14374 ALI file is generated by a syntax-only compilation. Also, no units other
14375 than the one specified are accessed. For example, if a unit @code{X}
14376 @emph{with}s a unit @code{Y}, compiling unit @code{X} in syntax
14377 check only mode does not access the source file containing unit
14378 @code{Y}.
14379
14380 @geindex Multiple units
14381 @geindex syntax checking
14382
14383 Normally, GNAT allows only a single unit in a source file. However, this
14384 restriction does not apply in syntax-check-only mode, and it is possible
14385 to check a file containing multiple compilation units concatenated
14386 together. This is primarily used by the @code{gnatchop} utility
14387 (@ref{36,,Renaming Files with gnatchop}).
14388 @end table
14389
14390 @node Using gcc for Semantic Checking,Compiling Different Versions of Ada,Using gcc for Syntax Checking,Compiler Switches
14391 @anchor{gnat_ugn/building_executable_programs_with_gnat id21}@anchor{107}@anchor{gnat_ugn/building_executable_programs_with_gnat using-gcc-for-semantic-checking}@anchor{108}
14392 @subsection Using @code{gcc} for Semantic Checking
14393
14394
14395 @geindex -gnatc (gcc)
14396
14397
14398 @table @asis
14399
14400 @item @code{-gnatc}
14401
14402 The @code{c} stands for 'check'.
14403 Causes the compiler to operate in semantic check mode,
14404 with full checking for all illegalities specified in the
14405 Ada Reference Manual, but without generation of any object code
14406 (no object file is generated).
14407
14408 Because dependent files must be accessed, you must follow the GNAT
14409 semantic restrictions on file structuring to operate in this mode:
14410
14411
14412 @itemize *
14413
14414 @item
14415 The needed source files must be accessible
14416 (see @ref{89,,Search Paths and the Run-Time Library (RTL)}).
14417
14418 @item
14419 Each file must contain only one compilation unit.
14420
14421 @item
14422 The file name and unit name must match (@ref{52,,File Naming Rules}).
14423 @end itemize
14424
14425 The output consists of error messages as appropriate. No object file is
14426 generated. An @code{ALI} file is generated for use in the context of
14427 cross-reference tools, but this file is marked as not being suitable
14428 for binding (since no object file is generated).
14429 The checking corresponds exactly to the notion of
14430 legality in the Ada Reference Manual.
14431
14432 Any unit can be compiled in semantics-checking-only mode, including
14433 units that would not normally be compiled (subunits,
14434 and specifications where a separate body is present).
14435 @end table
14436
14437 @node Compiling Different Versions of Ada,Character Set Control,Using gcc for Semantic Checking,Compiler Switches
14438 @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{109}
14439 @subsection Compiling Different Versions of Ada
14440
14441
14442 The switches described in this section allow you to explicitly specify
14443 the version of the Ada language that your programs are written in.
14444 The default mode is Ada 2012,
14445 but you can also specify Ada 95, Ada 2005 mode, or
14446 indicate Ada 83 compatibility mode.
14447
14448 @geindex Compatibility with Ada 83
14449
14450 @geindex -gnat83 (gcc)
14451
14452 @geindex ACVC
14453 @geindex Ada 83 tests
14454
14455 @geindex Ada 83 mode
14456
14457
14458 @table @asis
14459
14460 @item @code{-gnat83} (Ada 83 Compatibility Mode)
14461
14462 Although GNAT is primarily an Ada 95 / Ada 2005 compiler, this switch
14463 specifies that the program is to be compiled in Ada 83 mode. With
14464 @code{-gnat83}, GNAT rejects most post-Ada 83 extensions and applies Ada 83
14465 semantics where this can be done easily.
14466 It is not possible to guarantee this switch does a perfect
14467 job; some subtle tests, such as are
14468 found in earlier ACVC tests (and that have been removed from the ACATS suite
14469 for Ada 95), might not compile correctly.
14470 Nevertheless, this switch may be useful in some circumstances, for example
14471 where, due to contractual reasons, existing code needs to be maintained
14472 using only Ada 83 features.
14473
14474 With few exceptions (most notably the need to use @code{<>} on
14475 unconstrained
14476 @geindex Generic formal parameters
14477 generic formal parameters,
14478 the use of the new Ada 95 / Ada 2005
14479 reserved words, and the use of packages
14480 with optional bodies), it is not necessary to specify the
14481 @code{-gnat83} switch when compiling Ada 83 programs, because, with rare
14482 exceptions, Ada 95 and Ada 2005 are upwardly compatible with Ada 83. Thus
14483 a correct Ada 83 program is usually also a correct program
14484 in these later versions of the language standard. For further information
14485 please refer to the @emph{Compatibility and Porting Guide} chapter in the
14486 @cite{GNAT Reference Manual}.
14487 @end table
14488
14489 @geindex -gnat95 (gcc)
14490
14491 @geindex Ada 95 mode
14492
14493
14494 @table @asis
14495
14496 @item @code{-gnat95} (Ada 95 mode)
14497
14498 This switch directs the compiler to implement the Ada 95 version of the
14499 language.
14500 Since Ada 95 is almost completely upwards
14501 compatible with Ada 83, Ada 83 programs may generally be compiled using
14502 this switch (see the description of the @code{-gnat83} switch for further
14503 information about Ada 83 mode).
14504 If an Ada 2005 program is compiled in Ada 95 mode,
14505 uses of the new Ada 2005 features will cause error
14506 messages or warnings.
14507
14508 This switch also can be used to cancel the effect of a previous
14509 @code{-gnat83}, @code{-gnat05/2005}, or @code{-gnat12/2012}
14510 switch earlier in the command line.
14511 @end table
14512
14513 @geindex -gnat05 (gcc)
14514
14515 @geindex -gnat2005 (gcc)
14516
14517 @geindex Ada 2005 mode
14518
14519
14520 @table @asis
14521
14522 @item @code{-gnat05} or @code{-gnat2005} (Ada 2005 mode)
14523
14524 This switch directs the compiler to implement the Ada 2005 version of the
14525 language, as documented in the official Ada standards document.
14526 Since Ada 2005 is almost completely upwards
14527 compatible with Ada 95 (and thus also with Ada 83), Ada 83 and Ada 95 programs
14528 may generally be compiled using this switch (see the description of the
14529 @code{-gnat83} and @code{-gnat95} switches for further
14530 information).
14531 @end table
14532
14533 @geindex -gnat12 (gcc)
14534
14535 @geindex -gnat2012 (gcc)
14536
14537 @geindex Ada 2012 mode
14538
14539
14540 @table @asis
14541
14542 @item @code{-gnat12} or @code{-gnat2012} (Ada 2012 mode)
14543
14544 This switch directs the compiler to implement the Ada 2012 version of the
14545 language (also the default).
14546 Since Ada 2012 is almost completely upwards
14547 compatible with Ada 2005 (and thus also with Ada 83, and Ada 95),
14548 Ada 83 and Ada 95 programs
14549 may generally be compiled using this switch (see the description of the
14550 @code{-gnat83}, @code{-gnat95}, and @code{-gnat05/2005} switches
14551 for further information).
14552 @end table
14553
14554 @geindex -gnatX (gcc)
14555
14556 @geindex Ada language extensions
14557
14558 @geindex GNAT extensions
14559
14560
14561 @table @asis
14562
14563 @item @code{-gnatX} (Enable GNAT Extensions)
14564
14565 This switch directs the compiler to implement the latest version of the
14566 language (currently Ada 2012) and also to enable certain GNAT implementation
14567 extensions that are not part of any Ada standard. For a full list of these
14568 extensions, see the GNAT reference manual.
14569 @end table
14570
14571 @node Character Set Control,File Naming Control,Compiling Different Versions of Ada,Compiler Switches
14572 @anchor{gnat_ugn/building_executable_programs_with_gnat id23}@anchor{10a}@anchor{gnat_ugn/building_executable_programs_with_gnat character-set-control}@anchor{48}
14573 @subsection Character Set Control
14574
14575
14576 @geindex -gnati (gcc)
14577
14578
14579 @table @asis
14580
14581 @item @code{-gnati@emph{c}}
14582
14583 Normally GNAT recognizes the Latin-1 character set in source program
14584 identifiers, as described in the Ada Reference Manual.
14585 This switch causes
14586 GNAT to recognize alternate character sets in identifiers. @code{c} is a
14587 single character indicating the character set, as follows:
14588
14589
14590 @multitable {xxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
14591 @item
14592
14593 @emph{1}
14594
14595 @tab
14596
14597 ISO 8859-1 (Latin-1) identifiers
14598
14599 @item
14600
14601 @emph{2}
14602
14603 @tab
14604
14605 ISO 8859-2 (Latin-2) letters allowed in identifiers
14606
14607 @item
14608
14609 @emph{3}
14610
14611 @tab
14612
14613 ISO 8859-3 (Latin-3) letters allowed in identifiers
14614
14615 @item
14616
14617 @emph{4}
14618
14619 @tab
14620
14621 ISO 8859-4 (Latin-4) letters allowed in identifiers
14622
14623 @item
14624
14625 @emph{5}
14626
14627 @tab
14628
14629 ISO 8859-5 (Cyrillic) letters allowed in identifiers
14630
14631 @item
14632
14633 @emph{9}
14634
14635 @tab
14636
14637 ISO 8859-15 (Latin-9) letters allowed in identifiers
14638
14639 @item
14640
14641 @emph{p}
14642
14643 @tab
14644
14645 IBM PC letters (code page 437) allowed in identifiers
14646
14647 @item
14648
14649 @emph{8}
14650
14651 @tab
14652
14653 IBM PC letters (code page 850) allowed in identifiers
14654
14655 @item
14656
14657 @emph{f}
14658
14659 @tab
14660
14661 Full upper-half codes allowed in identifiers
14662
14663 @item
14664
14665 @emph{n}
14666
14667 @tab
14668
14669 No upper-half codes allowed in identifiers
14670
14671 @item
14672
14673 @emph{w}
14674
14675 @tab
14676
14677 Wide-character codes (that is, codes greater than 255)
14678 allowed in identifiers
14679
14680 @end multitable
14681
14682
14683 See @ref{3e,,Foreign Language Representation} for full details on the
14684 implementation of these character sets.
14685 @end table
14686
14687 @geindex -gnatW (gcc)
14688
14689
14690 @table @asis
14691
14692 @item @code{-gnatW@emph{e}}
14693
14694 Specify the method of encoding for wide characters.
14695 @code{e} is one of the following:
14696
14697
14698 @multitable {xxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
14699 @item
14700
14701 @emph{h}
14702
14703 @tab
14704
14705 Hex encoding (brackets coding also recognized)
14706
14707 @item
14708
14709 @emph{u}
14710
14711 @tab
14712
14713 Upper half encoding (brackets encoding also recognized)
14714
14715 @item
14716
14717 @emph{s}
14718
14719 @tab
14720
14721 Shift/JIS encoding (brackets encoding also recognized)
14722
14723 @item
14724
14725 @emph{e}
14726
14727 @tab
14728
14729 EUC encoding (brackets encoding also recognized)
14730
14731 @item
14732
14733 @emph{8}
14734
14735 @tab
14736
14737 UTF-8 encoding (brackets encoding also recognized)
14738
14739 @item
14740
14741 @emph{b}
14742
14743 @tab
14744
14745 Brackets encoding only (default value)
14746
14747 @end multitable
14748
14749
14750 For full details on these encoding
14751 methods see @ref{4e,,Wide_Character Encodings}.
14752 Note that brackets coding is always accepted, even if one of the other
14753 options is specified, so for example @code{-gnatW8} specifies that both
14754 brackets and UTF-8 encodings will be recognized. The units that are
14755 with'ed directly or indirectly will be scanned using the specified
14756 representation scheme, and so if one of the non-brackets scheme is
14757 used, it must be used consistently throughout the program. However,
14758 since brackets encoding is always recognized, it may be conveniently
14759 used in standard libraries, allowing these libraries to be used with
14760 any of the available coding schemes.
14761
14762 Note that brackets encoding only applies to program text. Within comments,
14763 brackets are considered to be normal graphic characters, and bracket sequences
14764 are never recognized as wide characters.
14765
14766 If no @code{-gnatW?} parameter is present, then the default
14767 representation is normally Brackets encoding only. However, if the
14768 first three characters of the file are 16#EF# 16#BB# 16#BF# (the standard
14769 byte order mark or BOM for UTF-8), then these three characters are
14770 skipped and the default representation for the file is set to UTF-8.
14771
14772 Note that the wide character representation that is specified (explicitly
14773 or by default) for the main program also acts as the default encoding used
14774 for Wide_Text_IO files if not specifically overridden by a WCEM form
14775 parameter.
14776 @end table
14777
14778 When no @code{-gnatW?} is specified, then characters (other than wide
14779 characters represented using brackets notation) are treated as 8-bit
14780 Latin-1 codes. The codes recognized are the Latin-1 graphic characters,
14781 and ASCII format effectors (CR, LF, HT, VT). Other lower half control
14782 characters in the range 16#00#..16#1F# are not accepted in program text
14783 or in comments. Upper half control characters (16#80#..16#9F#) are rejected
14784 in program text, but allowed and ignored in comments. Note in particular
14785 that the Next Line (NEL) character whose encoding is 16#85# is not recognized
14786 as an end of line in this default mode. If your source program contains
14787 instances of the NEL character used as a line terminator,
14788 you must use UTF-8 encoding for the whole
14789 source program. In default mode, all lines must be ended by a standard
14790 end of line sequence (CR, CR/LF, or LF).
14791
14792 Note that the convention of simply accepting all upper half characters in
14793 comments means that programs that use standard ASCII for program text, but
14794 UTF-8 encoding for comments are accepted in default mode, providing that the
14795 comments are ended by an appropriate (CR, or CR/LF, or LF) line terminator.
14796 This is a common mode for many programs with foreign language comments.
14797
14798 @node File Naming Control,Subprogram Inlining Control,Character Set Control,Compiler Switches
14799 @anchor{gnat_ugn/building_executable_programs_with_gnat file-naming-control}@anchor{10b}@anchor{gnat_ugn/building_executable_programs_with_gnat id24}@anchor{10c}
14800 @subsection File Naming Control
14801
14802
14803 @geindex -gnatk (gcc)
14804
14805
14806 @table @asis
14807
14808 @item @code{-gnatk@emph{n}}
14809
14810 Activates file name 'krunching'. @code{n}, a decimal integer in the range
14811 1-999, indicates the maximum allowable length of a file name (not
14812 including the @code{.ads} or @code{.adb} extension). The default is not
14813 to enable file name krunching.
14814
14815 For the source file naming rules, @ref{52,,File Naming Rules}.
14816 @end table
14817
14818 @node Subprogram Inlining Control,Auxiliary Output Control,File Naming Control,Compiler Switches
14819 @anchor{gnat_ugn/building_executable_programs_with_gnat subprogram-inlining-control}@anchor{10d}@anchor{gnat_ugn/building_executable_programs_with_gnat id25}@anchor{10e}
14820 @subsection Subprogram Inlining Control
14821
14822
14823 @geindex -gnatn (gcc)
14824
14825
14826 @table @asis
14827
14828 @item @code{-gnatn[12]}
14829
14830 The @code{n} here is intended to suggest the first syllable of the word 'inline'.
14831 GNAT recognizes and processes @code{Inline} pragmas. However, for inlining to
14832 actually occur, optimization must be enabled and, by default, inlining of
14833 subprograms across units is not performed. If you want to additionally
14834 enable inlining of subprograms specified by pragma @code{Inline} across units,
14835 you must also specify this switch.
14836
14837 In the absence of this switch, GNAT does not attempt inlining across units
14838 and does not access the bodies of subprograms for which @code{pragma Inline} is
14839 specified if they are not in the current unit.
14840
14841 You can optionally specify the inlining level: 1 for moderate inlining across
14842 units, which is a good compromise between compilation times and performances
14843 at run time, or 2 for full inlining across units, which may bring about
14844 longer compilation times. If no inlining level is specified, the compiler will
14845 pick it based on the optimization level: 1 for @code{-O1}, @code{-O2} or
14846 @code{-Os} and 2 for @code{-O3}.
14847
14848 If you specify this switch the compiler will access these bodies,
14849 creating an extra source dependency for the resulting object file, and
14850 where possible, the call will be inlined.
14851 For further details on when inlining is possible
14852 see @ref{10f,,Inlining of Subprograms}.
14853 @end table
14854
14855 @geindex -gnatN (gcc)
14856
14857
14858 @table @asis
14859
14860 @item @code{-gnatN}
14861
14862 This switch activates front-end inlining which also
14863 generates additional dependencies.
14864
14865 When using a gcc-based back end (in practice this means using any version
14866 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
14867 @code{-gnatN} is deprecated, and the use of @code{-gnatn} is preferred.
14868 Historically front end inlining was more extensive than the gcc back end
14869 inlining, but that is no longer the case.
14870 @end table
14871
14872 @node Auxiliary Output Control,Debugging Control,Subprogram Inlining Control,Compiler Switches
14873 @anchor{gnat_ugn/building_executable_programs_with_gnat auxiliary-output-control}@anchor{110}@anchor{gnat_ugn/building_executable_programs_with_gnat id26}@anchor{111}
14874 @subsection Auxiliary Output Control
14875
14876
14877 @geindex -gnatu (gcc)
14878
14879
14880 @table @asis
14881
14882 @item @code{-gnatu}
14883
14884 Print a list of units required by this compilation on @code{stdout}.
14885 The listing includes all units on which the unit being compiled depends
14886 either directly or indirectly.
14887 @end table
14888
14889 @geindex -pass-exit-codes (gcc)
14890
14891
14892 @table @asis
14893
14894 @item @code{-pass-exit-codes}
14895
14896 If this switch is not used, the exit code returned by @code{gcc} when
14897 compiling multiple files indicates whether all source files have
14898 been successfully used to generate object files or not.
14899
14900 When @code{-pass-exit-codes} is used, @code{gcc} exits with an extended
14901 exit status and allows an integrated development environment to better
14902 react to a compilation failure. Those exit status are:
14903
14904
14905 @multitable {xxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
14906 @item
14907
14908 @emph{5}
14909
14910 @tab
14911
14912 There was an error in at least one source file.
14913
14914 @item
14915
14916 @emph{3}
14917
14918 @tab
14919
14920 At least one source file did not generate an object file.
14921
14922 @item
14923
14924 @emph{2}
14925
14926 @tab
14927
14928 The compiler died unexpectedly (internal error for example).
14929
14930 @item
14931
14932 @emph{0}
14933
14934 @tab
14935
14936 An object file has been generated for every source file.
14937
14938 @end multitable
14939
14940 @end table
14941
14942 @node Debugging Control,Exception Handling Control,Auxiliary Output Control,Compiler Switches
14943 @anchor{gnat_ugn/building_executable_programs_with_gnat debugging-control}@anchor{112}@anchor{gnat_ugn/building_executable_programs_with_gnat id27}@anchor{113}
14944 @subsection Debugging Control
14945
14946
14947 @quotation
14948
14949 @geindex Debugging options
14950 @end quotation
14951
14952 @geindex -gnatd (gcc)
14953
14954
14955 @table @asis
14956
14957 @item @code{-gnatd@emph{x}}
14958
14959 Activate internal debugging switches. @code{x} is a letter or digit, or
14960 string of letters or digits, which specifies the type of debugging
14961 outputs desired. Normally these are used only for internal development
14962 or system debugging purposes. You can find full documentation for these
14963 switches in the body of the @code{Debug} unit in the compiler source
14964 file @code{debug.adb}.
14965 @end table
14966
14967 @geindex -gnatG (gcc)
14968
14969
14970 @table @asis
14971
14972 @item @code{-gnatG[=@emph{nn}]}
14973
14974 This switch causes the compiler to generate auxiliary output containing
14975 a pseudo-source listing of the generated expanded code. Like most Ada
14976 compilers, GNAT works by first transforming the high level Ada code into
14977 lower level constructs. For example, tasking operations are transformed
14978 into calls to the tasking run-time routines. A unique capability of GNAT
14979 is to list this expanded code in a form very close to normal Ada source.
14980 This is very useful in understanding the implications of various Ada
14981 usage on the efficiency of the generated code. There are many cases in
14982 Ada (e.g., the use of controlled types), where simple Ada statements can
14983 generate a lot of run-time code. By using @code{-gnatG} you can identify
14984 these cases, and consider whether it may be desirable to modify the coding
14985 approach to improve efficiency.
14986
14987 The optional parameter @code{nn} if present after -gnatG specifies an
14988 alternative maximum line length that overrides the normal default of 72.
14989 This value is in the range 40-999999, values less than 40 being silently
14990 reset to 40. The equal sign is optional.
14991
14992 The format of the output is very similar to standard Ada source, and is
14993 easily understood by an Ada programmer. The following special syntactic
14994 additions correspond to low level features used in the generated code that
14995 do not have any exact analogies in pure Ada source form. The following
14996 is a partial list of these special constructions. See the spec
14997 of package @code{Sprint} in file @code{sprint.ads} for a full list.
14998
14999 @geindex -gnatL (gcc)
15000
15001 If the switch @code{-gnatL} is used in conjunction with
15002 @code{-gnatG}, then the original source lines are interspersed
15003 in the expanded source (as comment lines with the original line number).
15004
15005
15006 @table @asis
15007
15008 @item @code{new @emph{xxx} [storage_pool = @emph{yyy}]}
15009
15010 Shows the storage pool being used for an allocator.
15011
15012 @item @code{at end @emph{procedure-name};}
15013
15014 Shows the finalization (cleanup) procedure for a scope.
15015
15016 @item @code{(if @emph{expr} then @emph{expr} else @emph{expr})}
15017
15018 Conditional expression equivalent to the @code{x?y:z} construction in C.
15019
15020 @item @code{@emph{target}^(@emph{source})}
15021
15022 A conversion with floating-point truncation instead of rounding.
15023
15024 @item @code{@emph{target}?(@emph{source})}
15025
15026 A conversion that bypasses normal Ada semantic checking. In particular
15027 enumeration types and fixed-point types are treated simply as integers.
15028
15029 @item @code{@emph{target}?^(@emph{source})}
15030
15031 Combines the above two cases.
15032 @end table
15033
15034 @code{@emph{x} #/ @emph{y}}
15035
15036 @code{@emph{x} #mod @emph{y}}
15037
15038 @code{@emph{x} # @emph{y}}
15039
15040
15041 @table @asis
15042
15043 @item @code{@emph{x} #rem @emph{y}}
15044
15045 A division or multiplication of fixed-point values which are treated as
15046 integers without any kind of scaling.
15047
15048 @item @code{free @emph{expr} [storage_pool = @emph{xxx}]}
15049
15050 Shows the storage pool associated with a @code{free} statement.
15051
15052 @item @code{[subtype or type declaration]}
15053
15054 Used to list an equivalent declaration for an internally generated
15055 type that is referenced elsewhere in the listing.
15056
15057 @item @code{freeze @emph{type-name} [@emph{actions}]}
15058
15059 Shows the point at which @code{type-name} is frozen, with possible
15060 associated actions to be performed at the freeze point.
15061
15062 @item @code{reference @emph{itype}}
15063
15064 Reference (and hence definition) to internal type @code{itype}.
15065
15066 @item @code{@emph{function-name}! (@emph{arg}, @emph{arg}, @emph{arg})}
15067
15068 Intrinsic function call.
15069
15070 @item @code{@emph{label-name} : label}
15071
15072 Declaration of label @code{labelname}.
15073
15074 @item @code{#$ @emph{subprogram-name}}
15075
15076 An implicit call to a run-time support routine
15077 (to meet the requirement of H.3.1(9) in a
15078 convenient manner).
15079
15080 @item @code{@emph{expr} && @emph{expr} && @emph{expr} ... && @emph{expr}}
15081
15082 A multiple concatenation (same effect as @code{expr} & @code{expr} &
15083 @code{expr}, but handled more efficiently).
15084
15085 @item @code{[constraint_error]}
15086
15087 Raise the @code{Constraint_Error} exception.
15088
15089 @item @code{@emph{expression}'reference}
15090
15091 A pointer to the result of evaluating @{expression@}.
15092
15093 @item @code{@emph{target-type}!(@emph{source-expression})}
15094
15095 An unchecked conversion of @code{source-expression} to @code{target-type}.
15096
15097 @item @code{[@emph{numerator}/@emph{denominator}]}
15098
15099 Used to represent internal real literals (that) have no exact
15100 representation in base 2-16 (for example, the result of compile time
15101 evaluation of the expression 1.0/27.0).
15102 @end table
15103 @end table
15104
15105 @geindex -gnatD (gcc)
15106
15107
15108 @table @asis
15109
15110 @item @code{-gnatD[=nn]}
15111
15112 When used in conjunction with @code{-gnatG}, this switch causes
15113 the expanded source, as described above for
15114 @code{-gnatG} to be written to files with names
15115 @code{xxx.dg}, where @code{xxx} is the normal file name,
15116 instead of to the standard output file. For
15117 example, if the source file name is @code{hello.adb}, then a file
15118 @code{hello.adb.dg} will be written. The debugging
15119 information generated by the @code{gcc} @code{-g} switch
15120 will refer to the generated @code{xxx.dg} file. This allows
15121 you to do source level debugging using the generated code which is
15122 sometimes useful for complex code, for example to find out exactly
15123 which part of a complex construction raised an exception. This switch
15124 also suppresses generation of cross-reference information (see
15125 @code{-gnatx}) since otherwise the cross-reference information
15126 would refer to the @code{.dg} file, which would cause
15127 confusion since this is not the original source file.
15128
15129 Note that @code{-gnatD} actually implies @code{-gnatG}
15130 automatically, so it is not necessary to give both options.
15131 In other words @code{-gnatD} is equivalent to @code{-gnatDG}).
15132
15133 @geindex -gnatL (gcc)
15134
15135 If the switch @code{-gnatL} is used in conjunction with
15136 @code{-gnatDG}, then the original source lines are interspersed
15137 in the expanded source (as comment lines with the original line number).
15138
15139 The optional parameter @code{nn} if present after -gnatD specifies an
15140 alternative maximum line length that overrides the normal default of 72.
15141 This value is in the range 40-999999, values less than 40 being silently
15142 reset to 40. The equal sign is optional.
15143 @end table
15144
15145 @geindex -gnatr (gcc)
15146
15147 @geindex pragma Restrictions
15148
15149
15150 @table @asis
15151
15152 @item @code{-gnatr}
15153
15154 This switch causes pragma Restrictions to be treated as Restriction_Warnings
15155 so that violation of restrictions causes warnings rather than illegalities.
15156 This is useful during the development process when new restrictions are added
15157 or investigated. The switch also causes pragma Profile to be treated as
15158 Profile_Warnings, and pragma Restricted_Run_Time and pragma Ravenscar set
15159 restriction warnings rather than restrictions.
15160 @end table
15161
15162 @geindex -gnatR (gcc)
15163
15164
15165 @table @asis
15166
15167 @item @code{-gnatR[0|1|2|3|4][e][j][m][s]}
15168
15169 This switch controls output from the compiler of a listing showing
15170 representation information for declared types, objects and subprograms.
15171 For @code{-gnatR0}, no information is output (equivalent to omitting
15172 the @code{-gnatR} switch). For @code{-gnatR1} (which is the default,
15173 so @code{-gnatR} with no parameter has the same effect), size and
15174 alignment information is listed for declared array and record types.
15175
15176 For @code{-gnatR2}, size and alignment information is listed for all
15177 declared types and objects. The @code{Linker_Section} is also listed for any
15178 entity for which the @code{Linker_Section} is set explicitly or implicitly (the
15179 latter case occurs for objects of a type for which a @code{Linker_Section}
15180 is set).
15181
15182 For @code{-gnatR3}, symbolic expressions for values that are computed
15183 at run time for records are included. These symbolic expressions have
15184 a mostly obvious format with #n being used to represent the value of the
15185 n'th discriminant. See source files @code{repinfo.ads/adb} in the
15186 GNAT sources for full details on the format of @code{-gnatR3} output.
15187
15188 For @code{-gnatR4}, information for relevant compiler-generated types
15189 is also listed, i.e. when they are structurally part of other declared
15190 types and objects.
15191
15192 If the switch is followed by an @code{e} (e.g. @code{-gnatR2e}), then
15193 extended representation information for record sub-components of records
15194 is included.
15195
15196 If the switch is followed by an @code{m} (e.g. @code{-gnatRm}), then
15197 subprogram conventions and parameter passing mechanisms for all the
15198 subprograms are included.
15199
15200 If the switch is followed by a @code{j} (e.g., @code{-gnatRj}), then
15201 the output is in the JSON data interchange format specified by the
15202 ECMA-404 standard. The semantic description of this JSON output is
15203 available in the specification of the Repinfo unit present in the
15204 compiler sources.
15205
15206 If the switch is followed by an @code{s} (e.g., @code{-gnatR3s}), then
15207 the output is to a file with the name @code{file.rep} where @code{file} is
15208 the name of the corresponding source file, except if @code{j} is also
15209 specified, in which case the file name is @code{file.json}.
15210
15211 Note that it is possible for record components to have zero size. In
15212 this case, the component clause uses an obvious extension of permitted
15213 Ada syntax, for example @code{at 0 range 0 .. -1}.
15214 @end table
15215
15216 @geindex -gnatS (gcc)
15217
15218
15219 @table @asis
15220
15221 @item @code{-gnatS}
15222
15223 The use of the switch @code{-gnatS} for an
15224 Ada compilation will cause the compiler to output a
15225 representation of package Standard in a form very
15226 close to standard Ada. It is not quite possible to
15227 do this entirely in standard Ada (since new
15228 numeric base types cannot be created in standard
15229 Ada), but the output is easily
15230 readable to any Ada programmer, and is useful to
15231 determine the characteristics of target dependent
15232 types in package Standard.
15233 @end table
15234
15235 @geindex -gnatx (gcc)
15236
15237
15238 @table @asis
15239
15240 @item @code{-gnatx}
15241
15242 Normally the compiler generates full cross-referencing information in
15243 the @code{ALI} file. This information is used by a number of tools,
15244 including @code{gnatfind} and @code{gnatxref}. The @code{-gnatx} switch
15245 suppresses this information. This saves some space and may slightly
15246 speed up compilation, but means that these tools cannot be used.
15247 @end table
15248
15249 @geindex -fgnat-encodings (gcc)
15250
15251
15252 @table @asis
15253
15254 @item @code{-fgnat-encodings=[all|gdb|minimal]}
15255
15256 This switch controls the balance between GNAT encodings and standard DWARF
15257 emitted in the debug information.
15258
15259 Historically, old debug formats like stabs were not powerful enough to
15260 express some Ada types (for instance, variant records or fixed-point types).
15261 To work around this, GNAT introduced proprietary encodings that embed the
15262 missing information ("GNAT encodings").
15263
15264 Recent versions of the DWARF debug information format are now able to
15265 correctly describe most of these Ada constructs ("standard DWARF"). As
15266 third-party tools started to use this format, GNAT has been enhanced to
15267 generate it. However, most tools (including GDB) are still relying on GNAT
15268 encodings.
15269
15270 To support all tools, GNAT needs to be versatile about the balance between
15271 generation of GNAT encodings and standard DWARF. This is what
15272 @code{-fgnat-encodings} is about.
15273
15274
15275 @itemize *
15276
15277 @item
15278 @code{=all}: Emit all GNAT encodings, and then emit as much standard DWARF as
15279 possible so it does not conflict with GNAT encodings.
15280
15281 @item
15282 @code{=gdb}: Emit as much standard DWARF as possible as long as the current
15283 GDB handles it. Emit GNAT encodings for the rest.
15284
15285 @item
15286 @code{=minimal}: Emit as much standard DWARF as possible and emit GNAT
15287 encodings for the rest.
15288 @end itemize
15289 @end table
15290
15291 @node Exception Handling Control,Units to Sources Mapping Files,Debugging Control,Compiler Switches
15292 @anchor{gnat_ugn/building_executable_programs_with_gnat id28}@anchor{114}@anchor{gnat_ugn/building_executable_programs_with_gnat exception-handling-control}@anchor{115}
15293 @subsection Exception Handling Control
15294
15295
15296 GNAT uses two methods for handling exceptions at run time. The
15297 @code{setjmp/longjmp} method saves the context when entering
15298 a frame with an exception handler. Then when an exception is
15299 raised, the context can be restored immediately, without the
15300 need for tracing stack frames. This method provides very fast
15301 exception propagation, but introduces significant overhead for
15302 the use of exception handlers, even if no exception is raised.
15303
15304 The other approach is called 'zero cost' exception handling.
15305 With this method, the compiler builds static tables to describe
15306 the exception ranges. No dynamic code is required when entering
15307 a frame containing an exception handler. When an exception is
15308 raised, the tables are used to control a back trace of the
15309 subprogram invocation stack to locate the required exception
15310 handler. This method has considerably poorer performance for
15311 the propagation of exceptions, but there is no overhead for
15312 exception handlers if no exception is raised. Note that in this
15313 mode and in the context of mixed Ada and C/C++ programming,
15314 to propagate an exception through a C/C++ code, the C/C++ code
15315 must be compiled with the @code{-funwind-tables} GCC's
15316 option.
15317
15318 The following switches may be used to control which of the
15319 two exception handling methods is used.
15320
15321 @geindex --RTS=sjlj (gnatmake)
15322
15323
15324 @table @asis
15325
15326 @item @code{--RTS=sjlj}
15327
15328 This switch causes the setjmp/longjmp run-time (when available) to be used
15329 for exception handling. If the default
15330 mechanism for the target is zero cost exceptions, then
15331 this switch can be used to modify this default, and must be
15332 used for all units in the partition.
15333 This option is rarely used. One case in which it may be
15334 advantageous is if you have an application where exception
15335 raising is common and the overall performance of the
15336 application is improved by favoring exception propagation.
15337 @end table
15338
15339 @geindex --RTS=zcx (gnatmake)
15340
15341 @geindex Zero Cost Exceptions
15342
15343
15344 @table @asis
15345
15346 @item @code{--RTS=zcx}
15347
15348 This switch causes the zero cost approach to be used
15349 for exception handling. If this is the default mechanism for the
15350 target (see below), then this switch is unneeded. If the default
15351 mechanism for the target is setjmp/longjmp exceptions, then
15352 this switch can be used to modify this default, and must be
15353 used for all units in the partition.
15354 This option can only be used if the zero cost approach
15355 is available for the target in use, otherwise it will generate an error.
15356 @end table
15357
15358 The same option @code{--RTS} must be used both for @code{gcc}
15359 and @code{gnatbind}. Passing this option to @code{gnatmake}
15360 (@ref{dc,,Switches for gnatmake}) will ensure the required consistency
15361 through the compilation and binding steps.
15362
15363 @node Units to Sources Mapping Files,Code Generation Control,Exception Handling Control,Compiler Switches
15364 @anchor{gnat_ugn/building_executable_programs_with_gnat id29}@anchor{116}@anchor{gnat_ugn/building_executable_programs_with_gnat units-to-sources-mapping-files}@anchor{f7}
15365 @subsection Units to Sources Mapping Files
15366
15367
15368 @geindex -gnatem (gcc)
15369
15370
15371 @table @asis
15372
15373 @item @code{-gnatem=@emph{path}}
15374
15375 A mapping file is a way to communicate to the compiler two mappings:
15376 from unit names to file names (without any directory information) and from
15377 file names to path names (with full directory information). These mappings
15378 are used by the compiler to short-circuit the path search.
15379
15380 The use of mapping files is not required for correct operation of the
15381 compiler, but mapping files can improve efficiency, particularly when
15382 sources are read over a slow network connection. In normal operation,
15383 you need not be concerned with the format or use of mapping files,
15384 and the @code{-gnatem} switch is not a switch that you would use
15385 explicitly. It is intended primarily for use by automatic tools such as
15386 @code{gnatmake} running under the project file facility. The
15387 description here of the format of mapping files is provided
15388 for completeness and for possible use by other tools.
15389
15390 A mapping file is a sequence of sets of three lines. In each set, the
15391 first line is the unit name, in lower case, with @code{%s} appended
15392 for specs and @code{%b} appended for bodies; the second line is the
15393 file name; and the third line is the path name.
15394
15395 Example:
15396
15397 @example
15398 main%b
15399 main.2.ada
15400 /gnat/project1/sources/main.2.ada
15401 @end example
15402
15403 When the switch @code{-gnatem} is specified, the compiler will
15404 create in memory the two mappings from the specified file. If there is
15405 any problem (nonexistent file, truncated file or duplicate entries),
15406 no mapping will be created.
15407
15408 Several @code{-gnatem} switches may be specified; however, only the
15409 last one on the command line will be taken into account.
15410
15411 When using a project file, @code{gnatmake} creates a temporary
15412 mapping file and communicates it to the compiler using this switch.
15413 @end table
15414
15415 @node Code Generation Control,,Units to Sources Mapping Files,Compiler Switches
15416 @anchor{gnat_ugn/building_executable_programs_with_gnat code-generation-control}@anchor{117}@anchor{gnat_ugn/building_executable_programs_with_gnat id30}@anchor{118}
15417 @subsection Code Generation Control
15418
15419
15420 The GCC technology provides a wide range of target dependent
15421 @code{-m} switches for controlling
15422 details of code generation with respect to different versions of
15423 architectures. This includes variations in instruction sets (e.g.,
15424 different members of the power pc family), and different requirements
15425 for optimal arrangement of instructions (e.g., different members of
15426 the x86 family). The list of available @code{-m} switches may be
15427 found in the GCC documentation.
15428
15429 Use of these @code{-m} switches may in some cases result in improved
15430 code performance.
15431
15432 The GNAT technology is tested and qualified without any
15433 @code{-m} switches,
15434 so generally the most reliable approach is to avoid the use of these
15435 switches. However, we generally expect most of these switches to work
15436 successfully with GNAT, and many customers have reported successful
15437 use of these options.
15438
15439 Our general advice is to avoid the use of @code{-m} switches unless
15440 special needs lead to requirements in this area. In particular,
15441 there is no point in using @code{-m} switches to improve performance
15442 unless you actually see a performance improvement.
15443
15444 @node Linker Switches,Binding with gnatbind,Compiler Switches,Building Executable Programs with GNAT
15445 @anchor{gnat_ugn/building_executable_programs_with_gnat linker-switches}@anchor{119}@anchor{gnat_ugn/building_executable_programs_with_gnat id31}@anchor{11a}
15446 @section Linker Switches
15447
15448
15449 Linker switches can be specified after @code{-largs} builder switch.
15450
15451 @geindex -fuse-ld=name
15452
15453
15454 @table @asis
15455
15456 @item @code{-fuse-ld=@emph{name}}
15457
15458 Linker to be used. The default is @code{bfd} for @code{ld.bfd},
15459 the alternative being @code{gold} for @code{ld.gold}. The later is
15460 a more recent and faster linker, but only available on GNU/Linux
15461 platforms.
15462 @end table
15463
15464 @node Binding with gnatbind,Linking with gnatlink,Linker Switches,Building Executable Programs with GNAT
15465 @anchor{gnat_ugn/building_executable_programs_with_gnat binding-with-gnatbind}@anchor{1d}@anchor{gnat_ugn/building_executable_programs_with_gnat id32}@anchor{11b}
15466 @section Binding with @code{gnatbind}
15467
15468
15469 @geindex gnatbind
15470
15471 This chapter describes the GNAT binder, @code{gnatbind}, which is used
15472 to bind compiled GNAT objects.
15473
15474 The @code{gnatbind} program performs four separate functions:
15475
15476
15477 @itemize *
15478
15479 @item
15480 Checks that a program is consistent, in accordance with the rules in
15481 Chapter 10 of the Ada Reference Manual. In particular, error
15482 messages are generated if a program uses inconsistent versions of a
15483 given unit.
15484
15485 @item
15486 Checks that an acceptable order of elaboration exists for the program
15487 and issues an error message if it cannot find an order of elaboration
15488 that satisfies the rules in Chapter 10 of the Ada Language Manual.
15489
15490 @item
15491 Generates a main program incorporating the given elaboration order.
15492 This program is a small Ada package (body and spec) that
15493 must be subsequently compiled
15494 using the GNAT compiler. The necessary compilation step is usually
15495 performed automatically by @code{gnatlink}. The two most important
15496 functions of this program
15497 are to call the elaboration routines of units in an appropriate order
15498 and to call the main program.
15499
15500 @item
15501 Determines the set of object files required by the given main program.
15502 This information is output in the forms of comments in the generated program,
15503 to be read by the @code{gnatlink} utility used to link the Ada application.
15504 @end itemize
15505
15506 @menu
15507 * Running gnatbind::
15508 * Switches for gnatbind::
15509 * Command-Line Access::
15510 * Search Paths for gnatbind::
15511 * Examples of gnatbind Usage::
15512
15513 @end menu
15514
15515 @node Running gnatbind,Switches for gnatbind,,Binding with gnatbind
15516 @anchor{gnat_ugn/building_executable_programs_with_gnat running-gnatbind}@anchor{11c}@anchor{gnat_ugn/building_executable_programs_with_gnat id33}@anchor{11d}
15517 @subsection Running @code{gnatbind}
15518
15519
15520 The form of the @code{gnatbind} command is
15521
15522 @example
15523 $ gnatbind [ switches ] mainprog[.ali] [ switches ]
15524 @end example
15525
15526 where @code{mainprog.adb} is the Ada file containing the main program
15527 unit body. @code{gnatbind} constructs an Ada
15528 package in two files whose names are
15529 @code{b~mainprog.ads}, and @code{b~mainprog.adb}.
15530 For example, if given the
15531 parameter @code{hello.ali}, for a main program contained in file
15532 @code{hello.adb}, the binder output files would be @code{b~hello.ads}
15533 and @code{b~hello.adb}.
15534
15535 When doing consistency checking, the binder takes into consideration
15536 any source files it can locate. For example, if the binder determines
15537 that the given main program requires the package @code{Pack}, whose
15538 @code{.ALI}
15539 file is @code{pack.ali} and whose corresponding source spec file is
15540 @code{pack.ads}, it attempts to locate the source file @code{pack.ads}
15541 (using the same search path conventions as previously described for the
15542 @code{gcc} command). If it can locate this source file, it checks that
15543 the time stamps
15544 or source checksums of the source and its references to in @code{ALI} files
15545 match. In other words, any @code{ALI} files that mentions this spec must have
15546 resulted from compiling this version of the source file (or in the case
15547 where the source checksums match, a version close enough that the
15548 difference does not matter).
15549
15550 @geindex Source files
15551 @geindex use by binder
15552
15553 The effect of this consistency checking, which includes source files, is
15554 that the binder ensures that the program is consistent with the latest
15555 version of the source files that can be located at bind time. Editing a
15556 source file without compiling files that depend on the source file cause
15557 error messages to be generated by the binder.
15558
15559 For example, suppose you have a main program @code{hello.adb} and a
15560 package @code{P}, from file @code{p.ads} and you perform the following
15561 steps:
15562
15563
15564 @itemize *
15565
15566 @item
15567 Enter @code{gcc -c hello.adb} to compile the main program.
15568
15569 @item
15570 Enter @code{gcc -c p.ads} to compile package @code{P}.
15571
15572 @item
15573 Edit file @code{p.ads}.
15574
15575 @item
15576 Enter @code{gnatbind hello}.
15577 @end itemize
15578
15579 At this point, the file @code{p.ali} contains an out-of-date time stamp
15580 because the file @code{p.ads} has been edited. The attempt at binding
15581 fails, and the binder generates the following error messages:
15582
15583 @example
15584 error: "hello.adb" must be recompiled ("p.ads" has been modified)
15585 error: "p.ads" has been modified and must be recompiled
15586 @end example
15587
15588 Now both files must be recompiled as indicated, and then the bind can
15589 succeed, generating a main program. You need not normally be concerned
15590 with the contents of this file, but for reference purposes a sample
15591 binder output file is given in @ref{e,,Example of Binder Output File}.
15592
15593 In most normal usage, the default mode of @code{gnatbind} which is to
15594 generate the main package in Ada, as described in the previous section.
15595 In particular, this means that any Ada programmer can read and understand
15596 the generated main program. It can also be debugged just like any other
15597 Ada code provided the @code{-g} switch is used for
15598 @code{gnatbind} and @code{gnatlink}.
15599
15600 @node Switches for gnatbind,Command-Line Access,Running gnatbind,Binding with gnatbind
15601 @anchor{gnat_ugn/building_executable_programs_with_gnat id34}@anchor{11e}@anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gnatbind}@anchor{11f}
15602 @subsection Switches for @code{gnatbind}
15603
15604
15605 The following switches are available with @code{gnatbind}; details will
15606 be presented in subsequent sections.
15607
15608 @geindex --version (gnatbind)
15609
15610
15611 @table @asis
15612
15613 @item @code{--version}
15614
15615 Display Copyright and version, then exit disregarding all other options.
15616 @end table
15617
15618 @geindex --help (gnatbind)
15619
15620
15621 @table @asis
15622
15623 @item @code{--help}
15624
15625 If @code{--version} was not used, display usage, then exit disregarding
15626 all other options.
15627 @end table
15628
15629 @geindex -a (gnatbind)
15630
15631
15632 @table @asis
15633
15634 @item @code{-a}
15635
15636 Indicates that, if supported by the platform, the adainit procedure should
15637 be treated as an initialisation routine by the linker (a constructor). This
15638 is intended to be used by the Project Manager to automatically initialize
15639 shared Stand-Alone Libraries.
15640 @end table
15641
15642 @geindex -aO (gnatbind)
15643
15644
15645 @table @asis
15646
15647 @item @code{-aO}
15648
15649 Specify directory to be searched for ALI files.
15650 @end table
15651
15652 @geindex -aI (gnatbind)
15653
15654
15655 @table @asis
15656
15657 @item @code{-aI}
15658
15659 Specify directory to be searched for source file.
15660 @end table
15661
15662 @geindex -A (gnatbind)
15663
15664
15665 @table @asis
15666
15667 @item @code{-A[=@emph{filename}]}
15668
15669 Output ALI list (to standard output or to the named file).
15670 @end table
15671
15672 @geindex -b (gnatbind)
15673
15674
15675 @table @asis
15676
15677 @item @code{-b}
15678
15679 Generate brief messages to @code{stderr} even if verbose mode set.
15680 @end table
15681
15682 @geindex -c (gnatbind)
15683
15684
15685 @table @asis
15686
15687 @item @code{-c}
15688
15689 Check only, no generation of binder output file.
15690 @end table
15691
15692 @geindex -dnn[k|m] (gnatbind)
15693
15694
15695 @table @asis
15696
15697 @item @code{-d@emph{nn}[k|m]}
15698
15699 This switch can be used to change the default task stack size value
15700 to a specified size @code{nn}, which is expressed in bytes by default, or
15701 in kilobytes when suffixed with @code{k} or in megabytes when suffixed
15702 with @code{m}.
15703 In the absence of a @code{[k|m]} suffix, this switch is equivalent,
15704 in effect, to completing all task specs with
15705
15706 @example
15707 pragma Storage_Size (nn);
15708 @end example
15709
15710 When they do not already have such a pragma.
15711 @end table
15712
15713 @geindex -D (gnatbind)
15714
15715
15716 @table @asis
15717
15718 @item @code{-D@emph{nn}[k|m]}
15719
15720 Set the default secondary stack size to @code{nn}. The suffix indicates whether
15721 the size is in bytes (no suffix), kilobytes (@code{k} suffix) or megabytes
15722 (@code{m} suffix).
15723
15724 The secondary stack holds objects of unconstrained types that are returned by
15725 functions, for example unconstrained Strings. The size of the secondary stack
15726 can be dynamic or fixed depending on the target.
15727
15728 For most targets, the secondary stack grows on demand and is implemented as
15729 a chain of blocks in the heap. In this case, the default secondary stack size
15730 determines the initial size of the secondary stack for each task and the
15731 smallest amount the secondary stack can grow by.
15732
15733 For Ravenscar, ZFP, and Cert run-times the size of the secondary stack is
15734 fixed. This switch can be used to change the default size of these stacks.
15735 The default secondary stack size can be overridden on a per-task basis if
15736 individual tasks have different secondary stack requirements. This is
15737 achieved through the Secondary_Stack_Size aspect that takes the size of the
15738 secondary stack in bytes.
15739 @end table
15740
15741 @geindex -e (gnatbind)
15742
15743
15744 @table @asis
15745
15746 @item @code{-e}
15747
15748 Output complete list of elaboration-order dependencies.
15749 @end table
15750
15751 @geindex -Ea (gnatbind)
15752
15753
15754 @table @asis
15755
15756 @item @code{-Ea}
15757
15758 Store tracebacks in exception occurrences when the target supports it.
15759 The "a" is for "address"; tracebacks will contain hexadecimal addresses,
15760 unless symbolic tracebacks are enabled.
15761
15762 See also the packages @code{GNAT.Traceback} and
15763 @code{GNAT.Traceback.Symbolic} for more information.
15764 Note that on x86 ports, you must not use @code{-fomit-frame-pointer}
15765 @code{gcc} option.
15766 @end table
15767
15768 @geindex -Es (gnatbind)
15769
15770
15771 @table @asis
15772
15773 @item @code{-Es}
15774
15775 Store tracebacks in exception occurrences when the target supports it.
15776 The "s" is for "symbolic"; symbolic tracebacks are enabled.
15777 @end table
15778
15779 @geindex -E (gnatbind)
15780
15781
15782 @table @asis
15783
15784 @item @code{-E}
15785
15786 Currently the same as @code{-Ea}.
15787 @end table
15788
15789 @geindex -f (gnatbind)
15790
15791
15792 @table @asis
15793
15794 @item @code{-f@emph{elab-order}}
15795
15796 Force elaboration order. For further details see @ref{120,,Elaboration Control}
15797 and @ref{f,,Elaboration Order Handling in GNAT}.
15798 @end table
15799
15800 @geindex -F (gnatbind)
15801
15802
15803 @table @asis
15804
15805 @item @code{-F}
15806
15807 Force the checks of elaboration flags. @code{gnatbind} does not normally
15808 generate checks of elaboration flags for the main executable, except when
15809 a Stand-Alone Library is used. However, there are cases when this cannot be
15810 detected by gnatbind. An example is importing an interface of a Stand-Alone
15811 Library through a pragma Import and only specifying through a linker switch
15812 this Stand-Alone Library. This switch is used to guarantee that elaboration
15813 flag checks are generated.
15814 @end table
15815
15816 @geindex -h (gnatbind)
15817
15818
15819 @table @asis
15820
15821 @item @code{-h}
15822
15823 Output usage (help) information.
15824 @end table
15825
15826 @geindex -H (gnatbind)
15827
15828
15829 @table @asis
15830
15831 @item @code{-H}
15832
15833 Legacy elaboration order model enabled. For further details see
15834 @ref{f,,Elaboration Order Handling in GNAT}.
15835 @end table
15836
15837 @geindex -H32 (gnatbind)
15838
15839
15840 @table @asis
15841
15842 @item @code{-H32}
15843
15844 Use 32-bit allocations for @code{__gnat_malloc} (and thus for access types).
15845 For further details see @ref{121,,Dynamic Allocation Control}.
15846 @end table
15847
15848 @geindex -H64 (gnatbind)
15849
15850 @geindex __gnat_malloc
15851
15852
15853 @table @asis
15854
15855 @item @code{-H64}
15856
15857 Use 64-bit allocations for @code{__gnat_malloc} (and thus for access types).
15858 For further details see @ref{121,,Dynamic Allocation Control}.
15859
15860 @geindex -I (gnatbind)
15861
15862 @item @code{-I}
15863
15864 Specify directory to be searched for source and ALI files.
15865
15866 @geindex -I- (gnatbind)
15867
15868 @item @code{-I-}
15869
15870 Do not look for sources in the current directory where @code{gnatbind} was
15871 invoked, and do not look for ALI files in the directory containing the
15872 ALI file named in the @code{gnatbind} command line.
15873
15874 @geindex -l (gnatbind)
15875
15876 @item @code{-l}
15877
15878 Output chosen elaboration order.
15879
15880 @geindex -L (gnatbind)
15881
15882 @item @code{-L@emph{xxx}}
15883
15884 Bind the units for library building. In this case the @code{adainit} and
15885 @code{adafinal} procedures (@ref{b4,,Binding with Non-Ada Main Programs})
15886 are renamed to @code{@emph{xxx}init} and
15887 @code{@emph{xxx}final}.
15888 Implies -n.
15889 (@ref{15,,GNAT and Libraries}, for more details.)
15890
15891 @geindex -M (gnatbind)
15892
15893 @item @code{-M@emph{xyz}}
15894
15895 Rename generated main program from main to xyz. This option is
15896 supported on cross environments only.
15897
15898 @geindex -m (gnatbind)
15899
15900 @item @code{-m@emph{n}}
15901
15902 Limit number of detected errors or warnings to @code{n}, where @code{n} is
15903 in the range 1..999999. The default value if no switch is
15904 given is 9999. If the number of warnings reaches this limit, then a
15905 message is output and further warnings are suppressed, the bind
15906 continues in this case. If the number of errors reaches this
15907 limit, then a message is output and the bind is abandoned.
15908 A value of zero means that no limit is enforced. The equal
15909 sign is optional.
15910
15911 @geindex -minimal (gnatbind)
15912
15913 @item @code{-minimal}
15914
15915 Generate a binder file suitable for space-constrained applications. When
15916 active, binder-generated objects not required for program operation are no
15917 longer generated. @strong{Warning:} this option comes with the following
15918 limitations:
15919
15920
15921 @itemize *
15922
15923 @item
15924 Starting the program's execution in the debugger will cause it to
15925 stop at the start of the @code{main} function instead of the main subprogram.
15926 This can be worked around by manually inserting a breakpoint on that
15927 subprogram and resuming the program's execution until reaching that breakpoint.
15928
15929 @item
15930 Programs using GNAT.Compiler_Version will not link.
15931 @end itemize
15932
15933 @geindex -n (gnatbind)
15934
15935 @item @code{-n}
15936
15937 No main program.
15938
15939 @geindex -nostdinc (gnatbind)
15940
15941 @item @code{-nostdinc}
15942
15943 Do not look for sources in the system default directory.
15944
15945 @geindex -nostdlib (gnatbind)
15946
15947 @item @code{-nostdlib}
15948
15949 Do not look for library files in the system default directory.
15950
15951 @geindex --RTS (gnatbind)
15952
15953 @item @code{--RTS=@emph{rts-path}}
15954
15955 Specifies the default location of the run-time library. Same meaning as the
15956 equivalent @code{gnatmake} flag (@ref{dc,,Switches for gnatmake}).
15957
15958 @geindex -o (gnatbind)
15959
15960 @item @code{-o @emph{file}}
15961
15962 Name the output file @code{file} (default is @code{b~`xxx}.adb`).
15963 Note that if this option is used, then linking must be done manually,
15964 gnatlink cannot be used.
15965
15966 @geindex -O (gnatbind)
15967
15968 @item @code{-O[=@emph{filename}]}
15969
15970 Output object list (to standard output or to the named file).
15971
15972 @geindex -p (gnatbind)
15973
15974 @item @code{-p}
15975
15976 Pessimistic (worst-case) elaboration order.
15977
15978 @geindex -P (gnatbind)
15979
15980 @item @code{-P}
15981
15982 Generate binder file suitable for CodePeer.
15983
15984 @geindex -R (gnatbind)
15985
15986 @item @code{-R}
15987
15988 Output closure source list, which includes all non-run-time units that are
15989 included in the bind.
15990
15991 @geindex -Ra (gnatbind)
15992
15993 @item @code{-Ra}
15994
15995 Like @code{-R} but the list includes run-time units.
15996
15997 @geindex -s (gnatbind)
15998
15999 @item @code{-s}
16000
16001 Require all source files to be present.
16002
16003 @geindex -S (gnatbind)
16004
16005 @item @code{-S@emph{xxx}}
16006
16007 Specifies the value to be used when detecting uninitialized scalar
16008 objects with pragma Initialize_Scalars.
16009 The @code{xxx} string specified with the switch is one of:
16010
16011
16012 @itemize *
16013
16014 @item
16015 @code{in} for an invalid value.
16016
16017 If zero is invalid for the discrete type in question,
16018 then the scalar value is set to all zero bits.
16019 For signed discrete types, the largest possible negative value of
16020 the underlying scalar is set (i.e. a one bit followed by all zero bits).
16021 For unsigned discrete types, the underlying scalar value is set to all
16022 one bits. For floating-point types, a NaN value is set
16023 (see body of package System.Scalar_Values for exact values).
16024
16025 @item
16026 @code{lo} for low value.
16027
16028 If zero is invalid for the discrete type in question,
16029 then the scalar value is set to all zero bits.
16030 For signed discrete types, the largest possible negative value of
16031 the underlying scalar is set (i.e. a one bit followed by all zero bits).
16032 For unsigned discrete types, the underlying scalar value is set to all
16033 zero bits. For floating-point, a small value is set
16034 (see body of package System.Scalar_Values for exact values).
16035
16036 @item
16037 @code{hi} for high value.
16038
16039 If zero is invalid for the discrete type in question,
16040 then the scalar value is set to all one bits.
16041 For signed discrete types, the largest possible positive value of
16042 the underlying scalar is set (i.e. a zero bit followed by all one bits).
16043 For unsigned discrete types, the underlying scalar value is set to all
16044 one bits. For floating-point, a large value is set
16045 (see body of package System.Scalar_Values for exact values).
16046
16047 @item
16048 @code{xx} for hex value (two hex digits).
16049
16050 The underlying scalar is set to a value consisting of repeated bytes, whose
16051 value corresponds to the given value. For example if @code{BF} is given,
16052 then a 32-bit scalar value will be set to the bit patterm @code{16#BFBFBFBF#}.
16053 @end itemize
16054
16055 @geindex GNAT_INIT_SCALARS
16056
16057 In addition, you can specify @code{-Sev} to indicate that the value is
16058 to be set at run time. In this case, the program will look for an environment
16059 variable of the form @code{GNAT_INIT_SCALARS=@emph{yy}}, where @code{yy} is one
16060 of @code{in/lo/hi/@emph{xx}} with the same meanings as above.
16061 If no environment variable is found, or if it does not have a valid value,
16062 then the default is @code{in} (invalid values).
16063 @end table
16064
16065 @geindex -static (gnatbind)
16066
16067
16068 @table @asis
16069
16070 @item @code{-static}
16071
16072 Link against a static GNAT run-time.
16073
16074 @geindex -shared (gnatbind)
16075
16076 @item @code{-shared}
16077
16078 Link against a shared GNAT run-time when available.
16079
16080 @geindex -t (gnatbind)
16081
16082 @item @code{-t}
16083
16084 Tolerate time stamp and other consistency errors.
16085
16086 @geindex -T (gnatbind)
16087
16088 @item @code{-T@emph{n}}
16089
16090 Set the time slice value to @code{n} milliseconds. If the system supports
16091 the specification of a specific time slice value, then the indicated value
16092 is used. If the system does not support specific time slice values, but
16093 does support some general notion of round-robin scheduling, then any
16094 nonzero value will activate round-robin scheduling.
16095
16096 A value of zero is treated specially. It turns off time
16097 slicing, and in addition, indicates to the tasking run-time that the
16098 semantics should match as closely as possible the Annex D
16099 requirements of the Ada RM, and in particular sets the default
16100 scheduling policy to @code{FIFO_Within_Priorities}.
16101
16102 @geindex -u (gnatbind)
16103
16104 @item @code{-u@emph{n}}
16105
16106 Enable dynamic stack usage, with @code{n} results stored and displayed
16107 at program termination. A result is generated when a task
16108 terminates. Results that can't be stored are displayed on the fly, at
16109 task termination. This option is currently not supported on Itanium
16110 platforms. (See @ref{122,,Dynamic Stack Usage Analysis} for details.)
16111
16112 @geindex -v (gnatbind)
16113
16114 @item @code{-v}
16115
16116 Verbose mode. Write error messages, header, summary output to
16117 @code{stdout}.
16118
16119 @geindex -V (gnatbind)
16120
16121 @item @code{-V@emph{key}=@emph{value}}
16122
16123 Store the given association of @code{key} to @code{value} in the bind environment.
16124 Values stored this way can be retrieved at run time using
16125 @code{GNAT.Bind_Environment}.
16126
16127 @geindex -w (gnatbind)
16128
16129 @item @code{-w@emph{x}}
16130
16131 Warning mode; @code{x} = s/e for suppress/treat as error.
16132
16133 @geindex -Wx (gnatbind)
16134
16135 @item @code{-Wx@emph{e}}
16136
16137 Override default wide character encoding for standard Text_IO files.
16138
16139 @geindex -x (gnatbind)
16140
16141 @item @code{-x}
16142
16143 Exclude source files (check object consistency only).
16144
16145 @geindex -Xnnn (gnatbind)
16146
16147 @item @code{-X@emph{nnn}}
16148
16149 Set default exit status value, normally 0 for POSIX compliance.
16150
16151 @geindex -y (gnatbind)
16152
16153 @item @code{-y}
16154
16155 Enable leap seconds support in @code{Ada.Calendar} and its children.
16156
16157 @geindex -z (gnatbind)
16158
16159 @item @code{-z}
16160
16161 No main subprogram.
16162 @end table
16163
16164 You may obtain this listing of switches by running @code{gnatbind} with
16165 no arguments.
16166
16167 @menu
16168 * Consistency-Checking Modes::
16169 * Binder Error Message Control::
16170 * Elaboration Control::
16171 * Output Control::
16172 * Dynamic Allocation Control::
16173 * Binding with Non-Ada Main Programs::
16174 * Binding Programs with No Main Subprogram::
16175
16176 @end menu
16177
16178 @node Consistency-Checking Modes,Binder Error Message Control,,Switches for gnatbind
16179 @anchor{gnat_ugn/building_executable_programs_with_gnat consistency-checking-modes}@anchor{123}@anchor{gnat_ugn/building_executable_programs_with_gnat id35}@anchor{124}
16180 @subsubsection Consistency-Checking Modes
16181
16182
16183 As described earlier, by default @code{gnatbind} checks
16184 that object files are consistent with one another and are consistent
16185 with any source files it can locate. The following switches control binder
16186 access to sources.
16187
16188 @quotation
16189
16190 @geindex -s (gnatbind)
16191 @end quotation
16192
16193
16194 @table @asis
16195
16196 @item @code{-s}
16197
16198 Require source files to be present. In this mode, the binder must be
16199 able to locate all source files that are referenced, in order to check
16200 their consistency. In normal mode, if a source file cannot be located it
16201 is simply ignored. If you specify this switch, a missing source
16202 file is an error.
16203
16204 @geindex -Wx (gnatbind)
16205
16206 @item @code{-Wx@emph{e}}
16207
16208 Override default wide character encoding for standard Text_IO files.
16209 Normally the default wide character encoding method used for standard
16210 [Wide_[Wide_]]Text_IO files is taken from the encoding specified for
16211 the main source input (see description of switch
16212 @code{-gnatWx} for the compiler). The
16213 use of this switch for the binder (which has the same set of
16214 possible arguments) overrides this default as specified.
16215
16216 @geindex -x (gnatbind)
16217
16218 @item @code{-x}
16219
16220 Exclude source files. In this mode, the binder only checks that ALI
16221 files are consistent with one another. Source files are not accessed.
16222 The binder runs faster in this mode, and there is still a guarantee that
16223 the resulting program is self-consistent.
16224 If a source file has been edited since it was last compiled, and you
16225 specify this switch, the binder will not detect that the object
16226 file is out of date with respect to the source file. Note that this is the
16227 mode that is automatically used by @code{gnatmake} because in this
16228 case the checking against sources has already been performed by
16229 @code{gnatmake} in the course of compilation (i.e., before binding).
16230 @end table
16231
16232 @node Binder Error Message Control,Elaboration Control,Consistency-Checking Modes,Switches for gnatbind
16233 @anchor{gnat_ugn/building_executable_programs_with_gnat id36}@anchor{125}@anchor{gnat_ugn/building_executable_programs_with_gnat binder-error-message-control}@anchor{126}
16234 @subsubsection Binder Error Message Control
16235
16236
16237 The following switches provide control over the generation of error
16238 messages from the binder:
16239
16240 @quotation
16241
16242 @geindex -v (gnatbind)
16243 @end quotation
16244
16245
16246 @table @asis
16247
16248 @item @code{-v}
16249
16250 Verbose mode. In the normal mode, brief error messages are generated to
16251 @code{stderr}. If this switch is present, a header is written
16252 to @code{stdout} and any error messages are directed to @code{stdout}.
16253 All that is written to @code{stderr} is a brief summary message.
16254
16255 @geindex -b (gnatbind)
16256
16257 @item @code{-b}
16258
16259 Generate brief error messages to @code{stderr} even if verbose mode is
16260 specified. This is relevant only when used with the
16261 @code{-v} switch.
16262
16263 @geindex -m (gnatbind)
16264
16265 @item @code{-m@emph{n}}
16266
16267 Limits the number of error messages to @code{n}, a decimal integer in the
16268 range 1-999. The binder terminates immediately if this limit is reached.
16269
16270 @geindex -M (gnatbind)
16271
16272 @item @code{-M@emph{xxx}}
16273
16274 Renames the generated main program from @code{main} to @code{xxx}.
16275 This is useful in the case of some cross-building environments, where
16276 the actual main program is separate from the one generated
16277 by @code{gnatbind}.
16278
16279 @geindex -ws (gnatbind)
16280
16281 @geindex Warnings
16282
16283 @item @code{-ws}
16284
16285 Suppress all warning messages.
16286
16287 @geindex -we (gnatbind)
16288
16289 @item @code{-we}
16290
16291 Treat any warning messages as fatal errors.
16292
16293 @geindex -t (gnatbind)
16294
16295 @geindex Time stamp checks
16296 @geindex in binder
16297
16298 @geindex Binder consistency checks
16299
16300 @geindex Consistency checks
16301 @geindex in binder
16302
16303 @item @code{-t}
16304
16305 The binder performs a number of consistency checks including:
16306
16307
16308 @itemize *
16309
16310 @item
16311 Check that time stamps of a given source unit are consistent
16312
16313 @item
16314 Check that checksums of a given source unit are consistent
16315
16316 @item
16317 Check that consistent versions of @code{GNAT} were used for compilation
16318
16319 @item
16320 Check consistency of configuration pragmas as required
16321 @end itemize
16322
16323 Normally failure of such checks, in accordance with the consistency
16324 requirements of the Ada Reference Manual, causes error messages to be
16325 generated which abort the binder and prevent the output of a binder
16326 file and subsequent link to obtain an executable.
16327
16328 The @code{-t} switch converts these error messages
16329 into warnings, so that
16330 binding and linking can continue to completion even in the presence of such
16331 errors. The result may be a failed link (due to missing symbols), or a
16332 non-functional executable which has undefined semantics.
16333
16334 @cartouche
16335 @quotation Note
16336 This means that @code{-t} should be used only in unusual situations,
16337 with extreme care.
16338 @end quotation
16339 @end cartouche
16340 @end table
16341
16342 @node Elaboration Control,Output Control,Binder Error Message Control,Switches for gnatbind
16343 @anchor{gnat_ugn/building_executable_programs_with_gnat id37}@anchor{127}@anchor{gnat_ugn/building_executable_programs_with_gnat elaboration-control}@anchor{120}
16344 @subsubsection Elaboration Control
16345
16346
16347 The following switches provide additional control over the elaboration
16348 order. For further details see @ref{f,,Elaboration Order Handling in GNAT}.
16349
16350 @geindex -f (gnatbind)
16351
16352
16353 @table @asis
16354
16355 @item @code{-f@emph{elab-order}}
16356
16357 Force elaboration order.
16358
16359 @code{elab-order} should be the name of a "forced elaboration order file", that
16360 is, a text file containing library item names, one per line. A name of the
16361 form "some.unit%s" or "some.unit (spec)" denotes the spec of Some.Unit. A
16362 name of the form "some.unit%b" or "some.unit (body)" denotes the body of
16363 Some.Unit. Each pair of lines is taken to mean that there is an elaboration
16364 dependence of the second line on the first. For example, if the file
16365 contains:
16366
16367 @example
16368 this (spec)
16369 this (body)
16370 that (spec)
16371 that (body)
16372 @end example
16373
16374 then the spec of This will be elaborated before the body of This, and the
16375 body of This will be elaborated before the spec of That, and the spec of That
16376 will be elaborated before the body of That. The first and last of these three
16377 dependences are already required by Ada rules, so this file is really just
16378 forcing the body of This to be elaborated before the spec of That.
16379
16380 The given order must be consistent with Ada rules, or else @code{gnatbind} will
16381 give elaboration cycle errors. For example, if you say x (body) should be
16382 elaborated before x (spec), there will be a cycle, because Ada rules require
16383 x (spec) to be elaborated before x (body); you can't have the spec and body
16384 both elaborated before each other.
16385
16386 If you later add "with That;" to the body of This, there will be a cycle, in
16387 which case you should erase either "this (body)" or "that (spec)" from the
16388 above forced elaboration order file.
16389
16390 Blank lines and Ada-style comments are ignored. Unit names that do not exist
16391 in the program are ignored. Units in the GNAT predefined library are also
16392 ignored.
16393 @end table
16394
16395 @geindex -p (gnatbind)
16396
16397
16398 @table @asis
16399
16400 @item @code{-p}
16401
16402 Pessimistic elaboration order
16403
16404 This switch is only applicable to the pre-20.x legacy elaboration models.
16405 The post-20.x elaboration model uses a more informed approach of ordering
16406 the units.
16407
16408 Normally the binder attempts to choose an elaboration order that is likely to
16409 minimize the likelihood of an elaboration order error resulting in raising a
16410 @code{Program_Error} exception. This switch reverses the action of the binder,
16411 and requests that it deliberately choose an order that is likely to maximize
16412 the likelihood of an elaboration error. This is useful in ensuring
16413 portability and avoiding dependence on accidental fortuitous elaboration
16414 ordering.
16415
16416 Normally it only makes sense to use the @code{-p} switch if dynamic
16417 elaboration checking is used (@code{-gnatE} switch used for compilation).
16418 This is because in the default static elaboration mode, all necessary
16419 @code{Elaborate} and @code{Elaborate_All} pragmas are implicitly inserted.
16420 These implicit pragmas are still respected by the binder in @code{-p}
16421 mode, so a safe elaboration order is assured.
16422
16423 Note that @code{-p} is not intended for production use; it is more for
16424 debugging/experimental use.
16425 @end table
16426
16427 @node Output Control,Dynamic Allocation Control,Elaboration Control,Switches for gnatbind
16428 @anchor{gnat_ugn/building_executable_programs_with_gnat output-control}@anchor{128}@anchor{gnat_ugn/building_executable_programs_with_gnat id38}@anchor{129}
16429 @subsubsection Output Control
16430
16431
16432 The following switches allow additional control over the output
16433 generated by the binder.
16434
16435 @quotation
16436
16437 @geindex -c (gnatbind)
16438 @end quotation
16439
16440
16441 @table @asis
16442
16443 @item @code{-c}
16444
16445 Check only. Do not generate the binder output file. In this mode the
16446 binder performs all error checks but does not generate an output file.
16447
16448 @geindex -e (gnatbind)
16449
16450 @item @code{-e}
16451
16452 Output complete list of elaboration-order dependencies, showing the
16453 reason for each dependency. This output can be rather extensive but may
16454 be useful in diagnosing problems with elaboration order. The output is
16455 written to @code{stdout}.
16456
16457 @geindex -h (gnatbind)
16458
16459 @item @code{-h}
16460
16461 Output usage information. The output is written to @code{stdout}.
16462
16463 @geindex -K (gnatbind)
16464
16465 @item @code{-K}
16466
16467 Output linker options to @code{stdout}. Includes library search paths,
16468 contents of pragmas Ident and Linker_Options, and libraries added
16469 by @code{gnatbind}.
16470
16471 @geindex -l (gnatbind)
16472
16473 @item @code{-l}
16474
16475 Output chosen elaboration order. The output is written to @code{stdout}.
16476
16477 @geindex -O (gnatbind)
16478
16479 @item @code{-O}
16480
16481 Output full names of all the object files that must be linked to provide
16482 the Ada component of the program. The output is written to @code{stdout}.
16483 This list includes the files explicitly supplied and referenced by the user
16484 as well as implicitly referenced run-time unit files. The latter are
16485 omitted if the corresponding units reside in shared libraries. The
16486 directory names for the run-time units depend on the system configuration.
16487
16488 @geindex -o (gnatbind)
16489
16490 @item @code{-o @emph{file}}
16491
16492 Set name of output file to @code{file} instead of the normal
16493 @code{b~`mainprog}.adb` default. Note that @code{file} denote the Ada
16494 binder generated body filename.
16495 Note that if this option is used, then linking must be done manually.
16496 It is not possible to use gnatlink in this case, since it cannot locate
16497 the binder file.
16498
16499 @geindex -r (gnatbind)
16500
16501 @item @code{-r}
16502
16503 Generate list of @code{pragma Restrictions} that could be applied to
16504 the current unit. This is useful for code audit purposes, and also may
16505 be used to improve code generation in some cases.
16506 @end table
16507
16508 @node Dynamic Allocation Control,Binding with Non-Ada Main Programs,Output Control,Switches for gnatbind
16509 @anchor{gnat_ugn/building_executable_programs_with_gnat dynamic-allocation-control}@anchor{121}@anchor{gnat_ugn/building_executable_programs_with_gnat id39}@anchor{12a}
16510 @subsubsection Dynamic Allocation Control
16511
16512
16513 The heap control switches -- @code{-H32} and @code{-H64} --
16514 determine whether dynamic allocation uses 32-bit or 64-bit memory.
16515 They only affect compiler-generated allocations via @code{__gnat_malloc};
16516 explicit calls to @code{malloc} and related functions from the C
16517 run-time library are unaffected.
16518
16519
16520 @table @asis
16521
16522 @item @code{-H32}
16523
16524 Allocate memory on 32-bit heap
16525
16526 @item @code{-H64}
16527
16528 Allocate memory on 64-bit heap. This is the default
16529 unless explicitly overridden by a @code{'Size} clause on the access type.
16530 @end table
16531
16532 These switches are only effective on VMS platforms.
16533
16534 @node Binding with Non-Ada Main Programs,Binding Programs with No Main Subprogram,Dynamic Allocation Control,Switches for gnatbind
16535 @anchor{gnat_ugn/building_executable_programs_with_gnat binding-with-non-ada-main-programs}@anchor{b4}@anchor{gnat_ugn/building_executable_programs_with_gnat id40}@anchor{12b}
16536 @subsubsection Binding with Non-Ada Main Programs
16537
16538
16539 The description so far has assumed that the main
16540 program is in Ada, and that the task of the binder is to generate a
16541 corresponding function @code{main} that invokes this Ada main
16542 program. GNAT also supports the building of executable programs where
16543 the main program is not in Ada, but some of the called routines are
16544 written in Ada and compiled using GNAT (@ref{44,,Mixed Language Programming}).
16545 The following switch is used in this situation:
16546
16547 @quotation
16548
16549 @geindex -n (gnatbind)
16550 @end quotation
16551
16552
16553 @table @asis
16554
16555 @item @code{-n}
16556
16557 No main program. The main program is not in Ada.
16558 @end table
16559
16560 In this case, most of the functions of the binder are still required,
16561 but instead of generating a main program, the binder generates a file
16562 containing the following callable routines:
16563
16564 @quotation
16565
16566 @geindex adainit
16567
16568
16569 @table @asis
16570
16571 @item @code{adainit}
16572
16573 You must call this routine to initialize the Ada part of the program by
16574 calling the necessary elaboration routines. A call to @code{adainit} is
16575 required before the first call to an Ada subprogram.
16576
16577 Note that it is assumed that the basic execution environment must be setup
16578 to be appropriate for Ada execution at the point where the first Ada
16579 subprogram is called. In particular, if the Ada code will do any
16580 floating-point operations, then the FPU must be setup in an appropriate
16581 manner. For the case of the x86, for example, full precision mode is
16582 required. The procedure GNAT.Float_Control.Reset may be used to ensure
16583 that the FPU is in the right state.
16584 @end table
16585
16586 @geindex adafinal
16587
16588
16589 @table @asis
16590
16591 @item @code{adafinal}
16592
16593 You must call this routine to perform any library-level finalization
16594 required by the Ada subprograms. A call to @code{adafinal} is required
16595 after the last call to an Ada subprogram, and before the program
16596 terminates.
16597 @end table
16598 @end quotation
16599
16600 @geindex -n (gnatbind)
16601
16602 @geindex Binder
16603 @geindex multiple input files
16604
16605 If the @code{-n} switch
16606 is given, more than one ALI file may appear on
16607 the command line for @code{gnatbind}. The normal @code{closure}
16608 calculation is performed for each of the specified units. Calculating
16609 the closure means finding out the set of units involved by tracing
16610 @emph{with} references. The reason it is necessary to be able to
16611 specify more than one ALI file is that a given program may invoke two or
16612 more quite separate groups of Ada units.
16613
16614 The binder takes the name of its output file from the last specified ALI
16615 file, unless overridden by the use of the @code{-o file}.
16616
16617 @geindex -o (gnatbind)
16618
16619 The output is an Ada unit in source form that can be compiled with GNAT.
16620 This compilation occurs automatically as part of the @code{gnatlink}
16621 processing.
16622
16623 Currently the GNAT run-time requires a FPU using 80 bits mode
16624 precision. Under targets where this is not the default it is required to
16625 call GNAT.Float_Control.Reset before using floating point numbers (this
16626 include float computation, float input and output) in the Ada code. A
16627 side effect is that this could be the wrong mode for the foreign code
16628 where floating point computation could be broken after this call.
16629
16630 @node Binding Programs with No Main Subprogram,,Binding with Non-Ada Main Programs,Switches for gnatbind
16631 @anchor{gnat_ugn/building_executable_programs_with_gnat binding-programs-with-no-main-subprogram}@anchor{12c}@anchor{gnat_ugn/building_executable_programs_with_gnat id41}@anchor{12d}
16632 @subsubsection Binding Programs with No Main Subprogram
16633
16634
16635 It is possible to have an Ada program which does not have a main
16636 subprogram. This program will call the elaboration routines of all the
16637 packages, then the finalization routines.
16638
16639 The following switch is used to bind programs organized in this manner:
16640
16641 @quotation
16642
16643 @geindex -z (gnatbind)
16644 @end quotation
16645
16646
16647 @table @asis
16648
16649 @item @code{-z}
16650
16651 Normally the binder checks that the unit name given on the command line
16652 corresponds to a suitable main subprogram. When this switch is used,
16653 a list of ALI files can be given, and the execution of the program
16654 consists of elaboration of these units in an appropriate order. Note
16655 that the default wide character encoding method for standard Text_IO
16656 files is always set to Brackets if this switch is set (you can use
16657 the binder switch
16658 @code{-Wx} to override this default).
16659 @end table
16660
16661 @node Command-Line Access,Search Paths for gnatbind,Switches for gnatbind,Binding with gnatbind
16662 @anchor{gnat_ugn/building_executable_programs_with_gnat id42}@anchor{12e}@anchor{gnat_ugn/building_executable_programs_with_gnat command-line-access}@anchor{12f}
16663 @subsection Command-Line Access
16664
16665
16666 The package @code{Ada.Command_Line} provides access to the command-line
16667 arguments and program name. In order for this interface to operate
16668 correctly, the two variables
16669
16670 @example
16671 int gnat_argc;
16672 char **gnat_argv;
16673 @end example
16674
16675 @geindex gnat_argv
16676
16677 @geindex gnat_argc
16678
16679 are declared in one of the GNAT library routines. These variables must
16680 be set from the actual @code{argc} and @code{argv} values passed to the
16681 main program. With no @emph{n} present, @code{gnatbind}
16682 generates the C main program to automatically set these variables.
16683 If the @emph{n} switch is used, there is no automatic way to
16684 set these variables. If they are not set, the procedures in
16685 @code{Ada.Command_Line} will not be available, and any attempt to use
16686 them will raise @code{Constraint_Error}. If command line access is
16687 required, your main program must set @code{gnat_argc} and
16688 @code{gnat_argv} from the @code{argc} and @code{argv} values passed to
16689 it.
16690
16691 @node Search Paths for gnatbind,Examples of gnatbind Usage,Command-Line Access,Binding with gnatbind
16692 @anchor{gnat_ugn/building_executable_programs_with_gnat search-paths-for-gnatbind}@anchor{8c}@anchor{gnat_ugn/building_executable_programs_with_gnat id43}@anchor{130}
16693 @subsection Search Paths for @code{gnatbind}
16694
16695
16696 The binder takes the name of an ALI file as its argument and needs to
16697 locate source files as well as other ALI files to verify object consistency.
16698
16699 For source files, it follows exactly the same search rules as @code{gcc}
16700 (see @ref{89,,Search Paths and the Run-Time Library (RTL)}). For ALI files the
16701 directories searched are:
16702
16703
16704 @itemize *
16705
16706 @item
16707 The directory containing the ALI file named in the command line, unless
16708 the switch @code{-I-} is specified.
16709
16710 @item
16711 All directories specified by @code{-I}
16712 switches on the @code{gnatbind}
16713 command line, in the order given.
16714
16715 @geindex ADA_PRJ_OBJECTS_FILE
16716
16717 @item
16718 Each of the directories listed in the text file whose name is given
16719 by the
16720 @geindex ADA_PRJ_OBJECTS_FILE
16721 @geindex environment variable; ADA_PRJ_OBJECTS_FILE
16722 @code{ADA_PRJ_OBJECTS_FILE} environment variable.
16723
16724 @geindex ADA_PRJ_OBJECTS_FILE
16725 @geindex environment variable; ADA_PRJ_OBJECTS_FILE
16726 @code{ADA_PRJ_OBJECTS_FILE} is normally set by gnatmake or by the gnat
16727 driver when project files are used. It should not normally be set
16728 by other means.
16729
16730 @geindex ADA_OBJECTS_PATH
16731
16732 @item
16733 Each of the directories listed in the value of the
16734 @geindex ADA_OBJECTS_PATH
16735 @geindex environment variable; ADA_OBJECTS_PATH
16736 @code{ADA_OBJECTS_PATH} environment variable.
16737 Construct this value
16738 exactly as the
16739 @geindex PATH
16740 @geindex environment variable; PATH
16741 @code{PATH} environment variable: a list of directory
16742 names separated by colons (semicolons when working with the NT version
16743 of GNAT).
16744
16745 @item
16746 The content of the @code{ada_object_path} file which is part of the GNAT
16747 installation tree and is used to store standard libraries such as the
16748 GNAT Run-Time Library (RTL) unless the switch @code{-nostdlib} is
16749 specified. See @ref{87,,Installing a library}
16750 @end itemize
16751
16752 @geindex -I (gnatbind)
16753
16754 @geindex -aI (gnatbind)
16755
16756 @geindex -aO (gnatbind)
16757
16758 In the binder the switch @code{-I}
16759 is used to specify both source and
16760 library file paths. Use @code{-aI}
16761 instead if you want to specify
16762 source paths only, and @code{-aO}
16763 if you want to specify library paths
16764 only. This means that for the binder
16765 @code{-I@emph{dir}} is equivalent to
16766 @code{-aI@emph{dir}}
16767 @code{-aO`@emph{dir}}.
16768 The binder generates the bind file (a C language source file) in the
16769 current working directory.
16770
16771 @geindex Ada
16772
16773 @geindex System
16774
16775 @geindex Interfaces
16776
16777 @geindex GNAT
16778
16779 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
16780 children make up the GNAT Run-Time Library, together with the package
16781 GNAT and its children, which contain a set of useful additional
16782 library functions provided by GNAT. The sources for these units are
16783 needed by the compiler and are kept together in one directory. The ALI
16784 files and object files generated by compiling the RTL are needed by the
16785 binder and the linker and are kept together in one directory, typically
16786 different from the directory containing the sources. In a normal
16787 installation, you need not specify these directory names when compiling
16788 or binding. Either the environment variables or the built-in defaults
16789 cause these files to be found.
16790
16791 Besides simplifying access to the RTL, a major use of search paths is
16792 in compiling sources from multiple directories. This can make
16793 development environments much more flexible.
16794
16795 @node Examples of gnatbind Usage,,Search Paths for gnatbind,Binding with gnatbind
16796 @anchor{gnat_ugn/building_executable_programs_with_gnat id44}@anchor{131}@anchor{gnat_ugn/building_executable_programs_with_gnat examples-of-gnatbind-usage}@anchor{132}
16797 @subsection Examples of @code{gnatbind} Usage
16798
16799
16800 Here are some examples of @code{gnatbind} invovations:
16801
16802 @quotation
16803
16804 @example
16805 gnatbind hello
16806 @end example
16807
16808 The main program @code{Hello} (source program in @code{hello.adb}) is
16809 bound using the standard switch settings. The generated main program is
16810 @code{b~hello.adb}. This is the normal, default use of the binder.
16811
16812 @example
16813 gnatbind hello -o mainprog.adb
16814 @end example
16815
16816 The main program @code{Hello} (source program in @code{hello.adb}) is
16817 bound using the standard switch settings. The generated main program is
16818 @code{mainprog.adb} with the associated spec in
16819 @code{mainprog.ads}. Note that you must specify the body here not the
16820 spec. Note that if this option is used, then linking must be done manually,
16821 since gnatlink will not be able to find the generated file.
16822 @end quotation
16823
16824 @node Linking with gnatlink,Using the GNU make Utility,Binding with gnatbind,Building Executable Programs with GNAT
16825 @anchor{gnat_ugn/building_executable_programs_with_gnat id45}@anchor{133}@anchor{gnat_ugn/building_executable_programs_with_gnat linking-with-gnatlink}@anchor{1e}
16826 @section Linking with @code{gnatlink}
16827
16828
16829 @geindex gnatlink
16830
16831 This chapter discusses @code{gnatlink}, a tool that links
16832 an Ada program and builds an executable file. This utility
16833 invokes the system linker (via the @code{gcc} command)
16834 with a correct list of object files and library references.
16835 @code{gnatlink} automatically determines the list of files and
16836 references for the Ada part of a program. It uses the binder file
16837 generated by the @code{gnatbind} to determine this list.
16838
16839 @menu
16840 * Running gnatlink::
16841 * Switches for gnatlink::
16842
16843 @end menu
16844
16845 @node Running gnatlink,Switches for gnatlink,,Linking with gnatlink
16846 @anchor{gnat_ugn/building_executable_programs_with_gnat id46}@anchor{134}@anchor{gnat_ugn/building_executable_programs_with_gnat running-gnatlink}@anchor{135}
16847 @subsection Running @code{gnatlink}
16848
16849
16850 The form of the @code{gnatlink} command is
16851
16852 @example
16853 $ gnatlink [ switches ] mainprog [.ali]
16854 [ non-Ada objects ] [ linker options ]
16855 @end example
16856
16857 The arguments of @code{gnatlink} (switches, main @code{ALI} file,
16858 non-Ada objects
16859 or linker options) may be in any order, provided that no non-Ada object may
16860 be mistaken for a main @code{ALI} file.
16861 Any file name @code{F} without the @code{.ali}
16862 extension will be taken as the main @code{ALI} file if a file exists
16863 whose name is the concatenation of @code{F} and @code{.ali}.
16864
16865 @code{mainprog.ali} references the ALI file of the main program.
16866 The @code{.ali} extension of this file can be omitted. From this
16867 reference, @code{gnatlink} locates the corresponding binder file
16868 @code{b~mainprog.adb} and, using the information in this file along
16869 with the list of non-Ada objects and linker options, constructs a
16870 linker command file to create the executable.
16871
16872 The arguments other than the @code{gnatlink} switches and the main
16873 @code{ALI} file are passed to the linker uninterpreted.
16874 They typically include the names of
16875 object files for units written in other languages than Ada and any library
16876 references required to resolve references in any of these foreign language
16877 units, or in @code{Import} pragmas in any Ada units.
16878
16879 @code{linker options} is an optional list of linker specific
16880 switches.
16881 The default linker called by gnatlink is @code{gcc} which in
16882 turn calls the appropriate system linker.
16883
16884 One useful option for the linker is @code{-s}: it reduces the size of the
16885 executable by removing all symbol table and relocation information from the
16886 executable.
16887
16888 Standard options for the linker such as @code{-lmy_lib} or
16889 @code{-Ldir} can be added as is.
16890 For options that are not recognized by
16891 @code{gcc} as linker options, use the @code{gcc} switches
16892 @code{-Xlinker} or @code{-Wl,}.
16893
16894 Refer to the GCC documentation for
16895 details.
16896
16897 Here is an example showing how to generate a linker map:
16898
16899 @example
16900 $ gnatlink my_prog -Wl,-Map,MAPFILE
16901 @end example
16902
16903 Using @code{linker options} it is possible to set the program stack and
16904 heap size.
16905 See @ref{136,,Setting Stack Size from gnatlink} and
16906 @ref{137,,Setting Heap Size from gnatlink}.
16907
16908 @code{gnatlink} determines the list of objects required by the Ada
16909 program and prepends them to the list of objects passed to the linker.
16910 @code{gnatlink} also gathers any arguments set by the use of
16911 @code{pragma Linker_Options} and adds them to the list of arguments
16912 presented to the linker.
16913
16914 @node Switches for gnatlink,,Running gnatlink,Linking with gnatlink
16915 @anchor{gnat_ugn/building_executable_programs_with_gnat id47}@anchor{138}@anchor{gnat_ugn/building_executable_programs_with_gnat switches-for-gnatlink}@anchor{139}
16916 @subsection Switches for @code{gnatlink}
16917
16918
16919 The following switches are available with the @code{gnatlink} utility:
16920
16921 @geindex --version (gnatlink)
16922
16923
16924 @table @asis
16925
16926 @item @code{--version}
16927
16928 Display Copyright and version, then exit disregarding all other options.
16929 @end table
16930
16931 @geindex --help (gnatlink)
16932
16933
16934 @table @asis
16935
16936 @item @code{--help}
16937
16938 If @code{--version} was not used, display usage, then exit disregarding
16939 all other options.
16940 @end table
16941
16942 @geindex Command line length
16943
16944 @geindex -f (gnatlink)
16945
16946
16947 @table @asis
16948
16949 @item @code{-f}
16950
16951 On some targets, the command line length is limited, and @code{gnatlink}
16952 will generate a separate file for the linker if the list of object files
16953 is too long.
16954 The @code{-f} switch forces this file
16955 to be generated even if
16956 the limit is not exceeded. This is useful in some cases to deal with
16957 special situations where the command line length is exceeded.
16958 @end table
16959
16960 @geindex Debugging information
16961 @geindex including
16962
16963 @geindex -g (gnatlink)
16964
16965
16966 @table @asis
16967
16968 @item @code{-g}
16969
16970 The option to include debugging information causes the Ada bind file (in
16971 other words, @code{b~mainprog.adb}) to be compiled with @code{-g}.
16972 In addition, the binder does not delete the @code{b~mainprog.adb},
16973 @code{b~mainprog.o} and @code{b~mainprog.ali} files.
16974 Without @code{-g}, the binder removes these files by default.
16975 @end table
16976
16977 @geindex -n (gnatlink)
16978
16979
16980 @table @asis
16981
16982 @item @code{-n}
16983
16984 Do not compile the file generated by the binder. This may be used when
16985 a link is rerun with different options, but there is no need to recompile
16986 the binder file.
16987 @end table
16988
16989 @geindex -v (gnatlink)
16990
16991
16992 @table @asis
16993
16994 @item @code{-v}
16995
16996 Verbose mode. Causes additional information to be output, including a full
16997 list of the included object files.
16998 This switch option is most useful when you want
16999 to see what set of object files are being used in the link step.
17000 @end table
17001
17002 @geindex -v -v (gnatlink)
17003
17004
17005 @table @asis
17006
17007 @item @code{-v -v}
17008
17009 Very verbose mode. Requests that the compiler operate in verbose mode when
17010 it compiles the binder file, and that the system linker run in verbose mode.
17011 @end table
17012
17013 @geindex -o (gnatlink)
17014
17015
17016 @table @asis
17017
17018 @item @code{-o @emph{exec-name}}
17019
17020 @code{exec-name} specifies an alternate name for the generated
17021 executable program. If this switch is omitted, the executable has the same
17022 name as the main unit. For example, @code{gnatlink try.ali} creates
17023 an executable called @code{try}.
17024 @end table
17025
17026 @geindex -B (gnatlink)
17027
17028
17029 @table @asis
17030
17031 @item @code{-B@emph{dir}}
17032
17033 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
17034 from @code{dir} instead of the default location. Only use this switch
17035 when multiple versions of the GNAT compiler are available.
17036 See the @code{Directory Options} section in @cite{The_GNU_Compiler_Collection}
17037 for further details. You would normally use the @code{-b} or
17038 @code{-V} switch instead.
17039 @end table
17040
17041 @geindex -M (gnatlink)
17042
17043
17044 @table @asis
17045
17046 @item @code{-M}
17047
17048 When linking an executable, create a map file. The name of the map file
17049 has the same name as the executable with extension ".map".
17050 @end table
17051
17052 @geindex -M= (gnatlink)
17053
17054
17055 @table @asis
17056
17057 @item @code{-M=@emph{mapfile}}
17058
17059 When linking an executable, create a map file. The name of the map file is
17060 @code{mapfile}.
17061 @end table
17062
17063 @geindex --GCC=compiler_name (gnatlink)
17064
17065
17066 @table @asis
17067
17068 @item @code{--GCC=@emph{compiler_name}}
17069
17070 Program used for compiling the binder file. The default is
17071 @code{gcc}. You need to use quotes around @code{compiler_name} if
17072 @code{compiler_name} contains spaces or other separator characters.
17073 As an example @code{--GCC="foo -x -y"} will instruct @code{gnatlink} to
17074 use @code{foo -x -y} as your compiler. Note that switch @code{-c} is always
17075 inserted after your command name. Thus in the above example the compiler
17076 command that will be used by @code{gnatlink} will be @code{foo -c -x -y}.
17077 A limitation of this syntax is that the name and path name of the executable
17078 itself must not include any embedded spaces. If the compiler executable is
17079 different from the default one (gcc or <prefix>-gcc), then the back-end
17080 switches in the ALI file are not used to compile the binder generated source.
17081 For example, this is the case with @code{--GCC="foo -x -y"}. But the back end
17082 switches will be used for @code{--GCC="gcc -gnatv"}. If several
17083 @code{--GCC=compiler_name} are used, only the last @code{compiler_name}
17084 is taken into account. However, all the additional switches are also taken
17085 into account. Thus,
17086 @code{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
17087 @code{--GCC="bar -x -y -z -t"}.
17088 @end table
17089
17090 @geindex --LINK= (gnatlink)
17091
17092
17093 @table @asis
17094
17095 @item @code{--LINK=@emph{name}}
17096
17097 @code{name} is the name of the linker to be invoked. This is especially
17098 useful in mixed language programs since languages such as C++ require
17099 their own linker to be used. When this switch is omitted, the default
17100 name for the linker is @code{gcc}. When this switch is used, the
17101 specified linker is called instead of @code{gcc} with exactly the same
17102 parameters that would have been passed to @code{gcc} so if the desired
17103 linker requires different parameters it is necessary to use a wrapper
17104 script that massages the parameters before invoking the real linker. It
17105 may be useful to control the exact invocation by using the verbose
17106 switch.
17107 @end table
17108
17109 @node Using the GNU make Utility,,Linking with gnatlink,Building Executable Programs with GNAT
17110 @anchor{gnat_ugn/building_executable_programs_with_gnat using-the-gnu-make-utility}@anchor{1f}@anchor{gnat_ugn/building_executable_programs_with_gnat id48}@anchor{13a}
17111 @section Using the GNU @code{make} Utility
17112
17113
17114 @geindex make (GNU)
17115 @geindex GNU make
17116
17117 This chapter offers some examples of makefiles that solve specific
17118 problems. It does not explain how to write a makefile, nor does it try to replace the
17119 @code{gnatmake} utility (@ref{1b,,Building with gnatmake}).
17120
17121 All the examples in this section are specific to the GNU version of
17122 make. Although @code{make} is a standard utility, and the basic language
17123 is the same, these examples use some advanced features found only in
17124 @code{GNU make}.
17125
17126 @menu
17127 * Using gnatmake in a Makefile::
17128 * Automatically Creating a List of Directories::
17129 * Generating the Command Line Switches::
17130 * Overcoming Command Line Length Limits::
17131
17132 @end menu
17133
17134 @node Using gnatmake in a Makefile,Automatically Creating a List of Directories,,Using the GNU make Utility
17135 @anchor{gnat_ugn/building_executable_programs_with_gnat using-gnatmake-in-a-makefile}@anchor{13b}@anchor{gnat_ugn/building_executable_programs_with_gnat id49}@anchor{13c}
17136 @subsection Using gnatmake in a Makefile
17137
17138
17139 @c index makefile (GNU make)
17140
17141 Complex project organizations can be handled in a very powerful way by
17142 using GNU make combined with gnatmake. For instance, here is a Makefile
17143 which allows you to build each subsystem of a big project into a separate
17144 shared library. Such a makefile allows you to significantly reduce the link
17145 time of very big applications while maintaining full coherence at
17146 each step of the build process.
17147
17148 The list of dependencies are handled automatically by
17149 @code{gnatmake}. The Makefile is simply used to call gnatmake in each of
17150 the appropriate directories.
17151
17152 Note that you should also read the example on how to automatically
17153 create the list of directories
17154 (@ref{13d,,Automatically Creating a List of Directories})
17155 which might help you in case your project has a lot of subdirectories.
17156
17157 @example
17158 ## This Makefile is intended to be used with the following directory
17159 ## configuration:
17160 ## - The sources are split into a series of csc (computer software components)
17161 ## Each of these csc is put in its own directory.
17162 ## Their name are referenced by the directory names.
17163 ## They will be compiled into shared library (although this would also work
17164 ## with static libraries
17165 ## - The main program (and possibly other packages that do not belong to any
17166 ## csc is put in the top level directory (where the Makefile is).
17167 ## toplevel_dir __ first_csc (sources) __ lib (will contain the library)
17168 ## \\_ second_csc (sources) __ lib (will contain the library)
17169 ## \\_ ...
17170 ## Although this Makefile is build for shared library, it is easy to modify
17171 ## to build partial link objects instead (modify the lines with -shared and
17172 ## gnatlink below)
17173 ##
17174 ## With this makefile, you can change any file in the system or add any new
17175 ## file, and everything will be recompiled correctly (only the relevant shared
17176 ## objects will be recompiled, and the main program will be re-linked).
17177
17178 # The list of computer software component for your project. This might be
17179 # generated automatically.
17180 CSC_LIST=aa bb cc
17181
17182 # Name of the main program (no extension)
17183 MAIN=main
17184
17185 # If we need to build objects with -fPIC, uncomment the following line
17186 #NEED_FPIC=-fPIC
17187
17188 # The following variable should give the directory containing libgnat.so
17189 # You can get this directory through 'gnatls -v'. This is usually the last
17190 # directory in the Object_Path.
17191 GLIB=...
17192
17193 # The directories for the libraries
17194 # (This macro expands the list of CSC to the list of shared libraries, you
17195 # could simply use the expanded form:
17196 # LIB_DIR=aa/lib/libaa.so bb/lib/libbb.so cc/lib/libcc.so
17197 LIB_DIR=$@{foreach dir,$@{CSC_LIST@},$@{dir@}/lib/lib$@{dir@}.so@}
17198
17199 $@{MAIN@}: objects $@{LIB_DIR@}
17200 gnatbind $@{MAIN@} $@{CSC_LIST:%=-aO%/lib@} -shared
17201 gnatlink $@{MAIN@} $@{CSC_LIST:%=-l%@}
17202
17203 objects::
17204 # recompile the sources
17205 gnatmake -c -i $@{MAIN@}.adb $@{NEED_FPIC@} $@{CSC_LIST:%=-I%@}
17206
17207 # Note: In a future version of GNAT, the following commands will be simplified
17208 # by a new tool, gnatmlib
17209 $@{LIB_DIR@}:
17210 mkdir -p $@{dir $@@ @}
17211 cd $@{dir $@@ @} && gcc -shared -o $@{notdir $@@ @} ../*.o -L$@{GLIB@} -lgnat
17212 cd $@{dir $@@ @} && cp -f ../*.ali .
17213
17214 # The dependencies for the modules
17215 # Note that we have to force the expansion of *.o, since in some cases
17216 # make won't be able to do it itself.
17217 aa/lib/libaa.so: $@{wildcard aa/*.o@}
17218 bb/lib/libbb.so: $@{wildcard bb/*.o@}
17219 cc/lib/libcc.so: $@{wildcard cc/*.o@}
17220
17221 # Make sure all of the shared libraries are in the path before starting the
17222 # program
17223 run::
17224 LD_LIBRARY_PATH=`pwd`/aa/lib:`pwd`/bb/lib:`pwd`/cc/lib ./$@{MAIN@}
17225
17226 clean::
17227 $@{RM@} -rf $@{CSC_LIST:%=%/lib@}
17228 $@{RM@} $@{CSC_LIST:%=%/*.ali@}
17229 $@{RM@} $@{CSC_LIST:%=%/*.o@}
17230 $@{RM@} *.o *.ali $@{MAIN@}
17231 @end example
17232
17233 @node Automatically Creating a List of Directories,Generating the Command Line Switches,Using gnatmake in a Makefile,Using the GNU make Utility
17234 @anchor{gnat_ugn/building_executable_programs_with_gnat id50}@anchor{13e}@anchor{gnat_ugn/building_executable_programs_with_gnat automatically-creating-a-list-of-directories}@anchor{13d}
17235 @subsection Automatically Creating a List of Directories
17236
17237
17238 In most makefiles, you will have to specify a list of directories, and
17239 store it in a variable. For small projects, it is often easier to
17240 specify each of them by hand, since you then have full control over what
17241 is the proper order for these directories, which ones should be
17242 included.
17243
17244 However, in larger projects, which might involve hundreds of
17245 subdirectories, it might be more convenient to generate this list
17246 automatically.
17247
17248 The example below presents two methods. The first one, although less
17249 general, gives you more control over the list. It involves wildcard
17250 characters, that are automatically expanded by @code{make}. Its
17251 shortcoming is that you need to explicitly specify some of the
17252 organization of your project, such as for instance the directory tree
17253 depth, whether some directories are found in a separate tree, etc.
17254
17255 The second method is the most general one. It requires an external
17256 program, called @code{find}, which is standard on all Unix systems. All
17257 the directories found under a given root directory will be added to the
17258 list.
17259
17260 @example
17261 # The examples below are based on the following directory hierarchy:
17262 # All the directories can contain any number of files
17263 # ROOT_DIRECTORY -> a -> aa -> aaa
17264 # -> ab
17265 # -> ac
17266 # -> b -> ba -> baa
17267 # -> bb
17268 # -> bc
17269 # This Makefile creates a variable called DIRS, that can be reused any time
17270 # you need this list (see the other examples in this section)
17271
17272 # The root of your project's directory hierarchy
17273 ROOT_DIRECTORY=.
17274
17275 ####
17276 # First method: specify explicitly the list of directories
17277 # This allows you to specify any subset of all the directories you need.
17278 ####
17279
17280 DIRS := a/aa/ a/ab/ b/ba/
17281
17282 ####
17283 # Second method: use wildcards
17284 # Note that the argument(s) to wildcard below should end with a '/'.
17285 # Since wildcards also return file names, we have to filter them out
17286 # to avoid duplicate directory names.
17287 # We thus use make's `@w{`}dir`@w{`} and `@w{`}sort`@w{`} functions.
17288 # It sets DIRs to the following value (note that the directories aaa and baa
17289 # are not given, unless you change the arguments to wildcard).
17290 # DIRS= ./a/a/ ./b/ ./a/aa/ ./a/ab/ ./a/ac/ ./b/ba/ ./b/bb/ ./b/bc/
17291 ####
17292
17293 DIRS := $@{sort $@{dir $@{wildcard $@{ROOT_DIRECTORY@}/*/
17294 $@{ROOT_DIRECTORY@}/*/*/@}@}@}
17295
17296 ####
17297 # Third method: use an external program
17298 # This command is much faster if run on local disks, avoiding NFS slowdowns.
17299 # This is the most complete command: it sets DIRs to the following value:
17300 # DIRS= ./a ./a/aa ./a/aa/aaa ./a/ab ./a/ac ./b ./b/ba ./b/ba/baa ./b/bb ./b/bc
17301 ####
17302
17303 DIRS := $@{shell find $@{ROOT_DIRECTORY@} -type d -print@}
17304 @end example
17305
17306 @node Generating the Command Line Switches,Overcoming Command Line Length Limits,Automatically Creating a List of Directories,Using the GNU make Utility
17307 @anchor{gnat_ugn/building_executable_programs_with_gnat id51}@anchor{13f}@anchor{gnat_ugn/building_executable_programs_with_gnat generating-the-command-line-switches}@anchor{140}
17308 @subsection Generating the Command Line Switches
17309
17310
17311 Once you have created the list of directories as explained in the
17312 previous section (@ref{13d,,Automatically Creating a List of Directories}),
17313 you can easily generate the command line arguments to pass to gnatmake.
17314
17315 For the sake of completeness, this example assumes that the source path
17316 is not the same as the object path, and that you have two separate lists
17317 of directories.
17318
17319 @example
17320 # see "Automatically creating a list of directories" to create
17321 # these variables
17322 SOURCE_DIRS=
17323 OBJECT_DIRS=
17324
17325 GNATMAKE_SWITCHES := $@{patsubst %,-aI%,$@{SOURCE_DIRS@}@}
17326 GNATMAKE_SWITCHES += $@{patsubst %,-aO%,$@{OBJECT_DIRS@}@}
17327
17328 all:
17329 gnatmake $@{GNATMAKE_SWITCHES@} main_unit
17330 @end example
17331
17332 @node Overcoming Command Line Length Limits,,Generating the Command Line Switches,Using the GNU make Utility
17333 @anchor{gnat_ugn/building_executable_programs_with_gnat overcoming-command-line-length-limits}@anchor{141}@anchor{gnat_ugn/building_executable_programs_with_gnat id52}@anchor{142}
17334 @subsection Overcoming Command Line Length Limits
17335
17336
17337 One problem that might be encountered on big projects is that many
17338 operating systems limit the length of the command line. It is thus hard to give
17339 gnatmake the list of source and object directories.
17340
17341 This example shows how you can set up environment variables, which will
17342 make @code{gnatmake} behave exactly as if the directories had been
17343 specified on the command line, but have a much higher length limit (or
17344 even none on most systems).
17345
17346 It assumes that you have created a list of directories in your Makefile,
17347 using one of the methods presented in
17348 @ref{13d,,Automatically Creating a List of Directories}.
17349 For the sake of completeness, we assume that the object
17350 path (where the ALI files are found) is different from the sources patch.
17351
17352 Note a small trick in the Makefile below: for efficiency reasons, we
17353 create two temporary variables (SOURCE_LIST and OBJECT_LIST), that are
17354 expanded immediately by @code{make}. This way we overcome the standard
17355 make behavior which is to expand the variables only when they are
17356 actually used.
17357
17358 On Windows, if you are using the standard Windows command shell, you must
17359 replace colons with semicolons in the assignments to these variables.
17360
17361 @example
17362 # In this example, we create both ADA_INCLUDE_PATH and ADA_OBJECTS_PATH.
17363 # This is the same thing as putting the -I arguments on the command line.
17364 # (the equivalent of using -aI on the command line would be to define
17365 # only ADA_INCLUDE_PATH, the equivalent of -aO is ADA_OBJECTS_PATH).
17366 # You can of course have different values for these variables.
17367 #
17368 # Note also that we need to keep the previous values of these variables, since
17369 # they might have been set before running 'make' to specify where the GNAT
17370 # library is installed.
17371
17372 # see "Automatically creating a list of directories" to create these
17373 # variables
17374 SOURCE_DIRS=
17375 OBJECT_DIRS=
17376
17377 empty:=
17378 space:=$@{empty@} $@{empty@}
17379 SOURCE_LIST := $@{subst $@{space@},:,$@{SOURCE_DIRS@}@}
17380 OBJECT_LIST := $@{subst $@{space@},:,$@{OBJECT_DIRS@}@}
17381 ADA_INCLUDE_PATH += $@{SOURCE_LIST@}
17382 ADA_OBJECTS_PATH += $@{OBJECT_LIST@}
17383 export ADA_INCLUDE_PATH
17384 export ADA_OBJECTS_PATH
17385
17386 all:
17387 gnatmake main_unit
17388 @end example
17389
17390 @node GNAT Utility Programs,GNAT and Program Execution,Building Executable Programs with GNAT,Top
17391 @anchor{gnat_ugn/gnat_utility_programs doc}@anchor{143}@anchor{gnat_ugn/gnat_utility_programs gnat-utility-programs}@anchor{b}@anchor{gnat_ugn/gnat_utility_programs id1}@anchor{144}
17392 @chapter GNAT Utility Programs
17393
17394
17395 This chapter describes a number of utility programs:
17396
17397
17398
17399 @itemize *
17400
17401 @item
17402 @ref{20,,The File Cleanup Utility gnatclean}
17403
17404 @item
17405 @ref{21,,The GNAT Library Browser gnatls}
17406
17407 @item
17408 @ref{22,,The Cross-Referencing Tools gnatxref and gnatfind}
17409
17410 @item
17411 @ref{23,,The Ada to HTML Converter gnathtml}
17412 @end itemize
17413
17414 Other GNAT utilities are described elsewhere in this manual:
17415
17416
17417 @itemize *
17418
17419 @item
17420 @ref{59,,Handling Arbitrary File Naming Conventions with gnatname}
17421
17422 @item
17423 @ref{63,,File Name Krunching with gnatkr}
17424
17425 @item
17426 @ref{36,,Renaming Files with gnatchop}
17427
17428 @item
17429 @ref{17,,Preprocessing with gnatprep}
17430 @end itemize
17431
17432 @menu
17433 * The File Cleanup Utility gnatclean::
17434 * The GNAT Library Browser gnatls::
17435 * The Cross-Referencing Tools gnatxref and gnatfind::
17436 * The Ada to HTML Converter gnathtml::
17437
17438 @end menu
17439
17440 @node The File Cleanup Utility gnatclean,The GNAT Library Browser gnatls,,GNAT Utility Programs
17441 @anchor{gnat_ugn/gnat_utility_programs id2}@anchor{145}@anchor{gnat_ugn/gnat_utility_programs the-file-cleanup-utility-gnatclean}@anchor{20}
17442 @section The File Cleanup Utility @code{gnatclean}
17443
17444
17445 @geindex File cleanup tool
17446
17447 @geindex gnatclean
17448
17449 @code{gnatclean} is a tool that allows the deletion of files produced by the
17450 compiler, binder and linker, including ALI files, object files, tree files,
17451 expanded source files, library files, interface copy source files, binder
17452 generated files and executable files.
17453
17454 @menu
17455 * Running gnatclean::
17456 * Switches for gnatclean::
17457
17458 @end menu
17459
17460 @node Running gnatclean,Switches for gnatclean,,The File Cleanup Utility gnatclean
17461 @anchor{gnat_ugn/gnat_utility_programs running-gnatclean}@anchor{146}@anchor{gnat_ugn/gnat_utility_programs id3}@anchor{147}
17462 @subsection Running @code{gnatclean}
17463
17464
17465 The @code{gnatclean} command has the form:
17466
17467 @quotation
17468
17469 @example
17470 $ gnatclean switches names
17471 @end example
17472 @end quotation
17473
17474 where @code{names} is a list of source file names. Suffixes @code{.ads} and
17475 @code{adb} may be omitted. If a project file is specified using switch
17476 @code{-P}, then @code{names} may be completely omitted.
17477
17478 In normal mode, @code{gnatclean} delete the files produced by the compiler and,
17479 if switch @code{-c} is not specified, by the binder and
17480 the linker. In informative-only mode, specified by switch
17481 @code{-n}, the list of files that would have been deleted in
17482 normal mode is listed, but no file is actually deleted.
17483
17484 @node Switches for gnatclean,,Running gnatclean,The File Cleanup Utility gnatclean
17485 @anchor{gnat_ugn/gnat_utility_programs id4}@anchor{148}@anchor{gnat_ugn/gnat_utility_programs switches-for-gnatclean}@anchor{149}
17486 @subsection Switches for @code{gnatclean}
17487
17488
17489 @code{gnatclean} recognizes the following switches:
17490
17491 @geindex --version (gnatclean)
17492
17493
17494 @table @asis
17495
17496 @item @code{--version}
17497
17498 Display copyright and version, then exit disregarding all other options.
17499 @end table
17500
17501 @geindex --help (gnatclean)
17502
17503
17504 @table @asis
17505
17506 @item @code{--help}
17507
17508 If @code{--version} was not used, display usage, then exit disregarding
17509 all other options.
17510
17511 @item @code{--subdirs=@emph{subdir}}
17512
17513 Actual object directory of each project file is the subdirectory subdir of the
17514 object directory specified or defaulted in the project file.
17515
17516 @item @code{--unchecked-shared-lib-imports}
17517
17518 By default, shared library projects are not allowed to import static library
17519 projects. When this switch is used on the command line, this restriction is
17520 relaxed.
17521 @end table
17522
17523 @geindex -c (gnatclean)
17524
17525
17526 @table @asis
17527
17528 @item @code{-c}
17529
17530 Only attempt to delete the files produced by the compiler, not those produced
17531 by the binder or the linker. The files that are not to be deleted are library
17532 files, interface copy files, binder generated files and executable files.
17533 @end table
17534
17535 @geindex -D (gnatclean)
17536
17537
17538 @table @asis
17539
17540 @item @code{-D @emph{dir}}
17541
17542 Indicate that ALI and object files should normally be found in directory @code{dir}.
17543 @end table
17544
17545 @geindex -F (gnatclean)
17546
17547
17548 @table @asis
17549
17550 @item @code{-F}
17551
17552 When using project files, if some errors or warnings are detected during
17553 parsing and verbose mode is not in effect (no use of switch
17554 -v), then error lines start with the full path name of the project
17555 file, rather than its simple file name.
17556 @end table
17557
17558 @geindex -h (gnatclean)
17559
17560
17561 @table @asis
17562
17563 @item @code{-h}
17564
17565 Output a message explaining the usage of @code{gnatclean}.
17566 @end table
17567
17568 @geindex -n (gnatclean)
17569
17570
17571 @table @asis
17572
17573 @item @code{-n}
17574
17575 Informative-only mode. Do not delete any files. Output the list of the files
17576 that would have been deleted if this switch was not specified.
17577 @end table
17578
17579 @geindex -P (gnatclean)
17580
17581
17582 @table @asis
17583
17584 @item @code{-P@emph{project}}
17585
17586 Use project file @code{project}. Only one such switch can be used.
17587 When cleaning a project file, the files produced by the compilation of the
17588 immediate sources or inherited sources of the project files are to be
17589 deleted. This is not depending on the presence or not of executable names
17590 on the command line.
17591 @end table
17592
17593 @geindex -q (gnatclean)
17594
17595
17596 @table @asis
17597
17598 @item @code{-q}
17599
17600 Quiet output. If there are no errors, do not output anything, except in
17601 verbose mode (switch -v) or in informative-only mode
17602 (switch -n).
17603 @end table
17604
17605 @geindex -r (gnatclean)
17606
17607
17608 @table @asis
17609
17610 @item @code{-r}
17611
17612 When a project file is specified (using switch -P),
17613 clean all imported and extended project files, recursively. If this switch
17614 is not specified, only the files related to the main project file are to be
17615 deleted. This switch has no effect if no project file is specified.
17616 @end table
17617
17618 @geindex -v (gnatclean)
17619
17620
17621 @table @asis
17622
17623 @item @code{-v}
17624
17625 Verbose mode.
17626 @end table
17627
17628 @geindex -vP (gnatclean)
17629
17630
17631 @table @asis
17632
17633 @item @code{-vP@emph{x}}
17634
17635 Indicates the verbosity of the parsing of GNAT project files.
17636 @ref{de,,Switches Related to Project Files}.
17637 @end table
17638
17639 @geindex -X (gnatclean)
17640
17641
17642 @table @asis
17643
17644 @item @code{-X@emph{name}=@emph{value}}
17645
17646 Indicates that external variable @code{name} has the value @code{value}.
17647 The Project Manager will use this value for occurrences of
17648 @code{external(name)} when parsing the project file.
17649 See @ref{de,,Switches Related to Project Files}.
17650 @end table
17651
17652 @geindex -aO (gnatclean)
17653
17654
17655 @table @asis
17656
17657 @item @code{-aO@emph{dir}}
17658
17659 When searching for ALI and object files, look in directory @code{dir}.
17660 @end table
17661
17662 @geindex -I (gnatclean)
17663
17664
17665 @table @asis
17666
17667 @item @code{-I@emph{dir}}
17668
17669 Equivalent to @code{-aO@emph{dir}}.
17670 @end table
17671
17672 @geindex -I- (gnatclean)
17673
17674 @geindex Source files
17675 @geindex suppressing search
17676
17677
17678 @table @asis
17679
17680 @item @code{-I-}
17681
17682 Do not look for ALI or object files in the directory
17683 where @code{gnatclean} was invoked.
17684 @end table
17685
17686 @node The GNAT Library Browser gnatls,The Cross-Referencing Tools gnatxref and gnatfind,The File Cleanup Utility gnatclean,GNAT Utility Programs
17687 @anchor{gnat_ugn/gnat_utility_programs the-gnat-library-browser-gnatls}@anchor{21}@anchor{gnat_ugn/gnat_utility_programs id5}@anchor{14a}
17688 @section The GNAT Library Browser @code{gnatls}
17689
17690
17691 @geindex Library browser
17692
17693 @geindex gnatls
17694
17695 @code{gnatls} is a tool that outputs information about compiled
17696 units. It gives the relationship between objects, unit names and source
17697 files. It can also be used to check the source dependencies of a unit
17698 as well as various characteristics.
17699
17700 @menu
17701 * Running gnatls::
17702 * Switches for gnatls::
17703 * Example of gnatls Usage::
17704
17705 @end menu
17706
17707 @node Running gnatls,Switches for gnatls,,The GNAT Library Browser gnatls
17708 @anchor{gnat_ugn/gnat_utility_programs id6}@anchor{14b}@anchor{gnat_ugn/gnat_utility_programs running-gnatls}@anchor{14c}
17709 @subsection Running @code{gnatls}
17710
17711
17712 The @code{gnatls} command has the form
17713
17714 @quotation
17715
17716 @example
17717 $ gnatls switches object_or_ali_file
17718 @end example
17719 @end quotation
17720
17721 The main argument is the list of object or @code{ali} files
17722 (see @ref{42,,The Ada Library Information Files})
17723 for which information is requested.
17724
17725 In normal mode, without additional option, @code{gnatls} produces a
17726 four-column listing. Each line represents information for a specific
17727 object. The first column gives the full path of the object, the second
17728 column gives the name of the principal unit in this object, the third
17729 column gives the status of the source and the fourth column gives the
17730 full path of the source representing this unit.
17731 Here is a simple example of use:
17732
17733 @quotation
17734
17735 @example
17736 $ gnatls *.o
17737 ./demo1.o demo1 DIF demo1.adb
17738 ./demo2.o demo2 OK demo2.adb
17739 ./hello.o h1 OK hello.adb
17740 ./instr-child.o instr.child MOK instr-child.adb
17741 ./instr.o instr OK instr.adb
17742 ./tef.o tef DIF tef.adb
17743 ./text_io_example.o text_io_example OK text_io_example.adb
17744 ./tgef.o tgef DIF tgef.adb
17745 @end example
17746 @end quotation
17747
17748 The first line can be interpreted as follows: the main unit which is
17749 contained in
17750 object file @code{demo1.o} is demo1, whose main source is in
17751 @code{demo1.adb}. Furthermore, the version of the source used for the
17752 compilation of demo1 has been modified (DIF). Each source file has a status
17753 qualifier which can be:
17754
17755
17756 @table @asis
17757
17758 @item @emph{OK (unchanged)}
17759
17760 The version of the source file used for the compilation of the
17761 specified unit corresponds exactly to the actual source file.
17762
17763 @item @emph{MOK (slightly modified)}
17764
17765 The version of the source file used for the compilation of the
17766 specified unit differs from the actual source file but not enough to
17767 require recompilation. If you use gnatmake with the option
17768 @code{-m} (minimal recompilation), a file marked
17769 MOK will not be recompiled.
17770
17771 @item @emph{DIF (modified)}
17772
17773 No version of the source found on the path corresponds to the source
17774 used to build this object.
17775
17776 @item @emph{??? (file not found)}
17777
17778 No source file was found for this unit.
17779
17780 @item @emph{HID (hidden, unchanged version not first on PATH)}
17781
17782 The version of the source that corresponds exactly to the source used
17783 for compilation has been found on the path but it is hidden by another
17784 version of the same source that has been modified.
17785 @end table
17786
17787 @node Switches for gnatls,Example of gnatls Usage,Running gnatls,The GNAT Library Browser gnatls
17788 @anchor{gnat_ugn/gnat_utility_programs id7}@anchor{14d}@anchor{gnat_ugn/gnat_utility_programs switches-for-gnatls}@anchor{14e}
17789 @subsection Switches for @code{gnatls}
17790
17791
17792 @code{gnatls} recognizes the following switches:
17793
17794 @geindex --version (gnatls)
17795
17796
17797 @table @asis
17798
17799 @item @code{--version}
17800
17801 Display copyright and version, then exit disregarding all other options.
17802 @end table
17803
17804 @geindex --help (gnatls)
17805
17806
17807 @table @asis
17808
17809 @item @code{--help}
17810
17811 If @code{--version} was not used, display usage, then exit disregarding
17812 all other options.
17813 @end table
17814
17815 @geindex -a (gnatls)
17816
17817
17818 @table @asis
17819
17820 @item @code{-a}
17821
17822 Consider all units, including those of the predefined Ada library.
17823 Especially useful with @code{-d}.
17824 @end table
17825
17826 @geindex -d (gnatls)
17827
17828
17829 @table @asis
17830
17831 @item @code{-d}
17832
17833 List sources from which specified units depend on.
17834 @end table
17835
17836 @geindex -h (gnatls)
17837
17838
17839 @table @asis
17840
17841 @item @code{-h}
17842
17843 Output the list of options.
17844 @end table
17845
17846 @geindex -o (gnatls)
17847
17848
17849 @table @asis
17850
17851 @item @code{-o}
17852
17853 Only output information about object files.
17854 @end table
17855
17856 @geindex -s (gnatls)
17857
17858
17859 @table @asis
17860
17861 @item @code{-s}
17862
17863 Only output information about source files.
17864 @end table
17865
17866 @geindex -u (gnatls)
17867
17868
17869 @table @asis
17870
17871 @item @code{-u}
17872
17873 Only output information about compilation units.
17874 @end table
17875
17876 @geindex -files (gnatls)
17877
17878
17879 @table @asis
17880
17881 @item @code{-files=@emph{file}}
17882
17883 Take as arguments the files listed in text file @code{file}.
17884 Text file @code{file} may contain empty lines that are ignored.
17885 Each nonempty line should contain the name of an existing file.
17886 Several such switches may be specified simultaneously.
17887 @end table
17888
17889 @geindex -aO (gnatls)
17890
17891 @geindex -aI (gnatls)
17892
17893 @geindex -I (gnatls)
17894
17895 @geindex -I- (gnatls)
17896
17897
17898 @table @asis
17899
17900 @item @code{-aO@emph{dir}}, @code{-aI@emph{dir}}, @code{-I@emph{dir}}, @code{-I-}, @code{-nostdinc}
17901
17902 Source path manipulation. Same meaning as the equivalent @code{gnatmake}
17903 flags (@ref{dc,,Switches for gnatmake}).
17904 @end table
17905
17906 @geindex -aP (gnatls)
17907
17908
17909 @table @asis
17910
17911 @item @code{-aP@emph{dir}}
17912
17913 Add @code{dir} at the beginning of the project search dir.
17914 @end table
17915
17916 @geindex --RTS (gnatls)
17917
17918
17919 @table @asis
17920
17921 @item @code{--RTS=@emph{rts-path}}
17922
17923 Specifies the default location of the runtime library. Same meaning as the
17924 equivalent @code{gnatmake} flag (@ref{dc,,Switches for gnatmake}).
17925 @end table
17926
17927 @geindex -v (gnatls)
17928
17929
17930 @table @asis
17931
17932 @item @code{-v}
17933
17934 Verbose mode. Output the complete source, object and project paths. Do not use
17935 the default column layout but instead use long format giving as much as
17936 information possible on each requested units, including special
17937 characteristics such as:
17938
17939
17940 @itemize *
17941
17942 @item
17943 @emph{Preelaborable}: The unit is preelaborable in the Ada sense.
17944
17945 @item
17946 @emph{No_Elab_Code}: No elaboration code has been produced by the compiler for this unit.
17947
17948 @item
17949 @emph{Pure}: The unit is pure in the Ada sense.
17950
17951 @item
17952 @emph{Elaborate_Body}: The unit contains a pragma Elaborate_Body.
17953
17954 @item
17955 @emph{Remote_Types}: The unit contains a pragma Remote_Types.
17956
17957 @item
17958 @emph{Shared_Passive}: The unit contains a pragma Shared_Passive.
17959
17960 @item
17961 @emph{Predefined}: This unit is part of the predefined environment and cannot be modified
17962 by the user.
17963
17964 @item
17965 @emph{Remote_Call_Interface}: The unit contains a pragma Remote_Call_Interface.
17966 @end itemize
17967 @end table
17968
17969 @node Example of gnatls Usage,,Switches for gnatls,The GNAT Library Browser gnatls
17970 @anchor{gnat_ugn/gnat_utility_programs id8}@anchor{14f}@anchor{gnat_ugn/gnat_utility_programs example-of-gnatls-usage}@anchor{150}
17971 @subsection Example of @code{gnatls} Usage
17972
17973
17974 Example of using the verbose switch. Note how the source and
17975 object paths are affected by the -I switch.
17976
17977 @quotation
17978
17979 @example
17980 $ gnatls -v -I.. demo1.o
17981
17982 GNATLS 5.03w (20041123-34)
17983 Copyright 1997-2004 Free Software Foundation, Inc.
17984
17985 Source Search Path:
17986 <Current_Directory>
17987 ../
17988 /home/comar/local/adainclude/
17989
17990 Object Search Path:
17991 <Current_Directory>
17992 ../
17993 /home/comar/local/lib/gcc-lib/x86-linux/3.4.3/adalib/
17994
17995 Project Search Path:
17996 <Current_Directory>
17997 /home/comar/local/lib/gnat/
17998
17999 ./demo1.o
18000 Unit =>
18001 Name => demo1
18002 Kind => subprogram body
18003 Flags => No_Elab_Code
18004 Source => demo1.adb modified
18005 @end example
18006 @end quotation
18007
18008 The following is an example of use of the dependency list.
18009 Note the use of the -s switch
18010 which gives a straight list of source files. This can be useful for
18011 building specialized scripts.
18012
18013 @quotation
18014
18015 @example
18016 $ gnatls -d demo2.o
18017 ./demo2.o demo2 OK demo2.adb
18018 OK gen_list.ads
18019 OK gen_list.adb
18020 OK instr.ads
18021 OK instr-child.ads
18022
18023 $ gnatls -d -s -a demo1.o
18024 demo1.adb
18025 /home/comar/local/adainclude/ada.ads
18026 /home/comar/local/adainclude/a-finali.ads
18027 /home/comar/local/adainclude/a-filico.ads
18028 /home/comar/local/adainclude/a-stream.ads
18029 /home/comar/local/adainclude/a-tags.ads
18030 gen_list.ads
18031 gen_list.adb
18032 /home/comar/local/adainclude/gnat.ads
18033 /home/comar/local/adainclude/g-io.ads
18034 instr.ads
18035 /home/comar/local/adainclude/system.ads
18036 /home/comar/local/adainclude/s-exctab.ads
18037 /home/comar/local/adainclude/s-finimp.ads
18038 /home/comar/local/adainclude/s-finroo.ads
18039 /home/comar/local/adainclude/s-secsta.ads
18040 /home/comar/local/adainclude/s-stalib.ads
18041 /home/comar/local/adainclude/s-stoele.ads
18042 /home/comar/local/adainclude/s-stratt.ads
18043 /home/comar/local/adainclude/s-tasoli.ads
18044 /home/comar/local/adainclude/s-unstyp.ads
18045 /home/comar/local/adainclude/unchconv.ads
18046 @end example
18047 @end quotation
18048
18049 @node The Cross-Referencing Tools gnatxref and gnatfind,The Ada to HTML Converter gnathtml,The GNAT Library Browser gnatls,GNAT Utility Programs
18050 @anchor{gnat_ugn/gnat_utility_programs the-cross-referencing-tools-gnatxref-and-gnatfind}@anchor{22}@anchor{gnat_ugn/gnat_utility_programs id9}@anchor{151}
18051 @section The Cross-Referencing Tools @code{gnatxref} and @code{gnatfind}
18052
18053
18054 @geindex gnatxref
18055
18056 @geindex gnatfind
18057
18058 The compiler generates cross-referencing information (unless
18059 you set the @code{-gnatx} switch), which are saved in the @code{.ali} files.
18060 This information indicates where in the source each entity is declared and
18061 referenced. Note that entities in package Standard are not included, but
18062 entities in all other predefined units are included in the output.
18063
18064 Before using any of these two tools, you need to compile successfully your
18065 application, so that GNAT gets a chance to generate the cross-referencing
18066 information.
18067
18068 The two tools @code{gnatxref} and @code{gnatfind} take advantage of this
18069 information to provide the user with the capability to easily locate the
18070 declaration and references to an entity. These tools are quite similar,
18071 the difference being that @code{gnatfind} is intended for locating
18072 definitions and/or references to a specified entity or entities, whereas
18073 @code{gnatxref} is oriented to generating a full report of all
18074 cross-references.
18075
18076 To use these tools, you must not compile your application using the
18077 @code{-gnatx} switch on the @code{gnatmake} command line
18078 (see @ref{1b,,Building with gnatmake}). Otherwise, cross-referencing
18079 information will not be generated.
18080
18081 @menu
18082 * gnatxref Switches::
18083 * gnatfind Switches::
18084 * Configuration Files for gnatxref and gnatfind::
18085 * Regular Expressions in gnatfind and gnatxref::
18086 * Examples of gnatxref Usage::
18087 * Examples of gnatfind Usage::
18088
18089 @end menu
18090
18091 @node gnatxref Switches,gnatfind Switches,,The Cross-Referencing Tools gnatxref and gnatfind
18092 @anchor{gnat_ugn/gnat_utility_programs id10}@anchor{152}@anchor{gnat_ugn/gnat_utility_programs gnatxref-switches}@anchor{153}
18093 @subsection @code{gnatxref} Switches
18094
18095
18096 The command invocation for @code{gnatxref} is:
18097
18098 @quotation
18099
18100 @example
18101 $ gnatxref [ switches ] sourcefile1 [ sourcefile2 ... ]
18102 @end example
18103 @end quotation
18104
18105 where
18106
18107
18108 @table @asis
18109
18110 @item @code{sourcefile1} [, @code{sourcefile2} ...]
18111
18112 identify the source files for which a report is to be generated. The
18113 @code{with}ed units will be processed too. You must provide at least one file.
18114
18115 These file names are considered to be regular expressions, so for instance
18116 specifying @code{source*.adb} is the same as giving every file in the current
18117 directory whose name starts with @code{source} and whose extension is
18118 @code{adb}.
18119
18120 You shouldn't specify any directory name, just base names. @code{gnatxref}
18121 and @code{gnatfind} will be able to locate these files by themselves using
18122 the source path. If you specify directories, no result is produced.
18123 @end table
18124
18125 The following switches are available for @code{gnatxref}:
18126
18127 @geindex --version (gnatxref)
18128
18129
18130 @table @asis
18131
18132 @item @code{--version}
18133
18134 Display copyright and version, then exit disregarding all other options.
18135 @end table
18136
18137 @geindex --help (gnatxref)
18138
18139
18140 @table @asis
18141
18142 @item @code{--help}
18143
18144 If @code{--version} was not used, display usage, then exit disregarding
18145 all other options.
18146 @end table
18147
18148 @geindex -a (gnatxref)
18149
18150
18151 @table @asis
18152
18153 @item @code{-a}
18154
18155 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
18156 the read-only files found in the library search path. Otherwise, these files
18157 will be ignored. This option can be used to protect Gnat sources or your own
18158 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
18159 much faster, and their output much smaller. Read-only here refers to access
18160 or permissions status in the file system for the current user.
18161 @end table
18162
18163 @geindex -aIDIR (gnatxref)
18164
18165
18166 @table @asis
18167
18168 @item @code{-aI@emph{DIR}}
18169
18170 When looking for source files also look in directory DIR. The order in which
18171 source file search is undertaken is the same as for @code{gnatmake}.
18172 @end table
18173
18174 @geindex -aODIR (gnatxref)
18175
18176
18177 @table @asis
18178
18179 @item @code{aO@emph{DIR}}
18180
18181 When -searching for library and object files, look in directory
18182 DIR. The order in which library files are searched is the same as for
18183 @code{gnatmake}.
18184 @end table
18185
18186 @geindex -nostdinc (gnatxref)
18187
18188
18189 @table @asis
18190
18191 @item @code{-nostdinc}
18192
18193 Do not look for sources in the system default directory.
18194 @end table
18195
18196 @geindex -nostdlib (gnatxref)
18197
18198
18199 @table @asis
18200
18201 @item @code{-nostdlib}
18202
18203 Do not look for library files in the system default directory.
18204 @end table
18205
18206 @geindex --ext (gnatxref)
18207
18208
18209 @table @asis
18210
18211 @item @code{--ext=@emph{extension}}
18212
18213 Specify an alternate ali file extension. The default is @code{ali} and other
18214 extensions (e.g. @code{gli} for C/C++ sources) may be specified via this switch.
18215 Note that if this switch overrides the default, only the new extension will
18216 be considered.
18217 @end table
18218
18219 @geindex --RTS (gnatxref)
18220
18221
18222 @table @asis
18223
18224 @item @code{--RTS=@emph{rts-path}}
18225
18226 Specifies the default location of the runtime library. Same meaning as the
18227 equivalent @code{gnatmake} flag (@ref{dc,,Switches for gnatmake}).
18228 @end table
18229
18230 @geindex -d (gnatxref)
18231
18232
18233 @table @asis
18234
18235 @item @code{-d}
18236
18237 If this switch is set @code{gnatxref} will output the parent type
18238 reference for each matching derived types.
18239 @end table
18240
18241 @geindex -f (gnatxref)
18242
18243
18244 @table @asis
18245
18246 @item @code{-f}
18247
18248 If this switch is set, the output file names will be preceded by their
18249 directory (if the file was found in the search path). If this switch is
18250 not set, the directory will not be printed.
18251 @end table
18252
18253 @geindex -g (gnatxref)
18254
18255
18256 @table @asis
18257
18258 @item @code{-g}
18259
18260 If this switch is set, information is output only for library-level
18261 entities, ignoring local entities. The use of this switch may accelerate
18262 @code{gnatfind} and @code{gnatxref}.
18263 @end table
18264
18265 @geindex -IDIR (gnatxref)
18266
18267
18268 @table @asis
18269
18270 @item @code{-I@emph{DIR}}
18271
18272 Equivalent to @code{-aODIR -aIDIR}.
18273 @end table
18274
18275 @geindex -pFILE (gnatxref)
18276
18277
18278 @table @asis
18279
18280 @item @code{-p@emph{FILE}}
18281
18282 Specify a configuration file to use to list the source and object directories.
18283
18284 If a file is specified, then the content of the source directory and object
18285 directory lines are added as if they had been specified respectively
18286 by @code{-aI} and @code{-aO}.
18287
18288 See @ref{154,,Configuration Files for gnatxref and gnatfind} for the syntax
18289 of this configuration file.
18290
18291 @item @code{-u}
18292
18293 Output only unused symbols. This may be really useful if you give your
18294 main compilation unit on the command line, as @code{gnatxref} will then
18295 display every unused entity and 'with'ed package.
18296
18297 @item @code{-v}
18298
18299 Instead of producing the default output, @code{gnatxref} will generate a
18300 @code{tags} file that can be used by vi. For examples how to use this
18301 feature, see @ref{155,,Examples of gnatxref Usage}. The tags file is output
18302 to the standard output, thus you will have to redirect it to a file.
18303 @end table
18304
18305 All these switches may be in any order on the command line, and may even
18306 appear after the file names. They need not be separated by spaces, thus
18307 you can say @code{gnatxref -ag} instead of @code{gnatxref -a -g}.
18308
18309 @node gnatfind Switches,Configuration Files for gnatxref and gnatfind,gnatxref Switches,The Cross-Referencing Tools gnatxref and gnatfind
18310 @anchor{gnat_ugn/gnat_utility_programs id11}@anchor{156}@anchor{gnat_ugn/gnat_utility_programs gnatfind-switches}@anchor{157}
18311 @subsection @code{gnatfind} Switches
18312
18313
18314 The command invocation for @code{gnatfind} is:
18315
18316 @quotation
18317
18318 @example
18319 $ gnatfind [ switches ] pattern[:sourcefile[:line[:column]]]
18320 [file1 file2 ...]
18321 @end example
18322 @end quotation
18323
18324 with the following iterpretation of the command arguments:
18325
18326
18327 @table @asis
18328
18329 @item @emph{pattern}
18330
18331 An entity will be output only if it matches the regular expression found
18332 in @emph{pattern}, see @ref{158,,Regular Expressions in gnatfind and gnatxref}.
18333
18334 Omitting the pattern is equivalent to specifying @code{*}, which
18335 will match any entity. Note that if you do not provide a pattern, you
18336 have to provide both a sourcefile and a line.
18337
18338 Entity names are given in Latin-1, with uppercase/lowercase equivalence
18339 for matching purposes. At the current time there is no support for
18340 8-bit codes other than Latin-1, or for wide characters in identifiers.
18341
18342 @item @emph{sourcefile}
18343
18344 @code{gnatfind} will look for references, bodies or declarations
18345 of symbols referenced in @code{sourcefile}, at line @code{line}
18346 and column @code{column}. See @ref{159,,Examples of gnatfind Usage}
18347 for syntax examples.
18348
18349 @item @emph{line}
18350
18351 A decimal integer identifying the line number containing
18352 the reference to the entity (or entities) to be located.
18353
18354 @item @emph{column}
18355
18356 A decimal integer identifying the exact location on the
18357 line of the first character of the identifier for the
18358 entity reference. Columns are numbered from 1.
18359
18360 @item @emph{file1 file2 ...}
18361
18362 The search will be restricted to these source files. If none are given, then
18363 the search will be conducted for every library file in the search path.
18364 These files must appear only after the pattern or sourcefile.
18365
18366 These file names are considered to be regular expressions, so for instance
18367 specifying @code{source*.adb} is the same as giving every file in the current
18368 directory whose name starts with @code{source} and whose extension is
18369 @code{adb}.
18370
18371 The location of the spec of the entity will always be displayed, even if it
18372 isn't in one of @code{file1}, @code{file2}, ... The
18373 occurrences of the entity in the separate units of the ones given on the
18374 command line will also be displayed.
18375
18376 Note that if you specify at least one file in this part, @code{gnatfind} may
18377 sometimes not be able to find the body of the subprograms.
18378 @end table
18379
18380 At least one of 'sourcefile' or 'pattern' has to be present on
18381 the command line.
18382
18383 The following switches are available:
18384
18385 @geindex --version (gnatfind)
18386
18387
18388 @table @asis
18389
18390 @item @code{--version}
18391
18392 Display copyright and version, then exit disregarding all other options.
18393 @end table
18394
18395 @geindex --help (gnatfind)
18396
18397
18398 @table @asis
18399
18400 @item @code{--help}
18401
18402 If @code{--version} was not used, display usage, then exit disregarding
18403 all other options.
18404 @end table
18405
18406 @geindex -a (gnatfind)
18407
18408
18409 @table @asis
18410
18411 @item @code{-a}
18412
18413 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
18414 the read-only files found in the library search path. Otherwise, these files
18415 will be ignored. This option can be used to protect Gnat sources or your own
18416 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
18417 much faster, and their output much smaller. Read-only here refers to access
18418 or permission status in the file system for the current user.
18419 @end table
18420
18421 @geindex -aIDIR (gnatfind)
18422
18423
18424 @table @asis
18425
18426 @item @code{-aI@emph{DIR}}
18427
18428 When looking for source files also look in directory DIR. The order in which
18429 source file search is undertaken is the same as for @code{gnatmake}.
18430 @end table
18431
18432 @geindex -aODIR (gnatfind)
18433
18434
18435 @table @asis
18436
18437 @item @code{-aO@emph{DIR}}
18438
18439 When searching for library and object files, look in directory
18440 DIR. The order in which library files are searched is the same as for
18441 @code{gnatmake}.
18442 @end table
18443
18444 @geindex -nostdinc (gnatfind)
18445
18446
18447 @table @asis
18448
18449 @item @code{-nostdinc}
18450
18451 Do not look for sources in the system default directory.
18452 @end table
18453
18454 @geindex -nostdlib (gnatfind)
18455
18456
18457 @table @asis
18458
18459 @item @code{-nostdlib}
18460
18461 Do not look for library files in the system default directory.
18462 @end table
18463
18464 @geindex --ext (gnatfind)
18465
18466
18467 @table @asis
18468
18469 @item @code{--ext=@emph{extension}}
18470
18471 Specify an alternate ali file extension. The default is @code{ali} and other
18472 extensions may be specified via this switch. Note that if this switch
18473 overrides the default, only the new extension will be considered.
18474 @end table
18475
18476 @geindex --RTS (gnatfind)
18477
18478
18479 @table @asis
18480
18481 @item @code{--RTS=@emph{rts-path}}
18482
18483 Specifies the default location of the runtime library. Same meaning as the
18484 equivalent @code{gnatmake} flag (@ref{dc,,Switches for gnatmake}).
18485 @end table
18486
18487 @geindex -d (gnatfind)
18488
18489
18490 @table @asis
18491
18492 @item @code{-d}
18493
18494 If this switch is set, then @code{gnatfind} will output the parent type
18495 reference for each matching derived types.
18496 @end table
18497
18498 @geindex -e (gnatfind)
18499
18500
18501 @table @asis
18502
18503 @item @code{-e}
18504
18505 By default, @code{gnatfind} accept the simple regular expression set for
18506 @code{pattern}. If this switch is set, then the pattern will be
18507 considered as full Unix-style regular expression.
18508 @end table
18509
18510 @geindex -f (gnatfind)
18511
18512
18513 @table @asis
18514
18515 @item @code{-f}
18516
18517 If this switch is set, the output file names will be preceded by their
18518 directory (if the file was found in the search path). If this switch is
18519 not set, the directory will not be printed.
18520 @end table
18521
18522 @geindex -g (gnatfind)
18523
18524
18525 @table @asis
18526
18527 @item @code{-g}
18528
18529 If this switch is set, information is output only for library-level
18530 entities, ignoring local entities. The use of this switch may accelerate
18531 @code{gnatfind} and @code{gnatxref}.
18532 @end table
18533
18534 @geindex -IDIR (gnatfind)
18535
18536
18537 @table @asis
18538
18539 @item @code{-I@emph{DIR}}
18540
18541 Equivalent to @code{-aODIR -aIDIR}.
18542 @end table
18543
18544 @geindex -pFILE (gnatfind)
18545
18546
18547 @table @asis
18548
18549 @item @code{-p@emph{FILE}}
18550
18551 Specify a configuration file to use to list the source and object directories.
18552
18553 If a file is specified, then the content of the source directory and object
18554 directory lines are added as if they had been specified respectively
18555 by @code{-aI} and @code{-aO}.
18556
18557 See @ref{154,,Configuration Files for gnatxref and gnatfind} for the syntax
18558 of this configuration file.
18559 @end table
18560
18561 @geindex -r (gnatfind)
18562
18563
18564 @table @asis
18565
18566 @item @code{-r}
18567
18568 By default, @code{gnatfind} will output only the information about the
18569 declaration, body or type completion of the entities. If this switch is
18570 set, the @code{gnatfind} will locate every reference to the entities in
18571 the files specified on the command line (or in every file in the search
18572 path if no file is given on the command line).
18573 @end table
18574
18575 @geindex -s (gnatfind)
18576
18577
18578 @table @asis
18579
18580 @item @code{-s}
18581
18582 If this switch is set, then @code{gnatfind} will output the content
18583 of the Ada source file lines were the entity was found.
18584 @end table
18585
18586 @geindex -t (gnatfind)
18587
18588
18589 @table @asis
18590
18591 @item @code{-t}
18592
18593 If this switch is set, then @code{gnatfind} will output the type hierarchy for
18594 the specified type. It act like -d option but recursively from parent
18595 type to parent type. When this switch is set it is not possible to
18596 specify more than one file.
18597 @end table
18598
18599 All these switches may be in any order on the command line, and may even
18600 appear after the file names. They need not be separated by spaces, thus
18601 you can say @code{gnatxref -ag} instead of
18602 @code{gnatxref -a -g}.
18603
18604 As stated previously, @code{gnatfind} will search in every directory in the
18605 search path. You can force it to look only in the current directory if
18606 you specify @code{*} at the end of the command line.
18607
18608 @node Configuration Files for gnatxref and gnatfind,Regular Expressions in gnatfind and gnatxref,gnatfind Switches,The Cross-Referencing Tools gnatxref and gnatfind
18609 @anchor{gnat_ugn/gnat_utility_programs configuration-files-for-gnatxref-and-gnatfind}@anchor{154}@anchor{gnat_ugn/gnat_utility_programs id12}@anchor{15a}
18610 @subsection Configuration Files for @code{gnatxref} and @code{gnatfind}
18611
18612
18613 Configuration files are used by @code{gnatxref} and @code{gnatfind} to specify
18614 the list of source and object directories to consider. They can be
18615 specified via the @code{-p} switch.
18616
18617 The following lines can be included, in any order in the file:
18618
18619
18620 @itemize *
18621
18622 @item
18623
18624 @table @asis
18625
18626 @item @emph{src_dir=DIR}
18627
18628 [default: @code{"./"}].
18629 Specifies a directory where to look for source files. Multiple @code{src_dir}
18630 lines can be specified and they will be searched in the order they
18631 are specified.
18632 @end table
18633
18634 @item
18635
18636 @table @asis
18637
18638 @item @emph{obj_dir=DIR}
18639
18640 [default: @code{"./"}].
18641 Specifies a directory where to look for object and library files. Multiple
18642 @code{obj_dir} lines can be specified, and they will be searched in the order
18643 they are specified
18644 @end table
18645 @end itemize
18646
18647 Any other line will be silently ignored.
18648
18649 @node Regular Expressions in gnatfind and gnatxref,Examples of gnatxref Usage,Configuration Files for gnatxref and gnatfind,The Cross-Referencing Tools gnatxref and gnatfind
18650 @anchor{gnat_ugn/gnat_utility_programs id13}@anchor{15b}@anchor{gnat_ugn/gnat_utility_programs regular-expressions-in-gnatfind-and-gnatxref}@anchor{158}
18651 @subsection Regular Expressions in @code{gnatfind} and @code{gnatxref}
18652
18653
18654 As specified in the section about @code{gnatfind}, the pattern can be a
18655 regular expression. Two kinds of regular expressions
18656 are recognized:
18657
18658
18659 @itemize *
18660
18661 @item
18662
18663 @table @asis
18664
18665 @item @emph{Globbing pattern}
18666
18667 These are the most common regular expression. They are the same as are
18668 generally used in a Unix shell command line, or in a DOS session.
18669
18670 Here is a more formal grammar:
18671
18672 @example
18673 regexp ::= term
18674 term ::= elmt -- matches elmt
18675 term ::= elmt elmt -- concatenation (elmt then elmt)
18676 term ::= * -- any string of 0 or more characters
18677 term ::= ? -- matches any character
18678 term ::= [char @{char@}] -- matches any character listed
18679 term ::= [char - char] -- matches any character in range
18680 @end example
18681 @end table
18682
18683 @item
18684
18685 @table @asis
18686
18687 @item @emph{Full regular expression}
18688
18689 The second set of regular expressions is much more powerful. This is the
18690 type of regular expressions recognized by utilities such as @code{grep}.
18691
18692 The following is the form of a regular expression, expressed in same BNF
18693 style as is found in the Ada Reference Manual:
18694
18695 @example
18696 regexp ::= term @{| term@} -- alternation (term or term ...)
18697
18698 term ::= item @{item@} -- concatenation (item then item)
18699
18700 item ::= elmt -- match elmt
18701 item ::= elmt * -- zero or more elmt's
18702 item ::= elmt + -- one or more elmt's
18703 item ::= elmt ? -- matches elmt or nothing
18704
18705 elmt ::= nschar -- matches given character
18706 elmt ::= [nschar @{nschar@}] -- matches any character listed
18707 elmt ::= [^ nschar @{nschar@}] -- matches any character not listed
18708 elmt ::= [char - char] -- matches chars in given range
18709 elmt ::= \\ char -- matches given character
18710 elmt ::= . -- matches any single character
18711 elmt ::= ( regexp ) -- parens used for grouping
18712
18713 char ::= any character, including special characters
18714 nschar ::= any character except ()[].*+?^
18715 @end example
18716
18717 Here are a few examples:
18718
18719 @quotation
18720
18721
18722 @table @asis
18723
18724 @item @code{abcde|fghi}
18725
18726 will match any of the two strings @code{abcde} and @code{fghi},
18727
18728 @item @code{abc*d}
18729
18730 will match any string like @code{abd}, @code{abcd}, @code{abccd},
18731 @code{abcccd}, and so on,
18732
18733 @item @code{[a-z]+}
18734
18735 will match any string which has only lowercase characters in it (and at
18736 least one character.
18737 @end table
18738 @end quotation
18739 @end table
18740 @end itemize
18741
18742 @node Examples of gnatxref Usage,Examples of gnatfind Usage,Regular Expressions in gnatfind and gnatxref,The Cross-Referencing Tools gnatxref and gnatfind
18743 @anchor{gnat_ugn/gnat_utility_programs examples-of-gnatxref-usage}@anchor{155}@anchor{gnat_ugn/gnat_utility_programs id14}@anchor{15c}
18744 @subsection Examples of @code{gnatxref} Usage
18745
18746
18747 @menu
18748 * General Usage::
18749 * Using gnatxref with vi::
18750
18751 @end menu
18752
18753 @node General Usage,Using gnatxref with vi,,Examples of gnatxref Usage
18754 @anchor{gnat_ugn/gnat_utility_programs general-usage}@anchor{15d}
18755 @subsubsection General Usage
18756
18757
18758 For the following examples, we will consider the following units:
18759
18760 @quotation
18761
18762 @example
18763 main.ads:
18764 1: with Bar;
18765 2: package Main is
18766 3: procedure Foo (B : in Integer);
18767 4: C : Integer;
18768 5: private
18769 6: D : Integer;
18770 7: end Main;
18771
18772 main.adb:
18773 1: package body Main is
18774 2: procedure Foo (B : in Integer) is
18775 3: begin
18776 4: C := B;
18777 5: D := B;
18778 6: Bar.Print (B);
18779 7: Bar.Print (C);
18780 8: end Foo;
18781 9: end Main;
18782
18783 bar.ads:
18784 1: package Bar is
18785 2: procedure Print (B : Integer);
18786 3: end bar;
18787 @end example
18788 @end quotation
18789
18790 The first thing to do is to recompile your application (for instance, in
18791 that case just by doing a @code{gnatmake main}, so that GNAT generates
18792 the cross-referencing information.
18793 You can then issue any of the following commands:
18794
18795 @quotation
18796
18797
18798 @itemize *
18799
18800 @item
18801 @code{gnatxref main.adb}
18802 @code{gnatxref} generates cross-reference information for main.adb
18803 and every unit 'with'ed by main.adb.
18804
18805 The output would be:
18806
18807 @quotation
18808
18809 @example
18810 B Type: Integer
18811 Decl: bar.ads 2:22
18812 B Type: Integer
18813 Decl: main.ads 3:20
18814 Body: main.adb 2:20
18815 Ref: main.adb 4:13 5:13 6:19
18816 Bar Type: Unit
18817 Decl: bar.ads 1:9
18818 Ref: main.adb 6:8 7:8
18819 main.ads 1:6
18820 C Type: Integer
18821 Decl: main.ads 4:5
18822 Modi: main.adb 4:8
18823 Ref: main.adb 7:19
18824 D Type: Integer
18825 Decl: main.ads 6:5
18826 Modi: main.adb 5:8
18827 Foo Type: Unit
18828 Decl: main.ads 3:15
18829 Body: main.adb 2:15
18830 Main Type: Unit
18831 Decl: main.ads 2:9
18832 Body: main.adb 1:14
18833 Print Type: Unit
18834 Decl: bar.ads 2:15
18835 Ref: main.adb 6:12 7:12
18836 @end example
18837 @end quotation
18838
18839 This shows that the entity @code{Main} is declared in main.ads, line 2, column 9,
18840 its body is in main.adb, line 1, column 14 and is not referenced any where.
18841
18842 The entity @code{Print} is declared in @code{bar.ads}, line 2, column 15 and it
18843 is referenced in @code{main.adb}, line 6 column 12 and line 7 column 12.
18844
18845 @item
18846 @code{gnatxref package1.adb package2.ads}
18847 @code{gnatxref} will generates cross-reference information for
18848 @code{package1.adb}, @code{package2.ads} and any other package @code{with}ed by any
18849 of these.
18850 @end itemize
18851 @end quotation
18852
18853 @node Using gnatxref with vi,,General Usage,Examples of gnatxref Usage
18854 @anchor{gnat_ugn/gnat_utility_programs using-gnatxref-with-vi}@anchor{15e}
18855 @subsubsection Using @code{gnatxref} with @code{vi}
18856
18857
18858 @code{gnatxref} can generate a tags file output, which can be used
18859 directly from @code{vi}. Note that the standard version of @code{vi}
18860 will not work properly with overloaded symbols. Consider using another
18861 free implementation of @code{vi}, such as @code{vim}.
18862
18863 @quotation
18864
18865 @example
18866 $ gnatxref -v gnatfind.adb > tags
18867 @end example
18868 @end quotation
18869
18870 The following command will generate the tags file for @code{gnatfind} itself
18871 (if the sources are in the search path!):
18872
18873 @quotation
18874
18875 @example
18876 $ gnatxref -v gnatfind.adb > tags
18877 @end example
18878 @end quotation
18879
18880 From @code{vi}, you can then use the command @code{:tag @emph{entity}}
18881 (replacing @code{entity} by whatever you are looking for), and vi will
18882 display a new file with the corresponding declaration of entity.
18883
18884 @node Examples of gnatfind Usage,,Examples of gnatxref Usage,The Cross-Referencing Tools gnatxref and gnatfind
18885 @anchor{gnat_ugn/gnat_utility_programs id15}@anchor{15f}@anchor{gnat_ugn/gnat_utility_programs examples-of-gnatfind-usage}@anchor{159}
18886 @subsection Examples of @code{gnatfind} Usage
18887
18888
18889
18890 @itemize *
18891
18892 @item
18893 @code{gnatfind -f xyz:main.adb}
18894 Find declarations for all entities xyz referenced at least once in
18895 main.adb. The references are search in every library file in the search
18896 path.
18897
18898 The directories will be printed as well (as the @code{-f}
18899 switch is set)
18900
18901 The output will look like:
18902
18903 @quotation
18904
18905 @example
18906 directory/main.ads:106:14: xyz <= declaration
18907 directory/main.adb:24:10: xyz <= body
18908 directory/foo.ads:45:23: xyz <= declaration
18909 @end example
18910 @end quotation
18911
18912 I.e., one of the entities xyz found in main.adb is declared at
18913 line 12 of main.ads (and its body is in main.adb), and another one is
18914 declared at line 45 of foo.ads
18915
18916 @item
18917 @code{gnatfind -fs xyz:main.adb}
18918 This is the same command as the previous one, but @code{gnatfind} will
18919 display the content of the Ada source file lines.
18920
18921 The output will look like:
18922
18923 @example
18924 directory/main.ads:106:14: xyz <= declaration
18925 procedure xyz;
18926 directory/main.adb:24:10: xyz <= body
18927 procedure xyz is
18928 directory/foo.ads:45:23: xyz <= declaration
18929 xyz : Integer;
18930 @end example
18931
18932 This can make it easier to find exactly the location your are looking
18933 for.
18934
18935 @item
18936 @code{gnatfind -r "*x*":main.ads:123 foo.adb}
18937 Find references to all entities containing an x that are
18938 referenced on line 123 of main.ads.
18939 The references will be searched only in main.ads and foo.adb.
18940
18941 @item
18942 @code{gnatfind main.ads:123}
18943 Find declarations and bodies for all entities that are referenced on
18944 line 123 of main.ads.
18945
18946 This is the same as @code{gnatfind "*":main.adb:123`}
18947
18948 @item
18949 @code{gnatfind mydir/main.adb:123:45}
18950 Find the declaration for the entity referenced at column 45 in
18951 line 123 of file main.adb in directory mydir. Note that it
18952 is usual to omit the identifier name when the column is given,
18953 since the column position identifies a unique reference.
18954
18955 The column has to be the beginning of the identifier, and should not
18956 point to any character in the middle of the identifier.
18957 @end itemize
18958
18959 @node The Ada to HTML Converter gnathtml,,The Cross-Referencing Tools gnatxref and gnatfind,GNAT Utility Programs
18960 @anchor{gnat_ugn/gnat_utility_programs the-ada-to-html-converter-gnathtml}@anchor{23}@anchor{gnat_ugn/gnat_utility_programs id16}@anchor{160}
18961 @section The Ada to HTML Converter @code{gnathtml}
18962
18963
18964 @geindex gnathtml
18965
18966 @code{gnathtml} is a Perl script that allows Ada source files to be browsed using
18967 standard Web browsers. For installation information, see @ref{161,,Installing gnathtml}.
18968
18969 Ada reserved keywords are highlighted in a bold font and Ada comments in
18970 a blue font. Unless your program was compiled with the gcc @code{-gnatx}
18971 switch to suppress the generation of cross-referencing information, user
18972 defined variables and types will appear in a different color; you will
18973 be able to click on any identifier and go to its declaration.
18974
18975 @menu
18976 * Invoking gnathtml::
18977 * Installing gnathtml::
18978
18979 @end menu
18980
18981 @node Invoking gnathtml,Installing gnathtml,,The Ada to HTML Converter gnathtml
18982 @anchor{gnat_ugn/gnat_utility_programs invoking-gnathtml}@anchor{162}@anchor{gnat_ugn/gnat_utility_programs id17}@anchor{163}
18983 @subsection Invoking @code{gnathtml}
18984
18985
18986 The command line is as follows:
18987
18988 @quotation
18989
18990 @example
18991 $ perl gnathtml.pl [ switches ] ada-files
18992 @end example
18993 @end quotation
18994
18995 You can specify as many Ada files as you want. @code{gnathtml} will generate
18996 an html file for every ada file, and a global file called @code{index.htm}.
18997 This file is an index of every identifier defined in the files.
18998
18999 The following switches are available:
19000
19001 @geindex -83 (gnathtml)
19002
19003
19004 @table @asis
19005
19006 @item @code{83}
19007
19008 Only the Ada 83 subset of keywords will be highlighted.
19009 @end table
19010
19011 @geindex -cc (gnathtml)
19012
19013
19014 @table @asis
19015
19016 @item @code{cc @emph{color}}
19017
19018 This option allows you to change the color used for comments. The default
19019 value is green. The color argument can be any name accepted by html.
19020 @end table
19021
19022 @geindex -d (gnathtml)
19023
19024
19025 @table @asis
19026
19027 @item @code{d}
19028
19029 If the Ada files depend on some other files (for instance through
19030 @code{with} clauses, the latter files will also be converted to html.
19031 Only the files in the user project will be converted to html, not the files
19032 in the run-time library itself.
19033 @end table
19034
19035 @geindex -D (gnathtml)
19036
19037
19038 @table @asis
19039
19040 @item @code{D}
19041
19042 This command is the same as @code{-d} above, but @code{gnathtml} will
19043 also look for files in the run-time library, and generate html files for them.
19044 @end table
19045
19046 @geindex -ext (gnathtml)
19047
19048
19049 @table @asis
19050
19051 @item @code{ext @emph{extension}}
19052
19053 This option allows you to change the extension of the generated HTML files.
19054 If you do not specify an extension, it will default to @code{htm}.
19055 @end table
19056
19057 @geindex -f (gnathtml)
19058
19059
19060 @table @asis
19061
19062 @item @code{f}
19063
19064 By default, gnathtml will generate html links only for global entities
19065 ('with'ed units, global variables and types,...). If you specify
19066 @code{-f} on the command line, then links will be generated for local
19067 entities too.
19068 @end table
19069
19070 @geindex -l (gnathtml)
19071
19072
19073 @table @asis
19074
19075 @item @code{l @emph{number}}
19076
19077 If this switch is provided and @code{number} is not 0, then
19078 @code{gnathtml} will number the html files every @code{number} line.
19079 @end table
19080
19081 @geindex -I (gnathtml)
19082
19083
19084 @table @asis
19085
19086 @item @code{I @emph{dir}}
19087
19088 Specify a directory to search for library files (@code{.ALI} files) and
19089 source files. You can provide several -I switches on the command line,
19090 and the directories will be parsed in the order of the command line.
19091 @end table
19092
19093 @geindex -o (gnathtml)
19094
19095
19096 @table @asis
19097
19098 @item @code{o @emph{dir}}
19099
19100 Specify the output directory for html files. By default, gnathtml will
19101 saved the generated html files in a subdirectory named @code{html/}.
19102 @end table
19103
19104 @geindex -p (gnathtml)
19105
19106
19107 @table @asis
19108
19109 @item @code{p @emph{file}}
19110
19111 If you are using Emacs and the most recent Emacs Ada mode, which provides
19112 a full Integrated Development Environment for compiling, checking,
19113 running and debugging applications, you may use @code{.gpr} files
19114 to give the directories where Emacs can find sources and object files.
19115
19116 Using this switch, you can tell gnathtml to use these files.
19117 This allows you to get an html version of your application, even if it
19118 is spread over multiple directories.
19119 @end table
19120
19121 @geindex -sc (gnathtml)
19122
19123
19124 @table @asis
19125
19126 @item @code{sc @emph{color}}
19127
19128 This switch allows you to change the color used for symbol
19129 definitions.
19130 The default value is red. The color argument can be any name accepted by html.
19131 @end table
19132
19133 @geindex -t (gnathtml)
19134
19135
19136 @table @asis
19137
19138 @item @code{t @emph{file}}
19139
19140 This switch provides the name of a file. This file contains a list of
19141 file names to be converted, and the effect is exactly as though they had
19142 appeared explicitly on the command line. This
19143 is the recommended way to work around the command line length limit on some
19144 systems.
19145 @end table
19146
19147 @node Installing gnathtml,,Invoking gnathtml,The Ada to HTML Converter gnathtml
19148 @anchor{gnat_ugn/gnat_utility_programs installing-gnathtml}@anchor{161}@anchor{gnat_ugn/gnat_utility_programs id18}@anchor{164}
19149 @subsection Installing @code{gnathtml}
19150
19151
19152 @code{Perl} needs to be installed on your machine to run this script.
19153 @code{Perl} is freely available for almost every architecture and
19154 operating system via the Internet.
19155
19156 On Unix systems, you may want to modify the first line of the script
19157 @code{gnathtml}, to explicitly specify where Perl
19158 is located. The syntax of this line is:
19159
19160 @quotation
19161
19162 @example
19163 #!full_path_name_to_perl
19164 @end example
19165 @end quotation
19166
19167 Alternatively, you may run the script using the following command line:
19168
19169 @quotation
19170
19171 @example
19172 $ perl gnathtml.pl [ switches ] files
19173 @end example
19174 @end quotation
19175
19176 @c -- +---------------------------------------------------------------------+
19177
19178 @c -- | The following sections are present only in the PRO and GPL editions |
19179
19180 @c -- +---------------------------------------------------------------------+
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
19191
19192 @node GNAT and Program Execution,Platform-Specific Information,GNAT Utility Programs,Top
19193 @anchor{gnat_ugn/gnat_and_program_execution gnat-and-program-execution}@anchor{c}@anchor{gnat_ugn/gnat_and_program_execution doc}@anchor{165}@anchor{gnat_ugn/gnat_and_program_execution id1}@anchor{166}
19194 @chapter GNAT and Program Execution
19195
19196
19197 This chapter covers several topics:
19198
19199
19200 @itemize *
19201
19202 @item
19203 @ref{167,,Running and Debugging Ada Programs}
19204
19205 @item
19206 @ref{25,,Profiling}
19207
19208 @item
19209 @ref{168,,Improving Performance}
19210
19211 @item
19212 @ref{169,,Overflow Check Handling in GNAT}
19213
19214 @item
19215 @ref{16a,,Performing Dimensionality Analysis in GNAT}
19216
19217 @item
19218 @ref{16b,,Stack Related Facilities}
19219
19220 @item
19221 @ref{16c,,Memory Management Issues}
19222 @end itemize
19223
19224 @menu
19225 * Running and Debugging Ada Programs::
19226 * Profiling::
19227 * Improving Performance::
19228 * Overflow Check Handling in GNAT::
19229 * Performing Dimensionality Analysis in GNAT::
19230 * Stack Related Facilities::
19231 * Memory Management Issues::
19232
19233 @end menu
19234
19235 @node Running and Debugging Ada Programs,Profiling,,GNAT and Program Execution
19236 @anchor{gnat_ugn/gnat_and_program_execution id2}@anchor{167}@anchor{gnat_ugn/gnat_and_program_execution running-and-debugging-ada-programs}@anchor{24}
19237 @section Running and Debugging Ada Programs
19238
19239
19240 @geindex Debugging
19241
19242 This section discusses how to debug Ada programs.
19243
19244 An incorrect Ada program may be handled in three ways by the GNAT compiler:
19245
19246
19247 @itemize *
19248
19249 @item
19250 The illegality may be a violation of the static semantics of Ada. In
19251 that case GNAT diagnoses the constructs in the program that are illegal.
19252 It is then a straightforward matter for the user to modify those parts of
19253 the program.
19254
19255 @item
19256 The illegality may be a violation of the dynamic semantics of Ada. In
19257 that case the program compiles and executes, but may generate incorrect
19258 results, or may terminate abnormally with some exception.
19259
19260 @item
19261 When presented with a program that contains convoluted errors, GNAT
19262 itself may terminate abnormally without providing full diagnostics on
19263 the incorrect user program.
19264 @end itemize
19265
19266 @geindex Debugger
19267
19268 @geindex gdb
19269
19270 @menu
19271 * The GNAT Debugger GDB::
19272 * Running GDB::
19273 * Introduction to GDB Commands::
19274 * Using Ada Expressions::
19275 * Calling User-Defined Subprograms::
19276 * Using the next Command in a Function::
19277 * Stopping When Ada Exceptions Are Raised::
19278 * Ada Tasks::
19279 * Debugging Generic Units::
19280 * Remote Debugging with gdbserver::
19281 * GNAT Abnormal Termination or Failure to Terminate::
19282 * Naming Conventions for GNAT Source Files::
19283 * Getting Internal Debugging Information::
19284 * Stack Traceback::
19285 * Pretty-Printers for the GNAT runtime::
19286
19287 @end menu
19288
19289 @node The GNAT Debugger GDB,Running GDB,,Running and Debugging Ada Programs
19290 @anchor{gnat_ugn/gnat_and_program_execution the-gnat-debugger-gdb}@anchor{16d}@anchor{gnat_ugn/gnat_and_program_execution id3}@anchor{16e}
19291 @subsection The GNAT Debugger GDB
19292
19293
19294 @code{GDB} is a general purpose, platform-independent debugger that
19295 can be used to debug mixed-language programs compiled with @code{gcc},
19296 and in particular is capable of debugging Ada programs compiled with
19297 GNAT. The latest versions of @code{GDB} are Ada-aware and can handle
19298 complex Ada data structures.
19299
19300 See @cite{Debugging with GDB},
19301 for full details on the usage of @code{GDB}, including a section on
19302 its usage on programs. This manual should be consulted for full
19303 details. The section that follows is a brief introduction to the
19304 philosophy and use of @code{GDB}.
19305
19306 When GNAT programs are compiled, the compiler optionally writes debugging
19307 information into the generated object file, including information on
19308 line numbers, and on declared types and variables. This information is
19309 separate from the generated code. It makes the object files considerably
19310 larger, but it does not add to the size of the actual executable that
19311 will be loaded into memory, and has no impact on run-time performance. The
19312 generation of debug information is triggered by the use of the
19313 @code{-g} switch in the @code{gcc} or @code{gnatmake} command
19314 used to carry out the compilations. It is important to emphasize that
19315 the use of these options does not change the generated code.
19316
19317 The debugging information is written in standard system formats that
19318 are used by many tools, including debuggers and profilers. The format
19319 of the information is typically designed to describe C types and
19320 semantics, but GNAT implements a translation scheme which allows full
19321 details about Ada types and variables to be encoded into these
19322 standard C formats. Details of this encoding scheme may be found in
19323 the file exp_dbug.ads in the GNAT source distribution. However, the
19324 details of this encoding are, in general, of no interest to a user,
19325 since @code{GDB} automatically performs the necessary decoding.
19326
19327 When a program is bound and linked, the debugging information is
19328 collected from the object files, and stored in the executable image of
19329 the program. Again, this process significantly increases the size of
19330 the generated executable file, but it does not increase the size of
19331 the executable program itself. Furthermore, if this program is run in
19332 the normal manner, it runs exactly as if the debug information were
19333 not present, and takes no more actual memory.
19334
19335 However, if the program is run under control of @code{GDB}, the
19336 debugger is activated. The image of the program is loaded, at which
19337 point it is ready to run. If a run command is given, then the program
19338 will run exactly as it would have if @code{GDB} were not present. This
19339 is a crucial part of the @code{GDB} design philosophy. @code{GDB} is
19340 entirely non-intrusive until a breakpoint is encountered. If no
19341 breakpoint is ever hit, the program will run exactly as it would if no
19342 debugger were present. When a breakpoint is hit, @code{GDB} accesses
19343 the debugging information and can respond to user commands to inspect
19344 variables, and more generally to report on the state of execution.
19345
19346 @node Running GDB,Introduction to GDB Commands,The GNAT Debugger GDB,Running and Debugging Ada Programs
19347 @anchor{gnat_ugn/gnat_and_program_execution id4}@anchor{16f}@anchor{gnat_ugn/gnat_and_program_execution running-gdb}@anchor{170}
19348 @subsection Running GDB
19349
19350
19351 This section describes how to initiate the debugger.
19352
19353 The debugger can be launched from a @code{GNAT Studio} menu or
19354 directly from the command line. The description below covers the latter use.
19355 All the commands shown can be used in the @code{GNAT Studio} debug console window,
19356 but there are usually more GUI-based ways to achieve the same effect.
19357
19358 The command to run @code{GDB} is
19359
19360 @quotation
19361
19362 @example
19363 $ gdb program
19364 @end example
19365 @end quotation
19366
19367 where @code{program} is the name of the executable file. This
19368 activates the debugger and results in a prompt for debugger commands.
19369 The simplest command is simply @code{run}, which causes the program to run
19370 exactly as if the debugger were not present. The following section
19371 describes some of the additional commands that can be given to @code{GDB}.
19372
19373 @node Introduction to GDB Commands,Using Ada Expressions,Running GDB,Running and Debugging Ada Programs
19374 @anchor{gnat_ugn/gnat_and_program_execution introduction-to-gdb-commands}@anchor{171}@anchor{gnat_ugn/gnat_and_program_execution id5}@anchor{172}
19375 @subsection Introduction to GDB Commands
19376
19377
19378 @code{GDB} contains a large repertoire of commands.
19379 See @cite{Debugging with GDB} for extensive documentation on the use
19380 of these commands, together with examples of their use. Furthermore,
19381 the command @emph{help} invoked from within GDB activates a simple help
19382 facility which summarizes the available commands and their options.
19383 In this section we summarize a few of the most commonly
19384 used commands to give an idea of what @code{GDB} is about. You should create
19385 a simple program with debugging information and experiment with the use of
19386 these @code{GDB} commands on the program as you read through the
19387 following section.
19388
19389
19390 @itemize *
19391
19392 @item
19393
19394 @table @asis
19395
19396 @item @code{set args @emph{arguments}}
19397
19398 The @emph{arguments} list above is a list of arguments to be passed to
19399 the program on a subsequent run command, just as though the arguments
19400 had been entered on a normal invocation of the program. The @code{set args}
19401 command is not needed if the program does not require arguments.
19402 @end table
19403
19404 @item
19405
19406 @table @asis
19407
19408 @item @code{run}
19409
19410 The @code{run} command causes execution of the program to start from
19411 the beginning. If the program is already running, that is to say if
19412 you are currently positioned at a breakpoint, then a prompt will ask
19413 for confirmation that you want to abandon the current execution and
19414 restart.
19415 @end table
19416
19417 @item
19418
19419 @table @asis
19420
19421 @item @code{breakpoint @emph{location}}
19422
19423 The breakpoint command sets a breakpoint, that is to say a point at which
19424 execution will halt and @code{GDB} will await further
19425 commands. @emph{location} is
19426 either a line number within a file, given in the format @code{file:linenumber},
19427 or it is the name of a subprogram. If you request that a breakpoint be set on
19428 a subprogram that is overloaded, a prompt will ask you to specify on which of
19429 those subprograms you want to breakpoint. You can also
19430 specify that all of them should be breakpointed. If the program is run
19431 and execution encounters the breakpoint, then the program
19432 stops and @code{GDB} signals that the breakpoint was encountered by
19433 printing the line of code before which the program is halted.
19434 @end table
19435
19436 @item
19437
19438 @table @asis
19439
19440 @item @code{catch exception @emph{name}}
19441
19442 This command causes the program execution to stop whenever exception
19443 @code{name} is raised. If @code{name} is omitted, then the execution is
19444 suspended when any exception is raised.
19445 @end table
19446
19447 @item
19448
19449 @table @asis
19450
19451 @item @code{print @emph{expression}}
19452
19453 This will print the value of the given expression. Most simple
19454 Ada expression formats are properly handled by @code{GDB}, so the expression
19455 can contain function calls, variables, operators, and attribute references.
19456 @end table
19457
19458 @item
19459
19460 @table @asis
19461
19462 @item @code{continue}
19463
19464 Continues execution following a breakpoint, until the next breakpoint or the
19465 termination of the program.
19466 @end table
19467
19468 @item
19469
19470 @table @asis
19471
19472 @item @code{step}
19473
19474 Executes a single line after a breakpoint. If the next statement
19475 is a subprogram call, execution continues into (the first statement of)
19476 the called subprogram.
19477 @end table
19478
19479 @item
19480
19481 @table @asis
19482
19483 @item @code{next}
19484
19485 Executes a single line. If this line is a subprogram call, executes and
19486 returns from the call.
19487 @end table
19488
19489 @item
19490
19491 @table @asis
19492
19493 @item @code{list}
19494
19495 Lists a few lines around the current source location. In practice, it
19496 is usually more convenient to have a separate edit window open with the
19497 relevant source file displayed. Successive applications of this command
19498 print subsequent lines. The command can be given an argument which is a
19499 line number, in which case it displays a few lines around the specified one.
19500 @end table
19501
19502 @item
19503
19504 @table @asis
19505
19506 @item @code{backtrace}
19507
19508 Displays a backtrace of the call chain. This command is typically
19509 used after a breakpoint has occurred, to examine the sequence of calls that
19510 leads to the current breakpoint. The display includes one line for each
19511 activation record (frame) corresponding to an active subprogram.
19512 @end table
19513
19514 @item
19515
19516 @table @asis
19517
19518 @item @code{up}
19519
19520 At a breakpoint, @code{GDB} can display the values of variables local
19521 to the current frame. The command @code{up} can be used to
19522 examine the contents of other active frames, by moving the focus up
19523 the stack, that is to say from callee to caller, one frame at a time.
19524 @end table
19525
19526 @item
19527
19528 @table @asis
19529
19530 @item @code{down}
19531
19532 Moves the focus of @code{GDB} down from the frame currently being
19533 examined to the frame of its callee (the reverse of the previous command),
19534 @end table
19535
19536 @item
19537
19538 @table @asis
19539
19540 @item @code{frame @emph{n}}
19541
19542 Inspect the frame with the given number. The value 0 denotes the frame
19543 of the current breakpoint, that is to say the top of the call stack.
19544 @end table
19545
19546 @item
19547
19548 @table @asis
19549
19550 @item @code{kill}
19551
19552 Kills the child process in which the program is running under GDB.
19553 This may be useful for several purposes:
19554
19555
19556 @itemize *
19557
19558 @item
19559 It allows you to recompile and relink your program, since on many systems
19560 you cannot regenerate an executable file while it is running in a process.
19561
19562 @item
19563 You can run your program outside the debugger, on systems that do not
19564 permit executing a program outside GDB while breakpoints are set
19565 within GDB.
19566
19567 @item
19568 It allows you to debug a core dump rather than a running process.
19569 @end itemize
19570 @end table
19571 @end itemize
19572
19573 The above list is a very short introduction to the commands that
19574 @code{GDB} provides. Important additional capabilities, including conditional
19575 breakpoints, the ability to execute command sequences on a breakpoint,
19576 the ability to debug at the machine instruction level and many other
19577 features are described in detail in @cite{Debugging with GDB}.
19578 Note that most commands can be abbreviated
19579 (for example, c for continue, bt for backtrace).
19580
19581 @node Using Ada Expressions,Calling User-Defined Subprograms,Introduction to GDB Commands,Running and Debugging Ada Programs
19582 @anchor{gnat_ugn/gnat_and_program_execution id6}@anchor{173}@anchor{gnat_ugn/gnat_and_program_execution using-ada-expressions}@anchor{174}
19583 @subsection Using Ada Expressions
19584
19585
19586 @geindex Ada expressions (in gdb)
19587
19588 @code{GDB} supports a fairly large subset of Ada expression syntax, with some
19589 extensions. The philosophy behind the design of this subset is
19590
19591 @quotation
19592
19593
19594 @itemize *
19595
19596 @item
19597 That @code{GDB} should provide basic literals and access to operations for
19598 arithmetic, dereferencing, field selection, indexing, and subprogram calls,
19599 leaving more sophisticated computations to subprograms written into the
19600 program (which therefore may be called from @code{GDB}).
19601
19602 @item
19603 That type safety and strict adherence to Ada language restrictions
19604 are not particularly relevant in a debugging context.
19605
19606 @item
19607 That brevity is important to the @code{GDB} user.
19608 @end itemize
19609 @end quotation
19610
19611 Thus, for brevity, the debugger acts as if there were
19612 implicit @code{with} and @code{use} clauses in effect for all user-written
19613 packages, thus making it unnecessary to fully qualify most names with
19614 their packages, regardless of context. Where this causes ambiguity,
19615 @code{GDB} asks the user's intent.
19616
19617 For details on the supported Ada syntax, see @cite{Debugging with GDB}.
19618
19619 @node Calling User-Defined Subprograms,Using the next Command in a Function,Using Ada Expressions,Running and Debugging Ada Programs
19620 @anchor{gnat_ugn/gnat_and_program_execution id7}@anchor{175}@anchor{gnat_ugn/gnat_and_program_execution calling-user-defined-subprograms}@anchor{176}
19621 @subsection Calling User-Defined Subprograms
19622
19623
19624 An important capability of @code{GDB} is the ability to call user-defined
19625 subprograms while debugging. This is achieved simply by entering
19626 a subprogram call statement in the form:
19627
19628 @quotation
19629
19630 @example
19631 call subprogram-name (parameters)
19632 @end example
19633 @end quotation
19634
19635 The keyword @code{call} can be omitted in the normal case where the
19636 @code{subprogram-name} does not coincide with any of the predefined
19637 @code{GDB} commands.
19638
19639 The effect is to invoke the given subprogram, passing it the
19640 list of parameters that is supplied. The parameters can be expressions and
19641 can include variables from the program being debugged. The
19642 subprogram must be defined
19643 at the library level within your program, and @code{GDB} will call the
19644 subprogram within the environment of your program execution (which
19645 means that the subprogram is free to access or even modify variables
19646 within your program).
19647
19648 The most important use of this facility is in allowing the inclusion of
19649 debugging routines that are tailored to particular data structures
19650 in your program. Such debugging routines can be written to provide a suitably
19651 high-level description of an abstract type, rather than a low-level dump
19652 of its physical layout. After all, the standard
19653 @code{GDB print} command only knows the physical layout of your
19654 types, not their abstract meaning. Debugging routines can provide information
19655 at the desired semantic level and are thus enormously useful.
19656
19657 For example, when debugging GNAT itself, it is crucial to have access to
19658 the contents of the tree nodes used to represent the program internally.
19659 But tree nodes are represented simply by an integer value (which in turn
19660 is an index into a table of nodes).
19661 Using the @code{print} command on a tree node would simply print this integer
19662 value, which is not very useful. But the PN routine (defined in file
19663 treepr.adb in the GNAT sources) takes a tree node as input, and displays
19664 a useful high level representation of the tree node, which includes the
19665 syntactic category of the node, its position in the source, the integers
19666 that denote descendant nodes and parent node, as well as varied
19667 semantic information. To study this example in more detail, you might want to
19668 look at the body of the PN procedure in the stated file.
19669
19670 Another useful application of this capability is to deal with situations of
19671 complex data which are not handled suitably by GDB. For example, if you specify
19672 Convention Fortran for a multi-dimensional array, GDB does not know that
19673 the ordering of array elements has been switched and will not properly
19674 address the array elements. In such a case, instead of trying to print the
19675 elements directly from GDB, you can write a callable procedure that prints
19676 the elements in the desired format.
19677
19678 @node Using the next Command in a Function,Stopping When Ada Exceptions Are Raised,Calling User-Defined Subprograms,Running and Debugging Ada Programs
19679 @anchor{gnat_ugn/gnat_and_program_execution using-the-next-command-in-a-function}@anchor{177}@anchor{gnat_ugn/gnat_and_program_execution id8}@anchor{178}
19680 @subsection Using the @emph{next} Command in a Function
19681
19682
19683 When you use the @code{next} command in a function, the current source
19684 location will advance to the next statement as usual. A special case
19685 arises in the case of a @code{return} statement.
19686
19687 Part of the code for a return statement is the 'epilogue' of the function.
19688 This is the code that returns to the caller. There is only one copy of
19689 this epilogue code, and it is typically associated with the last return
19690 statement in the function if there is more than one return. In some
19691 implementations, this epilogue is associated with the first statement
19692 of the function.
19693
19694 The result is that if you use the @code{next} command from a return
19695 statement that is not the last return statement of the function you
19696 may see a strange apparent jump to the last return statement or to
19697 the start of the function. You should simply ignore this odd jump.
19698 The value returned is always that from the first return statement
19699 that was stepped through.
19700
19701 @node Stopping When Ada Exceptions Are Raised,Ada Tasks,Using the next Command in a Function,Running and Debugging Ada Programs
19702 @anchor{gnat_ugn/gnat_and_program_execution stopping-when-ada-exceptions-are-raised}@anchor{179}@anchor{gnat_ugn/gnat_and_program_execution id9}@anchor{17a}
19703 @subsection Stopping When Ada Exceptions Are Raised
19704
19705
19706 @geindex Exceptions (in gdb)
19707
19708 You can set catchpoints that stop the program execution when your program
19709 raises selected exceptions.
19710
19711
19712 @itemize *
19713
19714 @item
19715
19716 @table @asis
19717
19718 @item @code{catch exception}
19719
19720 Set a catchpoint that stops execution whenever (any task in the) program
19721 raises any exception.
19722 @end table
19723
19724 @item
19725
19726 @table @asis
19727
19728 @item @code{catch exception @emph{name}}
19729
19730 Set a catchpoint that stops execution whenever (any task in the) program
19731 raises the exception @emph{name}.
19732 @end table
19733
19734 @item
19735
19736 @table @asis
19737
19738 @item @code{catch exception unhandled}
19739
19740 Set a catchpoint that stops executing whenever (any task in the) program
19741 raises an exception for which there is no handler.
19742 @end table
19743
19744 @item
19745
19746 @table @asis
19747
19748 @item @code{info exceptions}, @code{info exceptions @emph{regexp}}
19749
19750 The @code{info exceptions} command permits the user to examine all defined
19751 exceptions within Ada programs. With a regular expression, @emph{regexp}, as
19752 argument, prints out only those exceptions whose name matches @emph{regexp}.
19753 @end table
19754 @end itemize
19755
19756 @geindex Tasks (in gdb)
19757
19758 @node Ada Tasks,Debugging Generic Units,Stopping When Ada Exceptions Are Raised,Running and Debugging Ada Programs
19759 @anchor{gnat_ugn/gnat_and_program_execution ada-tasks}@anchor{17b}@anchor{gnat_ugn/gnat_and_program_execution id10}@anchor{17c}
19760 @subsection Ada Tasks
19761
19762
19763 @code{GDB} allows the following task-related commands:
19764
19765
19766 @itemize *
19767
19768 @item
19769
19770 @table @asis
19771
19772 @item @code{info tasks}
19773
19774 This command shows a list of current Ada tasks, as in the following example:
19775
19776 @example
19777 (gdb) info tasks
19778 ID TID P-ID Thread Pri State Name
19779 1 8088000 0 807e000 15 Child Activation Wait main_task
19780 2 80a4000 1 80ae000 15 Accept/Select Wait b
19781 3 809a800 1 80a4800 15 Child Activation Wait a
19782 * 4 80ae800 3 80b8000 15 Running c
19783 @end example
19784
19785 In this listing, the asterisk before the first task indicates it to be the
19786 currently running task. The first column lists the task ID that is used
19787 to refer to tasks in the following commands.
19788 @end table
19789 @end itemize
19790
19791 @geindex Breakpoints and tasks
19792
19793
19794 @itemize *
19795
19796 @item
19797 @code{break`@w{`}*linespec* `@w{`}task} @emph{taskid}, @code{break} @emph{linespec} @code{task} @emph{taskid} @code{if} ...
19798
19799 @quotation
19800
19801 These commands are like the @code{break ... thread ...}.
19802 @emph{linespec} specifies source lines.
19803
19804 Use the qualifier @code{task @emph{taskid}} with a breakpoint command
19805 to specify that you only want @code{GDB} to stop the program when a
19806 particular Ada task reaches this breakpoint. @emph{taskid} is one of the
19807 numeric task identifiers assigned by @code{GDB}, shown in the first
19808 column of the @code{info tasks} display.
19809
19810 If you do not specify @code{task @emph{taskid}} when you set a
19811 breakpoint, the breakpoint applies to @emph{all} tasks of your
19812 program.
19813
19814 You can use the @code{task} qualifier on conditional breakpoints as
19815 well; in this case, place @code{task @emph{taskid}} before the
19816 breakpoint condition (before the @code{if}).
19817 @end quotation
19818 @end itemize
19819
19820 @geindex Task switching (in gdb)
19821
19822
19823 @itemize *
19824
19825 @item
19826 @code{task @emph{taskno}}
19827
19828 @quotation
19829
19830 This command allows switching to the task referred by @emph{taskno}. In
19831 particular, this allows browsing of the backtrace of the specified
19832 task. It is advisable to switch back to the original task before
19833 continuing execution otherwise the scheduling of the program may be
19834 perturbed.
19835 @end quotation
19836 @end itemize
19837
19838 For more detailed information on the tasking support,
19839 see @cite{Debugging with GDB}.
19840
19841 @geindex Debugging Generic Units
19842
19843 @geindex Generics
19844
19845 @node Debugging Generic Units,Remote Debugging with gdbserver,Ada Tasks,Running and Debugging Ada Programs
19846 @anchor{gnat_ugn/gnat_and_program_execution debugging-generic-units}@anchor{17d}@anchor{gnat_ugn/gnat_and_program_execution id11}@anchor{17e}
19847 @subsection Debugging Generic Units
19848
19849
19850 GNAT always uses code expansion for generic instantiation. This means that
19851 each time an instantiation occurs, a complete copy of the original code is
19852 made, with appropriate substitutions of formals by actuals.
19853
19854 It is not possible to refer to the original generic entities in
19855 @code{GDB}, but it is always possible to debug a particular instance of
19856 a generic, by using the appropriate expanded names. For example, if we have
19857
19858 @quotation
19859
19860 @example
19861 procedure g is
19862
19863 generic package k is
19864 procedure kp (v1 : in out integer);
19865 end k;
19866
19867 package body k is
19868 procedure kp (v1 : in out integer) is
19869 begin
19870 v1 := v1 + 1;
19871 end kp;
19872 end k;
19873
19874 package k1 is new k;
19875 package k2 is new k;
19876
19877 var : integer := 1;
19878
19879 begin
19880 k1.kp (var);
19881 k2.kp (var);
19882 k1.kp (var);
19883 k2.kp (var);
19884 end;
19885 @end example
19886 @end quotation
19887
19888 Then to break on a call to procedure kp in the k2 instance, simply
19889 use the command:
19890
19891 @quotation
19892
19893 @example
19894 (gdb) break g.k2.kp
19895 @end example
19896 @end quotation
19897
19898 When the breakpoint occurs, you can step through the code of the
19899 instance in the normal manner and examine the values of local variables, as for
19900 other units.
19901
19902 @geindex Remote Debugging with gdbserver
19903
19904 @node Remote Debugging with gdbserver,GNAT Abnormal Termination or Failure to Terminate,Debugging Generic Units,Running and Debugging Ada Programs
19905 @anchor{gnat_ugn/gnat_and_program_execution remote-debugging-with-gdbserver}@anchor{17f}@anchor{gnat_ugn/gnat_and_program_execution id12}@anchor{180}
19906 @subsection Remote Debugging with gdbserver
19907
19908
19909 On platforms where gdbserver is supported, it is possible to use this tool
19910 to debug your application remotely. This can be useful in situations
19911 where the program needs to be run on a target host that is different
19912 from the host used for development, particularly when the target has
19913 a limited amount of resources (either CPU and/or memory).
19914
19915 To do so, start your program using gdbserver on the target machine.
19916 gdbserver then automatically suspends the execution of your program
19917 at its entry point, waiting for a debugger to connect to it. The
19918 following commands starts an application and tells gdbserver to
19919 wait for a connection with the debugger on localhost port 4444.
19920
19921 @quotation
19922
19923 @example
19924 $ gdbserver localhost:4444 program
19925 Process program created; pid = 5685
19926 Listening on port 4444
19927 @end example
19928 @end quotation
19929
19930 Once gdbserver has started listening, we can tell the debugger to establish
19931 a connection with this gdbserver, and then start the same debugging session
19932 as if the program was being debugged on the same host, directly under
19933 the control of GDB.
19934
19935 @quotation
19936
19937 @example
19938 $ gdb program
19939 (gdb) target remote targethost:4444
19940 Remote debugging using targethost:4444
19941 0x00007f29936d0af0 in ?? () from /lib64/ld-linux-x86-64.so.
19942 (gdb) b foo.adb:3
19943 Breakpoint 1 at 0x401f0c: file foo.adb, line 3.
19944 (gdb) continue
19945 Continuing.
19946
19947 Breakpoint 1, foo () at foo.adb:4
19948 4 end foo;
19949 @end example
19950 @end quotation
19951
19952 It is also possible to use gdbserver to attach to an already running
19953 program, in which case the execution of that program is simply suspended
19954 until the connection between the debugger and gdbserver is established.
19955
19956 For more information on how to use gdbserver, see the @emph{Using the gdbserver Program}
19957 section in @cite{Debugging with GDB}.
19958 GNAT provides support for gdbserver on x86-linux, x86-windows and x86_64-linux.
19959
19960 @geindex Abnormal Termination or Failure to Terminate
19961
19962 @node GNAT Abnormal Termination or Failure to Terminate,Naming Conventions for GNAT Source Files,Remote Debugging with gdbserver,Running and Debugging Ada Programs
19963 @anchor{gnat_ugn/gnat_and_program_execution gnat-abnormal-termination-or-failure-to-terminate}@anchor{181}@anchor{gnat_ugn/gnat_and_program_execution id13}@anchor{182}
19964 @subsection GNAT Abnormal Termination or Failure to Terminate
19965
19966
19967 When presented with programs that contain serious errors in syntax
19968 or semantics,
19969 GNAT may on rare occasions experience problems in operation, such
19970 as aborting with a
19971 segmentation fault or illegal memory access, raising an internal
19972 exception, terminating abnormally, or failing to terminate at all.
19973 In such cases, you can activate
19974 various features of GNAT that can help you pinpoint the construct in your
19975 program that is the likely source of the problem.
19976
19977 The following strategies are presented in increasing order of
19978 difficulty, corresponding to your experience in using GNAT and your
19979 familiarity with compiler internals.
19980
19981
19982 @itemize *
19983
19984 @item
19985 Run @code{gcc} with the @code{-gnatf}. This first
19986 switch causes all errors on a given line to be reported. In its absence,
19987 only the first error on a line is displayed.
19988
19989 The @code{-gnatdO} switch causes errors to be displayed as soon as they
19990 are encountered, rather than after compilation is terminated. If GNAT
19991 terminates prematurely or goes into an infinite loop, the last error
19992 message displayed may help to pinpoint the culprit.
19993
19994 @item
19995 Run @code{gcc} with the @code{-v} (verbose) switch. In this
19996 mode, @code{gcc} produces ongoing information about the progress of the
19997 compilation and provides the name of each procedure as code is
19998 generated. This switch allows you to find which Ada procedure was being
19999 compiled when it encountered a code generation problem.
20000 @end itemize
20001
20002 @geindex -gnatdc switch
20003
20004
20005 @itemize *
20006
20007 @item
20008 Run @code{gcc} with the @code{-gnatdc} switch. This is a GNAT specific
20009 switch that does for the front-end what @code{-v} does
20010 for the back end. The system prints the name of each unit,
20011 either a compilation unit or nested unit, as it is being analyzed.
20012
20013 @item
20014 Finally, you can start
20015 @code{gdb} directly on the @code{gnat1} executable. @code{gnat1} is the
20016 front-end of GNAT, and can be run independently (normally it is just
20017 called from @code{gcc}). You can use @code{gdb} on @code{gnat1} as you
20018 would on a C program (but @ref{16d,,The GNAT Debugger GDB} for caveats). The
20019 @code{where} command is the first line of attack; the variable
20020 @code{lineno} (seen by @code{print lineno}), used by the second phase of
20021 @code{gnat1} and by the @code{gcc} backend, indicates the source line at
20022 which the execution stopped, and @code{input_file name} indicates the name of
20023 the source file.
20024 @end itemize
20025
20026 @node Naming Conventions for GNAT Source Files,Getting Internal Debugging Information,GNAT Abnormal Termination or Failure to Terminate,Running and Debugging Ada Programs
20027 @anchor{gnat_ugn/gnat_and_program_execution naming-conventions-for-gnat-source-files}@anchor{183}@anchor{gnat_ugn/gnat_and_program_execution id14}@anchor{184}
20028 @subsection Naming Conventions for GNAT Source Files
20029
20030
20031 In order to examine the workings of the GNAT system, the following
20032 brief description of its organization may be helpful:
20033
20034
20035 @itemize *
20036
20037 @item
20038 Files with prefix @code{sc} contain the lexical scanner.
20039
20040 @item
20041 All files prefixed with @code{par} are components of the parser. The
20042 numbers correspond to chapters of the Ada Reference Manual. For example,
20043 parsing of select statements can be found in @code{par-ch9.adb}.
20044
20045 @item
20046 All files prefixed with @code{sem} perform semantic analysis. The
20047 numbers correspond to chapters of the Ada standard. For example, all
20048 issues involving context clauses can be found in @code{sem_ch10.adb}. In
20049 addition, some features of the language require sufficient special processing
20050 to justify their own semantic files: sem_aggr for aggregates, sem_disp for
20051 dynamic dispatching, etc.
20052
20053 @item
20054 All files prefixed with @code{exp} perform normalization and
20055 expansion of the intermediate representation (abstract syntax tree, or AST).
20056 these files use the same numbering scheme as the parser and semantics files.
20057 For example, the construction of record initialization procedures is done in
20058 @code{exp_ch3.adb}.
20059
20060 @item
20061 The files prefixed with @code{bind} implement the binder, which
20062 verifies the consistency of the compilation, determines an order of
20063 elaboration, and generates the bind file.
20064
20065 @item
20066 The files @code{atree.ads} and @code{atree.adb} detail the low-level
20067 data structures used by the front-end.
20068
20069 @item
20070 The files @code{sinfo.ads} and @code{sinfo.adb} detail the structure of
20071 the abstract syntax tree as produced by the parser.
20072
20073 @item
20074 The files @code{einfo.ads} and @code{einfo.adb} detail the attributes of
20075 all entities, computed during semantic analysis.
20076
20077 @item
20078 Library management issues are dealt with in files with prefix
20079 @code{lib}.
20080
20081 @geindex Annex A (in Ada Reference Manual)
20082
20083 @item
20084 Ada files with the prefix @code{a-} are children of @code{Ada}, as
20085 defined in Annex A.
20086
20087 @geindex Annex B (in Ada reference Manual)
20088
20089 @item
20090 Files with prefix @code{i-} are children of @code{Interfaces}, as
20091 defined in Annex B.
20092
20093 @geindex System (package in Ada Reference Manual)
20094
20095 @item
20096 Files with prefix @code{s-} are children of @code{System}. This includes
20097 both language-defined children and GNAT run-time routines.
20098
20099 @geindex GNAT (package)
20100
20101 @item
20102 Files with prefix @code{g-} are children of @code{GNAT}. These are useful
20103 general-purpose packages, fully documented in their specs. All
20104 the other @code{.c} files are modifications of common @code{gcc} files.
20105 @end itemize
20106
20107 @node Getting Internal Debugging Information,Stack Traceback,Naming Conventions for GNAT Source Files,Running and Debugging Ada Programs
20108 @anchor{gnat_ugn/gnat_and_program_execution id15}@anchor{185}@anchor{gnat_ugn/gnat_and_program_execution getting-internal-debugging-information}@anchor{186}
20109 @subsection Getting Internal Debugging Information
20110
20111
20112 Most compilers have internal debugging switches and modes. GNAT
20113 does also, except GNAT internal debugging switches and modes are not
20114 secret. A summary and full description of all the compiler and binder
20115 debug flags are in the file @code{debug.adb}. You must obtain the
20116 sources of the compiler to see the full detailed effects of these flags.
20117
20118 The switches that print the source of the program (reconstructed from
20119 the internal tree) are of general interest for user programs, as are the
20120 options to print
20121 the full internal tree, and the entity table (the symbol table
20122 information). The reconstructed source provides a readable version of the
20123 program after the front-end has completed analysis and expansion,
20124 and is useful when studying the performance of specific constructs.
20125 For example, constraint checks are indicated, complex aggregates
20126 are replaced with loops and assignments, and tasking primitives
20127 are replaced with run-time calls.
20128
20129 @geindex traceback
20130
20131 @geindex stack traceback
20132
20133 @geindex stack unwinding
20134
20135 @node Stack Traceback,Pretty-Printers for the GNAT runtime,Getting Internal Debugging Information,Running and Debugging Ada Programs
20136 @anchor{gnat_ugn/gnat_and_program_execution stack-traceback}@anchor{187}@anchor{gnat_ugn/gnat_and_program_execution id16}@anchor{188}
20137 @subsection Stack Traceback
20138
20139
20140 Traceback is a mechanism to display the sequence of subprogram calls that
20141 leads to a specified execution point in a program. Often (but not always)
20142 the execution point is an instruction at which an exception has been raised.
20143 This mechanism is also known as @emph{stack unwinding} because it obtains
20144 its information by scanning the run-time stack and recovering the activation
20145 records of all active subprograms. Stack unwinding is one of the most
20146 important tools for program debugging.
20147
20148 The first entry stored in traceback corresponds to the deepest calling level,
20149 that is to say the subprogram currently executing the instruction
20150 from which we want to obtain the traceback.
20151
20152 Note that there is no runtime performance penalty when stack traceback
20153 is enabled, and no exception is raised during program execution.
20154
20155 @geindex traceback
20156 @geindex non-symbolic
20157
20158 @menu
20159 * Non-Symbolic Traceback::
20160 * Symbolic Traceback::
20161
20162 @end menu
20163
20164 @node Non-Symbolic Traceback,Symbolic Traceback,,Stack Traceback
20165 @anchor{gnat_ugn/gnat_and_program_execution non-symbolic-traceback}@anchor{189}@anchor{gnat_ugn/gnat_and_program_execution id17}@anchor{18a}
20166 @subsubsection Non-Symbolic Traceback
20167
20168
20169 Note: this feature is not supported on all platforms. See
20170 @code{GNAT.Traceback} spec in @code{g-traceb.ads}
20171 for a complete list of supported platforms.
20172
20173 @subsubheading Tracebacks From an Unhandled Exception
20174
20175
20176 A runtime non-symbolic traceback is a list of addresses of call instructions.
20177 To enable this feature you must use the @code{-E}
20178 @code{gnatbind} option. With this option a stack traceback is stored as part
20179 of exception information. You can retrieve this information using the
20180 @code{addr2line} tool.
20181
20182 Here is a simple example:
20183
20184 @quotation
20185
20186 @example
20187 procedure STB is
20188
20189 procedure P1 is
20190 begin
20191 raise Constraint_Error;
20192 end P1;
20193
20194 procedure P2 is
20195 begin
20196 P1;
20197 end P2;
20198
20199 begin
20200 P2;
20201 end STB;
20202 @end example
20203
20204 @example
20205 $ gnatmake stb -bargs -E
20206 $ stb
20207
20208 Execution terminated by unhandled exception
20209 Exception name: CONSTRAINT_ERROR
20210 Message: stb.adb:5
20211 Call stack traceback locations:
20212 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
20213 @end example
20214 @end quotation
20215
20216 As we see the traceback lists a sequence of addresses for the unhandled
20217 exception @code{CONSTRAINT_ERROR} raised in procedure P1. It is easy to
20218 guess that this exception come from procedure P1. To translate these
20219 addresses into the source lines where the calls appear, the
20220 @code{addr2line} tool, described below, is invaluable. The use of this tool
20221 requires the program to be compiled with debug information.
20222
20223 @quotation
20224
20225 @example
20226 $ gnatmake -g stb -bargs -E
20227 $ stb
20228
20229 Execution terminated by unhandled exception
20230 Exception name: CONSTRAINT_ERROR
20231 Message: stb.adb:5
20232 Call stack traceback locations:
20233 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
20234
20235 $ addr2line --exe=stb 0x401373 0x40138b 0x40139c 0x401335 0x4011c4
20236 0x4011f1 0x77e892a4
20237
20238 00401373 at d:/stb/stb.adb:5
20239 0040138B at d:/stb/stb.adb:10
20240 0040139C at d:/stb/stb.adb:14
20241 00401335 at d:/stb/b~stb.adb:104
20242 004011C4 at /build/.../crt1.c:200
20243 004011F1 at /build/.../crt1.c:222
20244 77E892A4 in ?? at ??:0
20245 @end example
20246 @end quotation
20247
20248 The @code{addr2line} tool has several other useful options:
20249
20250 @quotation
20251
20252
20253 @multitable {xxxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
20254 @item
20255
20256 @code{--functions}
20257
20258 @tab
20259
20260 to get the function name corresponding to any location
20261
20262 @item
20263
20264 @code{--demangle=gnat}
20265
20266 @tab
20267
20268 to use the gnat decoding mode for the function names.
20269 Note that for binutils version 2.9.x the option is
20270 simply @code{--demangle}.
20271
20272 @end multitable
20273
20274
20275 @example
20276 $ addr2line --exe=stb --functions --demangle=gnat 0x401373 0x40138b
20277 0x40139c 0x401335 0x4011c4 0x4011f1
20278
20279 00401373 in stb.p1 at d:/stb/stb.adb:5
20280 0040138B in stb.p2 at d:/stb/stb.adb:10
20281 0040139C in stb at d:/stb/stb.adb:14
20282 00401335 in main at d:/stb/b~stb.adb:104
20283 004011C4 in <__mingw_CRTStartup> at /build/.../crt1.c:200
20284 004011F1 in <mainCRTStartup> at /build/.../crt1.c:222
20285 @end example
20286 @end quotation
20287
20288 From this traceback we can see that the exception was raised in
20289 @code{stb.adb} at line 5, which was reached from a procedure call in
20290 @code{stb.adb} at line 10, and so on. The @code{b~std.adb} is the binder file,
20291 which contains the call to the main program.
20292 @ref{11c,,Running gnatbind}. The remaining entries are assorted runtime routines,
20293 and the output will vary from platform to platform.
20294
20295 It is also possible to use @code{GDB} with these traceback addresses to debug
20296 the program. For example, we can break at a given code location, as reported
20297 in the stack traceback:
20298
20299 @quotation
20300
20301 @example
20302 $ gdb -nw stb
20303 @end example
20304 @end quotation
20305
20306 Furthermore, this feature is not implemented inside Windows DLL. Only
20307 the non-symbolic traceback is reported in this case.
20308
20309 @quotation
20310
20311 @example
20312 (gdb) break *0x401373
20313 Breakpoint 1 at 0x401373: file stb.adb, line 5.
20314 @end example
20315 @end quotation
20316
20317 It is important to note that the stack traceback addresses
20318 do not change when debug information is included. This is particularly useful
20319 because it makes it possible to release software without debug information (to
20320 minimize object size), get a field report that includes a stack traceback
20321 whenever an internal bug occurs, and then be able to retrieve the sequence
20322 of calls with the same program compiled with debug information.
20323
20324 @subsubheading Tracebacks From Exception Occurrences
20325
20326
20327 Non-symbolic tracebacks are obtained by using the @code{-E} binder argument.
20328 The stack traceback is attached to the exception information string, and can
20329 be retrieved in an exception handler within the Ada program, by means of the
20330 Ada facilities defined in @code{Ada.Exceptions}. Here is a simple example:
20331
20332 @quotation
20333
20334 @example
20335 with Ada.Text_IO;
20336 with Ada.Exceptions;
20337
20338 procedure STB is
20339
20340 use Ada;
20341 use Ada.Exceptions;
20342
20343 procedure P1 is
20344 K : Positive := 1;
20345 begin
20346 K := K - 1;
20347 exception
20348 when E : others =>
20349 Text_IO.Put_Line (Exception_Information (E));
20350 end P1;
20351
20352 procedure P2 is
20353 begin
20354 P1;
20355 end P2;
20356
20357 begin
20358 P2;
20359 end STB;
20360 @end example
20361 @end quotation
20362
20363 This program will output:
20364
20365 @quotation
20366
20367 @example
20368 $ stb
20369
20370 Exception name: CONSTRAINT_ERROR
20371 Message: stb.adb:12
20372 Call stack traceback locations:
20373 0x4015e4 0x401633 0x401644 0x401461 0x4011c4 0x4011f1 0x77e892a4
20374 @end example
20375 @end quotation
20376
20377 @subsubheading Tracebacks From Anywhere in a Program
20378
20379
20380 It is also possible to retrieve a stack traceback from anywhere in a
20381 program. For this you need to
20382 use the @code{GNAT.Traceback} API. This package includes a procedure called
20383 @code{Call_Chain} that computes a complete stack traceback, as well as useful
20384 display procedures described below. It is not necessary to use the
20385 @code{-E} @code{gnatbind} option in this case, because the stack traceback mechanism
20386 is invoked explicitly.
20387
20388 In the following example we compute a traceback at a specific location in
20389 the program, and we display it using @code{GNAT.Debug_Utilities.Image} to
20390 convert addresses to strings:
20391
20392 @quotation
20393
20394 @example
20395 with Ada.Text_IO;
20396 with GNAT.Traceback;
20397 with GNAT.Debug_Utilities;
20398
20399 procedure STB is
20400
20401 use Ada;
20402 use GNAT;
20403 use GNAT.Traceback;
20404
20405 procedure P1 is
20406 TB : Tracebacks_Array (1 .. 10);
20407 -- We are asking for a maximum of 10 stack frames.
20408 Len : Natural;
20409 -- Len will receive the actual number of stack frames returned.
20410 begin
20411 Call_Chain (TB, Len);
20412
20413 Text_IO.Put ("In STB.P1 : ");
20414
20415 for K in 1 .. Len loop
20416 Text_IO.Put (Debug_Utilities.Image (TB (K)));
20417 Text_IO.Put (' ');
20418 end loop;
20419
20420 Text_IO.New_Line;
20421 end P1;
20422
20423 procedure P2 is
20424 begin
20425 P1;
20426 end P2;
20427
20428 begin
20429 P2;
20430 end STB;
20431 @end example
20432
20433 @example
20434 $ gnatmake -g stb
20435 $ stb
20436
20437 In STB.P1 : 16#0040_F1E4# 16#0040_14F2# 16#0040_170B# 16#0040_171C#
20438 16#0040_1461# 16#0040_11C4# 16#0040_11F1# 16#77E8_92A4#
20439 @end example
20440 @end quotation
20441
20442 You can then get further information by invoking the @code{addr2line}
20443 tool as described earlier (note that the hexadecimal addresses
20444 need to be specified in C format, with a leading '0x').
20445
20446 @geindex traceback
20447 @geindex symbolic
20448
20449 @node Symbolic Traceback,,Non-Symbolic Traceback,Stack Traceback
20450 @anchor{gnat_ugn/gnat_and_program_execution id18}@anchor{18b}@anchor{gnat_ugn/gnat_and_program_execution symbolic-traceback}@anchor{18c}
20451 @subsubsection Symbolic Traceback
20452
20453
20454 A symbolic traceback is a stack traceback in which procedure names are
20455 associated with each code location.
20456
20457 Note that this feature is not supported on all platforms. See
20458 @code{GNAT.Traceback.Symbolic} spec in @code{g-trasym.ads} for a complete
20459 list of currently supported platforms.
20460
20461 Note that the symbolic traceback requires that the program be compiled
20462 with debug information. If it is not compiled with debug information
20463 only the non-symbolic information will be valid.
20464
20465 @subsubheading Tracebacks From Exception Occurrences
20466
20467
20468 Here is an example:
20469
20470 @quotation
20471
20472 @example
20473 with Ada.Text_IO;
20474 with GNAT.Traceback.Symbolic;
20475
20476 procedure STB is
20477
20478 procedure P1 is
20479 begin
20480 raise Constraint_Error;
20481 end P1;
20482
20483 procedure P2 is
20484 begin
20485 P1;
20486 end P2;
20487
20488 procedure P3 is
20489 begin
20490 P2;
20491 end P3;
20492
20493 begin
20494 P3;
20495 exception
20496 when E : others =>
20497 Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
20498 end STB;
20499 @end example
20500
20501 @example
20502 $ gnatmake -g .\stb -bargs -E
20503 $ stb
20504
20505 0040149F in stb.p1 at stb.adb:8
20506 004014B7 in stb.p2 at stb.adb:13
20507 004014CF in stb.p3 at stb.adb:18
20508 004015DD in ada.stb at stb.adb:22
20509 00401461 in main at b~stb.adb:168
20510 004011C4 in __mingw_CRTStartup at crt1.c:200
20511 004011F1 in mainCRTStartup at crt1.c:222
20512 77E892A4 in ?? at ??:0
20513 @end example
20514 @end quotation
20515
20516 In the above example the @code{.\} syntax in the @code{gnatmake} command
20517 is currently required by @code{addr2line} for files that are in
20518 the current working directory.
20519 Moreover, the exact sequence of linker options may vary from platform
20520 to platform.
20521 The above @code{-largs} section is for Windows platforms. By contrast,
20522 under Unix there is no need for the @code{-largs} section.
20523 Differences across platforms are due to details of linker implementation.
20524
20525 @subsubheading Tracebacks From Anywhere in a Program
20526
20527
20528 It is possible to get a symbolic stack traceback
20529 from anywhere in a program, just as for non-symbolic tracebacks.
20530 The first step is to obtain a non-symbolic
20531 traceback, and then call @code{Symbolic_Traceback} to compute the symbolic
20532 information. Here is an example:
20533
20534 @quotation
20535
20536 @example
20537 with Ada.Text_IO;
20538 with GNAT.Traceback;
20539 with GNAT.Traceback.Symbolic;
20540
20541 procedure STB is
20542
20543 use Ada;
20544 use GNAT.Traceback;
20545 use GNAT.Traceback.Symbolic;
20546
20547 procedure P1 is
20548 TB : Tracebacks_Array (1 .. 10);
20549 -- We are asking for a maximum of 10 stack frames.
20550 Len : Natural;
20551 -- Len will receive the actual number of stack frames returned.
20552 begin
20553 Call_Chain (TB, Len);
20554 Text_IO.Put_Line (Symbolic_Traceback (TB (1 .. Len)));
20555 end P1;
20556
20557 procedure P2 is
20558 begin
20559 P1;
20560 end P2;
20561
20562 begin
20563 P2;
20564 end STB;
20565 @end example
20566 @end quotation
20567
20568 @subsubheading Automatic Symbolic Tracebacks
20569
20570
20571 Symbolic tracebacks may also be enabled by using the -Es switch to gnatbind (as
20572 in @code{gprbuild -g ... -bargs -Es}).
20573 This will cause the Exception_Information to contain a symbolic traceback,
20574 which will also be printed if an unhandled exception terminates the
20575 program.
20576
20577 @node Pretty-Printers for the GNAT runtime,,Stack Traceback,Running and Debugging Ada Programs
20578 @anchor{gnat_ugn/gnat_and_program_execution id19}@anchor{18d}@anchor{gnat_ugn/gnat_and_program_execution pretty-printers-for-the-gnat-runtime}@anchor{18e}
20579 @subsection Pretty-Printers for the GNAT runtime
20580
20581
20582 As discussed in @cite{Calling User-Defined Subprograms}, GDB's
20583 @code{print} command only knows about the physical layout of program data
20584 structures and therefore normally displays only low-level dumps, which
20585 are often hard to understand.
20586
20587 An example of this is when trying to display the contents of an Ada
20588 standard container, such as @code{Ada.Containers.Ordered_Maps.Map}:
20589
20590 @quotation
20591
20592 @example
20593 with Ada.Containers.Ordered_Maps;
20594
20595 procedure PP is
20596 package Int_To_Nat is
20597 new Ada.Containers.Ordered_Maps (Integer, Natural);
20598
20599 Map : Int_To_Nat.Map;
20600 begin
20601 Map.Insert (1, 10);
20602 Map.Insert (2, 20);
20603 Map.Insert (3, 30);
20604
20605 Map.Clear; -- BREAK HERE
20606 end PP;
20607 @end example
20608 @end quotation
20609
20610 When this program is built with debugging information and run under
20611 GDB up to the @code{Map.Clear} statement, trying to print @code{Map} will
20612 yield information that is only relevant to the developers of our standard
20613 containers:
20614
20615 @quotation
20616
20617 @example
20618 (gdb) print map
20619 $1 = (
20620 tree => (
20621 first => 0x64e010,
20622 last => 0x64e070,
20623 root => 0x64e040,
20624 length => 3,
20625 tc => (
20626 busy => 0,
20627 lock => 0
20628 )
20629 )
20630 )
20631 @end example
20632 @end quotation
20633
20634 Fortunately, GDB has a feature called pretty-printers@footnote{http://docs.adacore.com/gdb-docs/html/gdb.html#Pretty_002dPrinter-Introduction},
20635 which allows customizing how GDB displays data structures. The GDB
20636 shipped with GNAT embeds such pretty-printers for the most common
20637 containers in the standard library. To enable them, either run the
20638 following command manually under GDB or add it to your @code{.gdbinit} file:
20639
20640 @quotation
20641
20642 @example
20643 python import gnatdbg; gnatdbg.setup()
20644 @end example
20645 @end quotation
20646
20647 Once this is done, GDB's @code{print} command will automatically use
20648 these pretty-printers when appropriate. Using the previous example:
20649
20650 @quotation
20651
20652 @example
20653 (gdb) print map
20654 $1 = pp.int_to_nat.map of length 3 = @{
20655 [1] = 10,
20656 [2] = 20,
20657 [3] = 30
20658 @}
20659 @end example
20660 @end quotation
20661
20662 Pretty-printers are invoked each time GDB tries to display a value,
20663 including when displaying the arguments of a called subprogram (in
20664 GDB's @code{backtrace} command) or when printing the value returned by a
20665 function (in GDB's @code{finish} command).
20666
20667 To display a value without involving pretty-printers, @code{print} can be
20668 invoked with its @code{/r} option:
20669
20670 @quotation
20671
20672 @example
20673 (gdb) print/r map
20674 $1 = (
20675 tree => (...
20676 @end example
20677 @end quotation
20678
20679 Finer control of pretty-printers is also possible: see GDB's online documentation@footnote{http://docs.adacore.com/gdb-docs/html/gdb.html#Pretty_002dPrinter-Commands}
20680 for more information.
20681
20682 @geindex Profiling
20683
20684 @node Profiling,Improving Performance,Running and Debugging Ada Programs,GNAT and Program Execution
20685 @anchor{gnat_ugn/gnat_and_program_execution profiling}@anchor{25}@anchor{gnat_ugn/gnat_and_program_execution id20}@anchor{18f}
20686 @section Profiling
20687
20688
20689 This section describes how to use the @code{gprof} profiler tool on Ada programs.
20690
20691 @geindex gprof
20692
20693 @geindex Profiling
20694
20695 @menu
20696 * Profiling an Ada Program with gprof::
20697
20698 @end menu
20699
20700 @node Profiling an Ada Program with gprof,,,Profiling
20701 @anchor{gnat_ugn/gnat_and_program_execution id21}@anchor{190}@anchor{gnat_ugn/gnat_and_program_execution profiling-an-ada-program-with-gprof}@anchor{191}
20702 @subsection Profiling an Ada Program with gprof
20703
20704
20705 This section is not meant to be an exhaustive documentation of @code{gprof}.
20706 Full documentation for it can be found in the @cite{GNU Profiler User's Guide}
20707 documentation that is part of this GNAT distribution.
20708
20709 Profiling a program helps determine the parts of a program that are executed
20710 most often, and are therefore the most time-consuming.
20711
20712 @code{gprof} is the standard GNU profiling tool; it has been enhanced to
20713 better handle Ada programs and multitasking.
20714 It is currently supported on the following platforms
20715
20716
20717 @itemize *
20718
20719 @item
20720 linux x86/x86_64
20721
20722 @item
20723 windows x86
20724 @end itemize
20725
20726 In order to profile a program using @code{gprof}, several steps are needed:
20727
20728
20729 @enumerate
20730
20731 @item
20732 Instrument the code, which requires a full recompilation of the project with the
20733 proper switches.
20734
20735 @item
20736 Execute the program under the analysis conditions, i.e. with the desired
20737 input.
20738
20739 @item
20740 Analyze the results using the @code{gprof} tool.
20741 @end enumerate
20742
20743 The following sections detail the different steps, and indicate how
20744 to interpret the results.
20745
20746 @menu
20747 * Compilation for profiling::
20748 * Program execution::
20749 * Running gprof::
20750 * Interpretation of profiling results::
20751
20752 @end menu
20753
20754 @node Compilation for profiling,Program execution,,Profiling an Ada Program with gprof
20755 @anchor{gnat_ugn/gnat_and_program_execution id22}@anchor{192}@anchor{gnat_ugn/gnat_and_program_execution compilation-for-profiling}@anchor{193}
20756 @subsubsection Compilation for profiling
20757
20758
20759 @geindex -pg (gcc)
20760 @geindex for profiling
20761
20762 @geindex -pg (gnatlink)
20763 @geindex for profiling
20764
20765 In order to profile a program the first step is to tell the compiler
20766 to generate the necessary profiling information. The compiler switch to be used
20767 is @code{-pg}, which must be added to other compilation switches. This
20768 switch needs to be specified both during compilation and link stages, and can
20769 be specified once when using gnatmake:
20770
20771 @quotation
20772
20773 @example
20774 $ gnatmake -f -pg -P my_project
20775 @end example
20776 @end quotation
20777
20778 Note that only the objects that were compiled with the @code{-pg} switch will
20779 be profiled; if you need to profile your whole project, use the @code{-f}
20780 gnatmake switch to force full recompilation.
20781
20782 @node Program execution,Running gprof,Compilation for profiling,Profiling an Ada Program with gprof
20783 @anchor{gnat_ugn/gnat_and_program_execution program-execution}@anchor{194}@anchor{gnat_ugn/gnat_and_program_execution id23}@anchor{195}
20784 @subsubsection Program execution
20785
20786
20787 Once the program has been compiled for profiling, you can run it as usual.
20788
20789 The only constraint imposed by profiling is that the program must terminate
20790 normally. An interrupted program (via a Ctrl-C, kill, etc.) will not be
20791 properly analyzed.
20792
20793 Once the program completes execution, a data file called @code{gmon.out} is
20794 generated in the directory where the program was launched from. If this file
20795 already exists, it will be overwritten.
20796
20797 @node Running gprof,Interpretation of profiling results,Program execution,Profiling an Ada Program with gprof
20798 @anchor{gnat_ugn/gnat_and_program_execution running-gprof}@anchor{196}@anchor{gnat_ugn/gnat_and_program_execution id24}@anchor{197}
20799 @subsubsection Running gprof
20800
20801
20802 The @code{gprof} tool is called as follow:
20803
20804 @quotation
20805
20806 @example
20807 $ gprof my_prog gmon.out
20808 @end example
20809 @end quotation
20810
20811 or simply:
20812
20813 @quotation
20814
20815 @example
20816 $ gprof my_prog
20817 @end example
20818 @end quotation
20819
20820 The complete form of the gprof command line is the following:
20821
20822 @quotation
20823
20824 @example
20825 $ gprof [switches] [executable [data-file]]
20826 @end example
20827 @end quotation
20828
20829 @code{gprof} supports numerous switches. The order of these
20830 switch does not matter. The full list of options can be found in
20831 the GNU Profiler User's Guide documentation that comes with this documentation.
20832
20833 The following is the subset of those switches that is most relevant:
20834
20835 @geindex --demangle (gprof)
20836
20837
20838 @table @asis
20839
20840 @item @code{--demangle[=@emph{style}]}, @code{--no-demangle}
20841
20842 These options control whether symbol names should be demangled when
20843 printing output. The default is to demangle C++ symbols. The
20844 @code{--no-demangle} option may be used to turn off demangling. Different
20845 compilers have different mangling styles. The optional demangling style
20846 argument can be used to choose an appropriate demangling style for your
20847 compiler, in particular Ada symbols generated by GNAT can be demangled using
20848 @code{--demangle=gnat}.
20849 @end table
20850
20851 @geindex -e (gprof)
20852
20853
20854 @table @asis
20855
20856 @item @code{-e @emph{function_name}}
20857
20858 The @code{-e @emph{function}} option tells @code{gprof} not to print
20859 information about the function @code{function_name} (and its
20860 children...) in the call graph. The function will still be listed
20861 as a child of any functions that call it, but its index number will be
20862 shown as @code{[not printed]}. More than one @code{-e} option may be
20863 given; only one @code{function_name} may be indicated with each @code{-e}
20864 option.
20865 @end table
20866
20867 @geindex -E (gprof)
20868
20869
20870 @table @asis
20871
20872 @item @code{-E @emph{function_name}}
20873
20874 The @code{-E @emph{function}} option works like the @code{-e} option, but
20875 execution time spent in the function (and children who were not called from
20876 anywhere else), will not be used to compute the percentages-of-time for
20877 the call graph. More than one @code{-E} option may be given; only one
20878 @code{function_name} may be indicated with each @code{-E`} option.
20879 @end table
20880
20881 @geindex -f (gprof)
20882
20883
20884 @table @asis
20885
20886 @item @code{-f @emph{function_name}}
20887
20888 The @code{-f @emph{function}} option causes @code{gprof} to limit the
20889 call graph to the function @code{function_name} and its children (and
20890 their children...). More than one @code{-f} option may be given;
20891 only one @code{function_name} may be indicated with each @code{-f}
20892 option.
20893 @end table
20894
20895 @geindex -F (gprof)
20896
20897
20898 @table @asis
20899
20900 @item @code{-F @emph{function_name}}
20901
20902 The @code{-F @emph{function}} option works like the @code{-f} option, but
20903 only time spent in the function and its children (and their
20904 children...) will be used to determine total-time and
20905 percentages-of-time for the call graph. More than one @code{-F} option
20906 may be given; only one @code{function_name} may be indicated with each
20907 @code{-F} option. The @code{-F} option overrides the @code{-E} option.
20908 @end table
20909
20910 @node Interpretation of profiling results,,Running gprof,Profiling an Ada Program with gprof
20911 @anchor{gnat_ugn/gnat_and_program_execution id25}@anchor{198}@anchor{gnat_ugn/gnat_and_program_execution interpretation-of-profiling-results}@anchor{199}
20912 @subsubsection Interpretation of profiling results
20913
20914
20915 The results of the profiling analysis are represented by two arrays: the
20916 'flat profile' and the 'call graph'. Full documentation of those outputs
20917 can be found in the GNU Profiler User's Guide.
20918
20919 The flat profile shows the time spent in each function of the program, and how
20920 many time it has been called. This allows you to locate easily the most
20921 time-consuming functions.
20922
20923 The call graph shows, for each subprogram, the subprograms that call it,
20924 and the subprograms that it calls. It also provides an estimate of the time
20925 spent in each of those callers/called subprograms.
20926
20927 @node Improving Performance,Overflow Check Handling in GNAT,Profiling,GNAT and Program Execution
20928 @anchor{gnat_ugn/gnat_and_program_execution improving-performance}@anchor{26}@anchor{gnat_ugn/gnat_and_program_execution id26}@anchor{168}
20929 @section Improving Performance
20930
20931
20932 @geindex Improving performance
20933
20934 This section presents several topics related to program performance.
20935 It first describes some of the tradeoffs that need to be considered
20936 and some of the techniques for making your program run faster.
20937
20938 It then documents the unused subprogram/data elimination feature,
20939 which can reduce the size of program executables.
20940
20941 @menu
20942 * Performance Considerations::
20943 * Text_IO Suggestions::
20944 * Reducing Size of Executables with Unused Subprogram/Data Elimination::
20945
20946 @end menu
20947
20948 @node Performance Considerations,Text_IO Suggestions,,Improving Performance
20949 @anchor{gnat_ugn/gnat_and_program_execution performance-considerations}@anchor{19a}@anchor{gnat_ugn/gnat_and_program_execution id27}@anchor{19b}
20950 @subsection Performance Considerations
20951
20952
20953 The GNAT system provides a number of options that allow a trade-off
20954 between
20955
20956
20957 @itemize *
20958
20959 @item
20960 performance of the generated code
20961
20962 @item
20963 speed of compilation
20964
20965 @item
20966 minimization of dependences and recompilation
20967
20968 @item
20969 the degree of run-time checking.
20970 @end itemize
20971
20972 The defaults (if no options are selected) aim at improving the speed
20973 of compilation and minimizing dependences, at the expense of performance
20974 of the generated code:
20975
20976
20977 @itemize *
20978
20979 @item
20980 no optimization
20981
20982 @item
20983 no inlining of subprogram calls
20984
20985 @item
20986 all run-time checks enabled except overflow and elaboration checks
20987 @end itemize
20988
20989 These options are suitable for most program development purposes. This
20990 section describes how you can modify these choices, and also provides
20991 some guidelines on debugging optimized code.
20992
20993 @menu
20994 * Controlling Run-Time Checks::
20995 * Use of Restrictions::
20996 * Optimization Levels::
20997 * Debugging Optimized Code::
20998 * Inlining of Subprograms::
20999 * Floating_Point_Operations::
21000 * Vectorization of loops::
21001 * Other Optimization Switches::
21002 * Optimization and Strict Aliasing::
21003 * Aliased Variables and Optimization::
21004 * Atomic Variables and Optimization::
21005 * Passive Task Optimization::
21006
21007 @end menu
21008
21009 @node Controlling Run-Time Checks,Use of Restrictions,,Performance Considerations
21010 @anchor{gnat_ugn/gnat_and_program_execution id28}@anchor{19c}@anchor{gnat_ugn/gnat_and_program_execution controlling-run-time-checks}@anchor{19d}
21011 @subsubsection Controlling Run-Time Checks
21012
21013
21014 By default, GNAT generates all run-time checks, except stack overflow
21015 checks, and checks for access before elaboration on subprogram
21016 calls. The latter are not required in default mode, because all
21017 necessary checking is done at compile time.
21018
21019 @geindex -gnatp (gcc)
21020
21021 @geindex -gnato (gcc)
21022
21023 The gnat switch, @code{-gnatp} allows this default to be modified. See
21024 @ref{f9,,Run-Time Checks}.
21025
21026 Our experience is that the default is suitable for most development
21027 purposes.
21028
21029 Elaboration checks are off by default, and also not needed by default, since
21030 GNAT uses a static elaboration analysis approach that avoids the need for
21031 run-time checking. This manual contains a full chapter discussing the issue
21032 of elaboration checks, and if the default is not satisfactory for your use,
21033 you should read this chapter.
21034
21035 For validity checks, the minimal checks required by the Ada Reference
21036 Manual (for case statements and assignments to array elements) are on
21037 by default. These can be suppressed by use of the @code{-gnatVn} switch.
21038 Note that in Ada 83, there were no validity checks, so if the Ada 83 mode
21039 is acceptable (or when comparing GNAT performance with an Ada 83 compiler),
21040 it may be reasonable to routinely use @code{-gnatVn}. Validity checks
21041 are also suppressed entirely if @code{-gnatp} is used.
21042
21043 @geindex Overflow checks
21044
21045 @geindex Checks
21046 @geindex overflow
21047
21048 @geindex Suppress
21049
21050 @geindex Unsuppress
21051
21052 @geindex pragma Suppress
21053
21054 @geindex pragma Unsuppress
21055
21056 Note that the setting of the switches controls the default setting of
21057 the checks. They may be modified using either @code{pragma Suppress} (to
21058 remove checks) or @code{pragma Unsuppress} (to add back suppressed
21059 checks) in the program source.
21060
21061 @node Use of Restrictions,Optimization Levels,Controlling Run-Time Checks,Performance Considerations
21062 @anchor{gnat_ugn/gnat_and_program_execution id29}@anchor{19e}@anchor{gnat_ugn/gnat_and_program_execution use-of-restrictions}@anchor{19f}
21063 @subsubsection Use of Restrictions
21064
21065
21066 The use of pragma Restrictions allows you to control which features are
21067 permitted in your program. Apart from the obvious point that if you avoid
21068 relatively expensive features like finalization (enforceable by the use
21069 of pragma Restrictions (No_Finalization), the use of this pragma does not
21070 affect the generated code in most cases.
21071
21072 One notable exception to this rule is that the possibility of task abort
21073 results in some distributed overhead, particularly if finalization or
21074 exception handlers are used. The reason is that certain sections of code
21075 have to be marked as non-abortable.
21076
21077 If you use neither the @code{abort} statement, nor asynchronous transfer
21078 of control (@code{select ... then abort}), then this distributed overhead
21079 is removed, which may have a general positive effect in improving
21080 overall performance. Especially code involving frequent use of tasking
21081 constructs and controlled types will show much improved performance.
21082 The relevant restrictions pragmas are
21083
21084 @quotation
21085
21086 @example
21087 pragma Restrictions (No_Abort_Statements);
21088 pragma Restrictions (Max_Asynchronous_Select_Nesting => 0);
21089 @end example
21090 @end quotation
21091
21092 It is recommended that these restriction pragmas be used if possible. Note
21093 that this also means that you can write code without worrying about the
21094 possibility of an immediate abort at any point.
21095
21096 @node Optimization Levels,Debugging Optimized Code,Use of Restrictions,Performance Considerations
21097 @anchor{gnat_ugn/gnat_and_program_execution id30}@anchor{1a0}@anchor{gnat_ugn/gnat_and_program_execution optimization-levels}@anchor{fc}
21098 @subsubsection Optimization Levels
21099
21100
21101 @geindex -O (gcc)
21102
21103 Without any optimization option,
21104 the compiler's goal is to reduce the cost of
21105 compilation and to make debugging produce the expected results.
21106 Statements are independent: if you stop the program with a breakpoint between
21107 statements, you can then assign a new value to any variable or change
21108 the program counter to any other statement in the subprogram and get exactly
21109 the results you would expect from the source code.
21110
21111 Turning on optimization makes the compiler attempt to improve the
21112 performance and/or code size at the expense of compilation time and
21113 possibly the ability to debug the program.
21114
21115 If you use multiple
21116 -O options, with or without level numbers,
21117 the last such option is the one that is effective.
21118
21119 The default is optimization off. This results in the fastest compile
21120 times, but GNAT makes absolutely no attempt to optimize, and the
21121 generated programs are considerably larger and slower than when
21122 optimization is enabled. You can use the
21123 @code{-O} switch (the permitted forms are @code{-O0}, @code{-O1}
21124 @code{-O2}, @code{-O3}, and @code{-Os})
21125 to @code{gcc} to control the optimization level:
21126
21127
21128 @itemize *
21129
21130 @item
21131
21132 @table @asis
21133
21134 @item @code{-O0}
21135
21136 No optimization (the default);
21137 generates unoptimized code but has
21138 the fastest compilation time.
21139
21140 Note that many other compilers do substantial optimization even
21141 if 'no optimization' is specified. With gcc, it is very unusual
21142 to use @code{-O0} for production if execution time is of any concern,
21143 since @code{-O0} means (almost) no optimization. This difference
21144 between gcc and other compilers should be kept in mind when
21145 doing performance comparisons.
21146 @end table
21147
21148 @item
21149
21150 @table @asis
21151
21152 @item @code{-O1}
21153
21154 Moderate optimization;
21155 optimizes reasonably well but does not
21156 degrade compilation time significantly.
21157 @end table
21158
21159 @item
21160
21161 @table @asis
21162
21163 @item @code{-O2}
21164
21165 Full optimization;
21166 generates highly optimized code and has
21167 the slowest compilation time.
21168 @end table
21169
21170 @item
21171
21172 @table @asis
21173
21174 @item @code{-O3}
21175
21176 Full optimization as in @code{-O2};
21177 also uses more aggressive automatic inlining of subprograms within a unit
21178 (@ref{10f,,Inlining of Subprograms}) and attempts to vectorize loops.
21179 @end table
21180
21181 @item
21182
21183 @table @asis
21184
21185 @item @code{-Os}
21186
21187 Optimize space usage (code and data) of resulting program.
21188 @end table
21189 @end itemize
21190
21191 Higher optimization levels perform more global transformations on the
21192 program and apply more expensive analysis algorithms in order to generate
21193 faster and more compact code. The price in compilation time, and the
21194 resulting improvement in execution time,
21195 both depend on the particular application and the hardware environment.
21196 You should experiment to find the best level for your application.
21197
21198 Since the precise set of optimizations done at each level will vary from
21199 release to release (and sometime from target to target), it is best to think
21200 of the optimization settings in general terms.
21201 See the @emph{Options That Control Optimization} section in
21202 @cite{Using the GNU Compiler Collection (GCC)}
21203 for details about
21204 the @code{-O} settings and a number of @code{-f} options that
21205 individually enable or disable specific optimizations.
21206
21207 Unlike some other compilation systems, @code{gcc} has
21208 been tested extensively at all optimization levels. There are some bugs
21209 which appear only with optimization turned on, but there have also been
21210 bugs which show up only in @emph{unoptimized} code. Selecting a lower
21211 level of optimization does not improve the reliability of the code
21212 generator, which in practice is highly reliable at all optimization
21213 levels.
21214
21215 Note regarding the use of @code{-O3}: The use of this optimization level
21216 ought not to be automatically preferred over that of level @code{-O2},
21217 since it often results in larger executables which may run more slowly.
21218 See further discussion of this point in @ref{10f,,Inlining of Subprograms}.
21219
21220 @node Debugging Optimized Code,Inlining of Subprograms,Optimization Levels,Performance Considerations
21221 @anchor{gnat_ugn/gnat_and_program_execution debugging-optimized-code}@anchor{1a1}@anchor{gnat_ugn/gnat_and_program_execution id31}@anchor{1a2}
21222 @subsubsection Debugging Optimized Code
21223
21224
21225 @geindex Debugging optimized code
21226
21227 @geindex Optimization and debugging
21228
21229 Although it is possible to do a reasonable amount of debugging at
21230 nonzero optimization levels,
21231 the higher the level the more likely that
21232 source-level constructs will have been eliminated by optimization.
21233 For example, if a loop is strength-reduced, the loop
21234 control variable may be completely eliminated and thus cannot be
21235 displayed in the debugger.
21236 This can only happen at @code{-O2} or @code{-O3}.
21237 Explicit temporary variables that you code might be eliminated at
21238 level @code{-O1} or higher.
21239
21240 @geindex -g (gcc)
21241
21242 The use of the @code{-g} switch,
21243 which is needed for source-level debugging,
21244 affects the size of the program executable on disk,
21245 and indeed the debugging information can be quite large.
21246 However, it has no effect on the generated code (and thus does not
21247 degrade performance)
21248
21249 Since the compiler generates debugging tables for a compilation unit before
21250 it performs optimizations, the optimizing transformations may invalidate some
21251 of the debugging data. You therefore need to anticipate certain
21252 anomalous situations that may arise while debugging optimized code.
21253 These are the most common cases:
21254
21255
21256 @itemize *
21257
21258 @item
21259 @emph{The 'hopping Program Counter':} Repeated @code{step} or @code{next}
21260 commands show
21261 the PC bouncing back and forth in the code. This may result from any of
21262 the following optimizations:
21263
21264
21265 @itemize -
21266
21267 @item
21268 @emph{Common subexpression elimination:} using a single instance of code for a
21269 quantity that the source computes several times. As a result you
21270 may not be able to stop on what looks like a statement.
21271
21272 @item
21273 @emph{Invariant code motion:} moving an expression that does not change within a
21274 loop, to the beginning of the loop.
21275
21276 @item
21277 @emph{Instruction scheduling:} moving instructions so as to
21278 overlap loads and stores (typically) with other code, or in
21279 general to move computations of values closer to their uses. Often
21280 this causes you to pass an assignment statement without the assignment
21281 happening and then later bounce back to the statement when the
21282 value is actually needed. Placing a breakpoint on a line of code
21283 and then stepping over it may, therefore, not always cause all the
21284 expected side-effects.
21285 @end itemize
21286
21287 @item
21288 @emph{The 'big leap':} More commonly known as @emph{cross-jumping}, in which
21289 two identical pieces of code are merged and the program counter suddenly
21290 jumps to a statement that is not supposed to be executed, simply because
21291 it (and the code following) translates to the same thing as the code
21292 that @emph{was} supposed to be executed. This effect is typically seen in
21293 sequences that end in a jump, such as a @code{goto}, a @code{return}, or
21294 a @code{break} in a C @code{switch} statement.
21295
21296 @item
21297 @emph{The 'roving variable':} The symptom is an unexpected value in a variable.
21298 There are various reasons for this effect:
21299
21300
21301 @itemize -
21302
21303 @item
21304 In a subprogram prologue, a parameter may not yet have been moved to its
21305 'home'.
21306
21307 @item
21308 A variable may be dead, and its register re-used. This is
21309 probably the most common cause.
21310
21311 @item
21312 As mentioned above, the assignment of a value to a variable may
21313 have been moved.
21314
21315 @item
21316 A variable may be eliminated entirely by value propagation or
21317 other means. In this case, GCC may incorrectly generate debugging
21318 information for the variable
21319 @end itemize
21320
21321 In general, when an unexpected value appears for a local variable or parameter
21322 you should first ascertain if that value was actually computed by
21323 your program, as opposed to being incorrectly reported by the debugger.
21324 Record fields or
21325 array elements in an object designated by an access value
21326 are generally less of a problem, once you have ascertained that the access
21327 value is sensible.
21328 Typically, this means checking variables in the preceding code and in the
21329 calling subprogram to verify that the value observed is explainable from other
21330 values (one must apply the procedure recursively to those
21331 other values); or re-running the code and stopping a little earlier
21332 (perhaps before the call) and stepping to better see how the variable obtained
21333 the value in question; or continuing to step @emph{from} the point of the
21334 strange value to see if code motion had simply moved the variable's
21335 assignments later.
21336 @end itemize
21337
21338 In light of such anomalies, a recommended technique is to use @code{-O0}
21339 early in the software development cycle, when extensive debugging capabilities
21340 are most needed, and then move to @code{-O1} and later @code{-O2} as
21341 the debugger becomes less critical.
21342 Whether to use the @code{-g} switch in the release version is
21343 a release management issue.
21344 Note that if you use @code{-g} you can then use the @code{strip} program
21345 on the resulting executable,
21346 which removes both debugging information and global symbols.
21347
21348 @node Inlining of Subprograms,Floating_Point_Operations,Debugging Optimized Code,Performance Considerations
21349 @anchor{gnat_ugn/gnat_and_program_execution id32}@anchor{1a3}@anchor{gnat_ugn/gnat_and_program_execution inlining-of-subprograms}@anchor{10f}
21350 @subsubsection Inlining of Subprograms
21351
21352
21353 A call to a subprogram in the current unit is inlined if all the
21354 following conditions are met:
21355
21356
21357 @itemize *
21358
21359 @item
21360 The optimization level is at least @code{-O1}.
21361
21362 @item
21363 The called subprogram is suitable for inlining: It must be small enough
21364 and not contain something that @code{gcc} cannot support in inlined
21365 subprograms.
21366
21367 @geindex pragma Inline
21368
21369 @geindex Inline
21370
21371 @item
21372 Any one of the following applies: @code{pragma Inline} is applied to the
21373 subprogram; the subprogram is local to the unit and called once from
21374 within it; the subprogram is small and optimization level @code{-O2} is
21375 specified; optimization level @code{-O3} is specified.
21376 @end itemize
21377
21378 Calls to subprograms in @emph{with}ed units are normally not inlined.
21379 To achieve actual inlining (that is, replacement of the call by the code
21380 in the body of the subprogram), the following conditions must all be true:
21381
21382
21383 @itemize *
21384
21385 @item
21386 The optimization level is at least @code{-O1}.
21387
21388 @item
21389 The called subprogram is suitable for inlining: It must be small enough
21390 and not contain something that @code{gcc} cannot support in inlined
21391 subprograms.
21392
21393 @item
21394 There is a @code{pragma Inline} for the subprogram.
21395
21396 @item
21397 The @code{-gnatn} switch is used on the command line.
21398 @end itemize
21399
21400 Even if all these conditions are met, it may not be possible for
21401 the compiler to inline the call, due to the length of the body,
21402 or features in the body that make it impossible for the compiler
21403 to do the inlining.
21404
21405 Note that specifying the @code{-gnatn} switch causes additional
21406 compilation dependencies. Consider the following:
21407
21408 @quotation
21409
21410 @example
21411 package R is
21412 procedure Q;
21413 pragma Inline (Q);
21414 end R;
21415 package body R is
21416 ...
21417 end R;
21418
21419 with R;
21420 procedure Main is
21421 begin
21422 ...
21423 R.Q;
21424 end Main;
21425 @end example
21426 @end quotation
21427
21428 With the default behavior (no @code{-gnatn} switch specified), the
21429 compilation of the @code{Main} procedure depends only on its own source,
21430 @code{main.adb}, and the spec of the package in file @code{r.ads}. This
21431 means that editing the body of @code{R} does not require recompiling
21432 @code{Main}.
21433
21434 On the other hand, the call @code{R.Q} is not inlined under these
21435 circumstances. If the @code{-gnatn} switch is present when @code{Main}
21436 is compiled, the call will be inlined if the body of @code{Q} is small
21437 enough, but now @code{Main} depends on the body of @code{R} in
21438 @code{r.adb} as well as on the spec. This means that if this body is edited,
21439 the main program must be recompiled. Note that this extra dependency
21440 occurs whether or not the call is in fact inlined by @code{gcc}.
21441
21442 The use of front end inlining with @code{-gnatN} generates similar
21443 additional dependencies.
21444
21445 @geindex -fno-inline (gcc)
21446
21447 Note: The @code{-fno-inline} switch overrides all other conditions and ensures that
21448 no inlining occurs, unless requested with pragma Inline_Always for @code{gcc}
21449 back-ends. The extra dependences resulting from @code{-gnatn} will still be active,
21450 even if this switch is used to suppress the resulting inlining actions.
21451
21452 @geindex -fno-inline-functions (gcc)
21453
21454 Note: The @code{-fno-inline-functions} switch can be used to prevent
21455 automatic inlining of subprograms if @code{-O3} is used.
21456
21457 @geindex -fno-inline-small-functions (gcc)
21458
21459 Note: The @code{-fno-inline-small-functions} switch can be used to prevent
21460 automatic inlining of small subprograms if @code{-O2} is used.
21461
21462 @geindex -fno-inline-functions-called-once (gcc)
21463
21464 Note: The @code{-fno-inline-functions-called-once} switch
21465 can be used to prevent inlining of subprograms local to the unit
21466 and called once from within it if @code{-O1} is used.
21467
21468 Note regarding the use of @code{-O3}: @code{-gnatn} is made up of two
21469 sub-switches @code{-gnatn1} and @code{-gnatn2} that can be directly
21470 specified in lieu of it, @code{-gnatn} being translated into one of them
21471 based on the optimization level. With @code{-O2} or below, @code{-gnatn}
21472 is equivalent to @code{-gnatn1} which activates pragma @code{Inline} with
21473 moderate inlining across modules. With @code{-O3}, @code{-gnatn} is
21474 equivalent to @code{-gnatn2} which activates pragma @code{Inline} with
21475 full inlining across modules. If you have used pragma @code{Inline} in
21476 appropriate cases, then it is usually much better to use @code{-O2}
21477 and @code{-gnatn} and avoid the use of @code{-O3} which has the additional
21478 effect of inlining subprograms you did not think should be inlined. We have
21479 found that the use of @code{-O3} may slow down the compilation and increase
21480 the code size by performing excessive inlining, leading to increased
21481 instruction cache pressure from the increased code size and thus minor
21482 performance improvements. So the bottom line here is that you should not
21483 automatically assume that @code{-O3} is better than @code{-O2}, and
21484 indeed you should use @code{-O3} only if tests show that it actually
21485 improves performance for your program.
21486
21487 @node Floating_Point_Operations,Vectorization of loops,Inlining of Subprograms,Performance Considerations
21488 @anchor{gnat_ugn/gnat_and_program_execution floating-point-operations}@anchor{1a4}@anchor{gnat_ugn/gnat_and_program_execution id33}@anchor{1a5}
21489 @subsubsection Floating_Point_Operations
21490
21491
21492 @geindex Floating-Point Operations
21493
21494 On almost all targets, GNAT maps Float and Long_Float to the 32-bit and
21495 64-bit standard IEEE floating-point representations, and operations will
21496 use standard IEEE arithmetic as provided by the processor. On most, but
21497 not all, architectures, the attribute Machine_Overflows is False for these
21498 types, meaning that the semantics of overflow is implementation-defined.
21499 In the case of GNAT, these semantics correspond to the normal IEEE
21500 treatment of infinities and NaN (not a number) values. For example,
21501 1.0 / 0.0 yields plus infinitiy and 0.0 / 0.0 yields a NaN. By
21502 avoiding explicit overflow checks, the performance is greatly improved
21503 on many targets. However, if required, floating-point overflow can be
21504 enabled by the use of the pragma Check_Float_Overflow.
21505
21506 Another consideration that applies specifically to x86 32-bit
21507 architectures is which form of floating-point arithmetic is used.
21508 By default the operations use the old style x86 floating-point,
21509 which implements an 80-bit extended precision form (on these
21510 architectures the type Long_Long_Float corresponds to that form).
21511 In addition, generation of efficient code in this mode means that
21512 the extended precision form will be used for intermediate results.
21513 This may be helpful in improving the final precision of a complex
21514 expression. However it means that the results obtained on the x86
21515 will be different from those on other architectures, and for some
21516 algorithms, the extra intermediate precision can be detrimental.
21517
21518 In addition to this old-style floating-point, all modern x86 chips
21519 implement an alternative floating-point operation model referred
21520 to as SSE2. In this model there is no extended form, and furthermore
21521 execution performance is significantly enhanced. To force GNAT to use
21522 this more modern form, use both of the switches:
21523
21524 @quotation
21525
21526 -msse2 -mfpmath=sse
21527 @end quotation
21528
21529 A unit compiled with these switches will automatically use the more
21530 efficient SSE2 instruction set for Float and Long_Float operations.
21531 Note that the ABI has the same form for both floating-point models,
21532 so it is permissible to mix units compiled with and without these
21533 switches.
21534
21535 @node Vectorization of loops,Other Optimization Switches,Floating_Point_Operations,Performance Considerations
21536 @anchor{gnat_ugn/gnat_and_program_execution id34}@anchor{1a6}@anchor{gnat_ugn/gnat_and_program_execution vectorization-of-loops}@anchor{1a7}
21537 @subsubsection Vectorization of loops
21538
21539
21540 @geindex Optimization Switches
21541
21542 You can take advantage of the auto-vectorizer present in the @code{gcc}
21543 back end to vectorize loops with GNAT. The corresponding command line switch
21544 is @code{-ftree-vectorize} but, as it is enabled by default at @code{-O3}
21545 and other aggressive optimizations helpful for vectorization also are enabled
21546 by default at this level, using @code{-O3} directly is recommended.
21547
21548 You also need to make sure that the target architecture features a supported
21549 SIMD instruction set. For example, for the x86 architecture, you should at
21550 least specify @code{-msse2} to get significant vectorization (but you don't
21551 need to specify it for x86-64 as it is part of the base 64-bit architecture).
21552 Similarly, for the PowerPC architecture, you should specify @code{-maltivec}.
21553
21554 The preferred loop form for vectorization is the @code{for} iteration scheme.
21555 Loops with a @code{while} iteration scheme can also be vectorized if they are
21556 very simple, but the vectorizer will quickly give up otherwise. With either
21557 iteration scheme, the flow of control must be straight, in particular no
21558 @code{exit} statement may appear in the loop body. The loop may however
21559 contain a single nested loop, if it can be vectorized when considered alone:
21560
21561 @quotation
21562
21563 @example
21564 A : array (1..4, 1..4) of Long_Float;
21565 S : array (1..4) of Long_Float;
21566
21567 procedure Sum is
21568 begin
21569 for I in A'Range(1) loop
21570 for J in A'Range(2) loop
21571 S (I) := S (I) + A (I, J);
21572 end loop;
21573 end loop;
21574 end Sum;
21575 @end example
21576 @end quotation
21577
21578 The vectorizable operations depend on the targeted SIMD instruction set, but
21579 the adding and some of the multiplying operators are generally supported, as
21580 well as the logical operators for modular types. Note that compiling
21581 with @code{-gnatp} might well reveal cases where some checks do thwart
21582 vectorization.
21583
21584 Type conversions may also prevent vectorization if they involve semantics that
21585 are not directly supported by the code generator or the SIMD instruction set.
21586 A typical example is direct conversion from floating-point to integer types.
21587 The solution in this case is to use the following idiom:
21588
21589 @quotation
21590
21591 @example
21592 Integer (S'Truncation (F))
21593 @end example
21594 @end quotation
21595
21596 if @code{S} is the subtype of floating-point object @code{F}.
21597
21598 In most cases, the vectorizable loops are loops that iterate over arrays.
21599 All kinds of array types are supported, i.e. constrained array types with
21600 static bounds:
21601
21602 @quotation
21603
21604 @example
21605 type Array_Type is array (1 .. 4) of Long_Float;
21606 @end example
21607 @end quotation
21608
21609 constrained array types with dynamic bounds:
21610
21611 @quotation
21612
21613 @example
21614 type Array_Type is array (1 .. Q.N) of Long_Float;
21615
21616 type Array_Type is array (Q.K .. 4) of Long_Float;
21617
21618 type Array_Type is array (Q.K .. Q.N) of Long_Float;
21619 @end example
21620 @end quotation
21621
21622 or unconstrained array types:
21623
21624 @quotation
21625
21626 @example
21627 type Array_Type is array (Positive range <>) of Long_Float;
21628 @end example
21629 @end quotation
21630
21631 The quality of the generated code decreases when the dynamic aspect of the
21632 array type increases, the worst code being generated for unconstrained array
21633 types. This is so because, the less information the compiler has about the
21634 bounds of the array, the more fallback code it needs to generate in order to
21635 fix things up at run time.
21636
21637 It is possible to specify that a given loop should be subject to vectorization
21638 preferably to other optimizations by means of pragma @code{Loop_Optimize}:
21639
21640 @quotation
21641
21642 @example
21643 pragma Loop_Optimize (Vector);
21644 @end example
21645 @end quotation
21646
21647 placed immediately within the loop will convey the appropriate hint to the
21648 compiler for this loop.
21649
21650 It is also possible to help the compiler generate better vectorized code
21651 for a given loop by asserting that there are no loop-carried dependencies
21652 in the loop. Consider for example the procedure:
21653
21654 @quotation
21655
21656 @example
21657 type Arr is array (1 .. 4) of Long_Float;
21658
21659 procedure Add (X, Y : not null access Arr; R : not null access Arr) is
21660 begin
21661 for I in Arr'Range loop
21662 R(I) := X(I) + Y(I);
21663 end loop;
21664 end;
21665 @end example
21666 @end quotation
21667
21668 By default, the compiler cannot unconditionally vectorize the loop because
21669 assigning to a component of the array designated by R in one iteration could
21670 change the value read from the components of the array designated by X or Y
21671 in a later iteration. As a result, the compiler will generate two versions
21672 of the loop in the object code, one vectorized and the other not vectorized,
21673 as well as a test to select the appropriate version at run time. This can
21674 be overcome by another hint:
21675
21676 @quotation
21677
21678 @example
21679 pragma Loop_Optimize (Ivdep);
21680 @end example
21681 @end quotation
21682
21683 placed immediately within the loop will tell the compiler that it can safely
21684 omit the non-vectorized version of the loop as well as the run-time test.
21685
21686 @node Other Optimization Switches,Optimization and Strict Aliasing,Vectorization of loops,Performance Considerations
21687 @anchor{gnat_ugn/gnat_and_program_execution other-optimization-switches}@anchor{1a8}@anchor{gnat_ugn/gnat_and_program_execution id35}@anchor{1a9}
21688 @subsubsection Other Optimization Switches
21689
21690
21691 @geindex Optimization Switches
21692
21693 Since GNAT uses the @code{gcc} back end, all the specialized
21694 @code{gcc} optimization switches are potentially usable. These switches
21695 have not been extensively tested with GNAT but can generally be expected
21696 to work. Examples of switches in this category are @code{-funroll-loops}
21697 and the various target-specific @code{-m} options (in particular, it has
21698 been observed that @code{-march=xxx} can significantly improve performance
21699 on appropriate machines). For full details of these switches, see
21700 the @emph{Submodel Options} section in the @emph{Hardware Models and Configurations}
21701 chapter of @cite{Using the GNU Compiler Collection (GCC)}.
21702
21703 @node Optimization and Strict Aliasing,Aliased Variables and Optimization,Other Optimization Switches,Performance Considerations
21704 @anchor{gnat_ugn/gnat_and_program_execution optimization-and-strict-aliasing}@anchor{f3}@anchor{gnat_ugn/gnat_and_program_execution id36}@anchor{1aa}
21705 @subsubsection Optimization and Strict Aliasing
21706
21707
21708 @geindex Aliasing
21709
21710 @geindex Strict Aliasing
21711
21712 @geindex No_Strict_Aliasing
21713
21714 The strong typing capabilities of Ada allow an optimizer to generate
21715 efficient code in situations where other languages would be forced to
21716 make worst case assumptions preventing such optimizations. Consider
21717 the following example:
21718
21719 @quotation
21720
21721 @example
21722 procedure R is
21723 type Int1 is new Integer;
21724 type Int2 is new Integer;
21725 type Int1A is access Int1;
21726 type Int2A is access Int2;
21727 Int1V : Int1A;
21728 Int2V : Int2A;
21729 ...
21730
21731 begin
21732 ...
21733 for J in Data'Range loop
21734 if Data (J) = Int1V.all then
21735 Int2V.all := Int2V.all + 1;
21736 end if;
21737 end loop;
21738 ...
21739 end R;
21740 @end example
21741 @end quotation
21742
21743 In this example, since the variable @code{Int1V} can only access objects
21744 of type @code{Int1}, and @code{Int2V} can only access objects of type
21745 @code{Int2}, there is no possibility that the assignment to
21746 @code{Int2V.all} affects the value of @code{Int1V.all}. This means that
21747 the compiler optimizer can "know" that the value @code{Int1V.all} is constant
21748 for all iterations of the loop and avoid the extra memory reference
21749 required to dereference it each time through the loop.
21750
21751 This kind of optimization, called strict aliasing analysis, is
21752 triggered by specifying an optimization level of @code{-O2} or
21753 higher or @code{-Os} and allows GNAT to generate more efficient code
21754 when access values are involved.
21755
21756 However, although this optimization is always correct in terms of
21757 the formal semantics of the Ada Reference Manual, difficulties can
21758 arise if features like @code{Unchecked_Conversion} are used to break
21759 the typing system. Consider the following complete program example:
21760
21761 @quotation
21762
21763 @example
21764 package p1 is
21765 type int1 is new integer;
21766 type int2 is new integer;
21767 type a1 is access int1;
21768 type a2 is access int2;
21769 end p1;
21770
21771 with p1; use p1;
21772 package p2 is
21773 function to_a2 (Input : a1) return a2;
21774 end p2;
21775
21776 with Unchecked_Conversion;
21777 package body p2 is
21778 function to_a2 (Input : a1) return a2 is
21779 function to_a2u is
21780 new Unchecked_Conversion (a1, a2);
21781 begin
21782 return to_a2u (Input);
21783 end to_a2;
21784 end p2;
21785
21786 with p2; use p2;
21787 with p1; use p1;
21788 with Text_IO; use Text_IO;
21789 procedure m is
21790 v1 : a1 := new int1;
21791 v2 : a2 := to_a2 (v1);
21792 begin
21793 v1.all := 1;
21794 v2.all := 0;
21795 put_line (int1'image (v1.all));
21796 end;
21797 @end example
21798 @end quotation
21799
21800 This program prints out 0 in @code{-O0} or @code{-O1}
21801 mode, but it prints out 1 in @code{-O2} mode. That's
21802 because in strict aliasing mode, the compiler can and
21803 does assume that the assignment to @code{v2.all} could not
21804 affect the value of @code{v1.all}, since different types
21805 are involved.
21806
21807 This behavior is not a case of non-conformance with the standard, since
21808 the Ada RM specifies that an unchecked conversion where the resulting
21809 bit pattern is not a correct value of the target type can result in an
21810 abnormal value and attempting to reference an abnormal value makes the
21811 execution of a program erroneous. That's the case here since the result
21812 does not point to an object of type @code{int2}. This means that the
21813 effect is entirely unpredictable.
21814
21815 However, although that explanation may satisfy a language
21816 lawyer, in practice an applications programmer expects an
21817 unchecked conversion involving pointers to create true
21818 aliases and the behavior of printing 1 seems plain wrong.
21819 In this case, the strict aliasing optimization is unwelcome.
21820
21821 Indeed the compiler recognizes this possibility, and the
21822 unchecked conversion generates a warning:
21823
21824 @quotation
21825
21826 @example
21827 p2.adb:5:07: warning: possible aliasing problem with type "a2"
21828 p2.adb:5:07: warning: use -fno-strict-aliasing switch for references
21829 p2.adb:5:07: warning: or use "pragma No_Strict_Aliasing (a2);"
21830 @end example
21831 @end quotation
21832
21833 Unfortunately the problem is recognized when compiling the body of
21834 package @code{p2}, but the actual "bad" code is generated while
21835 compiling the body of @code{m} and this latter compilation does not see
21836 the suspicious @code{Unchecked_Conversion}.
21837
21838 As implied by the warning message, there are approaches you can use to
21839 avoid the unwanted strict aliasing optimization in a case like this.
21840
21841 One possibility is to simply avoid the use of @code{-O2}, but
21842 that is a bit drastic, since it throws away a number of useful
21843 optimizations that do not involve strict aliasing assumptions.
21844
21845 A less drastic approach is to compile the program using the
21846 option @code{-fno-strict-aliasing}. Actually it is only the
21847 unit containing the dereferencing of the suspicious pointer
21848 that needs to be compiled. So in this case, if we compile
21849 unit @code{m} with this switch, then we get the expected
21850 value of zero printed. Analyzing which units might need
21851 the switch can be painful, so a more reasonable approach
21852 is to compile the entire program with options @code{-O2}
21853 and @code{-fno-strict-aliasing}. If the performance is
21854 satisfactory with this combination of options, then the
21855 advantage is that the entire issue of possible "wrong"
21856 optimization due to strict aliasing is avoided.
21857
21858 To avoid the use of compiler switches, the configuration
21859 pragma @code{No_Strict_Aliasing} with no parameters may be
21860 used to specify that for all access types, the strict
21861 aliasing optimization should be suppressed.
21862
21863 However, these approaches are still overkill, in that they causes
21864 all manipulations of all access values to be deoptimized. A more
21865 refined approach is to concentrate attention on the specific
21866 access type identified as problematic.
21867
21868 First, if a careful analysis of uses of the pointer shows
21869 that there are no possible problematic references, then
21870 the warning can be suppressed by bracketing the
21871 instantiation of @code{Unchecked_Conversion} to turn
21872 the warning off:
21873
21874 @quotation
21875
21876 @example
21877 pragma Warnings (Off);
21878 function to_a2u is
21879 new Unchecked_Conversion (a1, a2);
21880 pragma Warnings (On);
21881 @end example
21882 @end quotation
21883
21884 Of course that approach is not appropriate for this particular
21885 example, since indeed there is a problematic reference. In this
21886 case we can take one of two other approaches.
21887
21888 The first possibility is to move the instantiation of unchecked
21889 conversion to the unit in which the type is declared. In
21890 this example, we would move the instantiation of
21891 @code{Unchecked_Conversion} from the body of package
21892 @code{p2} to the spec of package @code{p1}. Now the
21893 warning disappears. That's because any use of the
21894 access type knows there is a suspicious unchecked
21895 conversion, and the strict aliasing optimization
21896 is automatically suppressed for the type.
21897
21898 If it is not practical to move the unchecked conversion to the same unit
21899 in which the destination access type is declared (perhaps because the
21900 source type is not visible in that unit), you may use pragma
21901 @code{No_Strict_Aliasing} for the type. This pragma must occur in the
21902 same declarative sequence as the declaration of the access type:
21903
21904 @quotation
21905
21906 @example
21907 type a2 is access int2;
21908 pragma No_Strict_Aliasing (a2);
21909 @end example
21910 @end quotation
21911
21912 Here again, the compiler now knows that the strict aliasing optimization
21913 should be suppressed for any reference to type @code{a2} and the
21914 expected behavior is obtained.
21915
21916 Finally, note that although the compiler can generate warnings for
21917 simple cases of unchecked conversions, there are tricker and more
21918 indirect ways of creating type incorrect aliases which the compiler
21919 cannot detect. Examples are the use of address overlays and unchecked
21920 conversions involving composite types containing access types as
21921 components. In such cases, no warnings are generated, but there can
21922 still be aliasing problems. One safe coding practice is to forbid the
21923 use of address clauses for type overlaying, and to allow unchecked
21924 conversion only for primitive types. This is not really a significant
21925 restriction since any possible desired effect can be achieved by
21926 unchecked conversion of access values.
21927
21928 The aliasing analysis done in strict aliasing mode can certainly
21929 have significant benefits. We have seen cases of large scale
21930 application code where the time is increased by up to 5% by turning
21931 this optimization off. If you have code that includes significant
21932 usage of unchecked conversion, you might want to just stick with
21933 @code{-O1} and avoid the entire issue. If you get adequate
21934 performance at this level of optimization level, that's probably
21935 the safest approach. If tests show that you really need higher
21936 levels of optimization, then you can experiment with @code{-O2}
21937 and @code{-O2 -fno-strict-aliasing} to see how much effect this
21938 has on size and speed of the code. If you really need to use
21939 @code{-O2} with strict aliasing in effect, then you should
21940 review any uses of unchecked conversion of access types,
21941 particularly if you are getting the warnings described above.
21942
21943 @node Aliased Variables and Optimization,Atomic Variables and Optimization,Optimization and Strict Aliasing,Performance Considerations
21944 @anchor{gnat_ugn/gnat_and_program_execution id37}@anchor{1ab}@anchor{gnat_ugn/gnat_and_program_execution aliased-variables-and-optimization}@anchor{1ac}
21945 @subsubsection Aliased Variables and Optimization
21946
21947
21948 @geindex Aliasing
21949
21950 There are scenarios in which programs may
21951 use low level techniques to modify variables
21952 that otherwise might be considered to be unassigned. For example,
21953 a variable can be passed to a procedure by reference, which takes
21954 the address of the parameter and uses the address to modify the
21955 variable's value, even though it is passed as an IN parameter.
21956 Consider the following example:
21957
21958 @quotation
21959
21960 @example
21961 procedure P is
21962 Max_Length : constant Natural := 16;
21963 type Char_Ptr is access all Character;
21964
21965 procedure Get_String(Buffer: Char_Ptr; Size : Integer);
21966 pragma Import (C, Get_String, "get_string");
21967
21968 Name : aliased String (1 .. Max_Length) := (others => ' ');
21969 Temp : Char_Ptr;
21970
21971 function Addr (S : String) return Char_Ptr is
21972 function To_Char_Ptr is
21973 new Ada.Unchecked_Conversion (System.Address, Char_Ptr);
21974 begin
21975 return To_Char_Ptr (S (S'First)'Address);
21976 end;
21977
21978 begin
21979 Temp := Addr (Name);
21980 Get_String (Temp, Max_Length);
21981 end;
21982 @end example
21983 @end quotation
21984
21985 where Get_String is a C function that uses the address in Temp to
21986 modify the variable @code{Name}. This code is dubious, and arguably
21987 erroneous, and the compiler would be entitled to assume that
21988 @code{Name} is never modified, and generate code accordingly.
21989
21990 However, in practice, this would cause some existing code that
21991 seems to work with no optimization to start failing at high
21992 levels of optimzization.
21993
21994 What the compiler does for such cases is to assume that marking
21995 a variable as aliased indicates that some "funny business" may
21996 be going on. The optimizer recognizes the aliased keyword and
21997 inhibits optimizations that assume the value cannot be assigned.
21998 This means that the above example will in fact "work" reliably,
21999 that is, it will produce the expected results.
22000
22001 @node Atomic Variables and Optimization,Passive Task Optimization,Aliased Variables and Optimization,Performance Considerations
22002 @anchor{gnat_ugn/gnat_and_program_execution atomic-variables-and-optimization}@anchor{1ad}@anchor{gnat_ugn/gnat_and_program_execution id38}@anchor{1ae}
22003 @subsubsection Atomic Variables and Optimization
22004
22005
22006 @geindex Atomic
22007
22008 There are two considerations with regard to performance when
22009 atomic variables are used.
22010
22011 First, the RM only guarantees that access to atomic variables
22012 be atomic, it has nothing to say about how this is achieved,
22013 though there is a strong implication that this should not be
22014 achieved by explicit locking code. Indeed GNAT will never
22015 generate any locking code for atomic variable access (it will
22016 simply reject any attempt to make a variable or type atomic
22017 if the atomic access cannot be achieved without such locking code).
22018
22019 That being said, it is important to understand that you cannot
22020 assume that the entire variable will always be accessed. Consider
22021 this example:
22022
22023 @quotation
22024
22025 @example
22026 type R is record
22027 A,B,C,D : Character;
22028 end record;
22029 for R'Size use 32;
22030 for R'Alignment use 4;
22031
22032 RV : R;
22033 pragma Atomic (RV);
22034 X : Character;
22035 ...
22036 X := RV.B;
22037 @end example
22038 @end quotation
22039
22040 You cannot assume that the reference to @code{RV.B}
22041 will read the entire 32-bit
22042 variable with a single load instruction. It is perfectly legitimate if
22043 the hardware allows it to do a byte read of just the B field. This read
22044 is still atomic, which is all the RM requires. GNAT can and does take
22045 advantage of this, depending on the architecture and optimization level.
22046 Any assumption to the contrary is non-portable and risky. Even if you
22047 examine the assembly language and see a full 32-bit load, this might
22048 change in a future version of the compiler.
22049
22050 If your application requires that all accesses to @code{RV} in this
22051 example be full 32-bit loads, you need to make a copy for the access
22052 as in:
22053
22054 @quotation
22055
22056 @example
22057 declare
22058 RV_Copy : constant R := RV;
22059 begin
22060 X := RV_Copy.B;
22061 end;
22062 @end example
22063 @end quotation
22064
22065 Now the reference to RV must read the whole variable.
22066 Actually one can imagine some compiler which figures
22067 out that the whole copy is not required (because only
22068 the B field is actually accessed), but GNAT
22069 certainly won't do that, and we don't know of any
22070 compiler that would not handle this right, and the
22071 above code will in practice work portably across
22072 all architectures (that permit the Atomic declaration).
22073
22074 The second issue with atomic variables has to do with
22075 the possible requirement of generating synchronization
22076 code. For more details on this, consult the sections on
22077 the pragmas Enable/Disable_Atomic_Synchronization in the
22078 GNAT Reference Manual. If performance is critical, and
22079 such synchronization code is not required, it may be
22080 useful to disable it.
22081
22082 @node Passive Task Optimization,,Atomic Variables and Optimization,Performance Considerations
22083 @anchor{gnat_ugn/gnat_and_program_execution passive-task-optimization}@anchor{1af}@anchor{gnat_ugn/gnat_and_program_execution id39}@anchor{1b0}
22084 @subsubsection Passive Task Optimization
22085
22086
22087 @geindex Passive Task
22088
22089 A passive task is one which is sufficiently simple that
22090 in theory a compiler could recognize it an implement it
22091 efficiently without creating a new thread. The original design
22092 of Ada 83 had in mind this kind of passive task optimization, but
22093 only a few Ada 83 compilers attempted it. The problem was that
22094 it was difficult to determine the exact conditions under which
22095 the optimization was possible. The result is a very fragile
22096 optimization where a very minor change in the program can
22097 suddenly silently make a task non-optimizable.
22098
22099 With the revisiting of this issue in Ada 95, there was general
22100 agreement that this approach was fundamentally flawed, and the
22101 notion of protected types was introduced. When using protected
22102 types, the restrictions are well defined, and you KNOW that the
22103 operations will be optimized, and furthermore this optimized
22104 performance is fully portable.
22105
22106 Although it would theoretically be possible for GNAT to attempt to
22107 do this optimization, but it really doesn't make sense in the
22108 context of Ada 95, and none of the Ada 95 compilers implement
22109 this optimization as far as we know. In particular GNAT never
22110 attempts to perform this optimization.
22111
22112 In any new Ada 95 code that is written, you should always
22113 use protected types in place of tasks that might be able to
22114 be optimized in this manner.
22115 Of course this does not help if you have legacy Ada 83 code
22116 that depends on this optimization, but it is unusual to encounter
22117 a case where the performance gains from this optimization
22118 are significant.
22119
22120 Your program should work correctly without this optimization. If
22121 you have performance problems, then the most practical
22122 approach is to figure out exactly where these performance problems
22123 arise, and update those particular tasks to be protected types. Note
22124 that typically clients of the tasks who call entries, will not have
22125 to be modified, only the task definition itself.
22126
22127 @node Text_IO Suggestions,Reducing Size of Executables with Unused Subprogram/Data Elimination,Performance Considerations,Improving Performance
22128 @anchor{gnat_ugn/gnat_and_program_execution text-io-suggestions}@anchor{1b1}@anchor{gnat_ugn/gnat_and_program_execution id40}@anchor{1b2}
22129 @subsection @code{Text_IO} Suggestions
22130
22131
22132 @geindex Text_IO and performance
22133
22134 The @code{Ada.Text_IO} package has fairly high overheads due in part to
22135 the requirement of maintaining page and line counts. If performance
22136 is critical, a recommendation is to use @code{Stream_IO} instead of
22137 @code{Text_IO} for volume output, since this package has less overhead.
22138
22139 If @code{Text_IO} must be used, note that by default output to the standard
22140 output and standard error files is unbuffered (this provides better
22141 behavior when output statements are used for debugging, or if the
22142 progress of a program is observed by tracking the output, e.g. by
22143 using the Unix @emph{tail -f} command to watch redirected output.
22144
22145 If you are generating large volumes of output with @code{Text_IO} and
22146 performance is an important factor, use a designated file instead
22147 of the standard output file, or change the standard output file to
22148 be buffered using @code{Interfaces.C_Streams.setvbuf}.
22149
22150 @node Reducing Size of Executables with Unused Subprogram/Data Elimination,,Text_IO Suggestions,Improving Performance
22151 @anchor{gnat_ugn/gnat_and_program_execution id41}@anchor{1b3}@anchor{gnat_ugn/gnat_and_program_execution reducing-size-of-executables-with-unused-subprogram-data-elimination}@anchor{1b4}
22152 @subsection Reducing Size of Executables with Unused Subprogram/Data Elimination
22153
22154
22155 @geindex Uunused subprogram/data elimination
22156
22157 This section describes how you can eliminate unused subprograms and data from
22158 your executable just by setting options at compilation time.
22159
22160 @menu
22161 * About unused subprogram/data elimination::
22162 * Compilation options::
22163 * Example of unused subprogram/data elimination::
22164
22165 @end menu
22166
22167 @node About unused subprogram/data elimination,Compilation options,,Reducing Size of Executables with Unused Subprogram/Data Elimination
22168 @anchor{gnat_ugn/gnat_and_program_execution id42}@anchor{1b5}@anchor{gnat_ugn/gnat_and_program_execution about-unused-subprogram-data-elimination}@anchor{1b6}
22169 @subsubsection About unused subprogram/data elimination
22170
22171
22172 By default, an executable contains all code and data of its composing objects
22173 (directly linked or coming from statically linked libraries), even data or code
22174 never used by this executable.
22175
22176 This feature will allow you to eliminate such unused code from your
22177 executable, making it smaller (in disk and in memory).
22178
22179 This functionality is available on all Linux platforms except for the IA-64
22180 architecture and on all cross platforms using the ELF binary file format.
22181 In both cases GNU binutils version 2.16 or later are required to enable it.
22182
22183 @node Compilation options,Example of unused subprogram/data elimination,About unused subprogram/data elimination,Reducing Size of Executables with Unused Subprogram/Data Elimination
22184 @anchor{gnat_ugn/gnat_and_program_execution id43}@anchor{1b7}@anchor{gnat_ugn/gnat_and_program_execution compilation-options}@anchor{1b8}
22185 @subsubsection Compilation options
22186
22187
22188 The operation of eliminating the unused code and data from the final executable
22189 is directly performed by the linker.
22190
22191 @geindex -ffunction-sections (gcc)
22192
22193 @geindex -fdata-sections (gcc)
22194
22195 In order to do this, it has to work with objects compiled with the
22196 following options:
22197 @code{-ffunction-sections} @code{-fdata-sections}.
22198
22199 These options are usable with C and Ada files.
22200 They will place respectively each
22201 function or data in a separate section in the resulting object file.
22202
22203 Once the objects and static libraries are created with these options, the
22204 linker can perform the dead code elimination. You can do this by setting
22205 the @code{-Wl,--gc-sections} option to gcc command or in the
22206 @code{-largs} section of @code{gnatmake}. This will perform a
22207 garbage collection of code and data never referenced.
22208
22209 If the linker performs a partial link (@code{-r} linker option), then you
22210 will need to provide the entry point using the @code{-e} / @code{--entry}
22211 linker option.
22212
22213 Note that objects compiled without the @code{-ffunction-sections} and
22214 @code{-fdata-sections} options can still be linked with the executable.
22215 However, no dead code elimination will be performed on those objects (they will
22216 be linked as is).
22217
22218 The GNAT static library is now compiled with -ffunction-sections and
22219 -fdata-sections on some platforms. This allows you to eliminate the unused code
22220 and data of the GNAT library from your executable.
22221
22222 @node Example of unused subprogram/data elimination,,Compilation options,Reducing Size of Executables with Unused Subprogram/Data Elimination
22223 @anchor{gnat_ugn/gnat_and_program_execution example-of-unused-subprogram-data-elimination}@anchor{1b9}@anchor{gnat_ugn/gnat_and_program_execution id44}@anchor{1ba}
22224 @subsubsection Example of unused subprogram/data elimination
22225
22226
22227 Here is a simple example:
22228
22229 @quotation
22230
22231 @example
22232 with Aux;
22233
22234 procedure Test is
22235 begin
22236 Aux.Used (10);
22237 end Test;
22238
22239 package Aux is
22240 Used_Data : Integer;
22241 Unused_Data : Integer;
22242
22243 procedure Used (Data : Integer);
22244 procedure Unused (Data : Integer);
22245 end Aux;
22246
22247 package body Aux is
22248 procedure Used (Data : Integer) is
22249 begin
22250 Used_Data := Data;
22251 end Used;
22252
22253 procedure Unused (Data : Integer) is
22254 begin
22255 Unused_Data := Data;
22256 end Unused;
22257 end Aux;
22258 @end example
22259 @end quotation
22260
22261 @code{Unused} and @code{Unused_Data} are never referenced in this code
22262 excerpt, and hence they may be safely removed from the final executable.
22263
22264 @quotation
22265
22266 @example
22267 $ gnatmake test
22268
22269 $ nm test | grep used
22270 020015f0 T aux__unused
22271 02005d88 B aux__unused_data
22272 020015cc T aux__used
22273 02005d84 B aux__used_data
22274
22275 $ gnatmake test -cargs -fdata-sections -ffunction-sections \\
22276 -largs -Wl,--gc-sections
22277
22278 $ nm test | grep used
22279 02005350 T aux__used
22280 0201ffe0 B aux__used_data
22281 @end example
22282 @end quotation
22283
22284 It can be observed that the procedure @code{Unused} and the object
22285 @code{Unused_Data} are removed by the linker when using the
22286 appropriate options.
22287
22288 @geindex Overflow checks
22289
22290 @geindex Checks (overflow)
22291
22292 @node Overflow Check Handling in GNAT,Performing Dimensionality Analysis in GNAT,Improving Performance,GNAT and Program Execution
22293 @anchor{gnat_ugn/gnat_and_program_execution id45}@anchor{169}@anchor{gnat_ugn/gnat_and_program_execution overflow-check-handling-in-gnat}@anchor{27}
22294 @section Overflow Check Handling in GNAT
22295
22296
22297 This section explains how to control the handling of overflow checks.
22298
22299 @menu
22300 * Background::
22301 * Management of Overflows in GNAT::
22302 * Specifying the Desired Mode::
22303 * Default Settings::
22304 * Implementation Notes::
22305
22306 @end menu
22307
22308 @node Background,Management of Overflows in GNAT,,Overflow Check Handling in GNAT
22309 @anchor{gnat_ugn/gnat_and_program_execution id46}@anchor{1bb}@anchor{gnat_ugn/gnat_and_program_execution background}@anchor{1bc}
22310 @subsection Background
22311
22312
22313 Overflow checks are checks that the compiler may make to ensure
22314 that intermediate results are not out of range. For example:
22315
22316 @quotation
22317
22318 @example
22319 A : Integer;
22320 ...
22321 A := A + 1;
22322 @end example
22323 @end quotation
22324
22325 If @code{A} has the value @code{Integer'Last}, then the addition may cause
22326 overflow since the result is out of range of the type @code{Integer}.
22327 In this case @code{Constraint_Error} will be raised if checks are
22328 enabled.
22329
22330 A trickier situation arises in examples like the following:
22331
22332 @quotation
22333
22334 @example
22335 A, C : Integer;
22336 ...
22337 A := (A + 1) + C;
22338 @end example
22339 @end quotation
22340
22341 where @code{A} is @code{Integer'Last} and @code{C} is @code{-1}.
22342 Now the final result of the expression on the right hand side is
22343 @code{Integer'Last} which is in range, but the question arises whether the
22344 intermediate addition of @code{(A + 1)} raises an overflow error.
22345
22346 The (perhaps surprising) answer is that the Ada language
22347 definition does not answer this question. Instead it leaves
22348 it up to the implementation to do one of two things if overflow
22349 checks are enabled.
22350
22351
22352 @itemize *
22353
22354 @item
22355 raise an exception (@code{Constraint_Error}), or
22356
22357 @item
22358 yield the correct mathematical result which is then used in
22359 subsequent operations.
22360 @end itemize
22361
22362 If the compiler chooses the first approach, then the assignment of this
22363 example will indeed raise @code{Constraint_Error} if overflow checking is
22364 enabled, or result in erroneous execution if overflow checks are suppressed.
22365
22366 But if the compiler
22367 chooses the second approach, then it can perform both additions yielding
22368 the correct mathematical result, which is in range, so no exception
22369 will be raised, and the right result is obtained, regardless of whether
22370 overflow checks are suppressed.
22371
22372 Note that in the first example an
22373 exception will be raised in either case, since if the compiler
22374 gives the correct mathematical result for the addition, it will
22375 be out of range of the target type of the assignment, and thus
22376 fails the range check.
22377
22378 This lack of specified behavior in the handling of overflow for
22379 intermediate results is a source of non-portability, and can thus
22380 be problematic when programs are ported. Most typically this arises
22381 in a situation where the original compiler did not raise an exception,
22382 and then the application is moved to a compiler where the check is
22383 performed on the intermediate result and an unexpected exception is
22384 raised.
22385
22386 Furthermore, when using Ada 2012's preconditions and other
22387 assertion forms, another issue arises. Consider:
22388
22389 @quotation
22390
22391 @example
22392 procedure P (A, B : Integer) with
22393 Pre => A + B <= Integer'Last;
22394 @end example
22395 @end quotation
22396
22397 One often wants to regard arithmetic in a context like this from
22398 a mathematical point of view. So for example, if the two actual parameters
22399 for a call to @code{P} are both @code{Integer'Last}, then
22400 the precondition should be regarded as False. If we are executing
22401 in a mode with run-time checks enabled for preconditions, then we would
22402 like this precondition to fail, rather than raising an exception
22403 because of the intermediate overflow.
22404
22405 However, the language definition leaves the specification of
22406 whether the above condition fails (raising @code{Assert_Error}) or
22407 causes an intermediate overflow (raising @code{Constraint_Error})
22408 up to the implementation.
22409
22410 The situation is worse in a case such as the following:
22411
22412 @quotation
22413
22414 @example
22415 procedure Q (A, B, C : Integer) with
22416 Pre => A + B + C <= Integer'Last;
22417 @end example
22418 @end quotation
22419
22420 Consider the call
22421
22422 @quotation
22423
22424 @example
22425 Q (A => Integer'Last, B => 1, C => -1);
22426 @end example
22427 @end quotation
22428
22429 From a mathematical point of view the precondition
22430 is True, but at run time we may (but are not guaranteed to) get an
22431 exception raised because of the intermediate overflow (and we really
22432 would prefer this precondition to be considered True at run time).
22433
22434 @node Management of Overflows in GNAT,Specifying the Desired Mode,Background,Overflow Check Handling in GNAT
22435 @anchor{gnat_ugn/gnat_and_program_execution id47}@anchor{1bd}@anchor{gnat_ugn/gnat_and_program_execution management-of-overflows-in-gnat}@anchor{1be}
22436 @subsection Management of Overflows in GNAT
22437
22438
22439 To deal with the portability issue, and with the problem of
22440 mathematical versus run-time interpretation of the expressions in
22441 assertions, GNAT provides comprehensive control over the handling
22442 of intermediate overflow. GNAT can operate in three modes, and
22443 furthemore, permits separate selection of operating modes for
22444 the expressions within assertions (here the term 'assertions'
22445 is used in the technical sense, which includes preconditions and so forth)
22446 and for expressions appearing outside assertions.
22447
22448 The three modes are:
22449
22450
22451 @itemize *
22452
22453 @item
22454 @emph{Use base type for intermediate operations} (@code{STRICT})
22455
22456 In this mode, all intermediate results for predefined arithmetic
22457 operators are computed using the base type, and the result must
22458 be in range of the base type. If this is not the
22459 case then either an exception is raised (if overflow checks are
22460 enabled) or the execution is erroneous (if overflow checks are suppressed).
22461 This is the normal default mode.
22462
22463 @item
22464 @emph{Most intermediate overflows avoided} (@code{MINIMIZED})
22465
22466 In this mode, the compiler attempts to avoid intermediate overflows by
22467 using a larger integer type, typically @code{Long_Long_Integer},
22468 as the type in which arithmetic is
22469 performed for predefined arithmetic operators. This may be slightly more
22470 expensive at
22471 run time (compared to suppressing intermediate overflow checks), though
22472 the cost is negligible on modern 64-bit machines. For the examples given
22473 earlier, no intermediate overflows would have resulted in exceptions,
22474 since the intermediate results are all in the range of
22475 @code{Long_Long_Integer} (typically 64-bits on nearly all implementations
22476 of GNAT). In addition, if checks are enabled, this reduces the number of
22477 checks that must be made, so this choice may actually result in an
22478 improvement in space and time behavior.
22479
22480 However, there are cases where @code{Long_Long_Integer} is not large
22481 enough, consider the following example:
22482
22483 @quotation
22484
22485 @example
22486 procedure R (A, B, C, D : Integer) with
22487 Pre => (A**2 * B**2) / (C**2 * D**2) <= 10;
22488 @end example
22489 @end quotation
22490
22491 where @code{A} = @code{B} = @code{C} = @code{D} = @code{Integer'Last}.
22492 Now the intermediate results are
22493 out of the range of @code{Long_Long_Integer} even though the final result
22494 is in range and the precondition is True (from a mathematical point
22495 of view). In such a case, operating in this mode, an overflow occurs
22496 for the intermediate computation (which is why this mode
22497 says @emph{most} intermediate overflows are avoided). In this case,
22498 an exception is raised if overflow checks are enabled, and the
22499 execution is erroneous if overflow checks are suppressed.
22500
22501 @item
22502 @emph{All intermediate overflows avoided} (@code{ELIMINATED})
22503
22504 In this mode, the compiler avoids all intermediate overflows
22505 by using arbitrary precision arithmetic as required. In this
22506 mode, the above example with @code{A**2 * B**2} would
22507 not cause intermediate overflow, because the intermediate result
22508 would be evaluated using sufficient precision, and the result
22509 of evaluating the precondition would be True.
22510
22511 This mode has the advantage of avoiding any intermediate
22512 overflows, but at the expense of significant run-time overhead,
22513 including the use of a library (included automatically in this
22514 mode) for multiple-precision arithmetic.
22515
22516 This mode provides cleaner semantics for assertions, since now
22517 the run-time behavior emulates true arithmetic behavior for the
22518 predefined arithmetic operators, meaning that there is never a
22519 conflict between the mathematical view of the assertion, and its
22520 run-time behavior.
22521
22522 Note that in this mode, the behavior is unaffected by whether or
22523 not overflow checks are suppressed, since overflow does not occur.
22524 It is possible for gigantic intermediate expressions to raise
22525 @code{Storage_Error} as a result of attempting to compute the
22526 results of such expressions (e.g. @code{Integer'Last ** Integer'Last})
22527 but overflow is impossible.
22528 @end itemize
22529
22530 Note that these modes apply only to the evaluation of predefined
22531 arithmetic, membership, and comparison operators for signed integer
22532 arithmetic.
22533
22534 For fixed-point arithmetic, checks can be suppressed. But if checks
22535 are enabled
22536 then fixed-point values are always checked for overflow against the
22537 base type for intermediate expressions (that is such checks always
22538 operate in the equivalent of @code{STRICT} mode).
22539
22540 For floating-point, on nearly all architectures, @code{Machine_Overflows}
22541 is False, and IEEE infinities are generated, so overflow exceptions
22542 are never raised. If you want to avoid infinities, and check that
22543 final results of expressions are in range, then you can declare a
22544 constrained floating-point type, and range checks will be carried
22545 out in the normal manner (with infinite values always failing all
22546 range checks).
22547
22548 @node Specifying the Desired Mode,Default Settings,Management of Overflows in GNAT,Overflow Check Handling in GNAT
22549 @anchor{gnat_ugn/gnat_and_program_execution specifying-the-desired-mode}@anchor{f8}@anchor{gnat_ugn/gnat_and_program_execution id48}@anchor{1bf}
22550 @subsection Specifying the Desired Mode
22551
22552
22553 @geindex pragma Overflow_Mode
22554
22555 The desired mode of for handling intermediate overflow can be specified using
22556 either the @code{Overflow_Mode} pragma or an equivalent compiler switch.
22557 The pragma has the form
22558
22559 @quotation
22560
22561 @example
22562 pragma Overflow_Mode ([General =>] MODE [, [Assertions =>] MODE]);
22563 @end example
22564 @end quotation
22565
22566 where @code{MODE} is one of
22567
22568
22569 @itemize *
22570
22571 @item
22572 @code{STRICT}: intermediate overflows checked (using base type)
22573
22574 @item
22575 @code{MINIMIZED}: minimize intermediate overflows
22576
22577 @item
22578 @code{ELIMINATED}: eliminate intermediate overflows
22579 @end itemize
22580
22581 The case is ignored, so @code{MINIMIZED}, @code{Minimized} and
22582 @code{minimized} all have the same effect.
22583
22584 If only the @code{General} parameter is present, then the given @code{MODE} applies
22585 to expressions both within and outside assertions. If both arguments
22586 are present, then @code{General} applies to expressions outside assertions,
22587 and @code{Assertions} applies to expressions within assertions. For example:
22588
22589 @quotation
22590
22591 @example
22592 pragma Overflow_Mode
22593 (General => Minimized, Assertions => Eliminated);
22594 @end example
22595 @end quotation
22596
22597 specifies that general expressions outside assertions be evaluated
22598 in 'minimize intermediate overflows' mode, and expressions within
22599 assertions be evaluated in 'eliminate intermediate overflows' mode.
22600 This is often a reasonable choice, avoiding excessive overhead
22601 outside assertions, but assuring a high degree of portability
22602 when importing code from another compiler, while incurring
22603 the extra overhead for assertion expressions to ensure that
22604 the behavior at run time matches the expected mathematical
22605 behavior.
22606
22607 The @code{Overflow_Mode} pragma has the same scoping and placement
22608 rules as pragma @code{Suppress}, so it can occur either as a
22609 configuration pragma, specifying a default for the whole
22610 program, or in a declarative scope, where it applies to the
22611 remaining declarations and statements in that scope.
22612
22613 Note that pragma @code{Overflow_Mode} does not affect whether
22614 overflow checks are enabled or suppressed. It only controls the
22615 method used to compute intermediate values. To control whether
22616 overflow checking is enabled or suppressed, use pragma @code{Suppress}
22617 or @code{Unsuppress} in the usual manner.
22618
22619 @geindex -gnato? (gcc)
22620
22621 @geindex -gnato?? (gcc)
22622
22623 Additionally, a compiler switch @code{-gnato?} or @code{-gnato??}
22624 can be used to control the checking mode default (which can be subsequently
22625 overridden using pragmas).
22626
22627 Here @code{?} is one of the digits @code{1} through @code{3}:
22628
22629 @quotation
22630
22631
22632 @multitable {xxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
22633 @item
22634
22635 @code{1}
22636
22637 @tab
22638
22639 use base type for intermediate operations (@code{STRICT})
22640
22641 @item
22642
22643 @code{2}
22644
22645 @tab
22646
22647 minimize intermediate overflows (@code{MINIMIZED})
22648
22649 @item
22650
22651 @code{3}
22652
22653 @tab
22654
22655 eliminate intermediate overflows (@code{ELIMINATED})
22656
22657 @end multitable
22658
22659 @end quotation
22660
22661 As with the pragma, if only one digit appears then it applies to all
22662 cases; if two digits are given, then the first applies outside
22663 assertions, and the second within assertions. Thus the equivalent
22664 of the example pragma above would be
22665 @code{-gnato23}.
22666
22667 If no digits follow the @code{-gnato}, then it is equivalent to
22668 @code{-gnato11},
22669 causing all intermediate operations to be computed using the base
22670 type (@code{STRICT} mode).
22671
22672 @node Default Settings,Implementation Notes,Specifying the Desired Mode,Overflow Check Handling in GNAT
22673 @anchor{gnat_ugn/gnat_and_program_execution id49}@anchor{1c0}@anchor{gnat_ugn/gnat_and_program_execution default-settings}@anchor{1c1}
22674 @subsection Default Settings
22675
22676
22677 The default mode for overflow checks is
22678
22679 @quotation
22680
22681 @example
22682 General => Strict
22683 @end example
22684 @end quotation
22685
22686 which causes all computations both inside and outside assertions to use
22687 the base type.
22688
22689 This retains compatibility with previous versions of
22690 GNAT which suppressed overflow checks by default and always
22691 used the base type for computation of intermediate results.
22692
22693 @c Sphinx allows no emphasis within :index: role. As a workaround we
22694 @c point the index to "switch" and use emphasis for "-gnato".
22695
22696 The
22697 @geindex -gnato (gcc)
22698 switch @code{-gnato} (with no digits following)
22699 is equivalent to
22700
22701 @quotation
22702
22703 @example
22704 General => Strict
22705 @end example
22706 @end quotation
22707
22708 which causes overflow checking of all intermediate overflows
22709 both inside and outside assertions against the base type.
22710
22711 The pragma @code{Suppress (Overflow_Check)} disables overflow
22712 checking, but it has no effect on the method used for computing
22713 intermediate results.
22714
22715 The pragma @code{Unsuppress (Overflow_Check)} enables overflow
22716 checking, but it has no effect on the method used for computing
22717 intermediate results.
22718
22719 @node Implementation Notes,,Default Settings,Overflow Check Handling in GNAT
22720 @anchor{gnat_ugn/gnat_and_program_execution implementation-notes}@anchor{1c2}@anchor{gnat_ugn/gnat_and_program_execution id50}@anchor{1c3}
22721 @subsection Implementation Notes
22722
22723
22724 In practice on typical 64-bit machines, the @code{MINIMIZED} mode is
22725 reasonably efficient, and can be generally used. It also helps
22726 to ensure compatibility with code imported from some other
22727 compiler to GNAT.
22728
22729 Setting all intermediate overflows checking (@code{CHECKED} mode)
22730 makes sense if you want to
22731 make sure that your code is compatible with any other possible
22732 Ada implementation. This may be useful in ensuring portability
22733 for code that is to be exported to some other compiler than GNAT.
22734
22735 The Ada standard allows the reassociation of expressions at
22736 the same precedence level if no parentheses are present. For
22737 example, @code{A+B+C} parses as though it were @code{(A+B)+C}, but
22738 the compiler can reintepret this as @code{A+(B+C)}, possibly
22739 introducing or eliminating an overflow exception. The GNAT
22740 compiler never takes advantage of this freedom, and the
22741 expression @code{A+B+C} will be evaluated as @code{(A+B)+C}.
22742 If you need the other order, you can write the parentheses
22743 explicitly @code{A+(B+C)} and GNAT will respect this order.
22744
22745 The use of @code{ELIMINATED} mode will cause the compiler to
22746 automatically include an appropriate arbitrary precision
22747 integer arithmetic package. The compiler will make calls
22748 to this package, though only in cases where it cannot be
22749 sure that @code{Long_Long_Integer} is sufficient to guard against
22750 intermediate overflows. This package does not use dynamic
22751 allocation, but it does use the secondary stack, so an
22752 appropriate secondary stack package must be present (this
22753 is always true for standard full Ada, but may require
22754 specific steps for restricted run times such as ZFP).
22755
22756 Although @code{ELIMINATED} mode causes expressions to use arbitrary
22757 precision arithmetic, avoiding overflow, the final result
22758 must be in an appropriate range. This is true even if the
22759 final result is of type @code{[Long_[Long_]]Integer'Base}, which
22760 still has the same bounds as its associated constrained
22761 type at run-time.
22762
22763 Currently, the @code{ELIMINATED} mode is only available on target
22764 platforms for which @code{Long_Long_Integer} is 64-bits (nearly all GNAT
22765 platforms).
22766
22767 @node Performing Dimensionality Analysis in GNAT,Stack Related Facilities,Overflow Check Handling in GNAT,GNAT and Program Execution
22768 @anchor{gnat_ugn/gnat_and_program_execution performing-dimensionality-analysis-in-gnat}@anchor{28}@anchor{gnat_ugn/gnat_and_program_execution id51}@anchor{16a}
22769 @section Performing Dimensionality Analysis in GNAT
22770
22771
22772 @geindex Dimensionality analysis
22773
22774 The GNAT compiler supports dimensionality checking. The user can
22775 specify physical units for objects, and the compiler will verify that uses
22776 of these objects are compatible with their dimensions, in a fashion that is
22777 familiar to engineering practice. The dimensions of algebraic expressions
22778 (including powers with static exponents) are computed from their constituents.
22779
22780 @geindex Dimension_System aspect
22781
22782 @geindex Dimension aspect
22783
22784 This feature depends on Ada 2012 aspect specifications, and is available from
22785 version 7.0.1 of GNAT onwards.
22786 The GNAT-specific aspect @code{Dimension_System}
22787 allows you to define a system of units; the aspect @code{Dimension}
22788 then allows the user to declare dimensioned quantities within a given system.
22789 (These aspects are described in the @emph{Implementation Defined Aspects}
22790 chapter of the @emph{GNAT Reference Manual}).
22791
22792 The major advantage of this model is that it does not require the declaration of
22793 multiple operators for all possible combinations of types: it is only necessary
22794 to use the proper subtypes in object declarations.
22795
22796 @geindex System.Dim.Mks package (GNAT library)
22797
22798 @geindex MKS_Type type
22799
22800 The simplest way to impose dimensionality checking on a computation is to make
22801 use of one of the instantiations of the package @code{System.Dim.Generic_Mks}, which
22802 are part of the GNAT library. This generic package defines a floating-point
22803 type @code{MKS_Type}, for which a sequence of dimension names are specified,
22804 together with their conventional abbreviations. The following should be read
22805 together with the full specification of the package, in file
22806 @code{s-digemk.ads}.
22807
22808 @quotation
22809
22810 @geindex s-digemk.ads file
22811
22812 @example
22813 type Mks_Type is new Float_Type
22814 with
22815 Dimension_System => (
22816 (Unit_Name => Meter, Unit_Symbol => 'm', Dim_Symbol => 'L'),
22817 (Unit_Name => Kilogram, Unit_Symbol => "kg", Dim_Symbol => 'M'),
22818 (Unit_Name => Second, Unit_Symbol => 's', Dim_Symbol => 'T'),
22819 (Unit_Name => Ampere, Unit_Symbol => 'A', Dim_Symbol => 'I'),
22820 (Unit_Name => Kelvin, Unit_Symbol => 'K', Dim_Symbol => "Theta"),
22821 (Unit_Name => Mole, Unit_Symbol => "mol", Dim_Symbol => 'N'),
22822 (Unit_Name => Candela, Unit_Symbol => "cd", Dim_Symbol => 'J'));
22823 @end example
22824 @end quotation
22825
22826 The package then defines a series of subtypes that correspond to these
22827 conventional units. For example:
22828
22829 @quotation
22830
22831 @example
22832 subtype Length is Mks_Type
22833 with
22834 Dimension => (Symbol => 'm', Meter => 1, others => 0);
22835 @end example
22836 @end quotation
22837
22838 and similarly for @code{Mass}, @code{Time}, @code{Electric_Current},
22839 @code{Thermodynamic_Temperature}, @code{Amount_Of_Substance}, and
22840 @code{Luminous_Intensity} (the standard set of units of the SI system).
22841
22842 The package also defines conventional names for values of each unit, for
22843 example:
22844
22845 @quotation
22846
22847 @example
22848 m : constant Length := 1.0;
22849 kg : constant Mass := 1.0;
22850 s : constant Time := 1.0;
22851 A : constant Electric_Current := 1.0;
22852 @end example
22853 @end quotation
22854
22855 as well as useful multiples of these units:
22856
22857 @quotation
22858
22859 @example
22860 cm : constant Length := 1.0E-02;
22861 g : constant Mass := 1.0E-03;
22862 min : constant Time := 60.0;
22863 day : constant Time := 60.0 * 24.0 * min;
22864 ...
22865 @end example
22866 @end quotation
22867
22868 There are three instantiations of @code{System.Dim.Generic_Mks} defined in the
22869 GNAT library:
22870
22871
22872 @itemize *
22873
22874 @item
22875 @code{System.Dim.Float_Mks} based on @code{Float} defined in @code{s-diflmk.ads}.
22876
22877 @item
22878 @code{System.Dim.Long_Mks} based on @code{Long_Float} defined in @code{s-dilomk.ads}.
22879
22880 @item
22881 @code{System.Dim.Mks} based on @code{Long_Long_Float} defined in @code{s-dimmks.ads}.
22882 @end itemize
22883
22884 Using one of these packages, you can then define a derived unit by providing
22885 the aspect that specifies its dimensions within the MKS system, as well as the
22886 string to be used for output of a value of that unit:
22887
22888 @quotation
22889
22890 @example
22891 subtype Acceleration is Mks_Type
22892 with Dimension => ("m/sec^2",
22893 Meter => 1,
22894 Second => -2,
22895 others => 0);
22896 @end example
22897 @end quotation
22898
22899 Here is a complete example of use:
22900
22901 @quotation
22902
22903 @example
22904 with System.Dim.MKS; use System.Dim.Mks;
22905 with System.Dim.Mks_IO; use System.Dim.Mks_IO;
22906 with Text_IO; use Text_IO;
22907 procedure Free_Fall is
22908 subtype Acceleration is Mks_Type
22909 with Dimension => ("m/sec^2", 1, 0, -2, others => 0);
22910 G : constant acceleration := 9.81 * m / (s ** 2);
22911 T : Time := 10.0*s;
22912 Distance : Length;
22913
22914 begin
22915 Put ("Gravitational constant: ");
22916 Put (G, Aft => 2, Exp => 0); Put_Line ("");
22917 Distance := 0.5 * G * T ** 2;
22918 Put ("distance travelled in 10 seconds of free fall ");
22919 Put (Distance, Aft => 2, Exp => 0);
22920 Put_Line ("");
22921 end Free_Fall;
22922 @end example
22923 @end quotation
22924
22925 Execution of this program yields:
22926
22927 @quotation
22928
22929 @example
22930 Gravitational constant: 9.81 m/sec^2
22931 distance travelled in 10 seconds of free fall 490.50 m
22932 @end example
22933 @end quotation
22934
22935 However, incorrect assignments such as:
22936
22937 @quotation
22938
22939 @example
22940 Distance := 5.0;
22941 Distance := 5.0 * kg;
22942 @end example
22943 @end quotation
22944
22945 are rejected with the following diagnoses:
22946
22947 @quotation
22948
22949 @example
22950 Distance := 5.0;
22951 >>> dimensions mismatch in assignment
22952 >>> left-hand side has dimension [L]
22953 >>> right-hand side is dimensionless
22954
22955 Distance := 5.0 * kg:
22956 >>> dimensions mismatch in assignment
22957 >>> left-hand side has dimension [L]
22958 >>> right-hand side has dimension [M]
22959 @end example
22960 @end quotation
22961
22962 The dimensions of an expression are properly displayed, even if there is
22963 no explicit subtype for it. If we add to the program:
22964
22965 @quotation
22966
22967 @example
22968 Put ("Final velocity: ");
22969 Put (G * T, Aft =>2, Exp =>0);
22970 Put_Line ("");
22971 @end example
22972 @end quotation
22973
22974 then the output includes:
22975
22976 @quotation
22977
22978 @example
22979 Final velocity: 98.10 m.s**(-1)
22980 @end example
22981
22982 @geindex Dimensionable type
22983
22984 @geindex Dimensioned subtype
22985 @end quotation
22986
22987 The type @code{Mks_Type} is said to be a @emph{dimensionable type} since it has a
22988 @code{Dimension_System} aspect, and the subtypes @code{Length}, @code{Mass}, etc.,
22989 are said to be @emph{dimensioned subtypes} since each one has a @code{Dimension}
22990 aspect.
22991
22992 @quotation
22993
22994 @geindex Dimension Vector (for a dimensioned subtype)
22995
22996 @geindex Dimension aspect
22997
22998 @geindex Dimension_System aspect
22999 @end quotation
23000
23001 The @code{Dimension} aspect of a dimensioned subtype @code{S} defines a mapping
23002 from the base type's Unit_Names to integer (or, more generally, rational)
23003 values. This mapping is the @emph{dimension vector} (also referred to as the
23004 @emph{dimensionality}) for that subtype, denoted by @code{DV(S)}, and thus for each
23005 object of that subtype. Intuitively, the value specified for each
23006 @code{Unit_Name} is the exponent associated with that unit; a zero value
23007 means that the unit is not used. For example:
23008
23009 @quotation
23010
23011 @example
23012 declare
23013 Acc : Acceleration;
23014 ...
23015 begin
23016 ...
23017 end;
23018 @end example
23019 @end quotation
23020
23021 Here @code{DV(Acc)} = @code{DV(Acceleration)} =
23022 @code{(Meter=>1, Kilogram=>0, Second=>-2, Ampere=>0, Kelvin=>0, Mole=>0, Candela=>0)}.
23023 Symbolically, we can express this as @code{Meter / Second**2}.
23024
23025 The dimension vector of an arithmetic expression is synthesized from the
23026 dimension vectors of its components, with compile-time dimensionality checks
23027 that help prevent mismatches such as using an @code{Acceleration} where a
23028 @code{Length} is required.
23029
23030 The dimension vector of the result of an arithmetic expression @emph{expr}, or
23031 @code{DV(@emph{expr})}, is defined as follows, assuming conventional
23032 mathematical definitions for the vector operations that are used:
23033
23034
23035 @itemize *
23036
23037 @item
23038 If @emph{expr} is of the type @emph{universal_real}, or is not of a dimensioned subtype,
23039 then @emph{expr} is dimensionless; @code{DV(@emph{expr})} is the empty vector.
23040
23041 @item
23042 @code{DV(@emph{op expr})}, where @emph{op} is a unary operator, is @code{DV(@emph{expr})}
23043
23044 @item
23045 @code{DV(@emph{expr1 op expr2})} where @emph{op} is "+" or "-" is @code{DV(@emph{expr1})}
23046 provided that @code{DV(@emph{expr1})} = @code{DV(@emph{expr2})}.
23047 If this condition is not met then the construct is illegal.
23048
23049 @item
23050 @code{DV(@emph{expr1} * @emph{expr2})} is @code{DV(@emph{expr1})} + @code{DV(@emph{expr2})},
23051 and @code{DV(@emph{expr1} / @emph{expr2})} = @code{DV(@emph{expr1})} - @code{DV(@emph{expr2})}.
23052 In this context if one of the @emph{expr}s is dimensionless then its empty
23053 dimension vector is treated as @code{(others => 0)}.
23054
23055 @item
23056 @code{DV(@emph{expr} ** @emph{power})} is @emph{power} * @code{DV(@emph{expr})},
23057 provided that @emph{power} is a static rational value. If this condition is not
23058 met then the construct is illegal.
23059 @end itemize
23060
23061 Note that, by the above rules, it is illegal to use binary "+" or "-" to
23062 combine a dimensioned and dimensionless value. Thus an expression such as
23063 @code{acc-10.0} is illegal, where @code{acc} is an object of subtype
23064 @code{Acceleration}.
23065
23066 The dimensionality checks for relationals use the same rules as
23067 for "+" and "-", except when comparing to a literal; thus
23068
23069 @quotation
23070
23071 @example
23072 acc > len
23073 @end example
23074 @end quotation
23075
23076 is equivalent to
23077
23078 @quotation
23079
23080 @example
23081 acc-len > 0.0
23082 @end example
23083 @end quotation
23084
23085 and is thus illegal, but
23086
23087 @quotation
23088
23089 @example
23090 acc > 10.0
23091 @end example
23092 @end quotation
23093
23094 is accepted with a warning. Analogously a conditional expression requires the
23095 same dimension vector for each branch (with no exception for literals).
23096
23097 The dimension vector of a type conversion @code{T(@emph{expr})} is defined
23098 as follows, based on the nature of @code{T}:
23099
23100
23101 @itemize *
23102
23103 @item
23104 If @code{T} is a dimensioned subtype then @code{DV(T(@emph{expr}))} is @code{DV(T)}
23105 provided that either @emph{expr} is dimensionless or
23106 @code{DV(T)} = @code{DV(@emph{expr})}. The conversion is illegal
23107 if @emph{expr} is dimensioned and @code{DV(@emph{expr})} /= @code{DV(T)}.
23108 Note that vector equality does not require that the corresponding
23109 Unit_Names be the same.
23110
23111 As a consequence of the above rule, it is possible to convert between
23112 different dimension systems that follow the same international system
23113 of units, with the seven physical components given in the standard order
23114 (length, mass, time, etc.). Thus a length in meters can be converted to
23115 a length in inches (with a suitable conversion factor) but cannot be
23116 converted, for example, to a mass in pounds.
23117
23118 @item
23119 If @code{T} is the base type for @emph{expr} (and the dimensionless root type of
23120 the dimension system), then @code{DV(T(@emph{expr}))} is @code{DV(expr)}.
23121 Thus, if @emph{expr} is of a dimensioned subtype of @code{T}, the conversion may
23122 be regarded as a "view conversion" that preserves dimensionality.
23123
23124 This rule makes it possible to write generic code that can be instantiated
23125 with compatible dimensioned subtypes. The generic unit will contain
23126 conversions that will consequently be present in instantiations, but
23127 conversions to the base type will preserve dimensionality and make it
23128 possible to write generic code that is correct with respect to
23129 dimensionality.
23130
23131 @item
23132 Otherwise (i.e., @code{T} is neither a dimensioned subtype nor a dimensionable
23133 base type), @code{DV(T(@emph{expr}))} is the empty vector. Thus a dimensioned
23134 value can be explicitly converted to a non-dimensioned subtype, which
23135 of course then escapes dimensionality analysis.
23136 @end itemize
23137
23138 The dimension vector for a type qualification @code{T'(@emph{expr})} is the same
23139 as for the type conversion @code{T(@emph{expr})}.
23140
23141 An assignment statement
23142
23143 @quotation
23144
23145 @example
23146 Source := Target;
23147 @end example
23148 @end quotation
23149
23150 requires @code{DV(Source)} = @code{DV(Target)}, and analogously for parameter
23151 passing (the dimension vector for the actual parameter must be equal to the
23152 dimension vector for the formal parameter).
23153
23154 @node Stack Related Facilities,Memory Management Issues,Performing Dimensionality Analysis in GNAT,GNAT and Program Execution
23155 @anchor{gnat_ugn/gnat_and_program_execution stack-related-facilities}@anchor{29}@anchor{gnat_ugn/gnat_and_program_execution id52}@anchor{16b}
23156 @section Stack Related Facilities
23157
23158
23159 This section describes some useful tools associated with stack
23160 checking and analysis. In
23161 particular, it deals with dynamic and static stack usage measurements.
23162
23163 @menu
23164 * Stack Overflow Checking::
23165 * Static Stack Usage Analysis::
23166 * Dynamic Stack Usage Analysis::
23167
23168 @end menu
23169
23170 @node Stack Overflow Checking,Static Stack Usage Analysis,,Stack Related Facilities
23171 @anchor{gnat_ugn/gnat_and_program_execution id53}@anchor{1c4}@anchor{gnat_ugn/gnat_and_program_execution stack-overflow-checking}@anchor{f4}
23172 @subsection Stack Overflow Checking
23173
23174
23175 @geindex Stack Overflow Checking
23176
23177 @geindex -fstack-check (gcc)
23178
23179 For most operating systems, @code{gcc} does not perform stack overflow
23180 checking by default. This means that if the main environment task or
23181 some other task exceeds the available stack space, then unpredictable
23182 behavior will occur. Most native systems offer some level of protection by
23183 adding a guard page at the end of each task stack. This mechanism is usually
23184 not enough for dealing properly with stack overflow situations because
23185 a large local variable could "jump" above the guard page.
23186 Furthermore, when the
23187 guard page is hit, there may not be any space left on the stack for executing
23188 the exception propagation code. Enabling stack checking avoids
23189 such situations.
23190
23191 To activate stack checking, compile all units with the @code{gcc} option
23192 @code{-fstack-check}. For example:
23193
23194 @quotation
23195
23196 @example
23197 $ gcc -c -fstack-check package1.adb
23198 @end example
23199 @end quotation
23200
23201 Units compiled with this option will generate extra instructions to check
23202 that any use of the stack (for procedure calls or for declaring local
23203 variables in declare blocks) does not exceed the available stack space.
23204 If the space is exceeded, then a @code{Storage_Error} exception is raised.
23205
23206 For declared tasks, the default stack size is defined by the GNAT runtime,
23207 whose size may be modified at bind time through the @code{-d} bind switch
23208 (@ref{11f,,Switches for gnatbind}). Task specific stack sizes may be set using the
23209 @code{Storage_Size} pragma.
23210
23211 For the environment task, the stack size is determined by the operating system.
23212 Consequently, to modify the size of the environment task please refer to your
23213 operating system documentation.
23214
23215 @node Static Stack Usage Analysis,Dynamic Stack Usage Analysis,Stack Overflow Checking,Stack Related Facilities
23216 @anchor{gnat_ugn/gnat_and_program_execution id54}@anchor{1c5}@anchor{gnat_ugn/gnat_and_program_execution static-stack-usage-analysis}@anchor{f5}
23217 @subsection Static Stack Usage Analysis
23218
23219
23220 @geindex Static Stack Usage Analysis
23221
23222 @geindex -fstack-usage
23223
23224 A unit compiled with @code{-fstack-usage} will generate an extra file
23225 that specifies
23226 the maximum amount of stack used, on a per-function basis.
23227 The file has the same
23228 basename as the target object file with a @code{.su} extension.
23229 Each line of this file is made up of three fields:
23230
23231
23232 @itemize *
23233
23234 @item
23235 The name of the function.
23236
23237 @item
23238 A number of bytes.
23239
23240 @item
23241 One or more qualifiers: @code{static}, @code{dynamic}, @code{bounded}.
23242 @end itemize
23243
23244 The second field corresponds to the size of the known part of the function
23245 frame.
23246
23247 The qualifier @code{static} means that the function frame size
23248 is purely static.
23249 It usually means that all local variables have a static size.
23250 In this case, the second field is a reliable measure of the function stack
23251 utilization.
23252
23253 The qualifier @code{dynamic} means that the function frame size is not static.
23254 It happens mainly when some local variables have a dynamic size. When this
23255 qualifier appears alone, the second field is not a reliable measure
23256 of the function stack analysis. When it is qualified with @code{bounded}, it
23257 means that the second field is a reliable maximum of the function stack
23258 utilization.
23259
23260 A unit compiled with @code{-Wstack-usage} will issue a warning for each
23261 subprogram whose stack usage might be larger than the specified amount of
23262 bytes. The wording is in keeping with the qualifier documented above.
23263
23264 @node Dynamic Stack Usage Analysis,,Static Stack Usage Analysis,Stack Related Facilities
23265 @anchor{gnat_ugn/gnat_and_program_execution id55}@anchor{1c6}@anchor{gnat_ugn/gnat_and_program_execution dynamic-stack-usage-analysis}@anchor{122}
23266 @subsection Dynamic Stack Usage Analysis
23267
23268
23269 It is possible to measure the maximum amount of stack used by a task, by
23270 adding a switch to @code{gnatbind}, as:
23271
23272 @quotation
23273
23274 @example
23275 $ gnatbind -u0 file
23276 @end example
23277 @end quotation
23278
23279 With this option, at each task termination, its stack usage is output on
23280 @code{stderr}.
23281 Note that this switch is not compatible with tools like
23282 Valgrind and DrMemory; they will report errors.
23283
23284 It is not always convenient to output the stack usage when the program
23285 is still running. Hence, it is possible to delay this output until program
23286 termination. for a given number of tasks specified as the argument of the
23287 @code{-u} option. For instance:
23288
23289 @quotation
23290
23291 @example
23292 $ gnatbind -u100 file
23293 @end example
23294 @end quotation
23295
23296 will buffer the stack usage information of the first 100 tasks to terminate and
23297 output this info at program termination. Results are displayed in four
23298 columns:
23299
23300 @quotation
23301
23302 @example
23303 Index | Task Name | Stack Size | Stack Usage
23304 @end example
23305 @end quotation
23306
23307 where:
23308
23309
23310 @itemize *
23311
23312 @item
23313 @emph{Index} is a number associated with each task.
23314
23315 @item
23316 @emph{Task Name} is the name of the task analyzed.
23317
23318 @item
23319 @emph{Stack Size} is the maximum size for the stack.
23320
23321 @item
23322 @emph{Stack Usage} is the measure done by the stack analyzer.
23323 In order to prevent overflow, the stack
23324 is not entirely analyzed, and it's not possible to know exactly how
23325 much has actually been used.
23326 @end itemize
23327
23328 By default the environment task stack, the stack that contains the main unit,
23329 is not processed. To enable processing of the environment task stack, the
23330 environment variable GNAT_STACK_LIMIT needs to be set to the maximum size of
23331 the environment task stack. This amount is given in kilobytes. For example:
23332
23333 @quotation
23334
23335 @example
23336 $ set GNAT_STACK_LIMIT 1600
23337 @end example
23338 @end quotation
23339
23340 would specify to the analyzer that the environment task stack has a limit
23341 of 1.6 megabytes. Any stack usage beyond this will be ignored by the analysis.
23342
23343 The package @code{GNAT.Task_Stack_Usage} provides facilities to get
23344 stack-usage reports at run time. See its body for the details.
23345
23346 @node Memory Management Issues,,Stack Related Facilities,GNAT and Program Execution
23347 @anchor{gnat_ugn/gnat_and_program_execution id56}@anchor{16c}@anchor{gnat_ugn/gnat_and_program_execution memory-management-issues}@anchor{2a}
23348 @section Memory Management Issues
23349
23350
23351 This section describes some useful memory pools provided in the GNAT library
23352 and in particular the GNAT Debug Pool facility, which can be used to detect
23353 incorrect uses of access values (including 'dangling references').
23354
23355
23356 @menu
23357 * Some Useful Memory Pools::
23358 * The GNAT Debug Pool Facility::
23359
23360 @end menu
23361
23362 @node Some Useful Memory Pools,The GNAT Debug Pool Facility,,Memory Management Issues
23363 @anchor{gnat_ugn/gnat_and_program_execution id57}@anchor{1c7}@anchor{gnat_ugn/gnat_and_program_execution some-useful-memory-pools}@anchor{1c8}
23364 @subsection Some Useful Memory Pools
23365
23366
23367 @geindex Memory Pool
23368
23369 @geindex storage
23370 @geindex pool
23371
23372 The @code{System.Pool_Global} package offers the Unbounded_No_Reclaim_Pool
23373 storage pool. Allocations use the standard system call @code{malloc} while
23374 deallocations use the standard system call @code{free}. No reclamation is
23375 performed when the pool goes out of scope. For performance reasons, the
23376 standard default Ada allocators/deallocators do not use any explicit storage
23377 pools but if they did, they could use this storage pool without any change in
23378 behavior. That is why this storage pool is used when the user
23379 manages to make the default implicit allocator explicit as in this example:
23380
23381 @quotation
23382
23383 @example
23384 type T1 is access Something;
23385 -- no Storage pool is defined for T2
23386
23387 type T2 is access Something_Else;
23388 for T2'Storage_Pool use T1'Storage_Pool;
23389 -- the above is equivalent to
23390 for T2'Storage_Pool use System.Pool_Global.Global_Pool_Object;
23391 @end example
23392 @end quotation
23393
23394 The @code{System.Pool_Local} package offers the @code{Unbounded_Reclaim_Pool} storage
23395 pool. The allocation strategy is similar to @code{Pool_Local}
23396 except that the all
23397 storage allocated with this pool is reclaimed when the pool object goes out of
23398 scope. This pool provides a explicit mechanism similar to the implicit one
23399 provided by several Ada 83 compilers for allocations performed through a local
23400 access type and whose purpose was to reclaim memory when exiting the
23401 scope of a given local access. As an example, the following program does not
23402 leak memory even though it does not perform explicit deallocation:
23403
23404 @quotation
23405
23406 @example
23407 with System.Pool_Local;
23408 procedure Pooloc1 is
23409 procedure Internal is
23410 type A is access Integer;
23411 X : System.Pool_Local.Unbounded_Reclaim_Pool;
23412 for A'Storage_Pool use X;
23413 v : A;
23414 begin
23415 for I in 1 .. 50 loop
23416 v := new Integer;
23417 end loop;
23418 end Internal;
23419 begin
23420 for I in 1 .. 100 loop
23421 Internal;
23422 end loop;
23423 end Pooloc1;
23424 @end example
23425 @end quotation
23426
23427 The @code{System.Pool_Size} package implements the @code{Stack_Bounded_Pool} used when
23428 @code{Storage_Size} is specified for an access type.
23429 The whole storage for the pool is
23430 allocated at once, usually on the stack at the point where the access type is
23431 elaborated. It is automatically reclaimed when exiting the scope where the
23432 access type is defined. This package is not intended to be used directly by the
23433 user and it is implicitly used for each such declaration:
23434
23435 @quotation
23436
23437 @example
23438 type T1 is access Something;
23439 for T1'Storage_Size use 10_000;
23440 @end example
23441 @end quotation
23442
23443 @node The GNAT Debug Pool Facility,,Some Useful Memory Pools,Memory Management Issues
23444 @anchor{gnat_ugn/gnat_and_program_execution id58}@anchor{1c9}@anchor{gnat_ugn/gnat_and_program_execution the-gnat-debug-pool-facility}@anchor{1ca}
23445 @subsection The GNAT Debug Pool Facility
23446
23447
23448 @geindex Debug Pool
23449
23450 @geindex storage
23451 @geindex pool
23452 @geindex memory corruption
23453
23454 The use of unchecked deallocation and unchecked conversion can easily
23455 lead to incorrect memory references. The problems generated by such
23456 references are usually difficult to tackle because the symptoms can be
23457 very remote from the origin of the problem. In such cases, it is
23458 very helpful to detect the problem as early as possible. This is the
23459 purpose of the Storage Pool provided by @code{GNAT.Debug_Pools}.
23460
23461 In order to use the GNAT specific debugging pool, the user must
23462 associate a debug pool object with each of the access types that may be
23463 related to suspected memory problems. See Ada Reference Manual 13.11.
23464
23465 @quotation
23466
23467 @example
23468 type Ptr is access Some_Type;
23469 Pool : GNAT.Debug_Pools.Debug_Pool;
23470 for Ptr'Storage_Pool use Pool;
23471 @end example
23472 @end quotation
23473
23474 @code{GNAT.Debug_Pools} is derived from a GNAT-specific kind of
23475 pool: the @code{Checked_Pool}. Such pools, like standard Ada storage pools,
23476 allow the user to redefine allocation and deallocation strategies. They
23477 also provide a checkpoint for each dereference, through the use of
23478 the primitive operation @code{Dereference} which is implicitly called at
23479 each dereference of an access value.
23480
23481 Once an access type has been associated with a debug pool, operations on
23482 values of the type may raise four distinct exceptions,
23483 which correspond to four potential kinds of memory corruption:
23484
23485
23486 @itemize *
23487
23488 @item
23489 @code{GNAT.Debug_Pools.Accessing_Not_Allocated_Storage}
23490
23491 @item
23492 @code{GNAT.Debug_Pools.Accessing_Deallocated_Storage}
23493
23494 @item
23495 @code{GNAT.Debug_Pools.Freeing_Not_Allocated_Storage}
23496
23497 @item
23498 @code{GNAT.Debug_Pools.Freeing_Deallocated_Storage}
23499 @end itemize
23500
23501 For types associated with a Debug_Pool, dynamic allocation is performed using
23502 the standard GNAT allocation routine. References to all allocated chunks of
23503 memory are kept in an internal dictionary. Several deallocation strategies are
23504 provided, whereupon the user can choose to release the memory to the system,
23505 keep it allocated for further invalid access checks, or fill it with an easily
23506 recognizable pattern for debug sessions. The memory pattern is the old IBM
23507 hexadecimal convention: @code{16#DEADBEEF#}.
23508
23509 See the documentation in the file g-debpoo.ads for more information on the
23510 various strategies.
23511
23512 Upon each dereference, a check is made that the access value denotes a
23513 properly allocated memory location. Here is a complete example of use of
23514 @code{Debug_Pools}, that includes typical instances of memory corruption:
23515
23516 @quotation
23517
23518 @example
23519 with Gnat.Io; use Gnat.Io;
23520 with Unchecked_Deallocation;
23521 with Unchecked_Conversion;
23522 with GNAT.Debug_Pools;
23523 with System.Storage_Elements;
23524 with Ada.Exceptions; use Ada.Exceptions;
23525 procedure Debug_Pool_Test is
23526
23527 type T is access Integer;
23528 type U is access all T;
23529
23530 P : GNAT.Debug_Pools.Debug_Pool;
23531 for T'Storage_Pool use P;
23532
23533 procedure Free is new Unchecked_Deallocation (Integer, T);
23534 function UC is new Unchecked_Conversion (U, T);
23535 A, B : aliased T;
23536
23537 procedure Info is new GNAT.Debug_Pools.Print_Info(Put_Line);
23538
23539 begin
23540 Info (P);
23541 A := new Integer;
23542 B := new Integer;
23543 B := A;
23544 Info (P);
23545 Free (A);
23546 begin
23547 Put_Line (Integer'Image(B.all));
23548 exception
23549 when E : others => Put_Line ("raised: " & Exception_Name (E));
23550 end;
23551 begin
23552 Free (B);
23553 exception
23554 when E : others => Put_Line ("raised: " & Exception_Name (E));
23555 end;
23556 B := UC(A'Access);
23557 begin
23558 Put_Line (Integer'Image(B.all));
23559 exception
23560 when E : others => Put_Line ("raised: " & Exception_Name (E));
23561 end;
23562 begin
23563 Free (B);
23564 exception
23565 when E : others => Put_Line ("raised: " & Exception_Name (E));
23566 end;
23567 Info (P);
23568 end Debug_Pool_Test;
23569 @end example
23570 @end quotation
23571
23572 The debug pool mechanism provides the following precise diagnostics on the
23573 execution of this erroneous program:
23574
23575 @quotation
23576
23577 @example
23578 Debug Pool info:
23579 Total allocated bytes : 0
23580 Total deallocated bytes : 0
23581 Current Water Mark: 0
23582 High Water Mark: 0
23583
23584 Debug Pool info:
23585 Total allocated bytes : 8
23586 Total deallocated bytes : 0
23587 Current Water Mark: 8
23588 High Water Mark: 8
23589
23590 raised: GNAT.DEBUG_POOLS.ACCESSING_DEALLOCATED_STORAGE
23591 raised: GNAT.DEBUG_POOLS.FREEING_DEALLOCATED_STORAGE
23592 raised: GNAT.DEBUG_POOLS.ACCESSING_NOT_ALLOCATED_STORAGE
23593 raised: GNAT.DEBUG_POOLS.FREEING_NOT_ALLOCATED_STORAGE
23594 Debug Pool info:
23595 Total allocated bytes : 8
23596 Total deallocated bytes : 4
23597 Current Water Mark: 4
23598 High Water Mark: 8
23599 @end example
23600 @end quotation
23601
23602
23603 @c -- Non-breaking space in running text
23604 @c -- E.g. Ada |nbsp| 95
23605
23606 @node Platform-Specific Information,Example of Binder Output File,GNAT and Program Execution,Top
23607 @anchor{gnat_ugn/platform_specific_information platform-specific-information}@anchor{d}@anchor{gnat_ugn/platform_specific_information doc}@anchor{1cb}@anchor{gnat_ugn/platform_specific_information id1}@anchor{1cc}
23608 @chapter Platform-Specific Information
23609
23610
23611 This appendix contains information relating to the implementation
23612 of run-time libraries on various platforms and also covers
23613 topics related to the GNAT implementation on Windows and Mac OS.
23614
23615 @menu
23616 * Run-Time Libraries::
23617 * Specifying a Run-Time Library::
23618 * GNU/Linux Topics::
23619 * Microsoft Windows Topics::
23620 * Mac OS Topics::
23621
23622 @end menu
23623
23624 @node Run-Time Libraries,Specifying a Run-Time Library,,Platform-Specific Information
23625 @anchor{gnat_ugn/platform_specific_information id2}@anchor{1cd}@anchor{gnat_ugn/platform_specific_information run-time-libraries}@anchor{2b}
23626 @section Run-Time Libraries
23627
23628
23629 @geindex Tasking and threads libraries
23630
23631 @geindex Threads libraries and tasking
23632
23633 @geindex Run-time libraries (platform-specific information)
23634
23635 The GNAT run-time implementation may vary with respect to both the
23636 underlying threads library and the exception-handling scheme.
23637 For threads support, the default run-time will bind to the thread
23638 package of the underlying operating system.
23639
23640 For exception handling, either or both of two models are supplied:
23641
23642 @quotation
23643
23644 @geindex Zero-Cost Exceptions
23645
23646 @geindex ZCX (Zero-Cost Exceptions)
23647 @end quotation
23648
23649
23650 @itemize *
23651
23652 @item
23653 @strong{Zero-Cost Exceptions} ("ZCX"),
23654 which uses binder-generated tables that
23655 are interrogated at run time to locate a handler.
23656
23657 @geindex setjmp/longjmp Exception Model
23658
23659 @geindex SJLJ (setjmp/longjmp Exception Model)
23660
23661 @item
23662 @strong{setjmp / longjmp} ('SJLJ'),
23663 which uses dynamically-set data to establish
23664 the set of handlers
23665 @end itemize
23666
23667 Most programs should experience a substantial speed improvement by
23668 being compiled with a ZCX run-time.
23669 This is especially true for
23670 tasking applications or applications with many exception handlers.
23671 Note however that the ZCX run-time does not support asynchronous abort
23672 of tasks (@code{abort} and @code{select-then-abort} constructs) and will instead
23673 implement abort by polling points in the runtime. You can also add additional
23674 polling points explicitly if needed in your application via @code{pragma
23675 Abort_Defer}.
23676
23677 This section summarizes which combinations of threads and exception support
23678 are supplied on various GNAT platforms.
23679
23680 @menu
23681 * Summary of Run-Time Configurations::
23682
23683 @end menu
23684
23685 @node Summary of Run-Time Configurations,,,Run-Time Libraries
23686 @anchor{gnat_ugn/platform_specific_information summary-of-run-time-configurations}@anchor{1ce}@anchor{gnat_ugn/platform_specific_information id3}@anchor{1cf}
23687 @subsection Summary of Run-Time Configurations
23688
23689
23690
23691 @multitable {xxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxx} {xxxxxxxxxxxxxx}
23692 @headitem
23693
23694 Platform
23695
23696 @tab
23697
23698 Run-Time
23699
23700 @tab
23701
23702 Tasking
23703
23704 @tab
23705
23706 Exceptions
23707
23708 @item
23709
23710 GNU/Linux
23711
23712 @tab
23713
23714 rts-native
23715 (default)
23716
23717 @tab
23718
23719 pthread library
23720
23721 @tab
23722
23723 ZCX
23724
23725 @item
23726
23727 rts-sjlj
23728
23729 @tab
23730
23731 pthread library
23732
23733 @tab
23734
23735 SJLJ
23736
23737 @item
23738
23739 Windows
23740
23741 @tab
23742
23743 rts-native
23744 (default)
23745
23746 @tab
23747
23748 native Win32 threads
23749
23750 @tab
23751
23752 ZCX
23753
23754 @item
23755
23756 rts-sjlj
23757
23758 @tab
23759
23760 native Win32 threads
23761
23762 @tab
23763
23764 SJLJ
23765
23766 @item
23767
23768 Mac OS
23769
23770 @tab
23771
23772 rts-native
23773
23774 @tab
23775
23776 pthread library
23777
23778 @tab
23779
23780 ZCX
23781
23782 @end multitable
23783
23784
23785 @node Specifying a Run-Time Library,GNU/Linux Topics,Run-Time Libraries,Platform-Specific Information
23786 @anchor{gnat_ugn/platform_specific_information specifying-a-run-time-library}@anchor{1d0}@anchor{gnat_ugn/platform_specific_information id4}@anchor{1d1}
23787 @section Specifying a Run-Time Library
23788
23789
23790 The @code{adainclude} subdirectory containing the sources of the GNAT
23791 run-time library, and the @code{adalib} subdirectory containing the
23792 @code{ALI} files and the static and/or shared GNAT library, are located
23793 in the gcc target-dependent area:
23794
23795 @quotation
23796
23797 @example
23798 target=$prefix/lib/gcc/gcc-*dumpmachine*/gcc-*dumpversion*/
23799 @end example
23800 @end quotation
23801
23802 As indicated above, on some platforms several run-time libraries are supplied.
23803 These libraries are installed in the target dependent area and
23804 contain a complete source and binary subdirectory. The detailed description
23805 below explains the differences between the different libraries in terms of
23806 their thread support.
23807
23808 The default run-time library (when GNAT is installed) is @emph{rts-native}.
23809 This default run-time is selected by the means of soft links.
23810 For example on x86-linux:
23811
23812 @c --
23813 @c -- $(target-dir)
23814 @c -- |
23815 @c -- +--- adainclude----------+
23816 @c -- | |
23817 @c -- +--- adalib-----------+ |
23818 @c -- | | |
23819 @c -- +--- rts-native | |
23820 @c -- | | | |
23821 @c -- | +--- adainclude <---+
23822 @c -- | | |
23823 @c -- | +--- adalib <----+
23824 @c -- |
23825 @c -- +--- rts-sjlj
23826 @c -- |
23827 @c -- +--- adainclude
23828 @c -- |
23829 @c -- +--- adalib
23830
23831
23832 @example
23833 $(target-dir)
23834 __/ / \ \___
23835 _______/ / \ \_________________
23836 / / \ \
23837 / / \ \
23838 ADAINCLUDE ADALIB rts-native rts-sjlj
23839 : : / \ / \
23840 : : / \ / \
23841 : : / \ / \
23842 : : / \ / \
23843 +-------------> adainclude adalib adainclude adalib
23844 : ^
23845 : :
23846 +---------------------+
23847
23848 Run-Time Library Directory Structure
23849 (Upper-case names and dotted/dashed arrows represent soft links)
23850 @end example
23851
23852 If the @emph{rts-sjlj} library is to be selected on a permanent basis,
23853 these soft links can be modified with the following commands:
23854
23855 @quotation
23856
23857 @example
23858 $ cd $target
23859 $ rm -f adainclude adalib
23860 $ ln -s rts-sjlj/adainclude adainclude
23861 $ ln -s rts-sjlj/adalib adalib
23862 @end example
23863 @end quotation
23864
23865 Alternatively, you can specify @code{rts-sjlj/adainclude} in the file
23866 @code{$target/ada_source_path} and @code{rts-sjlj/adalib} in
23867 @code{$target/ada_object_path}.
23868
23869 @geindex --RTS option
23870
23871 Selecting another run-time library temporarily can be
23872 achieved by using the @code{--RTS} switch, e.g., @code{--RTS=sjlj}
23873 @anchor{gnat_ugn/platform_specific_information choosing-the-scheduling-policy}@anchor{1d2}
23874 @geindex SCHED_FIFO scheduling policy
23875
23876 @geindex SCHED_RR scheduling policy
23877
23878 @geindex SCHED_OTHER scheduling policy
23879
23880 @menu
23881 * Choosing the Scheduling Policy::
23882
23883 @end menu
23884
23885 @node Choosing the Scheduling Policy,,,Specifying a Run-Time Library
23886 @anchor{gnat_ugn/platform_specific_information id5}@anchor{1d3}
23887 @subsection Choosing the Scheduling Policy
23888
23889
23890 When using a POSIX threads implementation, you have a choice of several
23891 scheduling policies: @code{SCHED_FIFO}, @code{SCHED_RR} and @code{SCHED_OTHER}.
23892
23893 Typically, the default is @code{SCHED_OTHER}, while using @code{SCHED_FIFO}
23894 or @code{SCHED_RR} requires special (e.g., root) privileges.
23895
23896 @geindex pragma Time_Slice
23897
23898 @geindex -T0 option
23899
23900 @geindex pragma Task_Dispatching_Policy
23901
23902 By default, GNAT uses the @code{SCHED_OTHER} policy. To specify
23903 @code{SCHED_FIFO},
23904 you can use one of the following:
23905
23906
23907 @itemize *
23908
23909 @item
23910 @code{pragma Time_Slice (0.0)}
23911
23912 @item
23913 the corresponding binder option @code{-T0}
23914
23915 @item
23916 @code{pragma Task_Dispatching_Policy (FIFO_Within_Priorities)}
23917 @end itemize
23918
23919 To specify @code{SCHED_RR},
23920 you should use @code{pragma Time_Slice} with a
23921 value greater than 0.0, or else use the corresponding @code{-T}
23922 binder option.
23923
23924 To make sure a program is running as root, you can put something like
23925 this in a library package body in your application:
23926
23927 @quotation
23928
23929 @example
23930 function geteuid return Integer;
23931 pragma Import (C, geteuid, "geteuid");
23932 Ignore : constant Boolean :=
23933 (if geteuid = 0 then True else raise Program_Error with "must be root");
23934 @end example
23935 @end quotation
23936
23937 It gets the effective user id, and if it's not 0 (i.e. root), it raises
23938 Program_Error.
23939
23940 @geindex Linux
23941
23942 @geindex GNU/Linux
23943
23944 @node GNU/Linux Topics,Microsoft Windows Topics,Specifying a Run-Time Library,Platform-Specific Information
23945 @anchor{gnat_ugn/platform_specific_information id6}@anchor{1d4}@anchor{gnat_ugn/platform_specific_information gnu-linux-topics}@anchor{1d5}
23946 @section GNU/Linux Topics
23947
23948
23949 This section describes topics that are specific to GNU/Linux platforms.
23950
23951 @menu
23952 * Required Packages on GNU/Linux::
23953
23954 @end menu
23955
23956 @node Required Packages on GNU/Linux,,,GNU/Linux Topics
23957 @anchor{gnat_ugn/platform_specific_information id7}@anchor{1d6}@anchor{gnat_ugn/platform_specific_information required-packages-on-gnu-linux}@anchor{1d7}
23958 @subsection Required Packages on GNU/Linux
23959
23960
23961 GNAT requires the C library developer's package to be installed.
23962 The name of of that package depends on your GNU/Linux distribution:
23963
23964
23965 @itemize *
23966
23967 @item
23968 RedHat, SUSE: @code{glibc-devel};
23969
23970 @item
23971 Debian, Ubuntu: @code{libc6-dev} (normally installed by default).
23972 @end itemize
23973
23974 If using the 32-bit version of GNAT on a 64-bit version of GNU/Linux,
23975 you'll need the 32-bit version of the following packages:
23976
23977
23978 @itemize *
23979
23980 @item
23981 RedHat, SUSE: @code{glibc.i686}, @code{glibc-devel.i686}, @code{ncurses-libs.i686}
23982
23983 @item
23984 Debian, Ubuntu: @code{libc6:i386}, @code{libc6-dev:i386}, @code{lib32ncursesw5}
23985 @end itemize
23986
23987 Other GNU/Linux distributions might be choosing a different name
23988 for those packages.
23989
23990 @geindex Windows
23991
23992 @node Microsoft Windows Topics,Mac OS Topics,GNU/Linux Topics,Platform-Specific Information
23993 @anchor{gnat_ugn/platform_specific_information microsoft-windows-topics}@anchor{2c}@anchor{gnat_ugn/platform_specific_information id8}@anchor{1d8}
23994 @section Microsoft Windows Topics
23995
23996
23997 This section describes topics that are specific to the Microsoft Windows
23998 platforms.
23999
24000
24001 @menu
24002 * Using GNAT on Windows::
24003 * Using a network installation of GNAT::
24004 * CONSOLE and WINDOWS subsystems::
24005 * Temporary Files::
24006 * Disabling Command Line Argument Expansion::
24007 * Windows Socket Timeouts::
24008 * Mixed-Language Programming on Windows::
24009 * Windows Specific Add-Ons::
24010
24011 @end menu
24012
24013 @node Using GNAT on Windows,Using a network installation of GNAT,,Microsoft Windows Topics
24014 @anchor{gnat_ugn/platform_specific_information using-gnat-on-windows}@anchor{1d9}@anchor{gnat_ugn/platform_specific_information id9}@anchor{1da}
24015 @subsection Using GNAT on Windows
24016
24017
24018 One of the strengths of the GNAT technology is that its tool set
24019 (@code{gcc}, @code{gnatbind}, @code{gnatlink}, @code{gnatmake}, the
24020 @code{gdb} debugger, etc.) is used in the same way regardless of the
24021 platform.
24022
24023 On Windows this tool set is complemented by a number of Microsoft-specific
24024 tools that have been provided to facilitate interoperability with Windows
24025 when this is required. With these tools:
24026
24027
24028 @itemize *
24029
24030 @item
24031 You can build applications using the @code{CONSOLE} or @code{WINDOWS}
24032 subsystems.
24033
24034 @item
24035 You can use any Dynamically Linked Library (DLL) in your Ada code (both
24036 relocatable and non-relocatable DLLs are supported).
24037
24038 @item
24039 You can build Ada DLLs for use in other applications. These applications
24040 can be written in a language other than Ada (e.g., C, C++, etc). Again both
24041 relocatable and non-relocatable Ada DLLs are supported.
24042
24043 @item
24044 You can include Windows resources in your Ada application.
24045
24046 @item
24047 You can use or create COM/DCOM objects.
24048 @end itemize
24049
24050 Immediately below are listed all known general GNAT-for-Windows restrictions.
24051 Other restrictions about specific features like Windows Resources and DLLs
24052 are listed in separate sections below.
24053
24054
24055 @itemize *
24056
24057 @item
24058 It is not possible to use @code{GetLastError} and @code{SetLastError}
24059 when tasking, protected records, or exceptions are used. In these
24060 cases, in order to implement Ada semantics, the GNAT run-time system
24061 calls certain Win32 routines that set the last error variable to 0 upon
24062 success. It should be possible to use @code{GetLastError} and
24063 @code{SetLastError} when tasking, protected record, and exception
24064 features are not used, but it is not guaranteed to work.
24065
24066 @item
24067 It is not possible to link against Microsoft C++ libraries except for
24068 import libraries. Interfacing must be done by the mean of DLLs.
24069
24070 @item
24071 It is possible to link against Microsoft C libraries. Yet the preferred
24072 solution is to use C/C++ compiler that comes with GNAT, since it
24073 doesn't require having two different development environments and makes the
24074 inter-language debugging experience smoother.
24075
24076 @item
24077 When the compilation environment is located on FAT32 drives, users may
24078 experience recompilations of the source files that have not changed if
24079 Daylight Saving Time (DST) state has changed since the last time files
24080 were compiled. NTFS drives do not have this problem.
24081
24082 @item
24083 No components of the GNAT toolset use any entries in the Windows
24084 registry. The only entries that can be created are file associations and
24085 PATH settings, provided the user has chosen to create them at installation
24086 time, as well as some minimal book-keeping information needed to correctly
24087 uninstall or integrate different GNAT products.
24088 @end itemize
24089
24090 @node Using a network installation of GNAT,CONSOLE and WINDOWS subsystems,Using GNAT on Windows,Microsoft Windows Topics
24091 @anchor{gnat_ugn/platform_specific_information id10}@anchor{1db}@anchor{gnat_ugn/platform_specific_information using-a-network-installation-of-gnat}@anchor{1dc}
24092 @subsection Using a network installation of GNAT
24093
24094
24095 Make sure the system on which GNAT is installed is accessible from the
24096 current machine, i.e., the install location is shared over the network.
24097 Shared resources are accessed on Windows by means of UNC paths, which
24098 have the format @code{\\\\server\\sharename\\path}
24099
24100 In order to use such a network installation, simply add the UNC path of the
24101 @code{bin} directory of your GNAT installation in front of your PATH. For
24102 example, if GNAT is installed in @code{\GNAT} directory of a share location
24103 called @code{c-drive} on a machine @code{LOKI}, the following command will
24104 make it available:
24105
24106 @quotation
24107
24108 @example
24109 $ path \\loki\c-drive\gnat\bin;%path%`
24110 @end example
24111 @end quotation
24112
24113 Be aware that every compilation using the network installation results in the
24114 transfer of large amounts of data across the network and will likely cause
24115 serious performance penalty.
24116
24117 @node CONSOLE and WINDOWS subsystems,Temporary Files,Using a network installation of GNAT,Microsoft Windows Topics
24118 @anchor{gnat_ugn/platform_specific_information id11}@anchor{1dd}@anchor{gnat_ugn/platform_specific_information console-and-windows-subsystems}@anchor{1de}
24119 @subsection CONSOLE and WINDOWS subsystems
24120
24121
24122 @geindex CONSOLE Subsystem
24123
24124 @geindex WINDOWS Subsystem
24125
24126 @geindex -mwindows
24127
24128 There are two main subsystems under Windows. The @code{CONSOLE} subsystem
24129 (which is the default subsystem) will always create a console when
24130 launching the application. This is not something desirable when the
24131 application has a Windows GUI. To get rid of this console the
24132 application must be using the @code{WINDOWS} subsystem. To do so
24133 the @code{-mwindows} linker option must be specified.
24134
24135 @quotation
24136
24137 @example
24138 $ gnatmake winprog -largs -mwindows
24139 @end example
24140 @end quotation
24141
24142 @node Temporary Files,Disabling Command Line Argument Expansion,CONSOLE and WINDOWS subsystems,Microsoft Windows Topics
24143 @anchor{gnat_ugn/platform_specific_information id12}@anchor{1df}@anchor{gnat_ugn/platform_specific_information temporary-files}@anchor{1e0}
24144 @subsection Temporary Files
24145
24146
24147 @geindex Temporary files
24148
24149 It is possible to control where temporary files gets created by setting
24150 the
24151 @geindex TMP
24152 @geindex environment variable; TMP
24153 @code{TMP} environment variable. The file will be created:
24154
24155
24156 @itemize *
24157
24158 @item
24159 Under the directory pointed to by the
24160 @geindex TMP
24161 @geindex environment variable; TMP
24162 @code{TMP} environment variable if
24163 this directory exists.
24164
24165 @item
24166 Under @code{c:\temp}, if the
24167 @geindex TMP
24168 @geindex environment variable; TMP
24169 @code{TMP} environment variable is not
24170 set (or not pointing to a directory) and if this directory exists.
24171
24172 @item
24173 Under the current working directory otherwise.
24174 @end itemize
24175
24176 This allows you to determine exactly where the temporary
24177 file will be created. This is particularly useful in networked
24178 environments where you may not have write access to some
24179 directories.
24180
24181 @node Disabling Command Line Argument Expansion,Windows Socket Timeouts,Temporary Files,Microsoft Windows Topics
24182 @anchor{gnat_ugn/platform_specific_information disabling-command-line-argument-expansion}@anchor{1e1}
24183 @subsection Disabling Command Line Argument Expansion
24184
24185
24186 @geindex Command Line Argument Expansion
24187
24188 By default, an executable compiled for the Windows platform will do
24189 the following postprocessing on the arguments passed on the command
24190 line:
24191
24192
24193 @itemize *
24194
24195 @item
24196 If the argument contains the characters @code{*} and/or @code{?}, then
24197 file expansion will be attempted. For example, if the current directory
24198 contains @code{a.txt} and @code{b.txt}, then when calling:
24199
24200 @example
24201 $ my_ada_program *.txt
24202 @end example
24203
24204 The following arguments will effectively be passed to the main program
24205 (for example when using @code{Ada.Command_Line.Argument}):
24206
24207 @example
24208 Ada.Command_Line.Argument (1) -> "a.txt"
24209 Ada.Command_Line.Argument (2) -> "b.txt"
24210 @end example
24211
24212 @item
24213 Filename expansion can be disabled for a given argument by using single
24214 quotes. Thus, calling:
24215
24216 @example
24217 $ my_ada_program '*.txt'
24218 @end example
24219
24220 will result in:
24221
24222 @example
24223 Ada.Command_Line.Argument (1) -> "*.txt"
24224 @end example
24225 @end itemize
24226
24227 Note that if the program is launched from a shell such as Cygwin Bash
24228 then quote removal might be performed by the shell.
24229
24230 In some contexts it might be useful to disable this feature (for example if
24231 the program performs its own argument expansion). In order to do this, a C
24232 symbol needs to be defined and set to @code{0}. You can do this by
24233 adding the following code fragment in one of your Ada units:
24234
24235 @example
24236 Do_Argv_Expansion : Integer := 0;
24237 pragma Export (C, Do_Argv_Expansion, "__gnat_do_argv_expansion");
24238 @end example
24239
24240 The results of previous examples will be respectively:
24241
24242 @example
24243 Ada.Command_Line.Argument (1) -> "*.txt"
24244 @end example
24245
24246 and:
24247
24248 @example
24249 Ada.Command_Line.Argument (1) -> "'*.txt'"
24250 @end example
24251
24252 @node Windows Socket Timeouts,Mixed-Language Programming on Windows,Disabling Command Line Argument Expansion,Microsoft Windows Topics
24253 @anchor{gnat_ugn/platform_specific_information windows-socket-timeouts}@anchor{1e2}
24254 @subsection Windows Socket Timeouts
24255
24256
24257 Microsoft Windows desktops older than @code{8.0} and Microsoft Windows Servers
24258 older than @code{2019} set a socket timeout 500 milliseconds longer than the value
24259 set by setsockopt with @code{SO_RCVTIMEO} and @code{SO_SNDTIMEO} options. The GNAT
24260 runtime makes a correction for the difference in the corresponding Windows
24261 versions. For Windows Server starting with version @code{2019}, the user must
24262 provide a manifest file for the GNAT runtime to be able to recognize that
24263 the Windows version does not need the timeout correction. The manifest file
24264 should be located in the same directory as the executable file, and its file
24265 name must match the executable name suffixed by @code{.manifest}. For example,
24266 if the executable name is @code{sock_wto.exe}, then the manifest file name
24267 has to be @code{sock_wto.exe.manifest}. The manifest file must contain at
24268 least the following data:
24269
24270 @example
24271 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
24272 <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
24273 <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
24274 <application>
24275 <!-- Windows Vista -->
24276 <supportedOS Id="@{e2011457-1546-43c5-a5fe-008deee3d3f0@}"/>
24277 <!-- Windows 7 -->
24278 <supportedOS Id="@{35138b9a-5d96-4fbd-8e2d-a2440225f93a@}"/>
24279 <!-- Windows 8 -->
24280 <supportedOS Id="@{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38@}"/>
24281 <!-- Windows 8.1 -->
24282 <supportedOS Id="@{1f676c76-80e1-4239-95bb-83d0f6d0da78@}"/>
24283 <!-- Windows 10 -->
24284 <supportedOS Id="@{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a@}"/>
24285 </application>
24286 </compatibility>
24287 </assembly>
24288 @end example
24289
24290 Without the manifest file, the socket timeout is going to be overcorrected on
24291 these Windows Server versions and the actual time is going to be 500
24292 milliseconds shorter than what was set with GNAT.Sockets.Set_Socket_Option.
24293 Note that on Microsoft Windows versions where correction is necessary, there
24294 is no way to set a socket timeout shorter than 500 ms. If a socket timeout
24295 shorter than 500 ms is needed on these Windows versions, a call to
24296 Check_Selector should be added before any socket read or write operations.
24297
24298 @node Mixed-Language Programming on Windows,Windows Specific Add-Ons,Windows Socket Timeouts,Microsoft Windows Topics
24299 @anchor{gnat_ugn/platform_specific_information id13}@anchor{1e3}@anchor{gnat_ugn/platform_specific_information mixed-language-programming-on-windows}@anchor{1e4}
24300 @subsection Mixed-Language Programming on Windows
24301
24302
24303 Developing pure Ada applications on Windows is no different than on
24304 other GNAT-supported platforms. However, when developing or porting an
24305 application that contains a mix of Ada and C/C++, the choice of your
24306 Windows C/C++ development environment conditions your overall
24307 interoperability strategy.
24308
24309 If you use @code{gcc} or Microsoft C to compile the non-Ada part of
24310 your application, there are no Windows-specific restrictions that
24311 affect the overall interoperability with your Ada code. If you do want
24312 to use the Microsoft tools for your C++ code, you have two choices:
24313
24314
24315 @itemize *
24316
24317 @item
24318 Encapsulate your C++ code in a DLL to be linked with your Ada
24319 application. In this case, use the Microsoft or whatever environment to
24320 build the DLL and use GNAT to build your executable
24321 (@ref{1e5,,Using DLLs with GNAT}).
24322
24323 @item
24324 Or you can encapsulate your Ada code in a DLL to be linked with the
24325 other part of your application. In this case, use GNAT to build the DLL
24326 (@ref{1e6,,Building DLLs with GNAT Project files}) and use the Microsoft
24327 or whatever environment to build your executable.
24328 @end itemize
24329
24330 In addition to the description about C main in
24331 @ref{44,,Mixed Language Programming} section, if the C main uses a
24332 stand-alone library it is required on x86-windows to
24333 setup the SEH context. For this the C main must looks like this:
24334
24335 @quotation
24336
24337 @example
24338 /* main.c */
24339 extern void adainit (void);
24340 extern void adafinal (void);
24341 extern void __gnat_initialize(void*);
24342 extern void call_to_ada (void);
24343
24344 int main (int argc, char *argv[])
24345 @{
24346 int SEH [2];
24347
24348 /* Initialize the SEH context */
24349 __gnat_initialize (&SEH);
24350
24351 adainit();
24352
24353 /* Then call Ada services in the stand-alone library */
24354
24355 call_to_ada();
24356
24357 adafinal();
24358 @}
24359 @end example
24360 @end quotation
24361
24362 Note that this is not needed on x86_64-windows where the Windows
24363 native SEH support is used.
24364
24365 @menu
24366 * Windows Calling Conventions::
24367 * Introduction to Dynamic Link Libraries (DLLs): Introduction to Dynamic Link Libraries DLLs.
24368 * Using DLLs with GNAT::
24369 * Building DLLs with GNAT Project files::
24370 * Building DLLs with GNAT::
24371 * Building DLLs with gnatdll::
24372 * Ada DLLs and Finalization::
24373 * Creating a Spec for Ada DLLs::
24374 * GNAT and Windows Resources::
24375 * Using GNAT DLLs from Microsoft Visual Studio Applications::
24376 * Debugging a DLL::
24377 * Setting Stack Size from gnatlink::
24378 * Setting Heap Size from gnatlink::
24379
24380 @end menu
24381
24382 @node Windows Calling Conventions,Introduction to Dynamic Link Libraries DLLs,,Mixed-Language Programming on Windows
24383 @anchor{gnat_ugn/platform_specific_information windows-calling-conventions}@anchor{1e7}@anchor{gnat_ugn/platform_specific_information id14}@anchor{1e8}
24384 @subsubsection Windows Calling Conventions
24385
24386
24387 @geindex Stdcall
24388
24389 @geindex APIENTRY
24390
24391 This section pertain only to Win32. On Win64 there is a single native
24392 calling convention. All convention specifiers are ignored on this
24393 platform.
24394
24395 When a subprogram @code{F} (caller) calls a subprogram @code{G}
24396 (callee), there are several ways to push @code{G}'s parameters on the
24397 stack and there are several possible scenarios to clean up the stack
24398 upon @code{G}'s return. A calling convention is an agreed upon software
24399 protocol whereby the responsibilities between the caller (@code{F}) and
24400 the callee (@code{G}) are clearly defined. Several calling conventions
24401 are available for Windows:
24402
24403
24404 @itemize *
24405
24406 @item
24407 @code{C} (Microsoft defined)
24408
24409 @item
24410 @code{Stdcall} (Microsoft defined)
24411
24412 @item
24413 @code{Win32} (GNAT specific)
24414
24415 @item
24416 @code{DLL} (GNAT specific)
24417 @end itemize
24418
24419 @menu
24420 * C Calling Convention::
24421 * Stdcall Calling Convention::
24422 * Win32 Calling Convention::
24423 * DLL Calling Convention::
24424
24425 @end menu
24426
24427 @node C Calling Convention,Stdcall Calling Convention,,Windows Calling Conventions
24428 @anchor{gnat_ugn/platform_specific_information c-calling-convention}@anchor{1e9}@anchor{gnat_ugn/platform_specific_information id15}@anchor{1ea}
24429 @subsubsection @code{C} Calling Convention
24430
24431
24432 This is the default calling convention used when interfacing to C/C++
24433 routines compiled with either @code{gcc} or Microsoft Visual C++.
24434
24435 In the @code{C} calling convention subprogram parameters are pushed on the
24436 stack by the caller from right to left. The caller itself is in charge of
24437 cleaning up the stack after the call. In addition, the name of a routine
24438 with @code{C} calling convention is mangled by adding a leading underscore.
24439
24440 The name to use on the Ada side when importing (or exporting) a routine
24441 with @code{C} calling convention is the name of the routine. For
24442 instance the C function:
24443
24444 @quotation
24445
24446 @example
24447 int get_val (long);
24448 @end example
24449 @end quotation
24450
24451 should be imported from Ada as follows:
24452
24453 @quotation
24454
24455 @example
24456 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
24457 pragma Import (C, Get_Val, External_Name => "get_val");
24458 @end example
24459 @end quotation
24460
24461 Note that in this particular case the @code{External_Name} parameter could
24462 have been omitted since, when missing, this parameter is taken to be the
24463 name of the Ada entity in lower case. When the @code{Link_Name} parameter
24464 is missing, as in the above example, this parameter is set to be the
24465 @code{External_Name} with a leading underscore.
24466
24467 When importing a variable defined in C, you should always use the @code{C}
24468 calling convention unless the object containing the variable is part of a
24469 DLL (in which case you should use the @code{Stdcall} calling
24470 convention, @ref{1eb,,Stdcall Calling Convention}).
24471
24472 @node Stdcall Calling Convention,Win32 Calling Convention,C Calling Convention,Windows Calling Conventions
24473 @anchor{gnat_ugn/platform_specific_information stdcall-calling-convention}@anchor{1eb}@anchor{gnat_ugn/platform_specific_information id16}@anchor{1ec}
24474 @subsubsection @code{Stdcall} Calling Convention
24475
24476
24477 This convention, which was the calling convention used for Pascal
24478 programs, is used by Microsoft for all the routines in the Win32 API for
24479 efficiency reasons. It must be used to import any routine for which this
24480 convention was specified.
24481
24482 In the @code{Stdcall} calling convention subprogram parameters are pushed
24483 on the stack by the caller from right to left. The callee (and not the
24484 caller) is in charge of cleaning the stack on routine exit. In addition,
24485 the name of a routine with @code{Stdcall} calling convention is mangled by
24486 adding a leading underscore (as for the @code{C} calling convention) and a
24487 trailing @code{@@@emph{nn}}, where @code{nn} is the overall size (in
24488 bytes) of the parameters passed to the routine.
24489
24490 The name to use on the Ada side when importing a C routine with a
24491 @code{Stdcall} calling convention is the name of the C routine. The leading
24492 underscore and trailing @code{@@@emph{nn}} are added automatically by
24493 the compiler. For instance the Win32 function:
24494
24495 @quotation
24496
24497 @example
24498 APIENTRY int get_val (long);
24499 @end example
24500 @end quotation
24501
24502 should be imported from Ada as follows:
24503
24504 @quotation
24505
24506 @example
24507 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
24508 pragma Import (Stdcall, Get_Val);
24509 -- On the x86 a long is 4 bytes, so the Link_Name is "_get_val@@4"
24510 @end example
24511 @end quotation
24512
24513 As for the @code{C} calling convention, when the @code{External_Name}
24514 parameter is missing, it is taken to be the name of the Ada entity in lower
24515 case. If instead of writing the above import pragma you write:
24516
24517 @quotation
24518
24519 @example
24520 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
24521 pragma Import (Stdcall, Get_Val, External_Name => "retrieve_val");
24522 @end example
24523 @end quotation
24524
24525 then the imported routine is @code{_retrieve_val@@4}. However, if instead
24526 of specifying the @code{External_Name} parameter you specify the
24527 @code{Link_Name} as in the following example:
24528
24529 @quotation
24530
24531 @example
24532 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
24533 pragma Import (Stdcall, Get_Val, Link_Name => "retrieve_val");
24534 @end example
24535 @end quotation
24536
24537 then the imported routine is @code{retrieve_val}, that is, there is no
24538 decoration at all. No leading underscore and no Stdcall suffix
24539 @code{@@@emph{nn}}.
24540
24541 This is especially important as in some special cases a DLL's entry
24542 point name lacks a trailing @code{@@@emph{nn}} while the exported
24543 name generated for a call has it.
24544
24545 It is also possible to import variables defined in a DLL by using an
24546 import pragma for a variable. As an example, if a DLL contains a
24547 variable defined as:
24548
24549 @quotation
24550
24551 @example
24552 int my_var;
24553 @end example
24554 @end quotation
24555
24556 then, to access this variable from Ada you should write:
24557
24558 @quotation
24559
24560 @example
24561 My_Var : Interfaces.C.int;
24562 pragma Import (Stdcall, My_Var);
24563 @end example
24564 @end quotation
24565
24566 Note that to ease building cross-platform bindings this convention
24567 will be handled as a @code{C} calling convention on non-Windows platforms.
24568
24569 @node Win32 Calling Convention,DLL Calling Convention,Stdcall Calling Convention,Windows Calling Conventions
24570 @anchor{gnat_ugn/platform_specific_information win32-calling-convention}@anchor{1ed}@anchor{gnat_ugn/platform_specific_information id17}@anchor{1ee}
24571 @subsubsection @code{Win32} Calling Convention
24572
24573
24574 This convention, which is GNAT-specific is fully equivalent to the
24575 @code{Stdcall} calling convention described above.
24576
24577 @node DLL Calling Convention,,Win32 Calling Convention,Windows Calling Conventions
24578 @anchor{gnat_ugn/platform_specific_information id18}@anchor{1ef}@anchor{gnat_ugn/platform_specific_information dll-calling-convention}@anchor{1f0}
24579 @subsubsection @code{DLL} Calling Convention
24580
24581
24582 This convention, which is GNAT-specific is fully equivalent to the
24583 @code{Stdcall} calling convention described above.
24584
24585 @node Introduction to Dynamic Link Libraries DLLs,Using DLLs with GNAT,Windows Calling Conventions,Mixed-Language Programming on Windows
24586 @anchor{gnat_ugn/platform_specific_information id19}@anchor{1f1}@anchor{gnat_ugn/platform_specific_information introduction-to-dynamic-link-libraries-dlls}@anchor{1f2}
24587 @subsubsection Introduction to Dynamic Link Libraries (DLLs)
24588
24589
24590 @geindex DLL
24591
24592 A Dynamically Linked Library (DLL) is a library that can be shared by
24593 several applications running under Windows. A DLL can contain any number of
24594 routines and variables.
24595
24596 One advantage of DLLs is that you can change and enhance them without
24597 forcing all the applications that depend on them to be relinked or
24598 recompiled. However, you should be aware than all calls to DLL routines are
24599 slower since, as you will understand below, such calls are indirect.
24600
24601 To illustrate the remainder of this section, suppose that an application
24602 wants to use the services of a DLL @code{API.dll}. To use the services
24603 provided by @code{API.dll} you must statically link against the DLL or
24604 an import library which contains a jump table with an entry for each
24605 routine and variable exported by the DLL. In the Microsoft world this
24606 import library is called @code{API.lib}. When using GNAT this import
24607 library is called either @code{libAPI.dll.a}, @code{libapi.dll.a},
24608 @code{libAPI.a} or @code{libapi.a} (names are case insensitive).
24609
24610 After you have linked your application with the DLL or the import library
24611 and you run your application, here is what happens:
24612
24613
24614 @itemize *
24615
24616 @item
24617 Your application is loaded into memory.
24618
24619 @item
24620 The DLL @code{API.dll} is mapped into the address space of your
24621 application. This means that:
24622
24623
24624 @itemize -
24625
24626 @item
24627 The DLL will use the stack of the calling thread.
24628
24629 @item
24630 The DLL will use the virtual address space of the calling process.
24631
24632 @item
24633 The DLL will allocate memory from the virtual address space of the calling
24634 process.
24635
24636 @item
24637 Handles (pointers) can be safely exchanged between routines in the DLL
24638 routines and routines in the application using the DLL.
24639 @end itemize
24640
24641 @item
24642 The entries in the jump table (from the import library @code{libAPI.dll.a}
24643 or @code{API.lib} or automatically created when linking against a DLL)
24644 which is part of your application are initialized with the addresses
24645 of the routines and variables in @code{API.dll}.
24646
24647 @item
24648 If present in @code{API.dll}, routines @code{DllMain} or
24649 @code{DllMainCRTStartup} are invoked. These routines typically contain
24650 the initialization code needed for the well-being of the routines and
24651 variables exported by the DLL.
24652 @end itemize
24653
24654 There is an additional point which is worth mentioning. In the Windows
24655 world there are two kind of DLLs: relocatable and non-relocatable
24656 DLLs. Non-relocatable DLLs can only be loaded at a very specific address
24657 in the target application address space. If the addresses of two
24658 non-relocatable DLLs overlap and these happen to be used by the same
24659 application, a conflict will occur and the application will run
24660 incorrectly. Hence, when possible, it is always preferable to use and
24661 build relocatable DLLs. Both relocatable and non-relocatable DLLs are
24662 supported by GNAT. Note that the @code{-s} linker option (see GNU Linker
24663 User's Guide) removes the debugging symbols from the DLL but the DLL can
24664 still be relocated.
24665
24666 As a side note, an interesting difference between Microsoft DLLs and
24667 Unix shared libraries, is the fact that on most Unix systems all public
24668 routines are exported by default in a Unix shared library, while under
24669 Windows it is possible (but not required) to list exported routines in
24670 a definition file (see @ref{1f3,,The Definition File}).
24671
24672 @node Using DLLs with GNAT,Building DLLs with GNAT Project files,Introduction to Dynamic Link Libraries DLLs,Mixed-Language Programming on Windows
24673 @anchor{gnat_ugn/platform_specific_information id20}@anchor{1f4}@anchor{gnat_ugn/platform_specific_information using-dlls-with-gnat}@anchor{1e5}
24674 @subsubsection Using DLLs with GNAT
24675
24676
24677 To use the services of a DLL, say @code{API.dll}, in your Ada application
24678 you must have:
24679
24680
24681 @itemize *
24682
24683 @item
24684 The Ada spec for the routines and/or variables you want to access in
24685 @code{API.dll}. If not available this Ada spec must be built from the C/C++
24686 header files provided with the DLL.
24687
24688 @item
24689 The import library (@code{libAPI.dll.a} or @code{API.lib}). As previously
24690 mentioned an import library is a statically linked library containing the
24691 import table which will be filled at load time to point to the actual
24692 @code{API.dll} routines. Sometimes you don't have an import library for the
24693 DLL you want to use. The following sections will explain how to build
24694 one. Note that this is optional.
24695
24696 @item
24697 The actual DLL, @code{API.dll}.
24698 @end itemize
24699
24700 Once you have all the above, to compile an Ada application that uses the
24701 services of @code{API.dll} and whose main subprogram is @code{My_Ada_App},
24702 you simply issue the command
24703
24704 @quotation
24705
24706 @example
24707 $ gnatmake my_ada_app -largs -lAPI
24708 @end example
24709 @end quotation
24710
24711 The argument @code{-largs -lAPI} at the end of the @code{gnatmake} command
24712 tells the GNAT linker to look for an import library. The linker will
24713 look for a library name in this specific order:
24714
24715
24716 @itemize *
24717
24718 @item
24719 @code{libAPI.dll.a}
24720
24721 @item
24722 @code{API.dll.a}
24723
24724 @item
24725 @code{libAPI.a}
24726
24727 @item
24728 @code{API.lib}
24729
24730 @item
24731 @code{libAPI.dll}
24732
24733 @item
24734 @code{API.dll}
24735 @end itemize
24736
24737 The first three are the GNU style import libraries. The third is the
24738 Microsoft style import libraries. The last two are the actual DLL names.
24739
24740 Note that if the Ada package spec for @code{API.dll} contains the
24741 following pragma
24742
24743 @quotation
24744
24745 @example
24746 pragma Linker_Options ("-lAPI");
24747 @end example
24748 @end quotation
24749
24750 you do not have to add @code{-largs -lAPI} at the end of the
24751 @code{gnatmake} command.
24752
24753 If any one of the items above is missing you will have to create it
24754 yourself. The following sections explain how to do so using as an
24755 example a fictitious DLL called @code{API.dll}.
24756
24757 @menu
24758 * Creating an Ada Spec for the DLL Services::
24759 * Creating an Import Library::
24760
24761 @end menu
24762
24763 @node Creating an Ada Spec for the DLL Services,Creating an Import Library,,Using DLLs with GNAT
24764 @anchor{gnat_ugn/platform_specific_information id21}@anchor{1f5}@anchor{gnat_ugn/platform_specific_information creating-an-ada-spec-for-the-dll-services}@anchor{1f6}
24765 @subsubsection Creating an Ada Spec for the DLL Services
24766
24767
24768 A DLL typically comes with a C/C++ header file which provides the
24769 definitions of the routines and variables exported by the DLL. The Ada
24770 equivalent of this header file is a package spec that contains definitions
24771 for the imported entities. If the DLL you intend to use does not come with
24772 an Ada spec you have to generate one such spec yourself. For example if
24773 the header file of @code{API.dll} is a file @code{api.h} containing the
24774 following two definitions:
24775
24776 @quotation
24777
24778 @example
24779 int some_var;
24780 int get (char *);
24781 @end example
24782 @end quotation
24783
24784 then the equivalent Ada spec could be:
24785
24786 @quotation
24787
24788 @example
24789 with Interfaces.C.Strings;
24790 package API is
24791 use Interfaces;
24792
24793 Some_Var : C.int;
24794 function Get (Str : C.Strings.Chars_Ptr) return C.int;
24795
24796 private
24797 pragma Import (C, Get);
24798 pragma Import (DLL, Some_Var);
24799 end API;
24800 @end example
24801 @end quotation
24802
24803 @node Creating an Import Library,,Creating an Ada Spec for the DLL Services,Using DLLs with GNAT
24804 @anchor{gnat_ugn/platform_specific_information id22}@anchor{1f7}@anchor{gnat_ugn/platform_specific_information creating-an-import-library}@anchor{1f8}
24805 @subsubsection Creating an Import Library
24806
24807
24808 @geindex Import library
24809
24810 If a Microsoft-style import library @code{API.lib} or a GNAT-style
24811 import library @code{libAPI.dll.a} or @code{libAPI.a} is available
24812 with @code{API.dll} you can skip this section. You can also skip this
24813 section if @code{API.dll} or @code{libAPI.dll} is built with GNU tools
24814 as in this case it is possible to link directly against the
24815 DLL. Otherwise read on.
24816
24817 @geindex Definition file
24818 @anchor{gnat_ugn/platform_specific_information the-definition-file}@anchor{1f3}
24819 @subsubheading The Definition File
24820
24821
24822 As previously mentioned, and unlike Unix systems, the list of symbols
24823 that are exported from a DLL must be provided explicitly in Windows.
24824 The main goal of a definition file is precisely that: list the symbols
24825 exported by a DLL. A definition file (usually a file with a @code{.def}
24826 suffix) has the following structure:
24827
24828 @quotation
24829
24830 @example
24831 [LIBRARY `@w{`}name`@w{`}]
24832 [DESCRIPTION `@w{`}string`@w{`}]
24833 EXPORTS
24834 `@w{`}symbol1`@w{`}
24835 `@w{`}symbol2`@w{`}
24836 ...
24837 @end example
24838 @end quotation
24839
24840
24841 @table @asis
24842
24843 @item @emph{LIBRARY name}
24844
24845 This section, which is optional, gives the name of the DLL.
24846
24847 @item @emph{DESCRIPTION string}
24848
24849 This section, which is optional, gives a description string that will be
24850 embedded in the import library.
24851
24852 @item @emph{EXPORTS}
24853
24854 This section gives the list of exported symbols (procedures, functions or
24855 variables). For instance in the case of @code{API.dll} the @code{EXPORTS}
24856 section of @code{API.def} looks like:
24857
24858 @example
24859 EXPORTS
24860 some_var
24861 get
24862 @end example
24863 @end table
24864
24865 Note that you must specify the correct suffix (@code{@@@emph{nn}})
24866 (see @ref{1e7,,Windows Calling Conventions}) for a Stdcall
24867 calling convention function in the exported symbols list.
24868
24869 There can actually be other sections in a definition file, but these
24870 sections are not relevant to the discussion at hand.
24871 @anchor{gnat_ugn/platform_specific_information create-def-file-automatically}@anchor{1f9}
24872 @subsubheading Creating a Definition File Automatically
24873
24874
24875 You can automatically create the definition file @code{API.def}
24876 (see @ref{1f3,,The Definition File}) from a DLL.
24877 For that use the @code{dlltool} program as follows:
24878
24879 @quotation
24880
24881 @example
24882 $ dlltool API.dll -z API.def --export-all-symbols
24883 @end example
24884
24885 Note that if some routines in the DLL have the @code{Stdcall} convention
24886 (@ref{1e7,,Windows Calling Conventions}) with stripped @code{@@@emph{nn}}
24887 suffix then you'll have to edit @code{api.def} to add it, and specify
24888 @code{-k} to @code{gnatdll} when creating the import library.
24889
24890 Here are some hints to find the right @code{@@@emph{nn}} suffix.
24891
24892
24893 @itemize -
24894
24895 @item
24896 If you have the Microsoft import library (.lib), it is possible to get
24897 the right symbols by using Microsoft @code{dumpbin} tool (see the
24898 corresponding Microsoft documentation for further details).
24899
24900 @example
24901 $ dumpbin /exports api.lib
24902 @end example
24903
24904 @item
24905 If you have a message about a missing symbol at link time the compiler
24906 tells you what symbol is expected. You just have to go back to the
24907 definition file and add the right suffix.
24908 @end itemize
24909 @end quotation
24910 @anchor{gnat_ugn/platform_specific_information gnat-style-import-library}@anchor{1fa}
24911 @subsubheading GNAT-Style Import Library
24912
24913
24914 To create a static import library from @code{API.dll} with the GNAT tools
24915 you should create the .def file, then use @code{gnatdll} tool
24916 (see @ref{1fb,,Using gnatdll}) as follows:
24917
24918 @quotation
24919
24920 @example
24921 $ gnatdll -e API.def -d API.dll
24922 @end example
24923
24924 @code{gnatdll} takes as input a definition file @code{API.def} and the
24925 name of the DLL containing the services listed in the definition file
24926 @code{API.dll}. The name of the static import library generated is
24927 computed from the name of the definition file as follows: if the
24928 definition file name is @code{xyz.def}, the import library name will
24929 be @code{libxyz.a}. Note that in the previous example option
24930 @code{-e} could have been removed because the name of the definition
24931 file (before the @code{.def} suffix) is the same as the name of the
24932 DLL (@ref{1fb,,Using gnatdll} for more information about @code{gnatdll}).
24933 @end quotation
24934 @anchor{gnat_ugn/platform_specific_information msvs-style-import-library}@anchor{1fc}
24935 @subsubheading Microsoft-Style Import Library
24936
24937
24938 A Microsoft import library is needed only if you plan to make an
24939 Ada DLL available to applications developed with Microsoft
24940 tools (@ref{1e4,,Mixed-Language Programming on Windows}).
24941
24942 To create a Microsoft-style import library for @code{API.dll} you
24943 should create the .def file, then build the actual import library using
24944 Microsoft's @code{lib} utility:
24945
24946 @quotation
24947
24948 @example
24949 $ lib -machine:IX86 -def:API.def -out:API.lib
24950 @end example
24951
24952 If you use the above command the definition file @code{API.def} must
24953 contain a line giving the name of the DLL:
24954
24955 @example
24956 LIBRARY "API"
24957 @end example
24958
24959 See the Microsoft documentation for further details about the usage of
24960 @code{lib}.
24961 @end quotation
24962
24963 @node Building DLLs with GNAT Project files,Building DLLs with GNAT,Using DLLs with GNAT,Mixed-Language Programming on Windows
24964 @anchor{gnat_ugn/platform_specific_information id23}@anchor{1fd}@anchor{gnat_ugn/platform_specific_information building-dlls-with-gnat-project-files}@anchor{1e6}
24965 @subsubsection Building DLLs with GNAT Project files
24966
24967
24968 @geindex DLLs
24969 @geindex building
24970
24971 There is nothing specific to Windows in the build process.
24972 See the @emph{Library Projects} section in the @emph{GNAT Project Manager}
24973 chapter of the @emph{GPRbuild User's Guide}.
24974
24975 Due to a system limitation, it is not possible under Windows to create threads
24976 when inside the @code{DllMain} routine which is used for auto-initialization
24977 of shared libraries, so it is not possible to have library level tasks in SALs.
24978
24979 @node Building DLLs with GNAT,Building DLLs with gnatdll,Building DLLs with GNAT Project files,Mixed-Language Programming on Windows
24980 @anchor{gnat_ugn/platform_specific_information building-dlls-with-gnat}@anchor{1fe}@anchor{gnat_ugn/platform_specific_information id24}@anchor{1ff}
24981 @subsubsection Building DLLs with GNAT
24982
24983
24984 @geindex DLLs
24985 @geindex building
24986
24987 This section explain how to build DLLs using the GNAT built-in DLL
24988 support. With the following procedure it is straight forward to build
24989 and use DLLs with GNAT.
24990
24991
24992 @itemize *
24993
24994 @item
24995 Building object files.
24996 The first step is to build all objects files that are to be included
24997 into the DLL. This is done by using the standard @code{gnatmake} tool.
24998
24999 @item
25000 Building the DLL.
25001 To build the DLL you must use the @code{gcc} @code{-shared} and
25002 @code{-shared-libgcc} options. It is quite simple to use this method:
25003
25004 @example
25005 $ gcc -shared -shared-libgcc -o api.dll obj1.o obj2.o ...
25006 @end example
25007
25008 It is important to note that in this case all symbols found in the
25009 object files are automatically exported. It is possible to restrict
25010 the set of symbols to export by passing to @code{gcc} a definition
25011 file (see @ref{1f3,,The Definition File}).
25012 For example:
25013
25014 @example
25015 $ gcc -shared -shared-libgcc -o api.dll api.def obj1.o obj2.o ...
25016 @end example
25017
25018 If you use a definition file you must export the elaboration procedures
25019 for every package that required one. Elaboration procedures are named
25020 using the package name followed by "_E".
25021
25022 @item
25023 Preparing DLL to be used.
25024 For the DLL to be used by client programs the bodies must be hidden
25025 from it and the .ali set with read-only attribute. This is very important
25026 otherwise GNAT will recompile all packages and will not actually use
25027 the code in the DLL. For example:
25028
25029 @example
25030 $ mkdir apilib
25031 $ copy *.ads *.ali api.dll apilib
25032 $ attrib +R apilib\\*.ali
25033 @end example
25034 @end itemize
25035
25036 At this point it is possible to use the DLL by directly linking
25037 against it. Note that you must use the GNAT shared runtime when using
25038 GNAT shared libraries. This is achieved by using the @code{-shared} binder
25039 option.
25040
25041 @quotation
25042
25043 @example
25044 $ gnatmake main -Iapilib -bargs -shared -largs -Lapilib -lAPI
25045 @end example
25046 @end quotation
25047
25048 @node Building DLLs with gnatdll,Ada DLLs and Finalization,Building DLLs with GNAT,Mixed-Language Programming on Windows
25049 @anchor{gnat_ugn/platform_specific_information building-dlls-with-gnatdll}@anchor{200}@anchor{gnat_ugn/platform_specific_information id25}@anchor{201}
25050 @subsubsection Building DLLs with gnatdll
25051
25052
25053 @geindex DLLs
25054 @geindex building
25055
25056 Note that it is preferred to use GNAT Project files
25057 (@ref{1e6,,Building DLLs with GNAT Project files}) or the built-in GNAT
25058 DLL support (@ref{1fe,,Building DLLs with GNAT}) or to build DLLs.
25059
25060 This section explains how to build DLLs containing Ada code using
25061 @code{gnatdll}. These DLLs will be referred to as Ada DLLs in the
25062 remainder of this section.
25063
25064 The steps required to build an Ada DLL that is to be used by Ada as well as
25065 non-Ada applications are as follows:
25066
25067
25068 @itemize *
25069
25070 @item
25071 You need to mark each Ada entity exported by the DLL with a @code{C} or
25072 @code{Stdcall} calling convention to avoid any Ada name mangling for the
25073 entities exported by the DLL
25074 (see @ref{202,,Exporting Ada Entities}). You can
25075 skip this step if you plan to use the Ada DLL only from Ada applications.
25076
25077 @item
25078 Your Ada code must export an initialization routine which calls the routine
25079 @code{adainit} generated by @code{gnatbind} to perform the elaboration of
25080 the Ada code in the DLL (@ref{203,,Ada DLLs and Elaboration}). The initialization
25081 routine exported by the Ada DLL must be invoked by the clients of the DLL
25082 to initialize the DLL.
25083
25084 @item
25085 When useful, the DLL should also export a finalization routine which calls
25086 routine @code{adafinal} generated by @code{gnatbind} to perform the
25087 finalization of the Ada code in the DLL (@ref{204,,Ada DLLs and Finalization}).
25088 The finalization routine exported by the Ada DLL must be invoked by the
25089 clients of the DLL when the DLL services are no further needed.
25090
25091 @item
25092 You must provide a spec for the services exported by the Ada DLL in each
25093 of the programming languages to which you plan to make the DLL available.
25094
25095 @item
25096 You must provide a definition file listing the exported entities
25097 (@ref{1f3,,The Definition File}).
25098
25099 @item
25100 Finally you must use @code{gnatdll} to produce the DLL and the import
25101 library (@ref{1fb,,Using gnatdll}).
25102 @end itemize
25103
25104 Note that a relocatable DLL stripped using the @code{strip}
25105 binutils tool will not be relocatable anymore. To build a DLL without
25106 debug information pass @code{-largs -s} to @code{gnatdll}. This
25107 restriction does not apply to a DLL built using a Library Project.
25108 See the @emph{Library Projects} section in the @emph{GNAT Project Manager}
25109 chapter of the @emph{GPRbuild User's Guide}.
25110
25111 @c Limitations_When_Using_Ada_DLLs_from Ada:
25112
25113 @menu
25114 * Limitations When Using Ada DLLs from Ada::
25115 * Exporting Ada Entities::
25116 * Ada DLLs and Elaboration::
25117
25118 @end menu
25119
25120 @node Limitations When Using Ada DLLs from Ada,Exporting Ada Entities,,Building DLLs with gnatdll
25121 @anchor{gnat_ugn/platform_specific_information limitations-when-using-ada-dlls-from-ada}@anchor{205}
25122 @subsubsection Limitations When Using Ada DLLs from Ada
25123
25124
25125 When using Ada DLLs from Ada applications there is a limitation users
25126 should be aware of. Because on Windows the GNAT run-time is not in a DLL of
25127 its own, each Ada DLL includes a part of the GNAT run-time. Specifically,
25128 each Ada DLL includes the services of the GNAT run-time that are necessary
25129 to the Ada code inside the DLL. As a result, when an Ada program uses an
25130 Ada DLL there are two independent GNAT run-times: one in the Ada DLL and
25131 one in the main program.
25132
25133 It is therefore not possible to exchange GNAT run-time objects between the
25134 Ada DLL and the main Ada program. Example of GNAT run-time objects are file
25135 handles (e.g., @code{Text_IO.File_Type}), tasks types, protected objects
25136 types, etc.
25137
25138 It is completely safe to exchange plain elementary, array or record types,
25139 Windows object handles, etc.
25140
25141 @node Exporting Ada Entities,Ada DLLs and Elaboration,Limitations When Using Ada DLLs from Ada,Building DLLs with gnatdll
25142 @anchor{gnat_ugn/platform_specific_information exporting-ada-entities}@anchor{202}@anchor{gnat_ugn/platform_specific_information id26}@anchor{206}
25143 @subsubsection Exporting Ada Entities
25144
25145
25146 @geindex Export table
25147
25148 Building a DLL is a way to encapsulate a set of services usable from any
25149 application. As a result, the Ada entities exported by a DLL should be
25150 exported with the @code{C} or @code{Stdcall} calling conventions to avoid
25151 any Ada name mangling. As an example here is an Ada package
25152 @code{API}, spec and body, exporting two procedures, a function, and a
25153 variable:
25154
25155 @quotation
25156
25157 @example
25158 with Interfaces.C; use Interfaces;
25159 package API is
25160 Count : C.int := 0;
25161 function Factorial (Val : C.int) return C.int;
25162
25163 procedure Initialize_API;
25164 procedure Finalize_API;
25165 -- Initialization & Finalization routines. More in the next section.
25166 private
25167 pragma Export (C, Initialize_API);
25168 pragma Export (C, Finalize_API);
25169 pragma Export (C, Count);
25170 pragma Export (C, Factorial);
25171 end API;
25172 @end example
25173
25174 @example
25175 package body API is
25176 function Factorial (Val : C.int) return C.int is
25177 Fact : C.int := 1;
25178 begin
25179 Count := Count + 1;
25180 for K in 1 .. Val loop
25181 Fact := Fact * K;
25182 end loop;
25183 return Fact;
25184 end Factorial;
25185
25186 procedure Initialize_API is
25187 procedure Adainit;
25188 pragma Import (C, Adainit);
25189 begin
25190 Adainit;
25191 end Initialize_API;
25192
25193 procedure Finalize_API is
25194 procedure Adafinal;
25195 pragma Import (C, Adafinal);
25196 begin
25197 Adafinal;
25198 end Finalize_API;
25199 end API;
25200 @end example
25201 @end quotation
25202
25203 If the Ada DLL you are building will only be used by Ada applications
25204 you do not have to export Ada entities with a @code{C} or @code{Stdcall}
25205 convention. As an example, the previous package could be written as
25206 follows:
25207
25208 @quotation
25209
25210 @example
25211 package API is
25212 Count : Integer := 0;
25213 function Factorial (Val : Integer) return Integer;
25214
25215 procedure Initialize_API;
25216 procedure Finalize_API;
25217 -- Initialization and Finalization routines.
25218 end API;
25219 @end example
25220
25221 @example
25222 package body API is
25223 function Factorial (Val : Integer) return Integer is
25224 Fact : Integer := 1;
25225 begin
25226 Count := Count + 1;
25227 for K in 1 .. Val loop
25228 Fact := Fact * K;
25229 end loop;
25230 return Fact;
25231 end Factorial;
25232
25233 ...
25234 -- The remainder of this package body is unchanged.
25235 end API;
25236 @end example
25237 @end quotation
25238
25239 Note that if you do not export the Ada entities with a @code{C} or
25240 @code{Stdcall} convention you will have to provide the mangled Ada names
25241 in the definition file of the Ada DLL
25242 (@ref{207,,Creating the Definition File}).
25243
25244 @node Ada DLLs and Elaboration,,Exporting Ada Entities,Building DLLs with gnatdll
25245 @anchor{gnat_ugn/platform_specific_information ada-dlls-and-elaboration}@anchor{203}@anchor{gnat_ugn/platform_specific_information id27}@anchor{208}
25246 @subsubsection Ada DLLs and Elaboration
25247
25248
25249 @geindex DLLs and elaboration
25250
25251 The DLL that you are building contains your Ada code as well as all the
25252 routines in the Ada library that are needed by it. The first thing a
25253 user of your DLL must do is elaborate the Ada code
25254 (@ref{f,,Elaboration Order Handling in GNAT}).
25255
25256 To achieve this you must export an initialization routine
25257 (@code{Initialize_API} in the previous example), which must be invoked
25258 before using any of the DLL services. This elaboration routine must call
25259 the Ada elaboration routine @code{adainit} generated by the GNAT binder
25260 (@ref{b4,,Binding with Non-Ada Main Programs}). See the body of
25261 @code{Initialize_Api} for an example. Note that the GNAT binder is
25262 automatically invoked during the DLL build process by the @code{gnatdll}
25263 tool (@ref{1fb,,Using gnatdll}).
25264
25265 When a DLL is loaded, Windows systematically invokes a routine called
25266 @code{DllMain}. It would therefore be possible to call @code{adainit}
25267 directly from @code{DllMain} without having to provide an explicit
25268 initialization routine. Unfortunately, it is not possible to call
25269 @code{adainit} from the @code{DllMain} if your program has library level
25270 tasks because access to the @code{DllMain} entry point is serialized by
25271 the system (that is, only a single thread can execute 'through' it at a
25272 time), which means that the GNAT run-time will deadlock waiting for the
25273 newly created task to complete its initialization.
25274
25275 @node Ada DLLs and Finalization,Creating a Spec for Ada DLLs,Building DLLs with gnatdll,Mixed-Language Programming on Windows
25276 @anchor{gnat_ugn/platform_specific_information id28}@anchor{209}@anchor{gnat_ugn/platform_specific_information ada-dlls-and-finalization}@anchor{204}
25277 @subsubsection Ada DLLs and Finalization
25278
25279
25280 @geindex DLLs and finalization
25281
25282 When the services of an Ada DLL are no longer needed, the client code should
25283 invoke the DLL finalization routine, if available. The DLL finalization
25284 routine is in charge of releasing all resources acquired by the DLL. In the
25285 case of the Ada code contained in the DLL, this is achieved by calling
25286 routine @code{adafinal} generated by the GNAT binder
25287 (@ref{b4,,Binding with Non-Ada Main Programs}).
25288 See the body of @code{Finalize_Api} for an
25289 example. As already pointed out the GNAT binder is automatically invoked
25290 during the DLL build process by the @code{gnatdll} tool
25291 (@ref{1fb,,Using gnatdll}).
25292
25293 @node Creating a Spec for Ada DLLs,GNAT and Windows Resources,Ada DLLs and Finalization,Mixed-Language Programming on Windows
25294 @anchor{gnat_ugn/platform_specific_information id29}@anchor{20a}@anchor{gnat_ugn/platform_specific_information creating-a-spec-for-ada-dlls}@anchor{20b}
25295 @subsubsection Creating a Spec for Ada DLLs
25296
25297
25298 To use the services exported by the Ada DLL from another programming
25299 language (e.g., C), you have to translate the specs of the exported Ada
25300 entities in that language. For instance in the case of @code{API.dll},
25301 the corresponding C header file could look like:
25302
25303 @quotation
25304
25305 @example
25306 extern int *_imp__count;
25307 #define count (*_imp__count)
25308 int factorial (int);
25309 @end example
25310 @end quotation
25311
25312 It is important to understand that when building an Ada DLL to be used by
25313 other Ada applications, you need two different specs for the packages
25314 contained in the DLL: one for building the DLL and the other for using
25315 the DLL. This is because the @code{DLL} calling convention is needed to
25316 use a variable defined in a DLL, but when building the DLL, the variable
25317 must have either the @code{Ada} or @code{C} calling convention. As an
25318 example consider a DLL comprising the following package @code{API}:
25319
25320 @quotation
25321
25322 @example
25323 package API is
25324 Count : Integer := 0;
25325 ...
25326 -- Remainder of the package omitted.
25327 end API;
25328 @end example
25329 @end quotation
25330
25331 After producing a DLL containing package @code{API}, the spec that
25332 must be used to import @code{API.Count} from Ada code outside of the
25333 DLL is:
25334
25335 @quotation
25336
25337 @example
25338 package API is
25339 Count : Integer;
25340 pragma Import (DLL, Count);
25341 end API;
25342 @end example
25343 @end quotation
25344
25345 @menu
25346 * Creating the Definition File::
25347 * Using gnatdll::
25348
25349 @end menu
25350
25351 @node Creating the Definition File,Using gnatdll,,Creating a Spec for Ada DLLs
25352 @anchor{gnat_ugn/platform_specific_information creating-the-definition-file}@anchor{207}@anchor{gnat_ugn/platform_specific_information id30}@anchor{20c}
25353 @subsubsection Creating the Definition File
25354
25355
25356 The definition file is the last file needed to build the DLL. It lists
25357 the exported symbols. As an example, the definition file for a DLL
25358 containing only package @code{API} (where all the entities are exported
25359 with a @code{C} calling convention) is:
25360
25361 @quotation
25362
25363 @example
25364 EXPORTS
25365 count
25366 factorial
25367 finalize_api
25368 initialize_api
25369 @end example
25370 @end quotation
25371
25372 If the @code{C} calling convention is missing from package @code{API},
25373 then the definition file contains the mangled Ada names of the above
25374 entities, which in this case are:
25375
25376 @quotation
25377
25378 @example
25379 EXPORTS
25380 api__count
25381 api__factorial
25382 api__finalize_api
25383 api__initialize_api
25384 @end example
25385 @end quotation
25386
25387 @node Using gnatdll,,Creating the Definition File,Creating a Spec for Ada DLLs
25388 @anchor{gnat_ugn/platform_specific_information using-gnatdll}@anchor{1fb}@anchor{gnat_ugn/platform_specific_information id31}@anchor{20d}
25389 @subsubsection Using @code{gnatdll}
25390
25391
25392 @geindex gnatdll
25393
25394 @code{gnatdll} is a tool to automate the DLL build process once all the Ada
25395 and non-Ada sources that make up your DLL have been compiled.
25396 @code{gnatdll} is actually in charge of two distinct tasks: build the
25397 static import library for the DLL and the actual DLL. The form of the
25398 @code{gnatdll} command is
25399
25400 @quotation
25401
25402 @example
25403 $ gnatdll [ switches ] list-of-files [ -largs opts ]
25404 @end example
25405 @end quotation
25406
25407 where @code{list-of-files} is a list of ALI and object files. The object
25408 file list must be the exact list of objects corresponding to the non-Ada
25409 sources whose services are to be included in the DLL. The ALI file list
25410 must be the exact list of ALI files for the corresponding Ada sources
25411 whose services are to be included in the DLL. If @code{list-of-files} is
25412 missing, only the static import library is generated.
25413
25414 You may specify any of the following switches to @code{gnatdll}:
25415
25416 @quotation
25417
25418 @geindex -a (gnatdll)
25419 @end quotation
25420
25421
25422 @table @asis
25423
25424 @item @code{-a[@emph{address}]}
25425
25426 Build a non-relocatable DLL at @code{address}. If @code{address} is not
25427 specified the default address @code{0x11000000} will be used. By default,
25428 when this switch is missing, @code{gnatdll} builds relocatable DLL. We
25429 advise the reader to build relocatable DLL.
25430
25431 @geindex -b (gnatdll)
25432
25433 @item @code{-b @emph{address}}
25434
25435 Set the relocatable DLL base address. By default the address is
25436 @code{0x11000000}.
25437
25438 @geindex -bargs (gnatdll)
25439
25440 @item @code{-bargs @emph{opts}}
25441
25442 Binder options. Pass @code{opts} to the binder.
25443
25444 @geindex -d (gnatdll)
25445
25446 @item @code{-d @emph{dllfile}}
25447
25448 @code{dllfile} is the name of the DLL. This switch must be present for
25449 @code{gnatdll} to do anything. The name of the generated import library is
25450 obtained algorithmically from @code{dllfile} as shown in the following
25451 example: if @code{dllfile} is @code{xyz.dll}, the import library name is
25452 @code{libxyz.dll.a}. The name of the definition file to use (if not specified
25453 by option @code{-e}) is obtained algorithmically from @code{dllfile}
25454 as shown in the following example:
25455 if @code{dllfile} is @code{xyz.dll}, the definition
25456 file used is @code{xyz.def}.
25457
25458 @geindex -e (gnatdll)
25459
25460 @item @code{-e @emph{deffile}}
25461
25462 @code{deffile} is the name of the definition file.
25463
25464 @geindex -g (gnatdll)
25465
25466 @item @code{-g}
25467
25468 Generate debugging information. This information is stored in the object
25469 file and copied from there to the final DLL file by the linker,
25470 where it can be read by the debugger. You must use the
25471 @code{-g} switch if you plan on using the debugger or the symbolic
25472 stack traceback.
25473
25474 @geindex -h (gnatdll)
25475
25476 @item @code{-h}
25477
25478 Help mode. Displays @code{gnatdll} switch usage information.
25479
25480 @geindex -I (gnatdll)
25481
25482 @item @code{-I@emph{dir}}
25483
25484 Direct @code{gnatdll} to search the @code{dir} directory for source and
25485 object files needed to build the DLL.
25486 (@ref{89,,Search Paths and the Run-Time Library (RTL)}).
25487
25488 @geindex -k (gnatdll)
25489
25490 @item @code{-k}
25491
25492 Removes the @code{@@@emph{nn}} suffix from the import library's exported
25493 names, but keeps them for the link names. You must specify this
25494 option if you want to use a @code{Stdcall} function in a DLL for which
25495 the @code{@@@emph{nn}} suffix has been removed. This is the case for most
25496 of the Windows NT DLL for example. This option has no effect when
25497 @code{-n} option is specified.
25498
25499 @geindex -l (gnatdll)
25500
25501 @item @code{-l @emph{file}}
25502
25503 The list of ALI and object files used to build the DLL are listed in
25504 @code{file}, instead of being given in the command line. Each line in
25505 @code{file} contains the name of an ALI or object file.
25506
25507 @geindex -n (gnatdll)
25508
25509 @item @code{-n}
25510
25511 No Import. Do not create the import library.
25512
25513 @geindex -q (gnatdll)
25514
25515 @item @code{-q}
25516
25517 Quiet mode. Do not display unnecessary messages.
25518
25519 @geindex -v (gnatdll)
25520
25521 @item @code{-v}
25522
25523 Verbose mode. Display extra information.
25524
25525 @geindex -largs (gnatdll)
25526
25527 @item @code{-largs @emph{opts}}
25528
25529 Linker options. Pass @code{opts} to the linker.
25530 @end table
25531
25532 @subsubheading @code{gnatdll} Example
25533
25534
25535 As an example the command to build a relocatable DLL from @code{api.adb}
25536 once @code{api.adb} has been compiled and @code{api.def} created is
25537
25538 @quotation
25539
25540 @example
25541 $ gnatdll -d api.dll api.ali
25542 @end example
25543 @end quotation
25544
25545 The above command creates two files: @code{libapi.dll.a} (the import
25546 library) and @code{api.dll} (the actual DLL). If you want to create
25547 only the DLL, just type:
25548
25549 @quotation
25550
25551 @example
25552 $ gnatdll -d api.dll -n api.ali
25553 @end example
25554 @end quotation
25555
25556 Alternatively if you want to create just the import library, type:
25557
25558 @quotation
25559
25560 @example
25561 $ gnatdll -d api.dll
25562 @end example
25563 @end quotation
25564
25565 @subsubheading @code{gnatdll} behind the Scenes
25566
25567
25568 This section details the steps involved in creating a DLL. @code{gnatdll}
25569 does these steps for you. Unless you are interested in understanding what
25570 goes on behind the scenes, you should skip this section.
25571
25572 We use the previous example of a DLL containing the Ada package @code{API},
25573 to illustrate the steps necessary to build a DLL. The starting point is a
25574 set of objects that will make up the DLL and the corresponding ALI
25575 files. In the case of this example this means that @code{api.o} and
25576 @code{api.ali} are available. To build a relocatable DLL, @code{gnatdll} does
25577 the following:
25578
25579
25580 @itemize *
25581
25582 @item
25583 @code{gnatdll} builds the base file (@code{api.base}). A base file gives
25584 the information necessary to generate relocation information for the
25585 DLL.
25586
25587 @example
25588 $ gnatbind -n api
25589 $ gnatlink api -o api.jnk -mdll -Wl,--base-file,api.base
25590 @end example
25591
25592 In addition to the base file, the @code{gnatlink} command generates an
25593 output file @code{api.jnk} which can be discarded. The @code{-mdll} switch
25594 asks @code{gnatlink} to generate the routines @code{DllMain} and
25595 @code{DllMainCRTStartup} that are called by the Windows loader when the DLL
25596 is loaded into memory.
25597
25598 @item
25599 @code{gnatdll} uses @code{dlltool} (see @ref{20e,,Using dlltool}) to build the
25600 export table (@code{api.exp}). The export table contains the relocation
25601 information in a form which can be used during the final link to ensure
25602 that the Windows loader is able to place the DLL anywhere in memory.
25603
25604 @example
25605 $ dlltool --dllname api.dll --def api.def --base-file api.base \\
25606 --output-exp api.exp
25607 @end example
25608
25609 @item
25610 @code{gnatdll} builds the base file using the new export table. Note that
25611 @code{gnatbind} must be called once again since the binder generated file
25612 has been deleted during the previous call to @code{gnatlink}.
25613
25614 @example
25615 $ gnatbind -n api
25616 $ gnatlink api -o api.jnk api.exp -mdll
25617 -Wl,--base-file,api.base
25618 @end example
25619
25620 @item
25621 @code{gnatdll} builds the new export table using the new base file and
25622 generates the DLL import library @code{libAPI.dll.a}.
25623
25624 @example
25625 $ dlltool --dllname api.dll --def api.def --base-file api.base \\
25626 --output-exp api.exp --output-lib libAPI.a
25627 @end example
25628
25629 @item
25630 Finally @code{gnatdll} builds the relocatable DLL using the final export
25631 table.
25632
25633 @example
25634 $ gnatbind -n api
25635 $ gnatlink api api.exp -o api.dll -mdll
25636 @end example
25637 @end itemize
25638 @anchor{gnat_ugn/platform_specific_information using-dlltool}@anchor{20e}
25639 @subsubheading Using @code{dlltool}
25640
25641
25642 @code{dlltool} is the low-level tool used by @code{gnatdll} to build
25643 DLLs and static import libraries. This section summarizes the most
25644 common @code{dlltool} switches. The form of the @code{dlltool} command
25645 is
25646
25647 @quotation
25648
25649 @example
25650 $ dlltool [`switches`]
25651 @end example
25652 @end quotation
25653
25654 @code{dlltool} switches include:
25655
25656 @geindex --base-file (dlltool)
25657
25658
25659 @table @asis
25660
25661 @item @code{--base-file @emph{basefile}}
25662
25663 Read the base file @code{basefile} generated by the linker. This switch
25664 is used to create a relocatable DLL.
25665 @end table
25666
25667 @geindex --def (dlltool)
25668
25669
25670 @table @asis
25671
25672 @item @code{--def @emph{deffile}}
25673
25674 Read the definition file.
25675 @end table
25676
25677 @geindex --dllname (dlltool)
25678
25679
25680 @table @asis
25681
25682 @item @code{--dllname @emph{name}}
25683
25684 Gives the name of the DLL. This switch is used to embed the name of the
25685 DLL in the static import library generated by @code{dlltool} with switch
25686 @code{--output-lib}.
25687 @end table
25688
25689 @geindex -k (dlltool)
25690
25691
25692 @table @asis
25693
25694 @item @code{-k}
25695
25696 Kill @code{@@@emph{nn}} from exported names
25697 (@ref{1e7,,Windows Calling Conventions}
25698 for a discussion about @code{Stdcall}-style symbols.
25699 @end table
25700
25701 @geindex --help (dlltool)
25702
25703
25704 @table @asis
25705
25706 @item @code{--help}
25707
25708 Prints the @code{dlltool} switches with a concise description.
25709 @end table
25710
25711 @geindex --output-exp (dlltool)
25712
25713
25714 @table @asis
25715
25716 @item @code{--output-exp @emph{exportfile}}
25717
25718 Generate an export file @code{exportfile}. The export file contains the
25719 export table (list of symbols in the DLL) and is used to create the DLL.
25720 @end table
25721
25722 @geindex --output-lib (dlltool)
25723
25724
25725 @table @asis
25726
25727 @item @code{--output-lib @emph{libfile}}
25728
25729 Generate a static import library @code{libfile}.
25730 @end table
25731
25732 @geindex -v (dlltool)
25733
25734
25735 @table @asis
25736
25737 @item @code{-v}
25738
25739 Verbose mode.
25740 @end table
25741
25742 @geindex --as (dlltool)
25743
25744
25745 @table @asis
25746
25747 @item @code{--as @emph{assembler-name}}
25748
25749 Use @code{assembler-name} as the assembler. The default is @code{as}.
25750 @end table
25751
25752 @node GNAT and Windows Resources,Using GNAT DLLs from Microsoft Visual Studio Applications,Creating a Spec for Ada DLLs,Mixed-Language Programming on Windows
25753 @anchor{gnat_ugn/platform_specific_information gnat-and-windows-resources}@anchor{20f}@anchor{gnat_ugn/platform_specific_information id32}@anchor{210}
25754 @subsubsection GNAT and Windows Resources
25755
25756
25757 @geindex Resources
25758 @geindex windows
25759
25760 Resources are an easy way to add Windows specific objects to your
25761 application. The objects that can be added as resources include:
25762
25763
25764 @itemize *
25765
25766 @item
25767 menus
25768
25769 @item
25770 accelerators
25771
25772 @item
25773 dialog boxes
25774
25775 @item
25776 string tables
25777
25778 @item
25779 bitmaps
25780
25781 @item
25782 cursors
25783
25784 @item
25785 icons
25786
25787 @item
25788 fonts
25789
25790 @item
25791 version information
25792 @end itemize
25793
25794 For example, a version information resource can be defined as follow and
25795 embedded into an executable or DLL:
25796
25797 A version information resource can be used to embed information into an
25798 executable or a DLL. These information can be viewed using the file properties
25799 from the Windows Explorer. Here is an example of a version information
25800 resource:
25801
25802 @quotation
25803
25804 @example
25805 1 VERSIONINFO
25806 FILEVERSION 1,0,0,0
25807 PRODUCTVERSION 1,0,0,0
25808 BEGIN
25809 BLOCK "StringFileInfo"
25810 BEGIN
25811 BLOCK "080904E4"
25812 BEGIN
25813 VALUE "CompanyName", "My Company Name"
25814 VALUE "FileDescription", "My application"
25815 VALUE "FileVersion", "1.0"
25816 VALUE "InternalName", "my_app"
25817 VALUE "LegalCopyright", "My Name"
25818 VALUE "OriginalFilename", "my_app.exe"
25819 VALUE "ProductName", "My App"
25820 VALUE "ProductVersion", "1.0"
25821 END
25822 END
25823
25824 BLOCK "VarFileInfo"
25825 BEGIN
25826 VALUE "Translation", 0x809, 1252
25827 END
25828 END
25829 @end example
25830 @end quotation
25831
25832 The value @code{0809} (langID) is for the U.K English language and
25833 @code{04E4} (charsetID), which is equal to @code{1252} decimal, for
25834 multilingual.
25835
25836 This section explains how to build, compile and use resources. Note that this
25837 section does not cover all resource objects, for a complete description see
25838 the corresponding Microsoft documentation.
25839
25840 @menu
25841 * Building Resources::
25842 * Compiling Resources::
25843 * Using Resources::
25844
25845 @end menu
25846
25847 @node Building Resources,Compiling Resources,,GNAT and Windows Resources
25848 @anchor{gnat_ugn/platform_specific_information building-resources}@anchor{211}@anchor{gnat_ugn/platform_specific_information id33}@anchor{212}
25849 @subsubsection Building Resources
25850
25851
25852 @geindex Resources
25853 @geindex building
25854
25855 A resource file is an ASCII file. By convention resource files have an
25856 @code{.rc} extension.
25857 The easiest way to build a resource file is to use Microsoft tools
25858 such as @code{imagedit.exe} to build bitmaps, icons and cursors and
25859 @code{dlgedit.exe} to build dialogs.
25860 It is always possible to build an @code{.rc} file yourself by writing a
25861 resource script.
25862
25863 It is not our objective to explain how to write a resource file. A
25864 complete description of the resource script language can be found in the
25865 Microsoft documentation.
25866
25867 @node Compiling Resources,Using Resources,Building Resources,GNAT and Windows Resources
25868 @anchor{gnat_ugn/platform_specific_information compiling-resources}@anchor{213}@anchor{gnat_ugn/platform_specific_information id34}@anchor{214}
25869 @subsubsection Compiling Resources
25870
25871
25872 @geindex rc
25873
25874 @geindex windres
25875
25876 @geindex Resources
25877 @geindex compiling
25878
25879 This section describes how to build a GNAT-compatible (COFF) object file
25880 containing the resources. This is done using the Resource Compiler
25881 @code{windres} as follows:
25882
25883 @quotation
25884
25885 @example
25886 $ windres -i myres.rc -o myres.o
25887 @end example
25888 @end quotation
25889
25890 By default @code{windres} will run @code{gcc} to preprocess the @code{.rc}
25891 file. You can specify an alternate preprocessor (usually named
25892 @code{cpp.exe}) using the @code{windres} @code{--preprocessor}
25893 parameter. A list of all possible options may be obtained by entering
25894 the command @code{windres} @code{--help}.
25895
25896 It is also possible to use the Microsoft resource compiler @code{rc.exe}
25897 to produce a @code{.res} file (binary resource file). See the
25898 corresponding Microsoft documentation for further details. In this case
25899 you need to use @code{windres} to translate the @code{.res} file to a
25900 GNAT-compatible object file as follows:
25901
25902 @quotation
25903
25904 @example
25905 $ windres -i myres.res -o myres.o
25906 @end example
25907 @end quotation
25908
25909 @node Using Resources,,Compiling Resources,GNAT and Windows Resources
25910 @anchor{gnat_ugn/platform_specific_information using-resources}@anchor{215}@anchor{gnat_ugn/platform_specific_information id35}@anchor{216}
25911 @subsubsection Using Resources
25912
25913
25914 @geindex Resources
25915 @geindex using
25916
25917 To include the resource file in your program just add the
25918 GNAT-compatible object file for the resource(s) to the linker
25919 arguments. With @code{gnatmake} this is done by using the @code{-largs}
25920 option:
25921
25922 @quotation
25923
25924 @example
25925 $ gnatmake myprog -largs myres.o
25926 @end example
25927 @end quotation
25928
25929 @node Using GNAT DLLs from Microsoft Visual Studio Applications,Debugging a DLL,GNAT and Windows Resources,Mixed-Language Programming on Windows
25930 @anchor{gnat_ugn/platform_specific_information using-gnat-dll-from-msvs}@anchor{217}@anchor{gnat_ugn/platform_specific_information using-gnat-dlls-from-microsoft-visual-studio-applications}@anchor{218}
25931 @subsubsection Using GNAT DLLs from Microsoft Visual Studio Applications
25932
25933
25934 @geindex Microsoft Visual Studio
25935 @geindex use with GNAT DLLs
25936
25937 This section describes a common case of mixed GNAT/Microsoft Visual Studio
25938 application development, where the main program is developed using MSVS, and
25939 is linked with a DLL developed using GNAT. Such a mixed application should
25940 be developed following the general guidelines outlined above; below is the
25941 cookbook-style sequence of steps to follow:
25942
25943
25944 @enumerate
25945
25946 @item
25947 First develop and build the GNAT shared library using a library project
25948 (let's assume the project is @code{mylib.gpr}, producing the library @code{libmylib.dll}):
25949 @end enumerate
25950
25951 @quotation
25952
25953 @example
25954 $ gprbuild -p mylib.gpr
25955 @end example
25956 @end quotation
25957
25958
25959 @enumerate 2
25960
25961 @item
25962 Produce a .def file for the symbols you need to interface with, either by
25963 hand or automatically with possibly some manual adjustments
25964 (see @ref{1f9,,Creating Definition File Automatically}):
25965 @end enumerate
25966
25967 @quotation
25968
25969 @example
25970 $ dlltool libmylib.dll -z libmylib.def --export-all-symbols
25971 @end example
25972 @end quotation
25973
25974
25975 @enumerate 3
25976
25977 @item
25978 Make sure that MSVS command-line tools are accessible on the path.
25979
25980 @item
25981 Create the Microsoft-style import library (see @ref{1fc,,MSVS-Style Import Library}):
25982 @end enumerate
25983
25984 @quotation
25985
25986 @example
25987 $ lib -machine:IX86 -def:libmylib.def -out:libmylib.lib
25988 @end example
25989 @end quotation
25990
25991 If you are using a 64-bit toolchain, the above becomes...
25992
25993 @quotation
25994
25995 @example
25996 $ lib -machine:X64 -def:libmylib.def -out:libmylib.lib
25997 @end example
25998 @end quotation
25999
26000
26001 @enumerate 5
26002
26003 @item
26004 Build the C main
26005 @end enumerate
26006
26007 @quotation
26008
26009 @example
26010 $ cl /O2 /MD main.c libmylib.lib
26011 @end example
26012 @end quotation
26013
26014
26015 @enumerate 6
26016
26017 @item
26018 Before running the executable, make sure you have set the PATH to the DLL,
26019 or copy the DLL into into the directory containing the .exe.
26020 @end enumerate
26021
26022 @node Debugging a DLL,Setting Stack Size from gnatlink,Using GNAT DLLs from Microsoft Visual Studio Applications,Mixed-Language Programming on Windows
26023 @anchor{gnat_ugn/platform_specific_information id36}@anchor{219}@anchor{gnat_ugn/platform_specific_information debugging-a-dll}@anchor{21a}
26024 @subsubsection Debugging a DLL
26025
26026
26027 @geindex DLL debugging
26028
26029 Debugging a DLL is similar to debugging a standard program. But
26030 we have to deal with two different executable parts: the DLL and the
26031 program that uses it. We have the following four possibilities:
26032
26033
26034 @itemize *
26035
26036 @item
26037 The program and the DLL are built with GCC/GNAT.
26038
26039 @item
26040 The program is built with foreign tools and the DLL is built with
26041 GCC/GNAT.
26042
26043 @item
26044 The program is built with GCC/GNAT and the DLL is built with
26045 foreign tools.
26046 @end itemize
26047
26048 In this section we address only cases one and two above.
26049 There is no point in trying to debug
26050 a DLL with GNU/GDB, if there is no GDB-compatible debugging
26051 information in it. To do so you must use a debugger compatible with the
26052 tools suite used to build the DLL.
26053
26054 @menu
26055 * Program and DLL Both Built with GCC/GNAT::
26056 * Program Built with Foreign Tools and DLL Built with GCC/GNAT::
26057
26058 @end menu
26059
26060 @node Program and DLL Both Built with GCC/GNAT,Program Built with Foreign Tools and DLL Built with GCC/GNAT,,Debugging a DLL
26061 @anchor{gnat_ugn/platform_specific_information id37}@anchor{21b}@anchor{gnat_ugn/platform_specific_information program-and-dll-both-built-with-gcc-gnat}@anchor{21c}
26062 @subsubsection Program and DLL Both Built with GCC/GNAT
26063
26064
26065 This is the simplest case. Both the DLL and the program have @code{GDB}
26066 compatible debugging information. It is then possible to break anywhere in
26067 the process. Let's suppose here that the main procedure is named
26068 @code{ada_main} and that in the DLL there is an entry point named
26069 @code{ada_dll}.
26070
26071 The DLL (@ref{1f2,,Introduction to Dynamic Link Libraries (DLLs)}) and
26072 program must have been built with the debugging information (see GNAT -g
26073 switch). Here are the step-by-step instructions for debugging it:
26074
26075
26076 @itemize *
26077
26078 @item
26079 Launch @code{GDB} on the main program.
26080
26081 @example
26082 $ gdb -nw ada_main
26083 @end example
26084
26085 @item
26086 Start the program and stop at the beginning of the main procedure
26087
26088 @example
26089 (gdb) start
26090 @end example
26091
26092 This step is required to be able to set a breakpoint inside the DLL. As long
26093 as the program is not run, the DLL is not loaded. This has the
26094 consequence that the DLL debugging information is also not loaded, so it is not
26095 possible to set a breakpoint in the DLL.
26096
26097 @item
26098 Set a breakpoint inside the DLL
26099
26100 @example
26101 (gdb) break ada_dll
26102 (gdb) cont
26103 @end example
26104 @end itemize
26105
26106 At this stage a breakpoint is set inside the DLL. From there on
26107 you can use the standard approach to debug the whole program
26108 (@ref{24,,Running and Debugging Ada Programs}).
26109
26110 @node Program Built with Foreign Tools and DLL Built with GCC/GNAT,,Program and DLL Both Built with GCC/GNAT,Debugging a DLL
26111 @anchor{gnat_ugn/platform_specific_information program-built-with-foreign-tools-and-dll-built-with-gcc-gnat}@anchor{21d}@anchor{gnat_ugn/platform_specific_information id38}@anchor{21e}
26112 @subsubsection Program Built with Foreign Tools and DLL Built with GCC/GNAT
26113
26114
26115 In this case things are slightly more complex because it is not possible to
26116 start the main program and then break at the beginning to load the DLL and the
26117 associated DLL debugging information. It is not possible to break at the
26118 beginning of the program because there is no @code{GDB} debugging information,
26119 and therefore there is no direct way of getting initial control. This
26120 section addresses this issue by describing some methods that can be used
26121 to break somewhere in the DLL to debug it.
26122
26123 First suppose that the main procedure is named @code{main} (this is for
26124 example some C code built with Microsoft Visual C) and that there is a
26125 DLL named @code{test.dll} containing an Ada entry point named
26126 @code{ada_dll}.
26127
26128 The DLL (see @ref{1f2,,Introduction to Dynamic Link Libraries (DLLs)}) must have
26129 been built with debugging information (see the GNAT @code{-g} option).
26130
26131 @subsubheading Debugging the DLL Directly
26132
26133
26134
26135 @itemize *
26136
26137 @item
26138 Find out the executable starting address
26139
26140 @example
26141 $ objdump --file-header main.exe
26142 @end example
26143
26144 The starting address is reported on the last line. For example:
26145
26146 @example
26147 main.exe: file format pei-i386
26148 architecture: i386, flags 0x0000010a:
26149 EXEC_P, HAS_DEBUG, D_PAGED
26150 start address 0x00401010
26151 @end example
26152
26153 @item
26154 Launch the debugger on the executable.
26155
26156 @example
26157 $ gdb main.exe
26158 @end example
26159
26160 @item
26161 Set a breakpoint at the starting address, and launch the program.
26162
26163 @example
26164 $ (gdb) break *0x00401010
26165 $ (gdb) run
26166 @end example
26167
26168 The program will stop at the given address.
26169
26170 @item
26171 Set a breakpoint on a DLL subroutine.
26172
26173 @example
26174 (gdb) break ada_dll.adb:45
26175 @end example
26176
26177 Or if you want to break using a symbol on the DLL, you need first to
26178 select the Ada language (language used by the DLL).
26179
26180 @example
26181 (gdb) set language ada
26182 (gdb) break ada_dll
26183 @end example
26184
26185 @item
26186 Continue the program.
26187
26188 @example
26189 (gdb) cont
26190 @end example
26191
26192 This will run the program until it reaches the breakpoint that has been
26193 set. From that point you can use the standard way to debug a program
26194 as described in (@ref{24,,Running and Debugging Ada Programs}).
26195 @end itemize
26196
26197 It is also possible to debug the DLL by attaching to a running process.
26198
26199 @subsubheading Attaching to a Running Process
26200
26201
26202 @geindex DLL debugging
26203 @geindex attach to process
26204
26205 With @code{GDB} it is always possible to debug a running process by
26206 attaching to it. It is possible to debug a DLL this way. The limitation
26207 of this approach is that the DLL must run long enough to perform the
26208 attach operation. It may be useful for instance to insert a time wasting
26209 loop in the code of the DLL to meet this criterion.
26210
26211
26212 @itemize *
26213
26214 @item
26215 Launch the main program @code{main.exe}.
26216
26217 @example
26218 $ main
26219 @end example
26220
26221 @item
26222 Use the Windows @emph{Task Manager} to find the process ID. Let's say
26223 that the process PID for @code{main.exe} is 208.
26224
26225 @item
26226 Launch gdb.
26227
26228 @example
26229 $ gdb
26230 @end example
26231
26232 @item
26233 Attach to the running process to be debugged.
26234
26235 @example
26236 (gdb) attach 208
26237 @end example
26238
26239 @item
26240 Load the process debugging information.
26241
26242 @example
26243 (gdb) symbol-file main.exe
26244 @end example
26245
26246 @item
26247 Break somewhere in the DLL.
26248
26249 @example
26250 (gdb) break ada_dll
26251 @end example
26252
26253 @item
26254 Continue process execution.
26255
26256 @example
26257 (gdb) cont
26258 @end example
26259 @end itemize
26260
26261 This last step will resume the process execution, and stop at
26262 the breakpoint we have set. From there you can use the standard
26263 approach to debug a program as described in
26264 @ref{24,,Running and Debugging Ada Programs}.
26265
26266 @node Setting Stack Size from gnatlink,Setting Heap Size from gnatlink,Debugging a DLL,Mixed-Language Programming on Windows
26267 @anchor{gnat_ugn/platform_specific_information setting-stack-size-from-gnatlink}@anchor{136}@anchor{gnat_ugn/platform_specific_information id39}@anchor{21f}
26268 @subsubsection Setting Stack Size from @code{gnatlink}
26269
26270
26271 It is possible to specify the program stack size at link time. On modern
26272 versions of Windows, starting with XP, this is mostly useful to set the size of
26273 the main stack (environment task). The other task stacks are set with pragma
26274 Storage_Size or with the @emph{gnatbind -d} command.
26275
26276 Since older versions of Windows (2000, NT4, etc.) do not allow setting the
26277 reserve size of individual tasks, the link-time stack size applies to all
26278 tasks, and pragma Storage_Size has no effect.
26279 In particular, Stack Overflow checks are made against this
26280 link-time specified size.
26281
26282 This setting can be done with @code{gnatlink} using either of the following:
26283
26284
26285 @itemize *
26286
26287 @item
26288 @code{-Xlinker} linker option
26289
26290 @example
26291 $ gnatlink hello -Xlinker --stack=0x10000,0x1000
26292 @end example
26293
26294 This sets the stack reserve size to 0x10000 bytes and the stack commit
26295 size to 0x1000 bytes.
26296
26297 @item
26298 @code{-Wl} linker option
26299
26300 @example
26301 $ gnatlink hello -Wl,--stack=0x1000000
26302 @end example
26303
26304 This sets the stack reserve size to 0x1000000 bytes. Note that with
26305 @code{-Wl} option it is not possible to set the stack commit size
26306 because the comma is a separator for this option.
26307 @end itemize
26308
26309 @node Setting Heap Size from gnatlink,,Setting Stack Size from gnatlink,Mixed-Language Programming on Windows
26310 @anchor{gnat_ugn/platform_specific_information setting-heap-size-from-gnatlink}@anchor{137}@anchor{gnat_ugn/platform_specific_information id40}@anchor{220}
26311 @subsubsection Setting Heap Size from @code{gnatlink}
26312
26313
26314 Under Windows systems, it is possible to specify the program heap size from
26315 @code{gnatlink} using either of the following:
26316
26317
26318 @itemize *
26319
26320 @item
26321 @code{-Xlinker} linker option
26322
26323 @example
26324 $ gnatlink hello -Xlinker --heap=0x10000,0x1000
26325 @end example
26326
26327 This sets the heap reserve size to 0x10000 bytes and the heap commit
26328 size to 0x1000 bytes.
26329
26330 @item
26331 @code{-Wl} linker option
26332
26333 @example
26334 $ gnatlink hello -Wl,--heap=0x1000000
26335 @end example
26336
26337 This sets the heap reserve size to 0x1000000 bytes. Note that with
26338 @code{-Wl} option it is not possible to set the heap commit size
26339 because the comma is a separator for this option.
26340 @end itemize
26341
26342 @node Windows Specific Add-Ons,,Mixed-Language Programming on Windows,Microsoft Windows Topics
26343 @anchor{gnat_ugn/platform_specific_information windows-specific-add-ons}@anchor{221}@anchor{gnat_ugn/platform_specific_information win32-specific-addons}@anchor{222}
26344 @subsection Windows Specific Add-Ons
26345
26346
26347 This section describes the Windows specific add-ons.
26348
26349 @menu
26350 * Win32Ada::
26351 * wPOSIX::
26352
26353 @end menu
26354
26355 @node Win32Ada,wPOSIX,,Windows Specific Add-Ons
26356 @anchor{gnat_ugn/platform_specific_information win32ada}@anchor{223}@anchor{gnat_ugn/platform_specific_information id41}@anchor{224}
26357 @subsubsection Win32Ada
26358
26359
26360 Win32Ada is a binding for the Microsoft Win32 API. This binding can be
26361 easily installed from the provided installer. To use the Win32Ada
26362 binding you need to use a project file, and adding a single with_clause
26363 will give you full access to the Win32Ada binding sources and ensure
26364 that the proper libraries are passed to the linker.
26365
26366 @quotation
26367
26368 @example
26369 with "win32ada";
26370 project P is
26371 for Sources use ...;
26372 end P;
26373 @end example
26374 @end quotation
26375
26376 To build the application you just need to call gprbuild for the
26377 application's project, here p.gpr:
26378
26379 @quotation
26380
26381 @example
26382 gprbuild p.gpr
26383 @end example
26384 @end quotation
26385
26386 @node wPOSIX,,Win32Ada,Windows Specific Add-Ons
26387 @anchor{gnat_ugn/platform_specific_information id42}@anchor{225}@anchor{gnat_ugn/platform_specific_information wposix}@anchor{226}
26388 @subsubsection wPOSIX
26389
26390
26391 wPOSIX is a minimal POSIX binding whose goal is to help with building
26392 cross-platforms applications. This binding is not complete though, as
26393 the Win32 API does not provide the necessary support for all POSIX APIs.
26394
26395 To use the wPOSIX binding you need to use a project file, and adding
26396 a single with_clause will give you full access to the wPOSIX binding
26397 sources and ensure that the proper libraries are passed to the linker.
26398
26399 @quotation
26400
26401 @example
26402 with "wposix";
26403 project P is
26404 for Sources use ...;
26405 end P;
26406 @end example
26407 @end quotation
26408
26409 To build the application you just need to call gprbuild for the
26410 application's project, here p.gpr:
26411
26412 @quotation
26413
26414 @example
26415 gprbuild p.gpr
26416 @end example
26417 @end quotation
26418
26419 @node Mac OS Topics,,Microsoft Windows Topics,Platform-Specific Information
26420 @anchor{gnat_ugn/platform_specific_information mac-os-topics}@anchor{2d}@anchor{gnat_ugn/platform_specific_information id43}@anchor{227}
26421 @section Mac OS Topics
26422
26423
26424 @geindex OS X
26425
26426 This section describes topics that are specific to Apple's OS X
26427 platform.
26428
26429 @menu
26430 * Codesigning the Debugger::
26431
26432 @end menu
26433
26434 @node Codesigning the Debugger,,,Mac OS Topics
26435 @anchor{gnat_ugn/platform_specific_information codesigning-the-debugger}@anchor{228}
26436 @subsection Codesigning the Debugger
26437
26438
26439 The Darwin Kernel requires the debugger to have special permissions
26440 before it is allowed to control other processes. These permissions
26441 are granted by codesigning the GDB executable. Without these
26442 permissions, the debugger will report error messages such as:
26443
26444 @example
26445 Starting program: /x/y/foo
26446 Unable to find Mach task port for process-id 28885: (os/kern) failure (0x5).
26447 (please check gdb is codesigned - see taskgated(8))
26448 @end example
26449
26450 Codesigning requires a certificate. The following procedure explains
26451 how to create one:
26452
26453
26454 @itemize *
26455
26456 @item
26457 Start the Keychain Access application (in
26458 /Applications/Utilities/Keychain Access.app)
26459
26460 @item
26461 Select the Keychain Access -> Certificate Assistant ->
26462 Create a Certificate... menu
26463
26464 @item
26465 Then:
26466
26467
26468 @itemize *
26469
26470 @item
26471 Choose a name for the new certificate (this procedure will use
26472 "gdb-cert" as an example)
26473
26474 @item
26475 Set "Identity Type" to "Self Signed Root"
26476
26477 @item
26478 Set "Certificate Type" to "Code Signing"
26479
26480 @item
26481 Activate the "Let me override defaults" option
26482 @end itemize
26483
26484 @item
26485 Click several times on "Continue" until the "Specify a Location
26486 For The Certificate" screen appears, then set "Keychain" to "System"
26487
26488 @item
26489 Click on "Continue" until the certificate is created
26490
26491 @item
26492 Finally, in the view, double-click on the new certificate,
26493 and set "When using this certificate" to "Always Trust"
26494
26495 @item
26496 Exit the Keychain Access application and restart the computer
26497 (this is unfortunately required)
26498 @end itemize
26499
26500 Once a certificate has been created, the debugger can be codesigned
26501 as follow. In a Terminal, run the following command:
26502
26503 @quotation
26504
26505 @example
26506 $ codesign -f -s "gdb-cert" <gnat_install_prefix>/bin/gdb
26507 @end example
26508 @end quotation
26509
26510 where "gdb-cert" should be replaced by the actual certificate
26511 name chosen above, and <gnat_install_prefix> should be replaced by
26512 the location where you installed GNAT. Also, be sure that users are
26513 in the Unix group @code{_developer}.
26514
26515 @node Example of Binder Output File,Elaboration Order Handling in GNAT,Platform-Specific Information,Top
26516 @anchor{gnat_ugn/example_of_binder_output example-of-binder-output-file}@anchor{e}@anchor{gnat_ugn/example_of_binder_output doc}@anchor{229}@anchor{gnat_ugn/example_of_binder_output id1}@anchor{22a}
26517 @chapter Example of Binder Output File
26518
26519
26520 @geindex Binder output (example)
26521
26522 This Appendix displays the source code for the output file
26523 generated by @emph{gnatbind} for a simple 'Hello World' program.
26524 Comments have been added for clarification purposes.
26525
26526 @example
26527 -- The package is called Ada_Main unless this name is actually used
26528 -- as a unit name in the partition, in which case some other unique
26529 -- name is used.
26530
26531 pragma Ada_95;
26532 with System;
26533 package ada_main is
26534 pragma Warnings (Off);
26535
26536 -- The main program saves the parameters (argument count,
26537 -- argument values, environment pointer) in global variables
26538 -- for later access by other units including
26539 -- Ada.Command_Line.
26540
26541 gnat_argc : Integer;
26542 gnat_argv : System.Address;
26543 gnat_envp : System.Address;
26544
26545 -- The actual variables are stored in a library routine. This
26546 -- is useful for some shared library situations, where there
26547 -- are problems if variables are not in the library.
26548
26549 pragma Import (C, gnat_argc);
26550 pragma Import (C, gnat_argv);
26551 pragma Import (C, gnat_envp);
26552
26553 -- The exit status is similarly an external location
26554
26555 gnat_exit_status : Integer;
26556 pragma Import (C, gnat_exit_status);
26557
26558 GNAT_Version : constant String :=
26559 "GNAT Version: Pro 7.4.0w (20141119-49)" & ASCII.NUL;
26560 pragma Export (C, GNAT_Version, "__gnat_version");
26561
26562 Ada_Main_Program_Name : constant String := "_ada_hello" & ASCII.NUL;
26563 pragma Export (C, Ada_Main_Program_Name, "__gnat_ada_main_program_name");
26564
26565 -- This is the generated adainit routine that performs
26566 -- initialization at the start of execution. In the case
26567 -- where Ada is the main program, this main program makes
26568 -- a call to adainit at program startup.
26569
26570 procedure adainit;
26571 pragma Export (C, adainit, "adainit");
26572
26573 -- This is the generated adafinal routine that performs
26574 -- finalization at the end of execution. In the case where
26575 -- Ada is the main program, this main program makes a call
26576 -- to adafinal at program termination.
26577
26578 procedure adafinal;
26579 pragma Export (C, adafinal, "adafinal");
26580
26581 -- This routine is called at the start of execution. It is
26582 -- a dummy routine that is used by the debugger to breakpoint
26583 -- at the start of execution.
26584
26585 -- This is the actual generated main program (it would be
26586 -- suppressed if the no main program switch were used). As
26587 -- required by standard system conventions, this program has
26588 -- the external name main.
26589
26590 function main
26591 (argc : Integer;
26592 argv : System.Address;
26593 envp : System.Address)
26594 return Integer;
26595 pragma Export (C, main, "main");
26596
26597 -- The following set of constants give the version
26598 -- identification values for every unit in the bound
26599 -- partition. This identification is computed from all
26600 -- dependent semantic units, and corresponds to the
26601 -- string that would be returned by use of the
26602 -- Body_Version or Version attributes.
26603
26604 -- The following Export pragmas export the version numbers
26605 -- with symbolic names ending in B (for body) or S
26606 -- (for spec) so that they can be located in a link. The
26607 -- information provided here is sufficient to track down
26608 -- the exact versions of units used in a given build.
26609
26610 type Version_32 is mod 2 ** 32;
26611 u00001 : constant Version_32 := 16#8ad6e54a#;
26612 pragma Export (C, u00001, "helloB");
26613 u00002 : constant Version_32 := 16#fbff4c67#;
26614 pragma Export (C, u00002, "system__standard_libraryB");
26615 u00003 : constant Version_32 := 16#1ec6fd90#;
26616 pragma Export (C, u00003, "system__standard_libraryS");
26617 u00004 : constant Version_32 := 16#3ffc8e18#;
26618 pragma Export (C, u00004, "adaS");
26619 u00005 : constant Version_32 := 16#28f088c2#;
26620 pragma Export (C, u00005, "ada__text_ioB");
26621 u00006 : constant Version_32 := 16#f372c8ac#;
26622 pragma Export (C, u00006, "ada__text_ioS");
26623 u00007 : constant Version_32 := 16#2c143749#;
26624 pragma Export (C, u00007, "ada__exceptionsB");
26625 u00008 : constant Version_32 := 16#f4f0cce8#;
26626 pragma Export (C, u00008, "ada__exceptionsS");
26627 u00009 : constant Version_32 := 16#a46739c0#;
26628 pragma Export (C, u00009, "ada__exceptions__last_chance_handlerB");
26629 u00010 : constant Version_32 := 16#3aac8c92#;
26630 pragma Export (C, u00010, "ada__exceptions__last_chance_handlerS");
26631 u00011 : constant Version_32 := 16#1d274481#;
26632 pragma Export (C, u00011, "systemS");
26633 u00012 : constant Version_32 := 16#a207fefe#;
26634 pragma Export (C, u00012, "system__soft_linksB");
26635 u00013 : constant Version_32 := 16#467d9556#;
26636 pragma Export (C, u00013, "system__soft_linksS");
26637 u00014 : constant Version_32 := 16#b01dad17#;
26638 pragma Export (C, u00014, "system__parametersB");
26639 u00015 : constant Version_32 := 16#630d49fe#;
26640 pragma Export (C, u00015, "system__parametersS");
26641 u00016 : constant Version_32 := 16#b19b6653#;
26642 pragma Export (C, u00016, "system__secondary_stackB");
26643 u00017 : constant Version_32 := 16#b6468be8#;
26644 pragma Export (C, u00017, "system__secondary_stackS");
26645 u00018 : constant Version_32 := 16#39a03df9#;
26646 pragma Export (C, u00018, "system__storage_elementsB");
26647 u00019 : constant Version_32 := 16#30e40e85#;
26648 pragma Export (C, u00019, "system__storage_elementsS");
26649 u00020 : constant Version_32 := 16#41837d1e#;
26650 pragma Export (C, u00020, "system__stack_checkingB");
26651 u00021 : constant Version_32 := 16#93982f69#;
26652 pragma Export (C, u00021, "system__stack_checkingS");
26653 u00022 : constant Version_32 := 16#393398c1#;
26654 pragma Export (C, u00022, "system__exception_tableB");
26655 u00023 : constant Version_32 := 16#b33e2294#;
26656 pragma Export (C, u00023, "system__exception_tableS");
26657 u00024 : constant Version_32 := 16#ce4af020#;
26658 pragma Export (C, u00024, "system__exceptionsB");
26659 u00025 : constant Version_32 := 16#75442977#;
26660 pragma Export (C, u00025, "system__exceptionsS");
26661 u00026 : constant Version_32 := 16#37d758f1#;
26662 pragma Export (C, u00026, "system__exceptions__machineS");
26663 u00027 : constant Version_32 := 16#b895431d#;
26664 pragma Export (C, u00027, "system__exceptions_debugB");
26665 u00028 : constant Version_32 := 16#aec55d3f#;
26666 pragma Export (C, u00028, "system__exceptions_debugS");
26667 u00029 : constant Version_32 := 16#570325c8#;
26668 pragma Export (C, u00029, "system__img_intB");
26669 u00030 : constant Version_32 := 16#1ffca443#;
26670 pragma Export (C, u00030, "system__img_intS");
26671 u00031 : constant Version_32 := 16#b98c3e16#;
26672 pragma Export (C, u00031, "system__tracebackB");
26673 u00032 : constant Version_32 := 16#831a9d5a#;
26674 pragma Export (C, u00032, "system__tracebackS");
26675 u00033 : constant Version_32 := 16#9ed49525#;
26676 pragma Export (C, u00033, "system__traceback_entriesB");
26677 u00034 : constant Version_32 := 16#1d7cb2f1#;
26678 pragma Export (C, u00034, "system__traceback_entriesS");
26679 u00035 : constant Version_32 := 16#8c33a517#;
26680 pragma Export (C, u00035, "system__wch_conB");
26681 u00036 : constant Version_32 := 16#065a6653#;
26682 pragma Export (C, u00036, "system__wch_conS");
26683 u00037 : constant Version_32 := 16#9721e840#;
26684 pragma Export (C, u00037, "system__wch_stwB");
26685 u00038 : constant Version_32 := 16#2b4b4a52#;
26686 pragma Export (C, u00038, "system__wch_stwS");
26687 u00039 : constant Version_32 := 16#92b797cb#;
26688 pragma Export (C, u00039, "system__wch_cnvB");
26689 u00040 : constant Version_32 := 16#09eddca0#;
26690 pragma Export (C, u00040, "system__wch_cnvS");
26691 u00041 : constant Version_32 := 16#6033a23f#;
26692 pragma Export (C, u00041, "interfacesS");
26693 u00042 : constant Version_32 := 16#ece6fdb6#;
26694 pragma Export (C, u00042, "system__wch_jisB");
26695 u00043 : constant Version_32 := 16#899dc581#;
26696 pragma Export (C, u00043, "system__wch_jisS");
26697 u00044 : constant Version_32 := 16#10558b11#;
26698 pragma Export (C, u00044, "ada__streamsB");
26699 u00045 : constant Version_32 := 16#2e6701ab#;
26700 pragma Export (C, u00045, "ada__streamsS");
26701 u00046 : constant Version_32 := 16#db5c917c#;
26702 pragma Export (C, u00046, "ada__io_exceptionsS");
26703 u00047 : constant Version_32 := 16#12c8cd7d#;
26704 pragma Export (C, u00047, "ada__tagsB");
26705 u00048 : constant Version_32 := 16#ce72c228#;
26706 pragma Export (C, u00048, "ada__tagsS");
26707 u00049 : constant Version_32 := 16#c3335bfd#;
26708 pragma Export (C, u00049, "system__htableB");
26709 u00050 : constant Version_32 := 16#99e5f76b#;
26710 pragma Export (C, u00050, "system__htableS");
26711 u00051 : constant Version_32 := 16#089f5cd0#;
26712 pragma Export (C, u00051, "system__string_hashB");
26713 u00052 : constant Version_32 := 16#3bbb9c15#;
26714 pragma Export (C, u00052, "system__string_hashS");
26715 u00053 : constant Version_32 := 16#807fe041#;
26716 pragma Export (C, u00053, "system__unsigned_typesS");
26717 u00054 : constant Version_32 := 16#d27be59e#;
26718 pragma Export (C, u00054, "system__val_lluB");
26719 u00055 : constant Version_32 := 16#fa8db733#;
26720 pragma Export (C, u00055, "system__val_lluS");
26721 u00056 : constant Version_32 := 16#27b600b2#;
26722 pragma Export (C, u00056, "system__val_utilB");
26723 u00057 : constant Version_32 := 16#b187f27f#;
26724 pragma Export (C, u00057, "system__val_utilS");
26725 u00058 : constant Version_32 := 16#d1060688#;
26726 pragma Export (C, u00058, "system__case_utilB");
26727 u00059 : constant Version_32 := 16#392e2d56#;
26728 pragma Export (C, u00059, "system__case_utilS");
26729 u00060 : constant Version_32 := 16#84a27f0d#;
26730 pragma Export (C, u00060, "interfaces__c_streamsB");
26731 u00061 : constant Version_32 := 16#8bb5f2c0#;
26732 pragma Export (C, u00061, "interfaces__c_streamsS");
26733 u00062 : constant Version_32 := 16#6db6928f#;
26734 pragma Export (C, u00062, "system__crtlS");
26735 u00063 : constant Version_32 := 16#4e6a342b#;
26736 pragma Export (C, u00063, "system__file_ioB");
26737 u00064 : constant Version_32 := 16#ba56a5e4#;
26738 pragma Export (C, u00064, "system__file_ioS");
26739 u00065 : constant Version_32 := 16#b7ab275c#;
26740 pragma Export (C, u00065, "ada__finalizationB");
26741 u00066 : constant Version_32 := 16#19f764ca#;
26742 pragma Export (C, u00066, "ada__finalizationS");
26743 u00067 : constant Version_32 := 16#95817ed8#;
26744 pragma Export (C, u00067, "system__finalization_rootB");
26745 u00068 : constant Version_32 := 16#52d53711#;
26746 pragma Export (C, u00068, "system__finalization_rootS");
26747 u00069 : constant Version_32 := 16#769e25e6#;
26748 pragma Export (C, u00069, "interfaces__cB");
26749 u00070 : constant Version_32 := 16#4a38bedb#;
26750 pragma Export (C, u00070, "interfaces__cS");
26751 u00071 : constant Version_32 := 16#07e6ee66#;
26752 pragma Export (C, u00071, "system__os_libB");
26753 u00072 : constant Version_32 := 16#d7b69782#;
26754 pragma Export (C, u00072, "system__os_libS");
26755 u00073 : constant Version_32 := 16#1a817b8e#;
26756 pragma Export (C, u00073, "system__stringsB");
26757 u00074 : constant Version_32 := 16#639855e7#;
26758 pragma Export (C, u00074, "system__stringsS");
26759 u00075 : constant Version_32 := 16#e0b8de29#;
26760 pragma Export (C, u00075, "system__file_control_blockS");
26761 u00076 : constant Version_32 := 16#b5b2aca1#;
26762 pragma Export (C, u00076, "system__finalization_mastersB");
26763 u00077 : constant Version_32 := 16#69316dc1#;
26764 pragma Export (C, u00077, "system__finalization_mastersS");
26765 u00078 : constant Version_32 := 16#57a37a42#;
26766 pragma Export (C, u00078, "system__address_imageB");
26767 u00079 : constant Version_32 := 16#bccbd9bb#;
26768 pragma Export (C, u00079, "system__address_imageS");
26769 u00080 : constant Version_32 := 16#7268f812#;
26770 pragma Export (C, u00080, "system__img_boolB");
26771 u00081 : constant Version_32 := 16#e8fe356a#;
26772 pragma Export (C, u00081, "system__img_boolS");
26773 u00082 : constant Version_32 := 16#d7aac20c#;
26774 pragma Export (C, u00082, "system__ioB");
26775 u00083 : constant Version_32 := 16#8365b3ce#;
26776 pragma Export (C, u00083, "system__ioS");
26777 u00084 : constant Version_32 := 16#6d4d969a#;
26778 pragma Export (C, u00084, "system__storage_poolsB");
26779 u00085 : constant Version_32 := 16#e87cc305#;
26780 pragma Export (C, u00085, "system__storage_poolsS");
26781 u00086 : constant Version_32 := 16#e34550ca#;
26782 pragma Export (C, u00086, "system__pool_globalB");
26783 u00087 : constant Version_32 := 16#c88d2d16#;
26784 pragma Export (C, u00087, "system__pool_globalS");
26785 u00088 : constant Version_32 := 16#9d39c675#;
26786 pragma Export (C, u00088, "system__memoryB");
26787 u00089 : constant Version_32 := 16#445a22b5#;
26788 pragma Export (C, u00089, "system__memoryS");
26789 u00090 : constant Version_32 := 16#6a859064#;
26790 pragma Export (C, u00090, "system__storage_pools__subpoolsB");
26791 u00091 : constant Version_32 := 16#e3b008dc#;
26792 pragma Export (C, u00091, "system__storage_pools__subpoolsS");
26793 u00092 : constant Version_32 := 16#63f11652#;
26794 pragma Export (C, u00092, "system__storage_pools__subpools__finalizationB");
26795 u00093 : constant Version_32 := 16#fe2f4b3a#;
26796 pragma Export (C, u00093, "system__storage_pools__subpools__finalizationS");
26797
26798 -- BEGIN ELABORATION ORDER
26799 -- ada%s
26800 -- interfaces%s
26801 -- system%s
26802 -- system.case_util%s
26803 -- system.case_util%b
26804 -- system.htable%s
26805 -- system.img_bool%s
26806 -- system.img_bool%b
26807 -- system.img_int%s
26808 -- system.img_int%b
26809 -- system.io%s
26810 -- system.io%b
26811 -- system.parameters%s
26812 -- system.parameters%b
26813 -- system.crtl%s
26814 -- interfaces.c_streams%s
26815 -- interfaces.c_streams%b
26816 -- system.standard_library%s
26817 -- system.exceptions_debug%s
26818 -- system.exceptions_debug%b
26819 -- system.storage_elements%s
26820 -- system.storage_elements%b
26821 -- system.stack_checking%s
26822 -- system.stack_checking%b
26823 -- system.string_hash%s
26824 -- system.string_hash%b
26825 -- system.htable%b
26826 -- system.strings%s
26827 -- system.strings%b
26828 -- system.os_lib%s
26829 -- system.traceback_entries%s
26830 -- system.traceback_entries%b
26831 -- ada.exceptions%s
26832 -- system.soft_links%s
26833 -- system.unsigned_types%s
26834 -- system.val_llu%s
26835 -- system.val_util%s
26836 -- system.val_util%b
26837 -- system.val_llu%b
26838 -- system.wch_con%s
26839 -- system.wch_con%b
26840 -- system.wch_cnv%s
26841 -- system.wch_jis%s
26842 -- system.wch_jis%b
26843 -- system.wch_cnv%b
26844 -- system.wch_stw%s
26845 -- system.wch_stw%b
26846 -- ada.exceptions.last_chance_handler%s
26847 -- ada.exceptions.last_chance_handler%b
26848 -- system.address_image%s
26849 -- system.exception_table%s
26850 -- system.exception_table%b
26851 -- ada.io_exceptions%s
26852 -- ada.tags%s
26853 -- ada.streams%s
26854 -- ada.streams%b
26855 -- interfaces.c%s
26856 -- system.exceptions%s
26857 -- system.exceptions%b
26858 -- system.exceptions.machine%s
26859 -- system.finalization_root%s
26860 -- system.finalization_root%b
26861 -- ada.finalization%s
26862 -- ada.finalization%b
26863 -- system.storage_pools%s
26864 -- system.storage_pools%b
26865 -- system.finalization_masters%s
26866 -- system.storage_pools.subpools%s
26867 -- system.storage_pools.subpools.finalization%s
26868 -- system.storage_pools.subpools.finalization%b
26869 -- system.memory%s
26870 -- system.memory%b
26871 -- system.standard_library%b
26872 -- system.pool_global%s
26873 -- system.pool_global%b
26874 -- system.file_control_block%s
26875 -- system.file_io%s
26876 -- system.secondary_stack%s
26877 -- system.file_io%b
26878 -- system.storage_pools.subpools%b
26879 -- system.finalization_masters%b
26880 -- interfaces.c%b
26881 -- ada.tags%b
26882 -- system.soft_links%b
26883 -- system.os_lib%b
26884 -- system.secondary_stack%b
26885 -- system.address_image%b
26886 -- system.traceback%s
26887 -- ada.exceptions%b
26888 -- system.traceback%b
26889 -- ada.text_io%s
26890 -- ada.text_io%b
26891 -- hello%b
26892 -- END ELABORATION ORDER
26893
26894 end ada_main;
26895 @end example
26896
26897 @example
26898 pragma Ada_95;
26899 -- The following source file name pragmas allow the generated file
26900 -- names to be unique for different main programs. They are needed
26901 -- since the package name will always be Ada_Main.
26902
26903 pragma Source_File_Name (ada_main, Spec_File_Name => "b~hello.ads");
26904 pragma Source_File_Name (ada_main, Body_File_Name => "b~hello.adb");
26905
26906 pragma Suppress (Overflow_Check);
26907 with Ada.Exceptions;
26908
26909 -- Generated package body for Ada_Main starts here
26910
26911 package body ada_main is
26912 pragma Warnings (Off);
26913
26914 -- These values are reference counter associated to units which have
26915 -- been elaborated. It is also used to avoid elaborating the
26916 -- same unit twice.
26917
26918 E72 : Short_Integer; pragma Import (Ada, E72, "system__os_lib_E");
26919 E13 : Short_Integer; pragma Import (Ada, E13, "system__soft_links_E");
26920 E23 : Short_Integer; pragma Import (Ada, E23, "system__exception_table_E");
26921 E46 : Short_Integer; pragma Import (Ada, E46, "ada__io_exceptions_E");
26922 E48 : Short_Integer; pragma Import (Ada, E48, "ada__tags_E");
26923 E45 : Short_Integer; pragma Import (Ada, E45, "ada__streams_E");
26924 E70 : Short_Integer; pragma Import (Ada, E70, "interfaces__c_E");
26925 E25 : Short_Integer; pragma Import (Ada, E25, "system__exceptions_E");
26926 E68 : Short_Integer; pragma Import (Ada, E68, "system__finalization_root_E");
26927 E66 : Short_Integer; pragma Import (Ada, E66, "ada__finalization_E");
26928 E85 : Short_Integer; pragma Import (Ada, E85, "system__storage_pools_E");
26929 E77 : Short_Integer; pragma Import (Ada, E77, "system__finalization_masters_E");
26930 E91 : Short_Integer; pragma Import (Ada, E91, "system__storage_pools__subpools_E");
26931 E87 : Short_Integer; pragma Import (Ada, E87, "system__pool_global_E");
26932 E75 : Short_Integer; pragma Import (Ada, E75, "system__file_control_block_E");
26933 E64 : Short_Integer; pragma Import (Ada, E64, "system__file_io_E");
26934 E17 : Short_Integer; pragma Import (Ada, E17, "system__secondary_stack_E");
26935 E06 : Short_Integer; pragma Import (Ada, E06, "ada__text_io_E");
26936
26937 Local_Priority_Specific_Dispatching : constant String := "";
26938 Local_Interrupt_States : constant String := "";
26939
26940 Is_Elaborated : Boolean := False;
26941
26942 procedure finalize_library is
26943 begin
26944 E06 := E06 - 1;
26945 declare
26946 procedure F1;
26947 pragma Import (Ada, F1, "ada__text_io__finalize_spec");
26948 begin
26949 F1;
26950 end;
26951 E77 := E77 - 1;
26952 E91 := E91 - 1;
26953 declare
26954 procedure F2;
26955 pragma Import (Ada, F2, "system__file_io__finalize_body");
26956 begin
26957 E64 := E64 - 1;
26958 F2;
26959 end;
26960 declare
26961 procedure F3;
26962 pragma Import (Ada, F3, "system__file_control_block__finalize_spec");
26963 begin
26964 E75 := E75 - 1;
26965 F3;
26966 end;
26967 E87 := E87 - 1;
26968 declare
26969 procedure F4;
26970 pragma Import (Ada, F4, "system__pool_global__finalize_spec");
26971 begin
26972 F4;
26973 end;
26974 declare
26975 procedure F5;
26976 pragma Import (Ada, F5, "system__storage_pools__subpools__finalize_spec");
26977 begin
26978 F5;
26979 end;
26980 declare
26981 procedure F6;
26982 pragma Import (Ada, F6, "system__finalization_masters__finalize_spec");
26983 begin
26984 F6;
26985 end;
26986 declare
26987 procedure Reraise_Library_Exception_If_Any;
26988 pragma Import (Ada, Reraise_Library_Exception_If_Any, "__gnat_reraise_library_exception_if_any");
26989 begin
26990 Reraise_Library_Exception_If_Any;
26991 end;
26992 end finalize_library;
26993
26994 -------------
26995 -- adainit --
26996 -------------
26997
26998 procedure adainit is
26999
27000 Main_Priority : Integer;
27001 pragma Import (C, Main_Priority, "__gl_main_priority");
27002 Time_Slice_Value : Integer;
27003 pragma Import (C, Time_Slice_Value, "__gl_time_slice_val");
27004 WC_Encoding : Character;
27005 pragma Import (C, WC_Encoding, "__gl_wc_encoding");
27006 Locking_Policy : Character;
27007 pragma Import (C, Locking_Policy, "__gl_locking_policy");
27008 Queuing_Policy : Character;
27009 pragma Import (C, Queuing_Policy, "__gl_queuing_policy");
27010 Task_Dispatching_Policy : Character;
27011 pragma Import (C, Task_Dispatching_Policy, "__gl_task_dispatching_policy");
27012 Priority_Specific_Dispatching : System.Address;
27013 pragma Import (C, Priority_Specific_Dispatching, "__gl_priority_specific_dispatching");
27014 Num_Specific_Dispatching : Integer;
27015 pragma Import (C, Num_Specific_Dispatching, "__gl_num_specific_dispatching");
27016 Main_CPU : Integer;
27017 pragma Import (C, Main_CPU, "__gl_main_cpu");
27018 Interrupt_States : System.Address;
27019 pragma Import (C, Interrupt_States, "__gl_interrupt_states");
27020 Num_Interrupt_States : Integer;
27021 pragma Import (C, Num_Interrupt_States, "__gl_num_interrupt_states");
27022 Unreserve_All_Interrupts : Integer;
27023 pragma Import (C, Unreserve_All_Interrupts, "__gl_unreserve_all_interrupts");
27024 Detect_Blocking : Integer;
27025 pragma Import (C, Detect_Blocking, "__gl_detect_blocking");
27026 Default_Stack_Size : Integer;
27027 pragma Import (C, Default_Stack_Size, "__gl_default_stack_size");
27028 Leap_Seconds_Support : Integer;
27029 pragma Import (C, Leap_Seconds_Support, "__gl_leap_seconds_support");
27030
27031 procedure Runtime_Initialize;
27032 pragma Import (C, Runtime_Initialize, "__gnat_runtime_initialize");
27033
27034 Finalize_Library_Objects : No_Param_Proc;
27035 pragma Import (C, Finalize_Library_Objects, "__gnat_finalize_library_objects");
27036
27037 -- Start of processing for adainit
27038
27039 begin
27040
27041 -- Record various information for this partition. The values
27042 -- are derived by the binder from information stored in the ali
27043 -- files by the compiler.
27044
27045 if Is_Elaborated then
27046 return;
27047 end if;
27048 Is_Elaborated := True;
27049 Main_Priority := -1;
27050 Time_Slice_Value := -1;
27051 WC_Encoding := 'b';
27052 Locking_Policy := ' ';
27053 Queuing_Policy := ' ';
27054 Task_Dispatching_Policy := ' ';
27055 Priority_Specific_Dispatching :=
27056 Local_Priority_Specific_Dispatching'Address;
27057 Num_Specific_Dispatching := 0;
27058 Main_CPU := -1;
27059 Interrupt_States := Local_Interrupt_States'Address;
27060 Num_Interrupt_States := 0;
27061 Unreserve_All_Interrupts := 0;
27062 Detect_Blocking := 0;
27063 Default_Stack_Size := -1;
27064 Leap_Seconds_Support := 0;
27065
27066 Runtime_Initialize;
27067
27068 Finalize_Library_Objects := finalize_library'access;
27069
27070 -- Now we have the elaboration calls for all units in the partition.
27071 -- The Elab_Spec and Elab_Body attributes generate references to the
27072 -- implicit elaboration procedures generated by the compiler for
27073 -- each unit that requires elaboration. Increment a counter of
27074 -- reference for each unit.
27075
27076 System.Soft_Links'Elab_Spec;
27077 System.Exception_Table'Elab_Body;
27078 E23 := E23 + 1;
27079 Ada.Io_Exceptions'Elab_Spec;
27080 E46 := E46 + 1;
27081 Ada.Tags'Elab_Spec;
27082 Ada.Streams'Elab_Spec;
27083 E45 := E45 + 1;
27084 Interfaces.C'Elab_Spec;
27085 System.Exceptions'Elab_Spec;
27086 E25 := E25 + 1;
27087 System.Finalization_Root'Elab_Spec;
27088 E68 := E68 + 1;
27089 Ada.Finalization'Elab_Spec;
27090 E66 := E66 + 1;
27091 System.Storage_Pools'Elab_Spec;
27092 E85 := E85 + 1;
27093 System.Finalization_Masters'Elab_Spec;
27094 System.Storage_Pools.Subpools'Elab_Spec;
27095 System.Pool_Global'Elab_Spec;
27096 E87 := E87 + 1;
27097 System.File_Control_Block'Elab_Spec;
27098 E75 := E75 + 1;
27099 System.File_Io'Elab_Body;
27100 E64 := E64 + 1;
27101 E91 := E91 + 1;
27102 System.Finalization_Masters'Elab_Body;
27103 E77 := E77 + 1;
27104 E70 := E70 + 1;
27105 Ada.Tags'Elab_Body;
27106 E48 := E48 + 1;
27107 System.Soft_Links'Elab_Body;
27108 E13 := E13 + 1;
27109 System.Os_Lib'Elab_Body;
27110 E72 := E72 + 1;
27111 System.Secondary_Stack'Elab_Body;
27112 E17 := E17 + 1;
27113 Ada.Text_Io'Elab_Spec;
27114 Ada.Text_Io'Elab_Body;
27115 E06 := E06 + 1;
27116 end adainit;
27117
27118 --------------
27119 -- adafinal --
27120 --------------
27121
27122 procedure adafinal is
27123 procedure s_stalib_adafinal;
27124 pragma Import (C, s_stalib_adafinal, "system__standard_library__adafinal");
27125
27126 procedure Runtime_Finalize;
27127 pragma Import (C, Runtime_Finalize, "__gnat_runtime_finalize");
27128
27129 begin
27130 if not Is_Elaborated then
27131 return;
27132 end if;
27133 Is_Elaborated := False;
27134 Runtime_Finalize;
27135 s_stalib_adafinal;
27136 end adafinal;
27137
27138 -- We get to the main program of the partition by using
27139 -- pragma Import because if we try to with the unit and
27140 -- call it Ada style, then not only do we waste time
27141 -- recompiling it, but also, we don't really know the right
27142 -- switches (e.g.@@: identifier character set) to be used
27143 -- to compile it.
27144
27145 procedure Ada_Main_Program;
27146 pragma Import (Ada, Ada_Main_Program, "_ada_hello");
27147
27148 ----------
27149 -- main --
27150 ----------
27151
27152 -- main is actually a function, as in the ANSI C standard,
27153 -- defined to return the exit status. The three parameters
27154 -- are the argument count, argument values and environment
27155 -- pointer.
27156
27157 function main
27158 (argc : Integer;
27159 argv : System.Address;
27160 envp : System.Address)
27161 return Integer
27162 is
27163 -- The initialize routine performs low level system
27164 -- initialization using a standard library routine which
27165 -- sets up signal handling and performs any other
27166 -- required setup. The routine can be found in file
27167 -- a-init.c.
27168
27169 procedure initialize;
27170 pragma Import (C, initialize, "__gnat_initialize");
27171
27172 -- The finalize routine performs low level system
27173 -- finalization using a standard library routine. The
27174 -- routine is found in file a-final.c and in the standard
27175 -- distribution is a dummy routine that does nothing, so
27176 -- really this is a hook for special user finalization.
27177
27178 procedure finalize;
27179 pragma Import (C, finalize, "__gnat_finalize");
27180
27181 -- The following is to initialize the SEH exceptions
27182
27183 SEH : aliased array (1 .. 2) of Integer;
27184
27185 Ensure_Reference : aliased System.Address := Ada_Main_Program_Name'Address;
27186 pragma Volatile (Ensure_Reference);
27187
27188 -- Start of processing for main
27189
27190 begin
27191 -- Save global variables
27192
27193 gnat_argc := argc;
27194 gnat_argv := argv;
27195 gnat_envp := envp;
27196
27197 -- Call low level system initialization
27198
27199 Initialize (SEH'Address);
27200
27201 -- Call our generated Ada initialization routine
27202
27203 adainit;
27204
27205 -- Now we call the main program of the partition
27206
27207 Ada_Main_Program;
27208
27209 -- Perform Ada finalization
27210
27211 adafinal;
27212
27213 -- Perform low level system finalization
27214
27215 Finalize;
27216
27217 -- Return the proper exit status
27218 return (gnat_exit_status);
27219 end;
27220
27221 -- This section is entirely comments, so it has no effect on the
27222 -- compilation of the Ada_Main package. It provides the list of
27223 -- object files and linker options, as well as some standard
27224 -- libraries needed for the link. The gnatlink utility parses
27225 -- this b~hello.adb file to read these comment lines to generate
27226 -- the appropriate command line arguments for the call to the
27227 -- system linker. The BEGIN/END lines are used for sentinels for
27228 -- this parsing operation.
27229
27230 -- The exact file names will of course depend on the environment,
27231 -- host/target and location of files on the host system.
27232
27233 -- BEGIN Object file/option list
27234 -- ./hello.o
27235 -- -L./
27236 -- -L/usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/
27237 -- /usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
27238 -- END Object file/option list
27239
27240 end ada_main;
27241 @end example
27242
27243 The Ada code in the above example is exactly what is generated by the
27244 binder. We have added comments to more clearly indicate the function
27245 of each part of the generated @code{Ada_Main} package.
27246
27247 The code is standard Ada in all respects, and can be processed by any
27248 tools that handle Ada. In particular, it is possible to use the debugger
27249 in Ada mode to debug the generated @code{Ada_Main} package. For example,
27250 suppose that for reasons that you do not understand, your program is crashing
27251 during elaboration of the body of @code{Ada.Text_IO}. To locate this bug,
27252 you can place a breakpoint on the call:
27253
27254 @quotation
27255
27256 @example
27257 Ada.Text_Io'Elab_Body;
27258 @end example
27259 @end quotation
27260
27261 and trace the elaboration routine for this package to find out where
27262 the problem might be (more usually of course you would be debugging
27263 elaboration code in your own application).
27264
27265 @c -- Example: A |withing| unit has a |with| clause, it |withs| a |withed| unit
27266
27267 @node Elaboration Order Handling in GNAT,Inline Assembler,Example of Binder Output File,Top
27268 @anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-order-handling-in-gnat}@anchor{f}@anchor{gnat_ugn/elaboration_order_handling_in_gnat doc}@anchor{22b}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id1}@anchor{22c}
27269 @chapter Elaboration Order Handling in GNAT
27270
27271
27272 @geindex Order of elaboration
27273
27274 @geindex Elaboration control
27275
27276 This appendix describes the handling of elaboration code in Ada and GNAT, and
27277 discusses how the order of elaboration of program units can be controlled in
27278 GNAT, either automatically or with explicit programming features.
27279
27280 @menu
27281 * Elaboration Code::
27282 * Elaboration Order::
27283 * Checking the Elaboration Order::
27284 * Controlling the Elaboration Order in Ada::
27285 * Controlling the Elaboration Order in GNAT::
27286 * Mixing Elaboration Models::
27287 * ABE Diagnostics::
27288 * SPARK Diagnostics::
27289 * Elaboration Circularities::
27290 * Resolving Elaboration Circularities::
27291 * Elaboration-related Compiler Switches::
27292 * Summary of Procedures for Elaboration Control::
27293 * Inspecting the Chosen Elaboration Order::
27294
27295 @end menu
27296
27297 @node Elaboration Code,Elaboration Order,,Elaboration Order Handling in GNAT
27298 @anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-code}@anchor{22d}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id2}@anchor{22e}
27299 @section Elaboration Code
27300
27301
27302 Ada defines the term @emph{execution} as the process by which a construct achieves
27303 its run-time effect. This process is also referred to as @strong{elaboration} for
27304 declarations and @emph{evaluation} for expressions.
27305
27306 The execution model in Ada allows for certain sections of an Ada program to be
27307 executed prior to execution of the program itself, primarily with the intent of
27308 initializing data. These sections are referred to as @strong{elaboration code}.
27309 Elaboration code is executed as follows:
27310
27311
27312 @itemize *
27313
27314 @item
27315 All partitions of an Ada program are executed in parallel with one another,
27316 possibly in a separate address space, and possibly on a separate computer.
27317
27318 @item
27319 The execution of a partition involves running the environment task for that
27320 partition.
27321
27322 @item
27323 The environment task executes all elaboration code (if available) for all
27324 units within that partition. This code is said to be executed at
27325 @strong{elaboration time}.
27326
27327 @item
27328 The environment task executes the Ada program (if available) for that
27329 partition.
27330 @end itemize
27331
27332 In addition to the Ada terminology, this appendix defines the following terms:
27333
27334
27335 @itemize *
27336
27337 @item
27338 @emph{Invocation}
27339
27340 The act of calling a subprogram, instantiating a generic, or activating a
27341 task.
27342
27343 @item
27344 @emph{Scenario}
27345
27346 A construct that is elaborated or invoked by elaboration code is referred to
27347 as an @emph{elaboration scenario} or simply a @strong{scenario}. GNAT recognizes the
27348 following scenarios:
27349
27350
27351 @itemize -
27352
27353 @item
27354 @code{'Access} of entries, operators, and subprograms
27355
27356 @item
27357 Activation of tasks
27358
27359 @item
27360 Calls to entries, operators, and subprograms
27361
27362 @item
27363 Instantiations of generic templates
27364 @end itemize
27365
27366 @item
27367 @emph{Target}
27368
27369 A construct elaborated by a scenario is referred to as @emph{elaboration target}
27370 or simply @strong{target}. GNAT recognizes the following targets:
27371
27372
27373 @itemize -
27374
27375 @item
27376 For @code{'Access} of entries, operators, and subprograms, the target is the
27377 entry, operator, or subprogram being aliased.
27378
27379 @item
27380 For activation of tasks, the target is the task body
27381
27382 @item
27383 For calls to entries, operators, and subprograms, the target is the entry,
27384 operator, or subprogram being invoked.
27385
27386 @item
27387 For instantiations of generic templates, the target is the generic template
27388 being instantiated.
27389 @end itemize
27390 @end itemize
27391
27392 Elaboration code may appear in two distinct contexts:
27393
27394
27395 @itemize *
27396
27397 @item
27398 @emph{Library level}
27399
27400 A scenario appears at the library level when it is encapsulated by a package
27401 [body] compilation unit, ignoring any other package [body] declarations in
27402 between.
27403
27404 @example
27405 with Server;
27406 package Client is
27407 procedure Proc;
27408
27409 package Nested is
27410 Val : ... := Server.Func;
27411 end Nested;
27412 end Client;
27413 @end example
27414
27415 In the example above, the call to @code{Server.Func} is an elaboration scenario
27416 because it appears at the library level of package @code{Client}. Note that the
27417 declaration of package @code{Nested} is ignored according to the definition
27418 given above. As a result, the call to @code{Server.Func} will be invoked when
27419 the spec of unit @code{Client} is elaborated.
27420
27421 @item
27422 @emph{Package body statements}
27423
27424 A scenario appears within the statement sequence of a package body when it is
27425 bounded by the region starting from the @code{begin} keyword of the package body
27426 and ending at the @code{end} keyword of the package body.
27427
27428 @example
27429 package body Client is
27430 procedure Proc is
27431 begin
27432 ...
27433 end Proc;
27434 begin
27435 Proc;
27436 end Client;
27437 @end example
27438
27439 In the example above, the call to @code{Proc} is an elaboration scenario because
27440 it appears within the statement sequence of package body @code{Client}. As a
27441 result, the call to @code{Proc} will be invoked when the body of @code{Client} is
27442 elaborated.
27443 @end itemize
27444
27445 @node Elaboration Order,Checking the Elaboration Order,Elaboration Code,Elaboration Order Handling in GNAT
27446 @anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-order}@anchor{22f}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id3}@anchor{230}
27447 @section Elaboration Order
27448
27449
27450 The sequence by which the elaboration code of all units within a partition is
27451 executed is referred to as @strong{elaboration order}.
27452
27453 Within a single unit, elaboration code is executed in sequential order.
27454
27455 @quotation
27456
27457 @example
27458 package body Client is
27459 Result : ... := Server.Func;
27460
27461 procedure Proc is
27462 package Inst is new Server.Gen;
27463 begin
27464 Inst.Eval (Result);
27465 end Proc;
27466 begin
27467 Proc;
27468 end Client;
27469 @end example
27470 @end quotation
27471
27472 In the example above, the elaboration order within package body @code{Client} is
27473 as follows:
27474
27475
27476 @enumerate
27477
27478 @item
27479 The object declaration of @code{Result} is elaborated.
27480
27481
27482 @itemize *
27483
27484 @item
27485 Function @code{Server.Func} is invoked.
27486 @end itemize
27487
27488 @item
27489 The subprogram body of @code{Proc} is elaborated.
27490
27491 @item
27492 Procedure @code{Proc} is invoked.
27493
27494
27495 @itemize *
27496
27497 @item
27498 Generic unit @code{Server.Gen} is instantiated as @code{Inst}.
27499
27500 @item
27501 Instance @code{Inst} is elaborated.
27502
27503 @item
27504 Procedure @code{Inst.Eval} is invoked.
27505 @end itemize
27506 @end enumerate
27507
27508 The elaboration order of all units within a partition depends on the following
27509 factors:
27510
27511
27512 @itemize *
27513
27514 @item
27515 @emph{with}ed units
27516
27517 @item
27518 parent units
27519
27520 @item
27521 purity of units
27522
27523 @item
27524 preelaborability of units
27525
27526 @item
27527 presence of elaboration-control pragmas
27528
27529 @item
27530 invocations performed in elaboration code
27531 @end itemize
27532
27533 A program may have several elaboration orders depending on its structure.
27534
27535 @quotation
27536
27537 @example
27538 package Server is
27539 function Func (Index : Integer) return Integer;
27540 end Server;
27541 @end example
27542
27543 @example
27544 package body Server is
27545 Results : array (1 .. 5) of Integer := (1, 2, 3, 4, 5);
27546
27547 function Func (Index : Integer) return Integer is
27548 begin
27549 return Results (Index);
27550 end Func;
27551 end Server;
27552 @end example
27553
27554 @example
27555 with Server;
27556 package Client is
27557 Val : constant Integer := Server.Func (3);
27558 end Client;
27559 @end example
27560
27561 @example
27562 with Client;
27563 procedure Main is begin null; end Main;
27564 @end example
27565 @end quotation
27566
27567 The following elaboration order exhibits a fundamental problem referred to as
27568 @emph{access-before-elaboration} or simply @strong{ABE}.
27569
27570 @quotation
27571
27572 @example
27573 spec of Server
27574 spec of Client
27575 body of Server
27576 body of Main
27577 @end example
27578 @end quotation
27579
27580 The elaboration of @code{Server}'s spec materializes function @code{Func}, making it
27581 callable. The elaboration of @code{Client}'s spec elaborates the declaration of
27582 @code{Val}. This invokes function @code{Server.Func}, however the body of
27583 @code{Server.Func} has not been elaborated yet because @code{Server}'s body comes
27584 after @code{Client}'s spec in the elaboration order. As a result, the value of
27585 constant @code{Val} is now undefined.
27586
27587 Without any guarantees from the language, an undetected ABE problem may hinder
27588 proper initialization of data, which in turn may lead to undefined behavior at
27589 run time. To prevent such ABE problems, Ada employs dynamic checks in the same
27590 vein as index or null exclusion checks. A failed ABE check raises exception
27591 @code{Program_Error}.
27592
27593 The following elaboration order avoids the ABE problem and the program can be
27594 successfully elaborated.
27595
27596 @quotation
27597
27598 @example
27599 spec of Server
27600 body of Server
27601 spec of Client
27602 body of Main
27603 @end example
27604 @end quotation
27605
27606 Ada states that a total elaboration order must exist, but it does not define
27607 what this order is. A compiler is thus tasked with choosing a suitable
27608 elaboration order which satisfies the dependencies imposed by @emph{with} clauses,
27609 unit categorization, elaboration-control pragmas, and invocations performed in
27610 elaboration code. Ideally an order that avoids ABE problems should be chosen,
27611 however a compiler may not always find such an order due to complications with
27612 respect to control and data flow.
27613
27614 @node Checking the Elaboration Order,Controlling the Elaboration Order in Ada,Elaboration Order,Elaboration Order Handling in GNAT
27615 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id4}@anchor{231}@anchor{gnat_ugn/elaboration_order_handling_in_gnat checking-the-elaboration-order}@anchor{232}
27616 @section Checking the Elaboration Order
27617
27618
27619 To avoid placing the entire elaboration-order burden on the programmer, Ada
27620 provides three lines of defense:
27621
27622
27623 @itemize *
27624
27625 @item
27626 @emph{Static semantics}
27627
27628 Static semantic rules restrict the possible choice of elaboration order. For
27629 instance, if unit Client @emph{with}s unit Server, then the spec of Server is
27630 always elaborated prior to Client. The same principle applies to child units
27631 - the spec of a parent unit is always elaborated prior to the child unit.
27632
27633 @item
27634 @emph{Dynamic semantics}
27635
27636 Dynamic checks are performed at run time, to ensure that a target is
27637 elaborated prior to a scenario that invokes it, thus avoiding ABE problems.
27638 A failed run-time check raises exception @code{Program_Error}. The following
27639 restrictions apply:
27640
27641
27642 @itemize -
27643
27644 @item
27645 @emph{Restrictions on calls}
27646
27647 An entry, operator, or subprogram can be called from elaboration code only
27648 when the corresponding body has been elaborated.
27649
27650 @item
27651 @emph{Restrictions on instantiations}
27652
27653 A generic unit can be instantiated by elaboration code only when the
27654 corresponding body has been elaborated.
27655
27656 @item
27657 @emph{Restrictions on task activation}
27658
27659 A task can be activated by elaboration code only when the body of the
27660 associated task type has been elaborated.
27661 @end itemize
27662
27663 The restrictions above can be summarized by the following rule:
27664
27665 @emph{If a target has a body, then this body must be elaborated prior to the
27666 scenario that invokes the target.}
27667
27668 @item
27669 @emph{Elaboration control}
27670
27671 Pragmas are provided for the programmer to specify the desired elaboration
27672 order.
27673 @end itemize
27674
27675 @node Controlling the Elaboration Order in Ada,Controlling the Elaboration Order in GNAT,Checking the Elaboration Order,Elaboration Order Handling in GNAT
27676 @anchor{gnat_ugn/elaboration_order_handling_in_gnat controlling-the-elaboration-order-in-ada}@anchor{233}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id5}@anchor{234}
27677 @section Controlling the Elaboration Order in Ada
27678
27679
27680 Ada provides several idioms and pragmas to aid the programmer with specifying
27681 the desired elaboration order and avoiding ABE problems altogether.
27682
27683
27684 @itemize *
27685
27686 @item
27687 @emph{Packages without a body}
27688
27689 A library package which does not require a completing body does not suffer
27690 from ABE problems.
27691
27692 @example
27693 package Pack is
27694 generic
27695 type Element is private;
27696 package Containers is
27697 type Element_Array is array (1 .. 10) of Element;
27698 end Containers;
27699 end Pack;
27700 @end example
27701
27702 In the example above, package @code{Pack} does not require a body because it
27703 does not contain any constructs which require completion in a body. As a
27704 result, generic @code{Pack.Containers} can be instantiated without encountering
27705 any ABE problems.
27706 @end itemize
27707
27708 @geindex pragma Pure
27709
27710
27711 @itemize *
27712
27713 @item
27714 @emph{pragma Pure}
27715
27716 Pragma @code{Pure} places sufficient restrictions on a unit to guarantee that no
27717 scenario within the unit can result in an ABE problem.
27718 @end itemize
27719
27720 @geindex pragma Preelaborate
27721
27722
27723 @itemize *
27724
27725 @item
27726 @emph{pragma Preelaborate}
27727
27728 Pragma @code{Preelaborate} is slightly less restrictive than pragma @code{Pure},
27729 but still strong enough to prevent ABE problems within a unit.
27730 @end itemize
27731
27732 @geindex pragma Elaborate_Body
27733
27734
27735 @itemize *
27736
27737 @item
27738 @emph{pragma Elaborate_Body}
27739
27740 Pragma @code{Elaborate_Body} requires that the body of a unit is elaborated
27741 immediately after its spec. This restriction guarantees that no client
27742 scenario can invoke a server target before the target body has been
27743 elaborated because the spec and body are effectively "glued" together.
27744
27745 @example
27746 package Server is
27747 pragma Elaborate_Body;
27748
27749 function Func return Integer;
27750 end Server;
27751 @end example
27752
27753 @example
27754 package body Server is
27755 function Func return Integer is
27756 begin
27757 ...
27758 end Func;
27759 end Server;
27760 @end example
27761
27762 @example
27763 with Server;
27764 package Client is
27765 Val : constant Integer := Server.Func;
27766 end Client;
27767 @end example
27768
27769 In the example above, pragma @code{Elaborate_Body} guarantees the following
27770 elaboration order:
27771
27772 @example
27773 spec of Server
27774 body of Server
27775 spec of Client
27776 @end example
27777
27778 because the spec of @code{Server} must be elaborated prior to @code{Client} by
27779 virtue of the @emph{with} clause, and in addition the body of @code{Server} must be
27780 elaborated immediately after the spec of @code{Server}.
27781
27782 Removing pragma @code{Elaborate_Body} could result in the following incorrect
27783 elaboration order:
27784
27785 @example
27786 spec of Server
27787 spec of Client
27788 body of Server
27789 @end example
27790
27791 where @code{Client} invokes @code{Server.Func}, but the body of @code{Server.Func} has
27792 not been elaborated yet.
27793 @end itemize
27794
27795 The pragmas outlined above allow a server unit to guarantee safe elaboration
27796 use by client units. Thus it is a good rule to mark units as @code{Pure} or
27797 @code{Preelaborate}, and if this is not possible, mark them as @code{Elaborate_Body}.
27798
27799 There are however situations where @code{Pure}, @code{Preelaborate}, and
27800 @code{Elaborate_Body} are not applicable. Ada provides another set of pragmas for
27801 use by client units to help ensure the elaboration safety of server units they
27802 depend on.
27803
27804 @geindex pragma Elaborate (Unit)
27805
27806
27807 @itemize *
27808
27809 @item
27810 @emph{pragma Elaborate (Unit)}
27811
27812 Pragma @code{Elaborate} can be placed in the context clauses of a unit, after a
27813 @emph{with} clause. It guarantees that both the spec and body of its argument will
27814 be elaborated prior to the unit with the pragma. Note that other unrelated
27815 units may be elaborated in between the spec and the body.
27816
27817 @example
27818 package Server is
27819 function Func return Integer;
27820 end Server;
27821 @end example
27822
27823 @example
27824 package body Server is
27825 function Func return Integer is
27826 begin
27827 ...
27828 end Func;
27829 end Server;
27830 @end example
27831
27832 @example
27833 with Server;
27834 pragma Elaborate (Server);
27835 package Client is
27836 Val : constant Integer := Server.Func;
27837 end Client;
27838 @end example
27839
27840 In the example above, pragma @code{Elaborate} guarantees the following
27841 elaboration order:
27842
27843 @example
27844 spec of Server
27845 body of Server
27846 spec of Client
27847 @end example
27848
27849 Removing pragma @code{Elaborate} could result in the following incorrect
27850 elaboration order:
27851
27852 @example
27853 spec of Server
27854 spec of Client
27855 body of Server
27856 @end example
27857
27858 where @code{Client} invokes @code{Server.Func}, but the body of @code{Server.Func}
27859 has not been elaborated yet.
27860 @end itemize
27861
27862 @geindex pragma Elaborate_All (Unit)
27863
27864
27865 @itemize *
27866
27867 @item
27868 @emph{pragma Elaborate_All (Unit)}
27869
27870 Pragma @code{Elaborate_All} is placed in the context clauses of a unit, after
27871 a @emph{with} clause. It guarantees that both the spec and body of its argument
27872 will be elaborated prior to the unit with the pragma, as well as all units
27873 @emph{with}ed by the spec and body of the argument, recursively. Note that other
27874 unrelated units may be elaborated in between the spec and the body.
27875
27876 @example
27877 package Math is
27878 function Factorial (Val : Natural) return Natural;
27879 end Math;
27880 @end example
27881
27882 @example
27883 package body Math is
27884 function Factorial (Val : Natural) return Natural is
27885 begin
27886 ...;
27887 end Factorial;
27888 end Math;
27889 @end example
27890
27891 @example
27892 package Computer is
27893 type Operation_Kind is (None, Op_Factorial);
27894
27895 function Compute
27896 (Val : Natural;
27897 Op : Operation_Kind) return Natural;
27898 end Computer;
27899 @end example
27900
27901 @example
27902 with Math;
27903 package body Computer is
27904 function Compute
27905 (Val : Natural;
27906 Op : Operation_Kind) return Natural
27907 is
27908 if Op = Op_Factorial then
27909 return Math.Factorial (Val);
27910 end if;
27911
27912 return 0;
27913 end Compute;
27914 end Computer;
27915 @end example
27916
27917 @example
27918 with Computer;
27919 pragma Elaborate_All (Computer);
27920 package Client is
27921 Val : constant Natural :=
27922 Computer.Compute (123, Computer.Op_Factorial);
27923 end Client;
27924 @end example
27925
27926 In the example above, pragma @code{Elaborate_All} can result in the following
27927 elaboration order:
27928
27929 @example
27930 spec of Math
27931 body of Math
27932 spec of Computer
27933 body of Computer
27934 spec of Client
27935 @end example
27936
27937 Note that there are several allowable suborders for the specs and bodies of
27938 @code{Math} and @code{Computer}, but the point is that these specs and bodies will
27939 be elaborated prior to @code{Client}.
27940
27941 Removing pragma @code{Elaborate_All} could result in the following incorrect
27942 elaboration order:
27943
27944 @example
27945 spec of Math
27946 spec of Computer
27947 body of Computer
27948 spec of Client
27949 body of Math
27950 @end example
27951
27952 where @code{Client} invokes @code{Computer.Compute}, which in turn invokes
27953 @code{Math.Factorial}, but the body of @code{Math.Factorial} has not been
27954 elaborated yet.
27955 @end itemize
27956
27957 All pragmas shown above can be summarized by the following rule:
27958
27959 @emph{If a client unit elaborates a server target directly or indirectly, then if
27960 the server unit requires a body and does not have pragma Pure, Preelaborate,
27961 or Elaborate_Body, then the client unit should have pragma Elaborate or
27962 Elaborate_All for the server unit.}
27963
27964 If the rule outlined above is not followed, then a program may fall in one of
27965 the following states:
27966
27967
27968 @itemize *
27969
27970 @item
27971 @emph{No elaboration order exists}
27972
27973 In this case a compiler must diagnose the situation, and refuse to build an
27974 executable program.
27975
27976 @item
27977 @emph{One or more incorrect elaboration orders exist}
27978
27979 In this case a compiler can build an executable program, but
27980 @code{Program_Error} will be raised when the program is run.
27981
27982 @item
27983 @emph{Several elaboration orders exist, some correct, some incorrect}
27984
27985 In this case the programmer has not controlled the elaboration order. As a
27986 result, a compiler may or may not pick one of the correct orders, and the
27987 program may or may not raise @code{Program_Error} when it is run. This is the
27988 worst possible state because the program may fail on another compiler, or
27989 even another version of the same compiler.
27990
27991 @item
27992 @emph{One or more correct orders exist}
27993
27994 In this case a compiler can build an executable program, and the program is
27995 run successfully. This state may be guaranteed by following the outlined
27996 rules, or may be the result of good program architecture.
27997 @end itemize
27998
27999 Note that one additional advantage of using @code{Elaborate} and @code{Elaborate_All}
28000 is that the program continues to stay in the last state (one or more correct
28001 orders exist) even if maintenance changes the bodies of targets.
28002
28003 @node Controlling the Elaboration Order in GNAT,Mixing Elaboration Models,Controlling the Elaboration Order in Ada,Elaboration Order Handling in GNAT
28004 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id6}@anchor{235}@anchor{gnat_ugn/elaboration_order_handling_in_gnat controlling-the-elaboration-order-in-gnat}@anchor{236}
28005 @section Controlling the Elaboration Order in GNAT
28006
28007
28008 In addition to Ada semantics and rules synthesized from them, GNAT offers
28009 three elaboration models to aid the programmer with specifying the correct
28010 elaboration order and to diagnose elaboration problems.
28011
28012 @geindex Dynamic elaboration model
28013
28014
28015 @itemize *
28016
28017 @item
28018 @emph{Dynamic elaboration model}
28019
28020 This is the most permissive of the three elaboration models and emulates the
28021 behavior specified by the Ada Reference Manual. When the dynamic model is in
28022 effect, GNAT makes the following assumptions:
28023
28024
28025 @itemize -
28026
28027 @item
28028 All code within all units in a partition is considered to be elaboration
28029 code.
28030
28031 @item
28032 Some of the invocations in elaboration code may not take place at run time
28033 due to conditional execution.
28034 @end itemize
28035
28036 GNAT performs extensive diagnostics on a unit-by-unit basis for all scenarios
28037 that invoke internal targets. In addition, GNAT generates run-time checks for
28038 all external targets and for all scenarios that may exhibit ABE problems.
28039
28040 The elaboration order is obtained by honoring all @emph{with} clauses, purity and
28041 preelaborability of units, and elaboration-control pragmas. The dynamic model
28042 attempts to take all invocations in elaboration code into account. If an
28043 invocation leads to a circularity, GNAT ignores the invocation based on the
28044 assumptions stated above. An order obtained using the dynamic model may fail
28045 an ABE check at run time when GNAT ignored an invocation.
28046
28047 The dynamic model is enabled with compiler switch @code{-gnatE}.
28048 @end itemize
28049
28050 @geindex Static elaboration model
28051
28052
28053 @itemize *
28054
28055 @item
28056 @emph{Static elaboration model}
28057
28058 This is the middle ground of the three models. When the static model is in
28059 effect, GNAT makes the following assumptions:
28060
28061
28062 @itemize -
28063
28064 @item
28065 Only code at the library level and in package body statements within all
28066 units in a partition is considered to be elaboration code.
28067
28068 @item
28069 All invocations in elaboration will take place at run time, regardless of
28070 conditional execution.
28071 @end itemize
28072
28073 GNAT performs extensive diagnostics on a unit-by-unit basis for all scenarios
28074 that invoke internal targets. In addition, GNAT generates run-time checks for
28075 all external targets and for all scenarios that may exhibit ABE problems.
28076
28077 The elaboration order is obtained by honoring all @emph{with} clauses, purity and
28078 preelaborability of units, presence of elaboration-control pragmas, and all
28079 invocations in elaboration code. An order obtained using the static model is
28080 guaranteed to be ABE problem-free, excluding dispatching calls and
28081 access-to-subprogram types.
28082
28083 The static model is the default model in GNAT.
28084 @end itemize
28085
28086 @geindex SPARK elaboration model
28087
28088
28089 @itemize *
28090
28091 @item
28092 @emph{SPARK elaboration model}
28093
28094 This is the most conservative of the three models and enforces the SPARK
28095 rules of elaboration as defined in the SPARK Reference Manual, section 7.7.
28096 The SPARK model is in effect only when a scenario and a target reside in a
28097 region subject to @code{SPARK_Mode On}, otherwise the dynamic or static model
28098 is in effect.
28099
28100 The SPARK model is enabled with compiler switch @code{-gnatd.v}.
28101 @end itemize
28102
28103 @geindex Legacy elaboration models
28104
28105
28106 @itemize *
28107
28108 @item
28109 @emph{Legacy elaboration models}
28110
28111 In addition to the three elaboration models outlined above, GNAT provides the
28112 following legacy models:
28113
28114
28115 @itemize -
28116
28117 @item
28118 @cite{Legacy elaboration-checking model} available in pre-18.x versions of GNAT.
28119 This model is enabled with compiler switch @code{-gnatH}.
28120
28121 @item
28122 @cite{Legacy elaboration-order model} available in pre-20.x versions of GNAT.
28123 This model is enabled with binder switch @code{-H}.
28124 @end itemize
28125 @end itemize
28126
28127 @geindex Relaxed elaboration mode
28128
28129 The dynamic, legacy, and static models can be relaxed using compiler switch
28130 @code{-gnatJ}, making them more permissive. Note that in this mode, GNAT
28131 may not diagnose certain elaboration issues or install run-time checks.
28132
28133 @node Mixing Elaboration Models,ABE Diagnostics,Controlling the Elaboration Order in GNAT,Elaboration Order Handling in GNAT
28134 @anchor{gnat_ugn/elaboration_order_handling_in_gnat mixing-elaboration-models}@anchor{237}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id7}@anchor{238}
28135 @section Mixing Elaboration Models
28136
28137
28138 It is possible to mix units compiled with a different elaboration model,
28139 however the following rules must be observed:
28140
28141
28142 @itemize *
28143
28144 @item
28145 A client unit compiled with the dynamic model can only @emph{with} a server unit
28146 that meets at least one of the following criteria:
28147
28148
28149 @itemize -
28150
28151 @item
28152 The server unit is compiled with the dynamic model.
28153
28154 @item
28155 The server unit is a GNAT implementation unit from the @code{Ada}, @code{GNAT},
28156 @code{Interfaces}, or @code{System} hierarchies.
28157
28158 @item
28159 The server unit has pragma @code{Pure} or @code{Preelaborate}.
28160
28161 @item
28162 The client unit has an explicit @code{Elaborate_All} pragma for the server
28163 unit.
28164 @end itemize
28165 @end itemize
28166
28167 These rules ensure that elaboration checks are not omitted. If the rules are
28168 violated, the binder emits a warning:
28169
28170 @quotation
28171
28172 @example
28173 warning: "x.ads" has dynamic elaboration checks and with's
28174 warning: "y.ads" which has static elaboration checks
28175 @end example
28176 @end quotation
28177
28178 The warnings can be suppressed by binder switch @code{-ws}.
28179
28180 @node ABE Diagnostics,SPARK Diagnostics,Mixing Elaboration Models,Elaboration Order Handling in GNAT
28181 @anchor{gnat_ugn/elaboration_order_handling_in_gnat abe-diagnostics}@anchor{239}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id8}@anchor{23a}
28182 @section ABE Diagnostics
28183
28184
28185 GNAT performs extensive diagnostics on a unit-by-unit basis for all scenarios
28186 that invoke internal targets, regardless of whether the dynamic, SPARK, or
28187 static model is in effect.
28188
28189 Note that GNAT emits warnings rather than hard errors whenever it encounters an
28190 elaboration problem. This is because the elaboration model in effect may be too
28191 conservative, or a particular scenario may not be invoked due conditional
28192 execution. The warnings can be suppressed selectively with @code{pragma Warnings
28193 (Off)} or globally with compiler switch @code{-gnatwL}.
28194
28195 A @emph{guaranteed ABE} arises when the body of a target is not elaborated early
28196 enough, and causes @emph{all} scenarios that directly invoke the target to fail.
28197
28198 @quotation
28199
28200 @example
28201 package body Guaranteed_ABE is
28202 function ABE return Integer;
28203
28204 Val : constant Integer := ABE;
28205
28206 function ABE return Integer is
28207 begin
28208 ...
28209 end ABE;
28210 end Guaranteed_ABE;
28211 @end example
28212 @end quotation
28213
28214 In the example above, the elaboration of @code{Guaranteed_ABE}'s body elaborates
28215 the declaration of @code{Val}. This invokes function @code{ABE}, however the body of
28216 @code{ABE} has not been elaborated yet. GNAT emits the following diagnostic:
28217
28218 @quotation
28219
28220 @example
28221 4. Val : constant Integer := ABE;
28222 |
28223 >>> warning: cannot call "ABE" before body seen
28224 >>> warning: Program_Error will be raised at run time
28225 @end example
28226 @end quotation
28227
28228 A @emph{conditional ABE} arises when the body of a target is not elaborated early
28229 enough, and causes @emph{some} scenarios that directly invoke the target to fail.
28230
28231 @quotation
28232
28233 @example
28234 1. package body Conditional_ABE is
28235 2. procedure Force_Body is null;
28236 3.
28237 4. generic
28238 5. with function Func return Integer;
28239 6. package Gen is
28240 7. Val : constant Integer := Func;
28241 8. end Gen;
28242 9.
28243 10. function ABE return Integer;
28244 11.
28245 12. function Cause_ABE return Boolean is
28246 13. package Inst is new Gen (ABE);
28247 14. begin
28248 15. ...
28249 16. end Cause_ABE;
28250 17.
28251 18. Val : constant Boolean := Cause_ABE;
28252 19.
28253 20. function ABE return Integer is
28254 21. begin
28255 22. ...
28256 23. end ABE;
28257 24.
28258 25. Safe : constant Boolean := Cause_ABE;
28259 26. end Conditional_ABE;
28260 @end example
28261 @end quotation
28262
28263 In the example above, the elaboration of package body @code{Conditional_ABE}
28264 elaborates the declaration of @code{Val}. This invokes function @code{Cause_ABE},
28265 which instantiates generic unit @code{Gen} as @code{Inst}. The elaboration of
28266 @code{Inst} invokes function @code{ABE}, however the body of @code{ABE} has not been
28267 elaborated yet. GNAT emits the following diagnostic:
28268
28269 @quotation
28270
28271 @example
28272 13. package Inst is new Gen (ABE);
28273 |
28274 >>> warning: in instantiation at line 7
28275 >>> warning: cannot call "ABE" before body seen
28276 >>> warning: Program_Error may be raised at run time
28277 >>> warning: body of unit "Conditional_ABE" elaborated
28278 >>> warning: function "Cause_ABE" called at line 18
28279 >>> warning: function "ABE" called at line 7, instance at line 13
28280 @end example
28281 @end quotation
28282
28283 Note that the same ABE problem does not occur with the elaboration of
28284 declaration @code{Safe} because the body of function @code{ABE} has already been
28285 elaborated at that point.
28286
28287 @node SPARK Diagnostics,Elaboration Circularities,ABE Diagnostics,Elaboration Order Handling in GNAT
28288 @anchor{gnat_ugn/elaboration_order_handling_in_gnat spark-diagnostics}@anchor{23b}@anchor{gnat_ugn/elaboration_order_handling_in_gnat id9}@anchor{23c}
28289 @section SPARK Diagnostics
28290
28291
28292 GNAT enforces the SPARK rules of elaboration as defined in the SPARK Reference
28293 Manual section 7.7 when compiler switch @code{-gnatd.v} is in effect. Note
28294 that GNAT emits hard errors whenever it encounters a violation of the SPARK
28295 rules.
28296
28297 @quotation
28298
28299 @example
28300 1. with Server;
28301 2. package body SPARK_Diagnostics with SPARK_Mode is
28302 3. Val : constant Integer := Server.Func;
28303 |
28304 >>> call to "Func" during elaboration in SPARK
28305 >>> unit "SPARK_Diagnostics" requires pragma "Elaborate_All" for "Server"
28306 >>> body of unit "SPARK_Model" elaborated
28307 >>> function "Func" called at line 3
28308
28309 4. end SPARK_Diagnostics;
28310 @end example
28311 @end quotation
28312
28313 @node Elaboration Circularities,Resolving Elaboration Circularities,SPARK Diagnostics,Elaboration Order Handling in GNAT
28314 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id10}@anchor{23d}@anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-circularities}@anchor{23e}
28315 @section Elaboration Circularities
28316
28317
28318 An @strong{elaboration circularity} occurs whenever the elaboration of a set of
28319 units enters a deadlocked state, where each unit is waiting for another unit
28320 to be elaborated. This situation may be the result of improper use of @emph{with}
28321 clauses, elaboration-control pragmas, or invocations in elaboration code.
28322
28323 The following example exhibits an elaboration circularity.
28324
28325 @quotation
28326
28327 @example
28328 with B; pragma Elaborate (B);
28329 package A is
28330 end A;
28331 @end example
28332
28333 @example
28334 package B is
28335 procedure Force_Body;
28336 end B;
28337 @end example
28338
28339 @example
28340 with C;
28341 package body B is
28342 procedure Force_Body is null;
28343
28344 Elab : constant Integer := C.Func;
28345 end B;
28346 @end example
28347
28348 @example
28349 package C is
28350 function Func return Integer;
28351 end C;
28352 @end example
28353
28354 @example
28355 with A;
28356 package body C is
28357 function Func return Integer is
28358 begin
28359 ...
28360 end Func;
28361 end C;
28362 @end example
28363 @end quotation
28364
28365 The binder emits the following diagnostic:
28366
28367 @quotation
28368
28369 @example
28370 error: Elaboration circularity detected
28371 info:
28372 info: Reason:
28373 info:
28374 info: unit "a (spec)" depends on its own elaboration
28375 info:
28376 info: Circularity:
28377 info:
28378 info: unit "a (spec)" has with clause and pragma Elaborate for unit "b (spec)"
28379 info: unit "b (body)" is in the closure of pragma Elaborate
28380 info: unit "b (body)" invokes a construct of unit "c (body)" at elaboration time
28381 info: unit "c (body)" has with clause for unit "a (spec)"
28382 info:
28383 info: Suggestions:
28384 info:
28385 info: remove pragma Elaborate for unit "b (body)" in unit "a (spec)"
28386 info: use the dynamic elaboration model (compiler switch -gnatE)
28387 @end example
28388 @end quotation
28389
28390 The diagnostic consist of the following sections:
28391
28392
28393 @itemize *
28394
28395 @item
28396 Reason
28397
28398 This section provides a short explanation describing why the set of units
28399 could not be ordered.
28400
28401 @item
28402 Circularity
28403
28404 This section enumerates the units comprising the deadlocked set, along with
28405 their interdependencies.
28406
28407 @item
28408 Suggestions
28409
28410 This section enumerates various tactics for eliminating the circularity.
28411 @end itemize
28412
28413 @node Resolving Elaboration Circularities,Elaboration-related Compiler Switches,Elaboration Circularities,Elaboration Order Handling in GNAT
28414 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id11}@anchor{23f}@anchor{gnat_ugn/elaboration_order_handling_in_gnat resolving-elaboration-circularities}@anchor{240}
28415 @section Resolving Elaboration Circularities
28416
28417
28418 The most desirable option from the point of view of long-term maintenance is to
28419 rearrange the program so that the elaboration problems are avoided. One useful
28420 technique is to place the elaboration code into separate child packages.
28421 Another is to move some of the initialization code to explicitly invoked
28422 subprograms, where the program controls the order of initialization explicitly.
28423 Although this is the most desirable option, it may be impractical and involve
28424 too much modification, especially in the case of complex legacy code.
28425
28426 When faced with an elaboration circularity, the programmer should also consider
28427 the tactics given in the suggestions section of the circularity diagnostic.
28428 Depending on the units involved in the circularity, their @emph{with} clauses,
28429 purity, preelaborability, presence of elaboration-control pragmas and
28430 invocations at elaboration time, the binder may suggest one or more of the
28431 following tactics to eliminate the circularity:
28432
28433
28434 @itemize *
28435
28436 @item
28437 Pragma Elaborate elimination
28438
28439 @example
28440 remove pragma Elaborate for unit "..." in unit "..."
28441 @end example
28442
28443 This tactic is suggested when the binder has determined that pragma
28444 @code{Elaborate}:
28445
28446
28447 @itemize -
28448
28449 @item
28450 Prevents a set of units from being elaborated.
28451
28452 @item
28453 The removal of the pragma will not eliminate the semantic effects of the
28454 pragma. In other words, the argument of the pragma will still be elaborated
28455 prior to the unit containing the pragma.
28456
28457 @item
28458 The removal of the pragma will enable the successful ordering of the units.
28459 @end itemize
28460
28461 The programmer should remove the pragma as advised, and rebuild the program.
28462
28463 @item
28464 Pragma Elaborate_All elimination
28465
28466 @example
28467 remove pragma Elaborate_All for unit "..." in unit "..."
28468 @end example
28469
28470 This tactic is suggested when the binder has determined that pragma
28471 @code{Elaborate_All}:
28472
28473
28474 @itemize -
28475
28476 @item
28477 Prevents a set of units from being elaborated.
28478
28479 @item
28480 The removal of the pragma will not eliminate the semantic effects of the
28481 pragma. In other words, the argument of the pragma along with its @emph{with}
28482 closure will still be elaborated prior to the unit containing the pragma.
28483
28484 @item
28485 The removal of the pragma will enable the successful ordering of the units.
28486 @end itemize
28487
28488 The programmer should remove the pragma as advised, and rebuild the program.
28489
28490 @item
28491 Pragma Elaborate_All downgrade
28492
28493 @example
28494 change pragma Elaborate_All for unit "..." to Elaborate in unit "..."
28495 @end example
28496
28497 This tactic is always suggested with the pragma @code{Elaborate_All} elimination
28498 tactic. It offers a different alernative of guaranteeing that the argument of
28499 the pragma will still be elaborated prior to the unit containing the pragma.
28500
28501 The programmer should update the pragma as advised, and rebuild the program.
28502
28503 @item
28504 Pragma Elaborate_Body elimination
28505
28506 @example
28507 remove pragma Elaborate_Body in unit "..."
28508 @end example
28509
28510 This tactic is suggested when the binder has determined that pragma
28511 @code{Elaborate_Body}:
28512
28513
28514 @itemize -
28515
28516 @item
28517 Prevents a set of units from being elaborated.
28518
28519 @item
28520 The removal of the pragma will enable the successful ordering of the units.
28521 @end itemize
28522
28523 Note that the binder cannot determine whether the pragma is required for
28524 other purposes, such as guaranteeing the initialization of a variable
28525 declared in the spec by elaboration code in the body.
28526
28527 The programmer should remove the pragma as advised, and rebuild the program.
28528
28529 @item
28530 Use of pragma Restrictions
28531
28532 @example
28533 use pragma Restrictions (No_Entry_Calls_In_Elaboration_Code)
28534 @end example
28535
28536 This tactic is suggested when the binder has determined that a task
28537 activation at elaboration time:
28538
28539
28540 @itemize -
28541
28542 @item
28543 Prevents a set of units from being elaborated.
28544 @end itemize
28545
28546 Note that the binder cannot determine with certainty whether the task will
28547 block at elaboration time.
28548
28549 The programmer should create a configuration file, place the pragma within,
28550 update the general compilation arguments, and rebuild the program.
28551
28552 @item
28553 Use of dynamic elaboration model
28554
28555 @example
28556 use the dynamic elaboration model (compiler switch -gnatE)
28557 @end example
28558
28559 This tactic is suggested when the binder has determined that an invocation at
28560 elaboration time:
28561
28562
28563 @itemize -
28564
28565 @item
28566 Prevents a set of units from being elaborated.
28567
28568 @item
28569 The use of the dynamic model will enable the successful ordering of the
28570 units.
28571 @end itemize
28572
28573 The programmer has two options:
28574
28575
28576 @itemize -
28577
28578 @item
28579 Determine the units involved in the invocation using the detailed
28580 invocation information, and add compiler switch @code{-gnatE} to the
28581 compilation arguments of selected files only. This approach will yield
28582 safer elaboration orders compared to the other option because it will
28583 minimize the opportunities presented to the dynamic model for ignoring
28584 invocations.
28585
28586 @item
28587 Add compiler switch @code{-gnatE} to the general compilation arguments.
28588 @end itemize
28589
28590 @item
28591 Use of detailed invocation information
28592
28593 @example
28594 use detailed invocation information (compiler switch -gnatd_F)
28595 @end example
28596
28597 This tactic is always suggested with the use of the dynamic model tactic. It
28598 causes the circularity section of the circularity diagnostic to describe the
28599 flow of elaboration code from a unit to a unit, enumerating all such paths in
28600 the process.
28601
28602 The programmer should analyze this information to determine which units
28603 should be compiled with the dynamic model.
28604
28605 @item
28606 Forced-dependency elimination
28607
28608 @example
28609 remove the dependency of unit "..." on unit "..." from the argument of switch -f
28610 @end example
28611
28612 This tactic is suggested when the binder has determined that a dependency
28613 present in the forced-elaboration-order file indicated by binder switch
28614 @code{-f}:
28615
28616
28617 @itemize -
28618
28619 @item
28620 Prevents a set of units from being elaborated.
28621
28622 @item
28623 The removal of the dependency will enable the successful ordering of the
28624 units.
28625 @end itemize
28626
28627 The programmer should edit the forced-elaboration-order file, remove the
28628 dependency, and rebind the program.
28629
28630 @item
28631 All forced-dependency elimination
28632
28633 @example
28634 remove switch -f
28635 @end example
28636
28637 This tactic is suggested in case editing the forced-elaboration-order file is
28638 not an option.
28639
28640 The programmer should remove binder switch @code{-f} from the binder
28641 arguments, and rebind.
28642
28643 @item
28644 Multiple-circularities diagnostic
28645
28646 @example
28647 diagnose all circularities (binder switch -d_C)
28648 @end example
28649
28650 By default, the binder will diagnose only the highest-precedence circularity.
28651 If the program contains multiple circularities, the binder will suggest the
28652 use of binder switch @code{-d_C} in order to obtain the diagnostics of all
28653 circularities.
28654
28655 The programmer should add binder switch @code{-d_C} to the binder
28656 arguments, and rebind.
28657 @end itemize
28658
28659 If none of the tactics suggested by the binder eliminate the elaboration
28660 circularity, the programmer should consider using one of the legacy elaboration
28661 models, in the following order:
28662
28663
28664 @itemize *
28665
28666 @item
28667 Use the pre-20.x legacy elaboration-order model, with binder switch
28668 @code{-H}.
28669
28670 @item
28671 Use both pre-18.x and pre-20.x legacy elaboration models, with compiler
28672 switch @code{-gnatH} and binder switch @code{-H}.
28673
28674 @item
28675 Use the relaxed static-elaboration model, with compiler switches
28676 @code{-gnatH} @code{-gnatJ} and binder switch @code{-H}.
28677
28678 @item
28679 Use the relaxed dynamic-elaboration model, with compiler switches
28680 @code{-gnatH} @code{-gnatJ} @code{-gnatE} and binder switch
28681 @code{-H}.
28682 @end itemize
28683
28684 @node Elaboration-related Compiler Switches,Summary of Procedures for Elaboration Control,Resolving Elaboration Circularities,Elaboration Order Handling in GNAT
28685 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id12}@anchor{241}@anchor{gnat_ugn/elaboration_order_handling_in_gnat elaboration-related-compiler-switches}@anchor{242}
28686 @section Elaboration-related Compiler Switches
28687
28688
28689 GNAT has several switches that affect the elaboration model and consequently
28690 the elaboration order chosen by the binder.
28691
28692 @geindex -gnatE (gnat)
28693
28694
28695 @table @asis
28696
28697 @item @code{-gnatE}
28698
28699 Dynamic elaboration checking mode enabled
28700
28701 When this switch is in effect, GNAT activates the dynamic model.
28702 @end table
28703
28704 @geindex -gnatel (gnat)
28705
28706
28707 @table @asis
28708
28709 @item @code{-gnatel}
28710
28711 Turn on info messages on generated Elaborate[_All] pragmas
28712
28713 This switch is only applicable to the pre-20.x legacy elaboration models.
28714 The post-20.x elaboration model no longer relies on implicitly generated
28715 @code{Elaborate} and @code{Elaborate_All} pragmas to order units.
28716
28717 When this switch is in effect, GNAT will emit the following supplementary
28718 information depending on the elaboration model in effect.
28719
28720
28721 @itemize -
28722
28723 @item
28724 @emph{Dynamic model}
28725
28726 GNAT will indicate missing @code{Elaborate} and @code{Elaborate_All} pragmas for
28727 all library-level scenarios within the partition.
28728
28729 @item
28730 @emph{Static model}
28731
28732 GNAT will indicate all scenarios invoked during elaboration. In addition,
28733 it will provide detailed traceback when an implicit @code{Elaborate} or
28734 @code{Elaborate_All} pragma is generated.
28735
28736 @item
28737 @emph{SPARK model}
28738
28739 GNAT will indicate how an elaboration requirement is met by the context of
28740 a unit. This diagnostic requires compiler switch @code{-gnatd.v}.
28741
28742 @example
28743 1. with Server; pragma Elaborate_All (Server);
28744 2. package Client with SPARK_Mode is
28745 3. Val : constant Integer := Server.Func;
28746 |
28747 >>> info: call to "Func" during elaboration in SPARK
28748 >>> info: "Elaborate_All" requirement for unit "Server" met by pragma at line 1
28749
28750 4. end Client;
28751 @end example
28752 @end itemize
28753 @end table
28754
28755 @geindex -gnatH (gnat)
28756
28757
28758 @table @asis
28759
28760 @item @code{-gnatH}
28761
28762 Legacy elaboration checking mode enabled
28763
28764 When this switch is in effect, GNAT will utilize the pre-18.x elaboration
28765 model.
28766 @end table
28767
28768 @geindex -gnatJ (gnat)
28769
28770
28771 @table @asis
28772
28773 @item @code{-gnatJ}
28774
28775 Relaxed elaboration checking mode enabled
28776
28777 When this switch is in effect, GNAT will not process certain scenarios,
28778 resulting in a more permissive elaboration model. Note that this may
28779 eliminate some diagnostics and run-time checks.
28780 @end table
28781
28782 @geindex -gnatw.f (gnat)
28783
28784
28785 @table @asis
28786
28787 @item @code{-gnatw.f}
28788
28789 Turn on warnings for suspicious Subp'Access
28790
28791 When this switch is in effect, GNAT will treat @code{'Access} of an entry,
28792 operator, or subprogram as a potential call to the target and issue warnings:
28793
28794 @example
28795 1. package body Attribute_Call is
28796 2. function Func return Integer;
28797 3. type Func_Ptr is access function return Integer;
28798 4.
28799 5. Ptr : constant Func_Ptr := Func'Access;
28800 |
28801 >>> warning: "Access" attribute of "Func" before body seen
28802 >>> warning: possible Program_Error on later references
28803 >>> warning: body of unit "Attribute_Call" elaborated
28804 >>> warning: "Access" of "Func" taken at line 5
28805
28806 6.
28807 7. function Func return Integer is
28808 8. begin
28809 9. ...
28810 10. end Func;
28811 11. end Attribute_Call;
28812 @end example
28813
28814 In the example above, the elaboration of declaration @code{Ptr} is assigned
28815 @code{Func'Access} before the body of @code{Func} has been elaborated.
28816 @end table
28817
28818 @geindex -gnatwl (gnat)
28819
28820
28821 @table @asis
28822
28823 @item @code{-gnatwl}
28824
28825 Turn on warnings for elaboration problems
28826
28827 When this switch is in effect, GNAT emits diagnostics in the form of warnings
28828 concerning various elaboration problems. The warnings are enabled by default.
28829 The switch is provided in case all warnings are suppressed, but elaboration
28830 warnings are still desired.
28831
28832 @item @code{-gnatwL}
28833
28834 Turn off warnings for elaboration problems
28835
28836 When this switch is in effect, GNAT no longer emits any diagnostics in the
28837 form of warnings. Selective suppression of elaboration problems is possible
28838 using @code{pragma Warnings (Off)}.
28839
28840 @example
28841 1. package body Selective_Suppression is
28842 2. function ABE return Integer;
28843 3.
28844 4. Val_1 : constant Integer := ABE;
28845 |
28846 >>> warning: cannot call "ABE" before body seen
28847 >>> warning: Program_Error will be raised at run time
28848
28849 5.
28850 6. pragma Warnings (Off);
28851 7. Val_2 : constant Integer := ABE;
28852 8. pragma Warnings (On);
28853 9.
28854 10. function ABE return Integer is
28855 11. begin
28856 12. ...
28857 13. end ABE;
28858 14. end Selective_Suppression;
28859 @end example
28860
28861 Note that suppressing elaboration warnings does not eliminate run-time
28862 checks. The example above will still fail at run time with an ABE.
28863 @end table
28864
28865 @node Summary of Procedures for Elaboration Control,Inspecting the Chosen Elaboration Order,Elaboration-related Compiler Switches,Elaboration Order Handling in GNAT
28866 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id13}@anchor{243}@anchor{gnat_ugn/elaboration_order_handling_in_gnat summary-of-procedures-for-elaboration-control}@anchor{244}
28867 @section Summary of Procedures for Elaboration Control
28868
28869
28870 A programmer should first compile the program with the default options, using
28871 none of the binder or compiler switches. If the binder succeeds in finding an
28872 elaboration order, then apart from possible cases involing dispatching calls
28873 and access-to-subprogram types, the program is free of elaboration errors.
28874
28875 If it is important for the program to be portable to compilers other than GNAT,
28876 then the programmer should use compiler switch @code{-gnatel} and consider
28877 the messages about missing or implicitly created @code{Elaborate} and
28878 @code{Elaborate_All} pragmas.
28879
28880 If the binder reports an elaboration circularity, the programmer has several
28881 options:
28882
28883
28884 @itemize *
28885
28886 @item
28887 Ensure that elaboration warnings are enabled. This will allow the static
28888 model to output trace information of elaboration issues. The trace
28889 information could shed light on previously unforeseen dependencies, as well
28890 as their origins. Elaboration warnings are enabled with compiler switch
28891 @code{-gnatwl}.
28892
28893 @item
28894 Cosider the tactics given in the suggestions section of the circularity
28895 diagnostic.
28896
28897 @item
28898 If none of the steps outlined above resolve the circularity, use a more
28899 permissive elaboration model, in the following order:
28900
28901
28902 @itemize -
28903
28904 @item
28905 Use the pre-20.x legacy elaboration-order model, with binder switch
28906 @code{-H}.
28907
28908 @item
28909 Use both pre-18.x and pre-20.x legacy elaboration models, with compiler
28910 switch @code{-gnatH} and binder switch @code{-H}.
28911
28912 @item
28913 Use the relaxed static elaboration model, with compiler switches
28914 @code{-gnatH} @code{-gnatJ} and binder switch @code{-H}.
28915
28916 @item
28917 Use the relaxed dynamic elaboration model, with compiler switches
28918 @code{-gnatH} @code{-gnatJ} @code{-gnatE} and binder switch
28919 @code{-H}.
28920 @end itemize
28921 @end itemize
28922
28923 @node Inspecting the Chosen Elaboration Order,,Summary of Procedures for Elaboration Control,Elaboration Order Handling in GNAT
28924 @anchor{gnat_ugn/elaboration_order_handling_in_gnat id14}@anchor{245}@anchor{gnat_ugn/elaboration_order_handling_in_gnat inspecting-the-chosen-elaboration-order}@anchor{246}
28925 @section Inspecting the Chosen Elaboration Order
28926
28927
28928 To see the elaboration order chosen by the binder, inspect the contents of file
28929 @cite{b~xxx.adb}. On certain targets, this file appears as @cite{b_xxx.adb}. The
28930 elaboration order appears as a sequence of calls to @code{Elab_Body} and
28931 @code{Elab_Spec}, interspersed with assignments to @cite{Exxx} which indicates that a
28932 particular unit is elaborated. For example:
28933
28934 @quotation
28935
28936 @example
28937 System.Soft_Links'Elab_Body;
28938 E14 := True;
28939 System.Secondary_Stack'Elab_Body;
28940 E18 := True;
28941 System.Exception_Table'Elab_Body;
28942 E24 := True;
28943 Ada.Io_Exceptions'Elab_Spec;
28944 E67 := True;
28945 Ada.Tags'Elab_Spec;
28946 Ada.Streams'Elab_Spec;
28947 E43 := True;
28948 Interfaces.C'Elab_Spec;
28949 E69 := True;
28950 System.Finalization_Root'Elab_Spec;
28951 E60 := True;
28952 System.Os_Lib'Elab_Body;
28953 E71 := True;
28954 System.Finalization_Implementation'Elab_Spec;
28955 System.Finalization_Implementation'Elab_Body;
28956 E62 := True;
28957 Ada.Finalization'Elab_Spec;
28958 E58 := True;
28959 Ada.Finalization.List_Controller'Elab_Spec;
28960 E76 := True;
28961 System.File_Control_Block'Elab_Spec;
28962 E74 := True;
28963 System.File_Io'Elab_Body;
28964 E56 := True;
28965 Ada.Tags'Elab_Body;
28966 E45 := True;
28967 Ada.Text_Io'Elab_Spec;
28968 Ada.Text_Io'Elab_Body;
28969 E07 := True;
28970 @end example
28971 @end quotation
28972
28973 Note also binder switch @code{-l}, which outputs the chosen elaboration
28974 order and provides a more readable form of the above:
28975
28976 @quotation
28977
28978 @example
28979 ada (spec)
28980 interfaces (spec)
28981 system (spec)
28982 system.case_util (spec)
28983 system.case_util (body)
28984 system.concat_2 (spec)
28985 system.concat_2 (body)
28986 system.concat_3 (spec)
28987 system.concat_3 (body)
28988 system.htable (spec)
28989 system.parameters (spec)
28990 system.parameters (body)
28991 system.crtl (spec)
28992 interfaces.c_streams (spec)
28993 interfaces.c_streams (body)
28994 system.restrictions (spec)
28995 system.restrictions (body)
28996 system.standard_library (spec)
28997 system.exceptions (spec)
28998 system.exceptions (body)
28999 system.storage_elements (spec)
29000 system.storage_elements (body)
29001 system.secondary_stack (spec)
29002 system.stack_checking (spec)
29003 system.stack_checking (body)
29004 system.string_hash (spec)
29005 system.string_hash (body)
29006 system.htable (body)
29007 system.strings (spec)
29008 system.strings (body)
29009 system.traceback (spec)
29010 system.traceback (body)
29011 system.traceback_entries (spec)
29012 system.traceback_entries (body)
29013 ada.exceptions (spec)
29014 ada.exceptions.last_chance_handler (spec)
29015 system.soft_links (spec)
29016 system.soft_links (body)
29017 ada.exceptions.last_chance_handler (body)
29018 system.secondary_stack (body)
29019 system.exception_table (spec)
29020 system.exception_table (body)
29021 ada.io_exceptions (spec)
29022 ada.tags (spec)
29023 ada.streams (spec)
29024 interfaces.c (spec)
29025 interfaces.c (body)
29026 system.finalization_root (spec)
29027 system.finalization_root (body)
29028 system.memory (spec)
29029 system.memory (body)
29030 system.standard_library (body)
29031 system.os_lib (spec)
29032 system.os_lib (body)
29033 system.unsigned_types (spec)
29034 system.stream_attributes (spec)
29035 system.stream_attributes (body)
29036 system.finalization_implementation (spec)
29037 system.finalization_implementation (body)
29038 ada.finalization (spec)
29039 ada.finalization (body)
29040 ada.finalization.list_controller (spec)
29041 ada.finalization.list_controller (body)
29042 system.file_control_block (spec)
29043 system.file_io (spec)
29044 system.file_io (body)
29045 system.val_uns (spec)
29046 system.val_util (spec)
29047 system.val_util (body)
29048 system.val_uns (body)
29049 system.wch_con (spec)
29050 system.wch_con (body)
29051 system.wch_cnv (spec)
29052 system.wch_jis (spec)
29053 system.wch_jis (body)
29054 system.wch_cnv (body)
29055 system.wch_stw (spec)
29056 system.wch_stw (body)
29057 ada.tags (body)
29058 ada.exceptions (body)
29059 ada.text_io (spec)
29060 ada.text_io (body)
29061 text_io (spec)
29062 gdbstr (body)
29063 @end example
29064 @end quotation
29065
29066 @node Inline Assembler,GNU Free Documentation License,Elaboration Order Handling in GNAT,Top
29067 @anchor{gnat_ugn/inline_assembler inline-assembler}@anchor{10}@anchor{gnat_ugn/inline_assembler doc}@anchor{247}@anchor{gnat_ugn/inline_assembler id1}@anchor{248}
29068 @chapter Inline Assembler
29069
29070
29071 @geindex Inline Assembler
29072
29073 If you need to write low-level software that interacts directly
29074 with the hardware, Ada provides two ways to incorporate assembly
29075 language code into your program. First, you can import and invoke
29076 external routines written in assembly language, an Ada feature fully
29077 supported by GNAT. However, for small sections of code it may be simpler
29078 or more efficient to include assembly language statements directly
29079 in your Ada source program, using the facilities of the implementation-defined
29080 package @code{System.Machine_Code}, which incorporates the gcc
29081 Inline Assembler. The Inline Assembler approach offers a number of advantages,
29082 including the following:
29083
29084
29085 @itemize *
29086
29087 @item
29088 No need to use non-Ada tools
29089
29090 @item
29091 Consistent interface over different targets
29092
29093 @item
29094 Automatic usage of the proper calling conventions
29095
29096 @item
29097 Access to Ada constants and variables
29098
29099 @item
29100 Definition of intrinsic routines
29101
29102 @item
29103 Possibility of inlining a subprogram comprising assembler code
29104
29105 @item
29106 Code optimizer can take Inline Assembler code into account
29107 @end itemize
29108
29109 This appendix presents a series of examples to show you how to use
29110 the Inline Assembler. Although it focuses on the Intel x86,
29111 the general approach applies also to other processors.
29112 It is assumed that you are familiar with Ada
29113 and with assembly language programming.
29114
29115 @menu
29116 * Basic Assembler Syntax::
29117 * A Simple Example of Inline Assembler::
29118 * Output Variables in Inline Assembler::
29119 * Input Variables in Inline Assembler::
29120 * Inlining Inline Assembler Code::
29121 * Other Asm Functionality::
29122
29123 @end menu
29124
29125 @node Basic Assembler Syntax,A Simple Example of Inline Assembler,,Inline Assembler
29126 @anchor{gnat_ugn/inline_assembler id2}@anchor{249}@anchor{gnat_ugn/inline_assembler basic-assembler-syntax}@anchor{24a}
29127 @section Basic Assembler Syntax
29128
29129
29130 The assembler used by GNAT and gcc is based not on the Intel assembly
29131 language, but rather on a language that descends from the AT&T Unix
29132 assembler @code{as} (and which is often referred to as 'AT&T syntax').
29133 The following table summarizes the main features of @code{as} syntax
29134 and points out the differences from the Intel conventions.
29135 See the gcc @code{as} and @code{gas} (an @code{as} macro
29136 pre-processor) documentation for further information.
29137
29138
29139 @display
29140 @emph{Register names}@w{ }
29141 @display
29142 gcc / @code{as}: Prefix with '%'; for example @code{%eax}@w{ }
29143 Intel: No extra punctuation; for example @code{eax}@w{ }
29144 @end display
29145 @end display
29146
29147
29148
29149
29150 @display
29151 @emph{Immediate operand}@w{ }
29152 @display
29153 gcc / @code{as}: Prefix with '$'; for example @code{$4}@w{ }
29154 Intel: No extra punctuation; for example @code{4}@w{ }
29155 @end display
29156 @end display
29157
29158
29159
29160
29161 @display
29162 @emph{Address}@w{ }
29163 @display
29164 gcc / @code{as}: Prefix with '$'; for example @code{$loc}@w{ }
29165 Intel: No extra punctuation; for example @code{loc}@w{ }
29166 @end display
29167 @end display
29168
29169
29170
29171
29172 @display
29173 @emph{Memory contents}@w{ }
29174 @display
29175 gcc / @code{as}: No extra punctuation; for example @code{loc}@w{ }
29176 Intel: Square brackets; for example @code{[loc]}@w{ }
29177 @end display
29178 @end display
29179
29180
29181
29182
29183 @display
29184 @emph{Register contents}@w{ }
29185 @display
29186 gcc / @code{as}: Parentheses; for example @code{(%eax)}@w{ }
29187 Intel: Square brackets; for example @code{[eax]}@w{ }
29188 @end display
29189 @end display
29190
29191
29192
29193
29194 @display
29195 @emph{Hexadecimal numbers}@w{ }
29196 @display
29197 gcc / @code{as}: Leading '0x' (C language syntax); for example @code{0xA0}@w{ }
29198 Intel: Trailing 'h'; for example @code{A0h}@w{ }
29199 @end display
29200 @end display
29201
29202
29203
29204
29205 @display
29206 @emph{Operand size}@w{ }
29207 @display
29208 gcc / @code{as}: Explicit in op code; for example @code{movw} to move a 16-bit word@w{ }
29209 Intel: Implicit, deduced by assembler; for example @code{mov}@w{ }
29210 @end display
29211 @end display
29212
29213
29214
29215
29216 @display
29217 @emph{Instruction repetition}@w{ }
29218 @display
29219 gcc / @code{as}: Split into two lines; for example@w{ }
29220 @display
29221 @code{rep}@w{ }
29222 @code{stosl}@w{ }
29223 @end display
29224 Intel: Keep on one line; for example @code{rep stosl}@w{ }
29225 @end display
29226 @end display
29227
29228
29229
29230
29231 @display
29232 @emph{Order of operands}@w{ }
29233 @display
29234 gcc / @code{as}: Source first; for example @code{movw $4, %eax}@w{ }
29235 Intel: Destination first; for example @code{mov eax, 4}@w{ }
29236 @end display
29237 @end display
29238
29239
29240
29241 @node A Simple Example of Inline Assembler,Output Variables in Inline Assembler,Basic Assembler Syntax,Inline Assembler
29242 @anchor{gnat_ugn/inline_assembler a-simple-example-of-inline-assembler}@anchor{24b}@anchor{gnat_ugn/inline_assembler id3}@anchor{24c}
29243 @section A Simple Example of Inline Assembler
29244
29245
29246 The following example will generate a single assembly language statement,
29247 @code{nop}, which does nothing. Despite its lack of run-time effect,
29248 the example will be useful in illustrating the basics of
29249 the Inline Assembler facility.
29250
29251 @quotation
29252
29253 @example
29254 with System.Machine_Code; use System.Machine_Code;
29255 procedure Nothing is
29256 begin
29257 Asm ("nop");
29258 end Nothing;
29259 @end example
29260 @end quotation
29261
29262 @code{Asm} is a procedure declared in package @code{System.Machine_Code};
29263 here it takes one parameter, a @emph{template string} that must be a static
29264 expression and that will form the generated instruction.
29265 @code{Asm} may be regarded as a compile-time procedure that parses
29266 the template string and additional parameters (none here),
29267 from which it generates a sequence of assembly language instructions.
29268
29269 The examples in this chapter will illustrate several of the forms
29270 for invoking @code{Asm}; a complete specification of the syntax
29271 is found in the @code{Machine_Code_Insertions} section of the
29272 @cite{GNAT Reference Manual}.
29273
29274 Under the standard GNAT conventions, the @code{Nothing} procedure
29275 should be in a file named @code{nothing.adb}.
29276 You can build the executable in the usual way:
29277
29278 @quotation
29279
29280 @example
29281 $ gnatmake nothing
29282 @end example
29283 @end quotation
29284
29285 However, the interesting aspect of this example is not its run-time behavior
29286 but rather the generated assembly code.
29287 To see this output, invoke the compiler as follows:
29288
29289 @quotation
29290
29291 @example
29292 $ gcc -c -S -fomit-frame-pointer -gnatp nothing.adb
29293 @end example
29294 @end quotation
29295
29296 where the options are:
29297
29298
29299 @itemize *
29300
29301 @item
29302
29303 @table @asis
29304
29305 @item @code{-c}
29306
29307 compile only (no bind or link)
29308 @end table
29309
29310 @item
29311
29312 @table @asis
29313
29314 @item @code{-S}
29315
29316 generate assembler listing
29317 @end table
29318
29319 @item
29320
29321 @table @asis
29322
29323 @item @code{-fomit-frame-pointer}
29324
29325 do not set up separate stack frames
29326 @end table
29327
29328 @item
29329
29330 @table @asis
29331
29332 @item @code{-gnatp}
29333
29334 do not add runtime checks
29335 @end table
29336 @end itemize
29337
29338 This gives a human-readable assembler version of the code. The resulting
29339 file will have the same name as the Ada source file, but with a @code{.s}
29340 extension. In our example, the file @code{nothing.s} has the following
29341 contents:
29342
29343 @quotation
29344
29345 @example
29346 .file "nothing.adb"
29347 gcc2_compiled.:
29348 ___gnu_compiled_ada:
29349 .text
29350 .align 4
29351 .globl __ada_nothing
29352 __ada_nothing:
29353 #APP
29354 nop
29355 #NO_APP
29356 jmp L1
29357 .align 2,0x90
29358 L1:
29359 ret
29360 @end example
29361 @end quotation
29362
29363 The assembly code you included is clearly indicated by
29364 the compiler, between the @code{#APP} and @code{#NO_APP}
29365 delimiters. The character before the 'APP' and 'NOAPP'
29366 can differ on different targets. For example, GNU/Linux uses '#APP' while
29367 on NT you will see '/APP'.
29368
29369 If you make a mistake in your assembler code (such as using the
29370 wrong size modifier, or using a wrong operand for the instruction) GNAT
29371 will report this error in a temporary file, which will be deleted when
29372 the compilation is finished. Generating an assembler file will help
29373 in such cases, since you can assemble this file separately using the
29374 @code{as} assembler that comes with gcc.
29375
29376 Assembling the file using the command
29377
29378 @quotation
29379
29380 @example
29381 $ as nothing.s
29382 @end example
29383 @end quotation
29384
29385 will give you error messages whose lines correspond to the assembler
29386 input file, so you can easily find and correct any mistakes you made.
29387 If there are no errors, @code{as} will generate an object file
29388 @code{nothing.out}.
29389
29390 @node Output Variables in Inline Assembler,Input Variables in Inline Assembler,A Simple Example of Inline Assembler,Inline Assembler
29391 @anchor{gnat_ugn/inline_assembler id4}@anchor{24d}@anchor{gnat_ugn/inline_assembler output-variables-in-inline-assembler}@anchor{24e}
29392 @section Output Variables in Inline Assembler
29393
29394
29395 The examples in this section, showing how to access the processor flags,
29396 illustrate how to specify the destination operands for assembly language
29397 statements.
29398
29399 @quotation
29400
29401 @example
29402 with Interfaces; use Interfaces;
29403 with Ada.Text_IO; use Ada.Text_IO;
29404 with System.Machine_Code; use System.Machine_Code;
29405 procedure Get_Flags is
29406 Flags : Unsigned_32;
29407 use ASCII;
29408 begin
29409 Asm ("pushfl" & LF & HT & -- push flags on stack
29410 "popl %%eax" & LF & HT & -- load eax with flags
29411 "movl %%eax, %0", -- store flags in variable
29412 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29413 Put_Line ("Flags register:" & Flags'Img);
29414 end Get_Flags;
29415 @end example
29416 @end quotation
29417
29418 In order to have a nicely aligned assembly listing, we have separated
29419 multiple assembler statements in the Asm template string with linefeed
29420 (ASCII.LF) and horizontal tab (ASCII.HT) characters.
29421 The resulting section of the assembly output file is:
29422
29423 @quotation
29424
29425 @example
29426 #APP
29427 pushfl
29428 popl %eax
29429 movl %eax, -40(%ebp)
29430 #NO_APP
29431 @end example
29432 @end quotation
29433
29434 It would have been legal to write the Asm invocation as:
29435
29436 @quotation
29437
29438 @example
29439 Asm ("pushfl popl %%eax movl %%eax, %0")
29440 @end example
29441 @end quotation
29442
29443 but in the generated assembler file, this would come out as:
29444
29445 @quotation
29446
29447 @example
29448 #APP
29449 pushfl popl %eax movl %eax, -40(%ebp)
29450 #NO_APP
29451 @end example
29452 @end quotation
29453
29454 which is not so convenient for the human reader.
29455
29456 We use Ada comments
29457 at the end of each line to explain what the assembler instructions
29458 actually do. This is a useful convention.
29459
29460 When writing Inline Assembler instructions, you need to precede each register
29461 and variable name with a percent sign. Since the assembler already requires
29462 a percent sign at the beginning of a register name, you need two consecutive
29463 percent signs for such names in the Asm template string, thus @code{%%eax}.
29464 In the generated assembly code, one of the percent signs will be stripped off.
29465
29466 Names such as @code{%0}, @code{%1}, @code{%2}, etc., denote input or output
29467 variables: operands you later define using @code{Input} or @code{Output}
29468 parameters to @code{Asm}.
29469 An output variable is illustrated in
29470 the third statement in the Asm template string:
29471
29472 @quotation
29473
29474 @example
29475 movl %%eax, %0
29476 @end example
29477 @end quotation
29478
29479 The intent is to store the contents of the eax register in a variable that can
29480 be accessed in Ada. Simply writing @code{movl %%eax, Flags} would not
29481 necessarily work, since the compiler might optimize by using a register
29482 to hold Flags, and the expansion of the @code{movl} instruction would not be
29483 aware of this optimization. The solution is not to store the result directly
29484 but rather to advise the compiler to choose the correct operand form;
29485 that is the purpose of the @code{%0} output variable.
29486
29487 Information about the output variable is supplied in the @code{Outputs}
29488 parameter to @code{Asm}:
29489
29490 @quotation
29491
29492 @example
29493 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29494 @end example
29495 @end quotation
29496
29497 The output is defined by the @code{Asm_Output} attribute of the target type;
29498 the general format is
29499
29500 @quotation
29501
29502 @example
29503 Type'Asm_Output (constraint_string, variable_name)
29504 @end example
29505 @end quotation
29506
29507 The constraint string directs the compiler how
29508 to store/access the associated variable. In the example
29509
29510 @quotation
29511
29512 @example
29513 Unsigned_32'Asm_Output ("=m", Flags);
29514 @end example
29515 @end quotation
29516
29517 the @code{"m"} (memory) constraint tells the compiler that the variable
29518 @code{Flags} should be stored in a memory variable, thus preventing
29519 the optimizer from keeping it in a register. In contrast,
29520
29521 @quotation
29522
29523 @example
29524 Unsigned_32'Asm_Output ("=r", Flags);
29525 @end example
29526 @end quotation
29527
29528 uses the @code{"r"} (register) constraint, telling the compiler to
29529 store the variable in a register.
29530
29531 If the constraint is preceded by the equal character '=', it tells
29532 the compiler that the variable will be used to store data into it.
29533
29534 In the @code{Get_Flags} example, we used the @code{"g"} (global) constraint,
29535 allowing the optimizer to choose whatever it deems best.
29536
29537 There are a fairly large number of constraints, but the ones that are
29538 most useful (for the Intel x86 processor) are the following:
29539
29540 @quotation
29541
29542
29543 @multitable {xxxxxxxx} {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}
29544 @item
29545
29546 @emph{=}
29547
29548 @tab
29549
29550 output constraint
29551
29552 @item
29553
29554 @emph{g}
29555
29556 @tab
29557
29558 global (i.e., can be stored anywhere)
29559
29560 @item
29561
29562 @emph{m}
29563
29564 @tab
29565
29566 in memory
29567
29568 @item
29569
29570 @emph{I}
29571
29572 @tab
29573
29574 a constant
29575
29576 @item
29577
29578 @emph{a}
29579
29580 @tab
29581
29582 use eax
29583
29584 @item
29585
29586 @emph{b}
29587
29588 @tab
29589
29590 use ebx
29591
29592 @item
29593
29594 @emph{c}
29595
29596 @tab
29597
29598 use ecx
29599
29600 @item
29601
29602 @emph{d}
29603
29604 @tab
29605
29606 use edx
29607
29608 @item
29609
29610 @emph{S}
29611
29612 @tab
29613
29614 use esi
29615
29616 @item
29617
29618 @emph{D}
29619
29620 @tab
29621
29622 use edi
29623
29624 @item
29625
29626 @emph{r}
29627
29628 @tab
29629
29630 use one of eax, ebx, ecx or edx
29631
29632 @item
29633
29634 @emph{q}
29635
29636 @tab
29637
29638 use one of eax, ebx, ecx, edx, esi or edi
29639
29640 @end multitable
29641
29642 @end quotation
29643
29644 The full set of constraints is described in the gcc and @code{as}
29645 documentation; note that it is possible to combine certain constraints
29646 in one constraint string.
29647
29648 You specify the association of an output variable with an assembler operand
29649 through the @code{%@emph{n}} notation, where @emph{n} is a non-negative
29650 integer. Thus in
29651
29652 @quotation
29653
29654 @example
29655 Asm ("pushfl" & LF & HT & -- push flags on stack
29656 "popl %%eax" & LF & HT & -- load eax with flags
29657 "movl %%eax, %0", -- store flags in variable
29658 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29659 @end example
29660 @end quotation
29661
29662 @code{%0} will be replaced in the expanded code by the appropriate operand,
29663 whatever
29664 the compiler decided for the @code{Flags} variable.
29665
29666 In general, you may have any number of output variables:
29667
29668
29669 @itemize *
29670
29671 @item
29672 Count the operands starting at 0; thus @code{%0}, @code{%1}, etc.
29673
29674 @item
29675 Specify the @code{Outputs} parameter as a parenthesized comma-separated list
29676 of @code{Asm_Output} attributes
29677 @end itemize
29678
29679 For example:
29680
29681 @quotation
29682
29683 @example
29684 Asm ("movl %%eax, %0" & LF & HT &
29685 "movl %%ebx, %1" & LF & HT &
29686 "movl %%ecx, %2",
29687 Outputs => (Unsigned_32'Asm_Output ("=g", Var_A), -- %0 = Var_A
29688 Unsigned_32'Asm_Output ("=g", Var_B), -- %1 = Var_B
29689 Unsigned_32'Asm_Output ("=g", Var_C))); -- %2 = Var_C
29690 @end example
29691 @end quotation
29692
29693 where @code{Var_A}, @code{Var_B}, and @code{Var_C} are variables
29694 in the Ada program.
29695
29696 As a variation on the @code{Get_Flags} example, we can use the constraints
29697 string to direct the compiler to store the eax register into the @code{Flags}
29698 variable, instead of including the store instruction explicitly in the
29699 @code{Asm} template string:
29700
29701 @quotation
29702
29703 @example
29704 with Interfaces; use Interfaces;
29705 with Ada.Text_IO; use Ada.Text_IO;
29706 with System.Machine_Code; use System.Machine_Code;
29707 procedure Get_Flags_2 is
29708 Flags : Unsigned_32;
29709 use ASCII;
29710 begin
29711 Asm ("pushfl" & LF & HT & -- push flags on stack
29712 "popl %%eax", -- save flags in eax
29713 Outputs => Unsigned_32'Asm_Output ("=a", Flags));
29714 Put_Line ("Flags register:" & Flags'Img);
29715 end Get_Flags_2;
29716 @end example
29717 @end quotation
29718
29719 The @code{"a"} constraint tells the compiler that the @code{Flags}
29720 variable will come from the eax register. Here is the resulting code:
29721
29722 @quotation
29723
29724 @example
29725 #APP
29726 pushfl
29727 popl %eax
29728 #NO_APP
29729 movl %eax,-40(%ebp)
29730 @end example
29731 @end quotation
29732
29733 The compiler generated the store of eax into Flags after
29734 expanding the assembler code.
29735
29736 Actually, there was no need to pop the flags into the eax register;
29737 more simply, we could just pop the flags directly into the program variable:
29738
29739 @quotation
29740
29741 @example
29742 with Interfaces; use Interfaces;
29743 with Ada.Text_IO; use Ada.Text_IO;
29744 with System.Machine_Code; use System.Machine_Code;
29745 procedure Get_Flags_3 is
29746 Flags : Unsigned_32;
29747 use ASCII;
29748 begin
29749 Asm ("pushfl" & LF & HT & -- push flags on stack
29750 "pop %0", -- save flags in Flags
29751 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
29752 Put_Line ("Flags register:" & Flags'Img);
29753 end Get_Flags_3;
29754 @end example
29755 @end quotation
29756
29757 @node Input Variables in Inline Assembler,Inlining Inline Assembler Code,Output Variables in Inline Assembler,Inline Assembler
29758 @anchor{gnat_ugn/inline_assembler id5}@anchor{24f}@anchor{gnat_ugn/inline_assembler input-variables-in-inline-assembler}@anchor{250}
29759 @section Input Variables in Inline Assembler
29760
29761
29762 The example in this section illustrates how to specify the source operands
29763 for assembly language statements.
29764 The program simply increments its input value by 1:
29765
29766 @quotation
29767
29768 @example
29769 with Interfaces; use Interfaces;
29770 with Ada.Text_IO; use Ada.Text_IO;
29771 with System.Machine_Code; use System.Machine_Code;
29772 procedure Increment is
29773
29774 function Incr (Value : Unsigned_32) return Unsigned_32 is
29775 Result : Unsigned_32;
29776 begin
29777 Asm ("incl %0",
29778 Outputs => Unsigned_32'Asm_Output ("=a", Result),
29779 Inputs => Unsigned_32'Asm_Input ("a", Value));
29780 return Result;
29781 end Incr;
29782
29783 Value : Unsigned_32;
29784
29785 begin
29786 Value := 5;
29787 Put_Line ("Value before is" & Value'Img);
29788 Value := Incr (Value);
29789 Put_Line ("Value after is" & Value'Img);
29790 end Increment;
29791 @end example
29792 @end quotation
29793
29794 The @code{Outputs} parameter to @code{Asm} specifies
29795 that the result will be in the eax register and that it is to be stored
29796 in the @code{Result} variable.
29797
29798 The @code{Inputs} parameter looks much like the @code{Outputs} parameter,
29799 but with an @code{Asm_Input} attribute.
29800 The @code{"="} constraint, indicating an output value, is not present.
29801
29802 You can have multiple input variables, in the same way that you can have more
29803 than one output variable.
29804
29805 The parameter count (%0, %1) etc, still starts at the first output statement,
29806 and continues with the input statements.
29807
29808 Just as the @code{Outputs} parameter causes the register to be stored into the
29809 target variable after execution of the assembler statements, so does the
29810 @code{Inputs} parameter cause its variable to be loaded into the register
29811 before execution of the assembler statements.
29812
29813 Thus the effect of the @code{Asm} invocation is:
29814
29815
29816 @itemize *
29817
29818 @item
29819 load the 32-bit value of @code{Value} into eax
29820
29821 @item
29822 execute the @code{incl %eax} instruction
29823
29824 @item
29825 store the contents of eax into the @code{Result} variable
29826 @end itemize
29827
29828 The resulting assembler file (with @code{-O2} optimization) contains:
29829
29830 @quotation
29831
29832 @example
29833 _increment__incr.1:
29834 subl $4,%esp
29835 movl 8(%esp),%eax
29836 #APP
29837 incl %eax
29838 #NO_APP
29839 movl %eax,%edx
29840 movl %ecx,(%esp)
29841 addl $4,%esp
29842 ret
29843 @end example
29844 @end quotation
29845
29846 @node Inlining Inline Assembler Code,Other Asm Functionality,Input Variables in Inline Assembler,Inline Assembler
29847 @anchor{gnat_ugn/inline_assembler id6}@anchor{251}@anchor{gnat_ugn/inline_assembler inlining-inline-assembler-code}@anchor{252}
29848 @section Inlining Inline Assembler Code
29849
29850
29851 For a short subprogram such as the @code{Incr} function in the previous
29852 section, the overhead of the call and return (creating / deleting the stack
29853 frame) can be significant, compared to the amount of code in the subprogram
29854 body. A solution is to apply Ada's @code{Inline} pragma to the subprogram,
29855 which directs the compiler to expand invocations of the subprogram at the
29856 point(s) of call, instead of setting up a stack frame for out-of-line calls.
29857 Here is the resulting program:
29858
29859 @quotation
29860
29861 @example
29862 with Interfaces; use Interfaces;
29863 with Ada.Text_IO; use Ada.Text_IO;
29864 with System.Machine_Code; use System.Machine_Code;
29865 procedure Increment_2 is
29866
29867 function Incr (Value : Unsigned_32) return Unsigned_32 is
29868 Result : Unsigned_32;
29869 begin
29870 Asm ("incl %0",
29871 Outputs => Unsigned_32'Asm_Output ("=a", Result),
29872 Inputs => Unsigned_32'Asm_Input ("a", Value));
29873 return Result;
29874 end Incr;
29875 pragma Inline (Increment);
29876
29877 Value : Unsigned_32;
29878
29879 begin
29880 Value := 5;
29881 Put_Line ("Value before is" & Value'Img);
29882 Value := Increment (Value);
29883 Put_Line ("Value after is" & Value'Img);
29884 end Increment_2;
29885 @end example
29886 @end quotation
29887
29888 Compile the program with both optimization (@code{-O2}) and inlining
29889 (@code{-gnatn}) enabled.
29890
29891 The @code{Incr} function is still compiled as usual, but at the
29892 point in @code{Increment} where our function used to be called:
29893
29894 @quotation
29895
29896 @example
29897 pushl %edi
29898 call _increment__incr.1
29899 @end example
29900 @end quotation
29901
29902 the code for the function body directly appears:
29903
29904 @quotation
29905
29906 @example
29907 movl %esi,%eax
29908 #APP
29909 incl %eax
29910 #NO_APP
29911 movl %eax,%edx
29912 @end example
29913 @end quotation
29914
29915 thus saving the overhead of stack frame setup and an out-of-line call.
29916
29917 @node Other Asm Functionality,,Inlining Inline Assembler Code,Inline Assembler
29918 @anchor{gnat_ugn/inline_assembler other-asm-functionality}@anchor{253}@anchor{gnat_ugn/inline_assembler id7}@anchor{254}
29919 @section Other @code{Asm} Functionality
29920
29921
29922 This section describes two important parameters to the @code{Asm}
29923 procedure: @code{Clobber}, which identifies register usage;
29924 and @code{Volatile}, which inhibits unwanted optimizations.
29925
29926 @menu
29927 * The Clobber Parameter::
29928 * The Volatile Parameter::
29929
29930 @end menu
29931
29932 @node The Clobber Parameter,The Volatile Parameter,,Other Asm Functionality
29933 @anchor{gnat_ugn/inline_assembler the-clobber-parameter}@anchor{255}@anchor{gnat_ugn/inline_assembler id8}@anchor{256}
29934 @subsection The @code{Clobber} Parameter
29935
29936
29937 One of the dangers of intermixing assembly language and a compiled language
29938 such as Ada is that the compiler needs to be aware of which registers are
29939 being used by the assembly code. In some cases, such as the earlier examples,
29940 the constraint string is sufficient to indicate register usage (e.g.,
29941 @code{"a"} for
29942 the eax register). But more generally, the compiler needs an explicit
29943 identification of the registers that are used by the Inline Assembly
29944 statements.
29945
29946 Using a register that the compiler doesn't know about
29947 could be a side effect of an instruction (like @code{mull}
29948 storing its result in both eax and edx).
29949 It can also arise from explicit register usage in your
29950 assembly code; for example:
29951
29952 @quotation
29953
29954 @example
29955 Asm ("movl %0, %%ebx" & LF & HT &
29956 "movl %%ebx, %1",
29957 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
29958 Inputs => Unsigned_32'Asm_Input ("g", Var_In));
29959 @end example
29960 @end quotation
29961
29962 where the compiler (since it does not analyze the @code{Asm} template string)
29963 does not know you are using the ebx register.
29964
29965 In such cases you need to supply the @code{Clobber} parameter to @code{Asm},
29966 to identify the registers that will be used by your assembly code:
29967
29968 @quotation
29969
29970 @example
29971 Asm ("movl %0, %%ebx" & LF & HT &
29972 "movl %%ebx, %1",
29973 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
29974 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
29975 Clobber => "ebx");
29976 @end example
29977 @end quotation
29978
29979 The Clobber parameter is a static string expression specifying the
29980 register(s) you are using. Note that register names are @emph{not} prefixed
29981 by a percent sign. Also, if more than one register is used then their names
29982 are separated by commas; e.g., @code{"eax, ebx"}
29983
29984 The @code{Clobber} parameter has several additional uses:
29985
29986
29987 @itemize *
29988
29989 @item
29990 Use 'register' name @code{cc} to indicate that flags might have changed
29991
29992 @item
29993 Use 'register' name @code{memory} if you changed a memory location
29994 @end itemize
29995
29996 @node The Volatile Parameter,,The Clobber Parameter,Other Asm Functionality
29997 @anchor{gnat_ugn/inline_assembler the-volatile-parameter}@anchor{257}@anchor{gnat_ugn/inline_assembler id9}@anchor{258}
29998 @subsection The @code{Volatile} Parameter
29999
30000
30001 @geindex Volatile parameter
30002
30003 Compiler optimizations in the presence of Inline Assembler may sometimes have
30004 unwanted effects. For example, when an @code{Asm} invocation with an input
30005 variable is inside a loop, the compiler might move the loading of the input
30006 variable outside the loop, regarding it as a one-time initialization.
30007
30008 If this effect is not desired, you can disable such optimizations by setting
30009 the @code{Volatile} parameter to @code{True}; for example:
30010
30011 @quotation
30012
30013 @example
30014 Asm ("movl %0, %%ebx" & LF & HT &
30015 "movl %%ebx, %1",
30016 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
30017 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
30018 Clobber => "ebx",
30019 Volatile => True);
30020 @end example
30021 @end quotation
30022
30023 By default, @code{Volatile} is set to @code{False} unless there is no
30024 @code{Outputs} parameter.
30025
30026 Although setting @code{Volatile} to @code{True} prevents unwanted
30027 optimizations, it will also disable other optimizations that might be
30028 important for efficiency. In general, you should set @code{Volatile}
30029 to @code{True} only if the compiler's optimizations have created
30030 problems.
30031
30032 @node GNU Free Documentation License,Index,Inline Assembler,Top
30033 @anchor{share/gnu_free_documentation_license gnu-fdl}@anchor{1}@anchor{share/gnu_free_documentation_license doc}@anchor{259}@anchor{share/gnu_free_documentation_license gnu-free-documentation-license}@anchor{25a}
30034 @chapter GNU Free Documentation License
30035
30036
30037 Version 1.3, 3 November 2008
30038
30039 Copyright 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc
30040 @indicateurl{http://fsf.org/}
30041
30042 Everyone is permitted to copy and distribute verbatim copies of this
30043 license document, but changing it is not allowed.
30044
30045 @strong{Preamble}
30046
30047 The purpose of this License is to make a manual, textbook, or other
30048 functional and useful document "free" in the sense of freedom: to
30049 assure everyone the effective freedom to copy and redistribute it,
30050 with or without modifying it, either commercially or noncommercially.
30051 Secondarily, this License preserves for the author and publisher a way
30052 to get credit for their work, while not being considered responsible
30053 for modifications made by others.
30054
30055 This License is a kind of "copyleft", which means that derivative
30056 works of the document must themselves be free in the same sense. It
30057 complements the GNU General Public License, which is a copyleft
30058 license designed for free software.
30059
30060 We have designed this License in order to use it for manuals for free
30061 software, because free software needs free documentation: a free
30062 program should come with manuals providing the same freedoms that the
30063 software does. But this License is not limited to software manuals;
30064 it can be used for any textual work, regardless of subject matter or
30065 whether it is published as a printed book. We recommend this License
30066 principally for works whose purpose is instruction or reference.
30067
30068 @strong{1. APPLICABILITY AND DEFINITIONS}
30069
30070 This License applies to any manual or other work, in any medium, that
30071 contains a notice placed by the copyright holder saying it can be
30072 distributed under the terms of this License. Such a notice grants a
30073 world-wide, royalty-free license, unlimited in duration, to use that
30074 work under the conditions stated herein. The @strong{Document}, below,
30075 refers to any such manual or work. Any member of the public is a
30076 licensee, and is addressed as "@strong{you}". You accept the license if you
30077 copy, modify or distribute the work in a way requiring permission
30078 under copyright law.
30079
30080 A "@strong{Modified Version}" of the Document means any work containing the
30081 Document or a portion of it, either copied verbatim, or with
30082 modifications and/or translated into another language.
30083
30084 A "@strong{Secondary Section}" is a named appendix or a front-matter section of
30085 the Document that deals exclusively with the relationship of the
30086 publishers or authors of the Document to the Document's overall subject
30087 (or to related matters) and contains nothing that could fall directly
30088 within that overall subject. (Thus, if the Document is in part a
30089 textbook of mathematics, a Secondary Section may not explain any
30090 mathematics.) The relationship could be a matter of historical
30091 connection with the subject or with related matters, or of legal,
30092 commercial, philosophical, ethical or political position regarding
30093 them.
30094
30095 The "@strong{Invariant Sections}" are certain Secondary Sections whose titles
30096 are designated, as being those of Invariant Sections, in the notice
30097 that says that the Document is released under this License. If a
30098 section does not fit the above definition of Secondary then it is not
30099 allowed to be designated as Invariant. The Document may contain zero
30100 Invariant Sections. If the Document does not identify any Invariant
30101 Sections then there are none.
30102
30103 The "@strong{Cover Texts}" are certain short passages of text that are listed,
30104 as Front-Cover Texts or Back-Cover Texts, in the notice that says that
30105 the Document is released under this License. A Front-Cover Text may
30106 be at most 5 words, and a Back-Cover Text may be at most 25 words.
30107
30108 A "@strong{Transparent}" copy of the Document means a machine-readable copy,
30109 represented in a format whose specification is available to the
30110 general public, that is suitable for revising the document
30111 straightforwardly with generic text editors or (for images composed of
30112 pixels) generic paint programs or (for drawings) some widely available
30113 drawing editor, and that is suitable for input to text formatters or
30114 for automatic translation to a variety of formats suitable for input
30115 to text formatters. A copy made in an otherwise Transparent file
30116 format whose markup, or absence of markup, has been arranged to thwart
30117 or discourage subsequent modification by readers is not Transparent.
30118 An image format is not Transparent if used for any substantial amount
30119 of text. A copy that is not "Transparent" is called @strong{Opaque}.
30120
30121 Examples of suitable formats for Transparent copies include plain
30122 ASCII without markup, Texinfo input format, LaTeX input format, SGML
30123 or XML using a publicly available DTD, and standard-conforming simple
30124 HTML, PostScript or PDF designed for human modification. Examples of
30125 transparent image formats include PNG, XCF and JPG. Opaque formats
30126 include proprietary formats that can be read and edited only by
30127 proprietary word processors, SGML or XML for which the DTD and/or
30128 processing tools are not generally available, and the
30129 machine-generated HTML, PostScript or PDF produced by some word
30130 processors for output purposes only.
30131
30132 The "@strong{Title Page}" means, for a printed book, the title page itself,
30133 plus such following pages as are needed to hold, legibly, the material
30134 this License requires to appear in the title page. For works in
30135 formats which do not have any title page as such, "Title Page" means
30136 the text near the most prominent appearance of the work's title,
30137 preceding the beginning of the body of the text.
30138
30139 The "@strong{publisher}" means any person or entity that distributes
30140 copies of the Document to the public.
30141
30142 A section "@strong{Entitled XYZ}" means a named subunit of the Document whose
30143 title either is precisely XYZ or contains XYZ in parentheses following
30144 text that translates XYZ in another language. (Here XYZ stands for a
30145 specific section name mentioned below, such as "@strong{Acknowledgements}",
30146 "@strong{Dedications}", "@strong{Endorsements}", or "@strong{History}".)
30147 To "@strong{Preserve the Title}"
30148 of such a section when you modify the Document means that it remains a
30149 section "Entitled XYZ" according to this definition.
30150
30151 The Document may include Warranty Disclaimers next to the notice which
30152 states that this License applies to the Document. These Warranty
30153 Disclaimers are considered to be included by reference in this
30154 License, but only as regards disclaiming warranties: any other
30155 implication that these Warranty Disclaimers may have is void and has
30156 no effect on the meaning of this License.
30157
30158 @strong{2. VERBATIM COPYING}
30159
30160 You may copy and distribute the Document in any medium, either
30161 commercially or noncommercially, provided that this License, the
30162 copyright notices, and the license notice saying this License applies
30163 to the Document are reproduced in all copies, and that you add no other
30164 conditions whatsoever to those of this License. You may not use
30165 technical measures to obstruct or control the reading or further
30166 copying of the copies you make or distribute. However, you may accept
30167 compensation in exchange for copies. If you distribute a large enough
30168 number of copies you must also follow the conditions in section 3.
30169
30170 You may also lend copies, under the same conditions stated above, and
30171 you may publicly display copies.
30172
30173 @strong{3. COPYING IN QUANTITY}
30174
30175 If you publish printed copies (or copies in media that commonly have
30176 printed covers) of the Document, numbering more than 100, and the
30177 Document's license notice requires Cover Texts, you must enclose the
30178 copies in covers that carry, clearly and legibly, all these Cover
30179 Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on
30180 the back cover. Both covers must also clearly and legibly identify
30181 you as the publisher of these copies. The front cover must present
30182 the full title with all words of the title equally prominent and
30183 visible. You may add other material on the covers in addition.
30184 Copying with changes limited to the covers, as long as they preserve
30185 the title of the Document and satisfy these conditions, can be treated
30186 as verbatim copying in other respects.
30187
30188 If the required texts for either cover are too voluminous to fit
30189 legibly, you should put the first ones listed (as many as fit
30190 reasonably) on the actual cover, and continue the rest onto adjacent
30191 pages.
30192
30193 If you publish or distribute Opaque copies of the Document numbering
30194 more than 100, you must either include a machine-readable Transparent
30195 copy along with each Opaque copy, or state in or with each Opaque copy
30196 a computer-network location from which the general network-using
30197 public has access to download using public-standard network protocols
30198 a complete Transparent copy of the Document, free of added material.
30199 If you use the latter option, you must take reasonably prudent steps,
30200 when you begin distribution of Opaque copies in quantity, to ensure
30201 that this Transparent copy will remain thus accessible at the stated
30202 location until at least one year after the last time you distribute an
30203 Opaque copy (directly or through your agents or retailers) of that
30204 edition to the public.
30205
30206 It is requested, but not required, that you contact the authors of the
30207 Document well before redistributing any large number of copies, to give
30208 them a chance to provide you with an updated version of the Document.
30209
30210 @strong{4. MODIFICATIONS}
30211
30212 You may copy and distribute a Modified Version of the Document under
30213 the conditions of sections 2 and 3 above, provided that you release
30214 the Modified Version under precisely this License, with the Modified
30215 Version filling the role of the Document, thus licensing distribution
30216 and modification of the Modified Version to whoever possesses a copy
30217 of it. In addition, you must do these things in the Modified Version:
30218
30219
30220 @enumerate A
30221
30222 @item
30223 Use in the Title Page (and on the covers, if any) a title distinct
30224 from that of the Document, and from those of previous versions
30225 (which should, if there were any, be listed in the History section
30226 of the Document). You may use the same title as a previous version
30227 if the original publisher of that version gives permission.
30228
30229 @item
30230 List on the Title Page, as authors, one or more persons or entities
30231 responsible for authorship of the modifications in the Modified
30232 Version, together with at least five of the principal authors of the
30233 Document (all of its principal authors, if it has fewer than five),
30234 unless they release you from this requirement.
30235
30236 @item
30237 State on the Title page the name of the publisher of the
30238 Modified Version, as the publisher.
30239
30240 @item
30241 Preserve all the copyright notices of the Document.
30242
30243 @item
30244 Add an appropriate copyright notice for your modifications
30245 adjacent to the other copyright notices.
30246
30247 @item
30248 Include, immediately after the copyright notices, a license notice
30249 giving the public permission to use the Modified Version under the
30250 terms of this License, in the form shown in the Addendum below.
30251
30252 @item
30253 Preserve in that license notice the full lists of Invariant Sections
30254 and required Cover Texts given in the Document's license notice.
30255
30256 @item
30257 Include an unaltered copy of this License.
30258
30259 @item
30260 Preserve the section Entitled "History", Preserve its Title, and add
30261 to it an item stating at least the title, year, new authors, and
30262 publisher of the Modified Version as given on the Title Page. If
30263 there is no section Entitled "History" in the Document, create one
30264 stating the title, year, authors, and publisher of the Document as
30265 given on its Title Page, then add an item describing the Modified
30266 Version as stated in the previous sentence.
30267
30268 @item
30269 Preserve the network location, if any, given in the Document for
30270 public access to a Transparent copy of the Document, and likewise
30271 the network locations given in the Document for previous versions
30272 it was based on. These may be placed in the "History" section.
30273 You may omit a network location for a work that was published at
30274 least four years before the Document itself, or if the original
30275 publisher of the version it refers to gives permission.
30276
30277 @item
30278 For any section Entitled "Acknowledgements" or "Dedications",
30279 Preserve the Title of the section, and preserve in the section all
30280 the substance and tone of each of the contributor acknowledgements
30281 and/or dedications given therein.
30282
30283 @item
30284 Preserve all the Invariant Sections of the Document,
30285 unaltered in their text and in their titles. Section numbers
30286 or the equivalent are not considered part of the section titles.
30287
30288 @item
30289 Delete any section Entitled "Endorsements". Such a section
30290 may not be included in the Modified Version.
30291
30292 @item
30293 Do not retitle any existing section to be Entitled "Endorsements"
30294 or to conflict in title with any Invariant Section.
30295
30296 @item
30297 Preserve any Warranty Disclaimers.
30298 @end enumerate
30299
30300 If the Modified Version includes new front-matter sections or
30301 appendices that qualify as Secondary Sections and contain no material
30302 copied from the Document, you may at your option designate some or all
30303 of these sections as invariant. To do this, add their titles to the
30304 list of Invariant Sections in the Modified Version's license notice.
30305 These titles must be distinct from any other section titles.
30306
30307 You may add a section Entitled "Endorsements", provided it contains
30308 nothing but endorsements of your Modified Version by various
30309 parties---for example, statements of peer review or that the text has
30310 been approved by an organization as the authoritative definition of a
30311 standard.
30312
30313 You may add a passage of up to five words as a Front-Cover Text, and a
30314 passage of up to 25 words as a Back-Cover Text, to the end of the list
30315 of Cover Texts in the Modified Version. Only one passage of
30316 Front-Cover Text and one of Back-Cover Text may be added by (or
30317 through arrangements made by) any one entity. If the Document already
30318 includes a cover text for the same cover, previously added by you or
30319 by arrangement made by the same entity you are acting on behalf of,
30320 you may not add another; but you may replace the old one, on explicit
30321 permission from the previous publisher that added the old one.
30322
30323 The author(s) and publisher(s) of the Document do not by this License
30324 give permission to use their names for publicity for or to assert or
30325 imply endorsement of any Modified Version.
30326
30327 @strong{5. COMBINING DOCUMENTS}
30328
30329 You may combine the Document with other documents released under this
30330 License, under the terms defined in section 4 above for modified
30331 versions, provided that you include in the combination all of the
30332 Invariant Sections of all of the original documents, unmodified, and
30333 list them all as Invariant Sections of your combined work in its
30334 license notice, and that you preserve all their Warranty Disclaimers.
30335
30336 The combined work need only contain one copy of this License, and
30337 multiple identical Invariant Sections may be replaced with a single
30338 copy. If there are multiple Invariant Sections with the same name but
30339 different contents, make the title of each such section unique by
30340 adding at the end of it, in parentheses, the name of the original
30341 author or publisher of that section if known, or else a unique number.
30342 Make the same adjustment to the section titles in the list of
30343 Invariant Sections in the license notice of the combined work.
30344
30345 In the combination, you must combine any sections Entitled "History"
30346 in the various original documents, forming one section Entitled
30347 "History"; likewise combine any sections Entitled "Acknowledgements",
30348 and any sections Entitled "Dedications". You must delete all sections
30349 Entitled "Endorsements".
30350
30351 @strong{6. COLLECTIONS OF DOCUMENTS}
30352
30353 You may make a collection consisting of the Document and other documents
30354 released under this License, and replace the individual copies of this
30355 License in the various documents with a single copy that is included in
30356 the collection, provided that you follow the rules of this License for
30357 verbatim copying of each of the documents in all other respects.
30358
30359 You may extract a single document from such a collection, and distribute
30360 it individually under this License, provided you insert a copy of this
30361 License into the extracted document, and follow this License in all
30362 other respects regarding verbatim copying of that document.
30363
30364 @strong{7. AGGREGATION WITH INDEPENDENT WORKS}
30365
30366 A compilation of the Document or its derivatives with other separate
30367 and independent documents or works, in or on a volume of a storage or
30368 distribution medium, is called an "aggregate" if the copyright
30369 resulting from the compilation is not used to limit the legal rights
30370 of the compilation's users beyond what the individual works permit.
30371 When the Document is included in an aggregate, this License does not
30372 apply to the other works in the aggregate which are not themselves
30373 derivative works of the Document.
30374
30375 If the Cover Text requirement of section 3 is applicable to these
30376 copies of the Document, then if the Document is less than one half of
30377 the entire aggregate, the Document's Cover Texts may be placed on
30378 covers that bracket the Document within the aggregate, or the
30379 electronic equivalent of covers if the Document is in electronic form.
30380 Otherwise they must appear on printed covers that bracket the whole
30381 aggregate.
30382
30383 @strong{8. TRANSLATION}
30384
30385 Translation is considered a kind of modification, so you may
30386 distribute translations of the Document under the terms of section 4.
30387 Replacing Invariant Sections with translations requires special
30388 permission from their copyright holders, but you may include
30389 translations of some or all Invariant Sections in addition to the
30390 original versions of these Invariant Sections. You may include a
30391 translation of this License, and all the license notices in the
30392 Document, and any Warranty Disclaimers, provided that you also include
30393 the original English version of this License and the original versions
30394 of those notices and disclaimers. In case of a disagreement between
30395 the translation and the original version of this License or a notice
30396 or disclaimer, the original version will prevail.
30397
30398 If a section in the Document is Entitled "Acknowledgements",
30399 "Dedications", or "History", the requirement (section 4) to Preserve
30400 its Title (section 1) will typically require changing the actual
30401 title.
30402
30403 @strong{9. TERMINATION}
30404
30405 You may not copy, modify, sublicense, or distribute the Document
30406 except as expressly provided under this License. Any attempt
30407 otherwise to copy, modify, sublicense, or distribute it is void, and
30408 will automatically terminate your rights under this License.
30409
30410 However, if you cease all violation of this License, then your license
30411 from a particular copyright holder is reinstated (a) provisionally,
30412 unless and until the copyright holder explicitly and finally
30413 terminates your license, and (b) permanently, if the copyright holder
30414 fails to notify you of the violation by some reasonable means prior to
30415 60 days after the cessation.
30416
30417 Moreover, your license from a particular copyright holder is
30418 reinstated permanently if the copyright holder notifies you of the
30419 violation by some reasonable means, this is the first time you have
30420 received notice of violation of this License (for any work) from that
30421 copyright holder, and you cure the violation prior to 30 days after
30422 your receipt of the notice.
30423
30424 Termination of your rights under this section does not terminate the
30425 licenses of parties who have received copies or rights from you under
30426 this License. If your rights have been terminated and not permanently
30427 reinstated, receipt of a copy of some or all of the same material does
30428 not give you any rights to use it.
30429
30430 @strong{10. FUTURE REVISIONS OF THIS LICENSE}
30431
30432 The Free Software Foundation may publish new, revised versions
30433 of the GNU Free Documentation License from time to time. Such new
30434 versions will be similar in spirit to the present version, but may
30435 differ in detail to address new problems or concerns. See
30436 @indicateurl{http://www.gnu.org/copyleft/}.
30437
30438 Each version of the License is given a distinguishing version number.
30439 If the Document specifies that a particular numbered version of this
30440 License "or any later version" applies to it, you have the option of
30441 following the terms and conditions either of that specified version or
30442 of any later version that has been published (not as a draft) by the
30443 Free Software Foundation. If the Document does not specify a version
30444 number of this License, you may choose any version ever published (not
30445 as a draft) by the Free Software Foundation. If the Document
30446 specifies that a proxy can decide which future versions of this
30447 License can be used, that proxy's public statement of acceptance of a
30448 version permanently authorizes you to choose that version for the
30449 Document.
30450
30451 @strong{11. RELICENSING}
30452
30453 "Massive Multiauthor Collaboration Site" (or "MMC Site") means any
30454 World Wide Web server that publishes copyrightable works and also
30455 provides prominent facilities for anybody to edit those works. A
30456 public wiki that anybody can edit is an example of such a server. A
30457 "Massive Multiauthor Collaboration" (or "MMC") contained in the
30458 site means any set of copyrightable works thus published on the MMC
30459 site.
30460
30461 "CC-BY-SA" means the Creative Commons Attribution-Share Alike 3.0
30462 license published by Creative Commons Corporation, a not-for-profit
30463 corporation with a principal place of business in San Francisco,
30464 California, as well as future copyleft versions of that license
30465 published by that same organization.
30466
30467 "Incorporate" means to publish or republish a Document, in whole or
30468 in part, as part of another Document.
30469
30470 An MMC is "eligible for relicensing" if it is licensed under this
30471 License, and if all works that were first published under this License
30472 somewhere other than this MMC, and subsequently incorporated in whole
30473 or in part into the MMC, (1) had no cover texts or invariant sections,
30474 and (2) were thus incorporated prior to November 1, 2008.
30475
30476 The operator of an MMC Site may republish an MMC contained in the site
30477 under CC-BY-SA on the same site at any time before August 1, 2009,
30478 provided the MMC is eligible for relicensing.
30479
30480 @strong{ADDENDUM: How to use this License for your documents}
30481
30482 To use this License in a document you have written, include a copy of
30483 the License in the document and put the following copyright and
30484 license notices just after the title page:
30485
30486 @quotation
30487
30488 Copyright © YEAR YOUR NAME.
30489 Permission is granted to copy, distribute and/or modify this document
30490 under the terms of the GNU Free Documentation License, Version 1.3
30491 or any later version published by the Free Software Foundation;
30492 with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
30493 A copy of the license is included in the section entitled "GNU
30494 Free Documentation License".
30495 @end quotation
30496
30497 If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts,
30498 replace the "with ... Texts." line with this:
30499
30500 @quotation
30501
30502 with the Invariant Sections being LIST THEIR TITLES, with the
30503 Front-Cover Texts being LIST, and with the Back-Cover Texts being LIST.
30504 @end quotation
30505
30506 If you have Invariant Sections without Cover Texts, or some other
30507 combination of the three, merge those two alternatives to suit the
30508 situation.
30509
30510 If your document contains nontrivial examples of program code, we
30511 recommend releasing these examples in parallel under your choice of
30512 free software license, such as the GNU General Public License,
30513 to permit their use in free software.
30514
30515 @node Index,,GNU Free Documentation License,Top
30516 @unnumbered Index
30517
30518
30519 @printindex ge
30520
30521 @anchor{de}@w{ }
30522 @anchor{gnat_ugn/gnat_utility_programs switches-related-to-project-files}@w{ }
30523
30524 @c %**end of body
30525 @bye