Iceboy makefile learning notes

Source: Internet
Author: User
I learned how to use makefile this evening. The tool used is Microsoft's nmake.

All of the following content has been found out, and the official documents have not been available yet.

1. Notes

#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# You may only use this code if you agree to the terms of the Windows research kernel source code License Agreement (see license.txt ).
# If you do not agree to the terms, do not use the code.
#

2. Define and use variables

The format is as follows:
Variable name = content
$ (Variable name)

The variable content can be multiple rows. Add the backslash '\' after each row.
The variable can also be defined in the nmake parameter in the format of 'nmake variable name = content '.
You can also use environment variables, such as $ (systemroot), $ (LIB), and $ (PATH ).
You can also use some built-in variables, such as $ (CC), $ (AS), $ (make), and $ (makedir)
Interestingly, CC, As, and make are the names of GNU.
In addition, the variable names are case sensitive, and the environment variable names are all capitalized.

Example:
Subargs =/$ (makeflags) ntos = $ (makedir) pub = $ (makedir) \... \ public

3. Pre-processing commands
Format:
! If expression
...
! Else if defined (variable name)
...
! Else
...
! Endif

! Error Message

! Include another MAKEFILE file

Note that &, |, And! Can Be Used in expressions ,! And other logical operators.

The defined operator indicates whether a variable is defined.

Example:
! If defined (x86 )&&! Defined (amd64)
Subargs = $ (subargs) targ = i386 topobj = $ (makedir) \ build \ obji386
Targ = i386
Lmachine = x86

! Else if! Defined (x86) & defined (amd64)
Subargs = $ (subargs) targ = amd64 topobj = $ (makedir) \ build \ objamd64
Targ = amd64
Lmachine = amd64

! Else
! Error usage: nmake (x86 = | amd64 =) [clean]
! Endif

4. Define a task
Multiple tasks can be defined in a makefile in the following format:
Default:
____ # Commands here
Task_a:
____ # Commands here
Task_ B:
____ # Commands here

Default indicates the default task.

Using nmake [task name], nmake will execute different command sequences. for example, if nmake task_ B is input, the command sequence below task_ B is executed. if the task name is not specified, the default task is executed.

Note that the task name cannot seem to be a single character long, and I don't know why.

In addition, multiple lines of commands can be followed by each task, but at least one space or tab must be placed before each line; otherwise, an error may occur. Because Baidu will eat spaces, I will replace them with underscores here.

You can also define dependencies between tasks in the following format:
Task_c: task_a task_ B
____ # Private commands here

Nmake will execute task_c after task _ A and task _ B are executed.
Note that the dependencies and task names must be written in the same line, separated by spaces.

As long as possible, nmake will automatically find an appropriate execution sequence based on the dependency of the task.
If loop dependency occurs, nmake reports the following error:

AA: bb
BB: AA

Nmake: Fatal error u1071: Cycle in dependency tree for target 'A'
Stop.

You can also define multiple tasks at a time:
Task_d task_e task_f:
____ @ Echo $ (@ r)

The preceding two rows define task _ d, task _ E, and task _ f, and print the task name using echo.
The @ in front of ECHO is the prefix of the batchcompute command line. This usage starts from DOS and does not output this command line.
@ R parameter indicates the prefix of the task name, which is equal to the task name here.
The prefix of a task name is 'abcd. efgh '.

5. Replace variable content
For example, we have the following two statements.
Var_a = aa_bb_cc_dd_ee
Var_ B = head _ $ (var_a: _ = _ Split _) _ tail

Let's guess what var_ B is?

With the previous knowledge, you may wish to write a makefile and try it:

Var_a = aa_bb_cc_dd_ee
Var_ B = head _ $ (var_a: _ = _ Split _) _ tail

Default:
____ @ Echo $ (var_ B)

6. A common technique

OBJ = OBJ $ (targ)

Modules = RTL config ex ob se mm ke ps io \ iomgr Io cache LPC dbgk raw fstub fsrtl WMI perf init
Buildtargets = $ (modules: =. Build). Build
Cleantargets = $ (modules: =. Clean). Clean

Default: checktoolpath $ (buildtargets) Kernel

Checktoolpath:
____ Checktoolarch $ (lmachine)

Kernel:
____ CD $ (makedir) \ build
________ @ $ (Make) $ (subargs) module = build
____ @ CD $ (makedir)

$ (Buildtargets ):
____ CD $ (makedir) \ $ (@ r) \ build
________ @ $ (Make) $ (subargs) module =$ (@ r)
____ @ CD $ (makedir)

Clean: $ (cleantargets) clean0

Clean0:
____-Del $ (makedir) \ build \ $ (OBJ) \ ** $ (makedir) \ build \ exe \ **/Q

$ (Cleantargets ):
____ CD $ (makedir) \ $ (@ r) \ build
________ @ $ (Make) $ (subargs) module = $ (@ r) clean
____ @ CD $ (makedir)

A brief explanation.

Modules = RTL config ex ob se mm ke ps io \ iomgr Io cache LPC dbgk raw fstub fsrtl WMI perf init
Buildtargets = $ (modules: =. Build). Build
Replace all spaces in the modules with '. build', and add'. build '.
So buildtargets = RTL. Build config. Build (... omitted...) Init. Build

$ (Buildtargets ):
____ CD $ (makedir) \ $ (@ r) \ build
________ @ $ (Make) $ (subargs) module =$ (@ r)
____ @ CD $ (makedir)

This defines RTL. Build, config. Build, and other tasks. Let's take a look at RTL. Build:
RTL. Build:
____ CD $ (makedir) \ RTL \ build
________ @ $ (Make) $ (subargs) module = RTL
____ @ CD $ (makedir)

Default: checktoolpath $ (buildtargets) Kernel
In this way, nmake will first checktoolpath, then build each module in sequence, and finally execute the kernel task.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.