CHAPTER
19

Makefile Generation

This section describes Sun WorkShop Visual's Makefile generation facilities. Sun WorkShop Visual can create two types of Makefile: simple or with templates. The simple Makefile only contains the build rules for the local .xd file and so is only useful for applications that are contained in a single file. A Makefile with templates can be updated to add files without overwriting your previous work. Unlike most generated files, Makefiles with templates can be edited and regenerated without losing your work.

This section describes the Makefile options available when you generate code for a design. It also provides a short tutorial with step-by-step instructions for creating a Makefile with templates and then updating the Makefile when a second design file is added to the application. Following this tutorial enables you to familiarize yourself with Sun WorkShop Visual's Makefile generation capabilities.

The dialog contains four toggles labelled "New Makefile", "Makefile template" and "Debugging" and a scrolled list. Setting "Debugging" simply adds the "-g" flag to the list of flags to be sent to the compiler so that you can build a version of your application for debugging.

When you generate code in one language and then generate another set of code files in another language, all Makefiles generated afterwards will contain rules for both sets of generated files. Setting the "Current language only" toggle ensures that you are generating a Makefile for the current language only.

New Makefile and Makefile template Toggles

"New Makefile" and "Makefile Template" relate to the two different types of makefile that you can generate: a simple makefile and a makefile with templates. Which one is generated depends on the way in which the toggles in this dialog are set. The two toggles work in conjunction with one another. There are four ways they can be set:

1.

The "New Makefile" toggle is set and the "Makefile Template" toggle is not set.

This generates a simple Makefile. You would use this option if you do not wish to add other design files to the application which the makefile will build.

2.

Both toggles are set.

This generates a makefile with templates - i.e. with structured comments which serve as templates for updating the file on subsequent generations. Use this option if you are going to add other design files to the application which the makefile will build.

3.

The "New Makefile" toggle is not set and the "Makefile Template" toggle is set.

This will add the current design code files to the Makefile which was generated as described in number 2 above.

4.

Neither toggle is set.

This works in exactly the same way as number 3 above.

The List of Makefile Types

The scrolled list in the Makefile Options dialog allows you to generate a Makefile for a specific target platform. For some platforms more than one option is available, allowing for different environments. On Solaris, for example, you can choose compile your application for the 32-bit or the 64-bit architectures. The default for your platform is selected initially. Change this if you wish to generate a Makefile to build your application on another platform or if you wish to use a different compiler from the default. You can, of course, generate any number of different Makefiles to match the environments in which your application will be running.

In some cases, where there is more than one option for a given platform, you can generate a "multi-target" Makefile. This allows you to specify any of the available targets using the one Makefile. For example, the following selection from the Makefile Options dialog:

Solaris 32/64 Multi-target C/C++ (sparc)

generates a Makefile capable of doing the same as all the other Solaris Makefiles available from the Makefile Options dialog. These are:

Solaris 32bit Ansi C/C++

Solaris 64bit Ansi C/C++ (sparc)

Solaris 32bit Workshop4 Compatible C++

Multi-target Makefiles are identified in the Makefile Options dialog by the words "Multi-target". Look at the Makefile itself for the list of command-line options available.

If you wish to build just one version of your application, choose the appropriate Makefile type. If you intend to build your application anumber of times - once for each different target on a given platform, you may find the "multi-target" Makefile most useful.

Makefile Generation Notes

There are a few points to bear in mind when generating a makefile:

If you are going to use the makefile with templates, make sure that you create the first makefile (the one with both "New Makefile" and "Makefile Template" toggles set) for the main design - usually the one which contains the application shell. Sun WorkShop Visual will assume that the name of the application is the same as the name of the primary source file of the design from which a "New Makefile" is generated.

When you choose "Makefile Template" only, make sure that you have specified the name of the makefile which has already been generated. Sun WorkShop Visual will report an error if you give another name.

You can edit a makefile with templates and regenerate it without losing your changes. This is not the case for simple makefiles.

Before you generate a Makefile, you must generate the code files that you want to include in it. Generating the code files sets the names for these files in the design file. Until you do this, the Makefile generation feature doesn't know the names of these files and can't add them to the Makefile.

4.

Display the Generate dialog and make sure that "C" is the selected language.

The Generate dialog will be primed with filenames based on the save file name or "untitled" if the design has not been saved.

5.

Type: myapp.c into the "Code" field and set the "Generate" toggle.

6.

Type: myapp.c into the "Main Program" field and set the "Generate" toggle.

If the "code" and "Main Program" filenames are the same, the code file is generated with a main procedure.

7.

In the Code Options dialog, set the "Include Header File" toggle.

8.

Type: app_stubs.c into the "Stubs" field and set the "Generate" toggle.

9.

In the Generate Options dialog, set the "Links" option menu to "None".

Now you can generate a Makefile that compiles and links myapp.c and app_stubs.c:

10.

Type: Makefile into the "Makefile" field and set the "Generate" toggle.

11.

In the Makefile Options dialog set both "New Makefile" and "Makefile Template" toggles on, as shown in Figure 19-3.

Once you have generated the initial Makefile, you can update it to reflect additional work. To demonstrate this, use the following instructions to build a popup dialog in another file, generate code for it and then update the Makefile to reflect the new modules.

You can edit and regenerate the Makefile without losing information. You can make the most commonly needed changes by editing the Makefile flags at the beginning of the file. For example, to make the compiler search the ../hdrs directory for header files, append the entry:

-I../hdrs

to the end of the CFLAGS line in the Makefile.

The change to CFLAGS is retained when you regenerate the Makefile with the "New makefile" toggle off. It is only lost if you generate a new Makefile.

Editing Template Lines

A large part of the generated Makefile consists of template lines. Template lines are comments that control the generation of information into the Makefile. Template lines have a #Sun WorkShop Visual: prefix. For example, the following template lines tell Sun WorkShop Visual how to generate the Makefile lines that produce a C object file from a C source file (XDG_C_SOURCE):

#Sun WorkShop Visual:XDG_C_OBJECT: XDG_C_SOURCE

#Sun WorkShop Visual: $(CC) $(CFLAGS) $(CPPFLAGS) -c XDG_C_SOURCE

Each time you update the Makefile to add a file to your application, Sun WorkShop Visual generates a template instance for each relevant template. These instances contain the actual build commands for your application.

Template instances are marked with a "DO NOT EDIT" comment at the beginning and at the end. A typical instance of the template shown above looks like:

#DO NOT EDIT >>>

error.o: error.c

$(CC) $(CFLAGS) $(CPPFLAGS) -c error.c

#<<< DO NOT EDIT

Template instances should not be edited because your edits may be lost the next time you generate the Makefile. Instead, to change the build commands, edit the corresponding template lines. After you edit a template line, delete any instances of that template line that already exist in the Makefile. The instances are found just after the template line.

For example, to build all C files for debugging, you would:

1.

Change the template line:

#Sun WorkShop Visual: $(CC) $(CFLAGS) $(CPPFLAGS) -c XDG_C_SOURCE

to

#Sun WorkShop Visual: $(CC) $(CFLAGS) $(CPPFLAGS) -g -c XDG_C_SOURCE

2.

Remove the instances following the template line.

3.

Regenerate the Makefile, with the "New" toggle off, for each design in the application.

This procedure generates new instances using the modified template.

Template Configuration

The original templates are specified by the file pointed to by the following resources:

visu.motifMakeTemplateFile: $VISUROOT/make_templates/motif

visu.mmfcMakeTemplateFile: $VISUROOT/make_templates/mfc

There are two resources so that you can have different templates customized to pick up the appropriate class libraries. The value for the resource can contain environment variables which will be expanded by /bin/sh.

If Sun WorkShop Visual cannot find the file specified, it will fall back to the template specified in the Sun WorkShop Visual resource file, using the makefileTemplate application resource. To make a change to the template apply globally to all new Makefiles, edit the resource file. For details, see the Configuration chapter.

Sun WorkShop Visual refers to the template only when you generate a new Makefile. To change templates in an existing Makefile, edit the file by hand as described in the previous section, or delete the file and start over.

Dependency Information

Sun WorkShop Visual does not generate dependency information into the Makefile. The default template includes a "depend" target that can be invoked using:

make depend

This operation invokes the makedepend utility, which scans the existing makefile and appends a standard dependency list. Use man makedepend for more information.

The $VISUROOT/make_templates directory contains the template used to generate makefiles. This template contains the include directories and libraries you will need for each system configuration supported. The configuration chosen from the Makefile Options dialog determines which "block" in the template to use. There is always a default configuration for your system so you do not necessarily need to select one from this dialog. See The List of Makefile Types for more details. A typical "block" is shown below: