source文件和makefile文件編寫



http://blog.csdn.net/kaylc/article/details/6263296


一. makefile (沒有擴展名,它名字就叫makefile),內容如下:
  !INCLUDE $(NTMAKEENV)/makefile.def
  WDM程序使用的所有makefile都這樣寫,我們只需寫一個,編譯時把它拷貝到工作目錄下就行了
  二. sources文件就需要我們根據不同的場合修改了,不過基本模板如下:

  TARGETNAME=驅動程序名 //(不含擴展名)

  TARGETPATH=obj // 固定不變

  TARGETTYPE=DRIVER // 固定不變(表明,連接成*.sys文件)

  DRIVERTYPE=WDM // 爲 Win32 Driver Model 驅動

  INCLUDES=$(BASEDIR)/inc/ddk;$(BASEDIR)/inc // 源程序可能使用的DDK頭文件所在的目錄,多個目錄用";" 隔開,多個文件用 '空格' 隔開

  其中"$(BASEDIR)"指DDK當前的安裝目錄,【windows通過添加系統環境變量即可】例如當前DDK安裝在D:上,則$(BASEDIR) 就是 "D:/DDK",所以上面的INCLUDES可以翻譯成D:/DDK/inc/ddk; D:/DDK/inc

  三. 注意:

  1. 編譯時必須保證 makefile,sources和源程序在同一目錄下

  2. 編寫sources文件時,其中的"="兩邊不能有空格

  3. 工程的工作目錄的絕對路徑中不能出現空格,而且表面上看來DDK好像是完成的編譯,實際上它什麼都沒做!

官方說明:

The sources file
This is where build gets most of its directives. Here are some of the more important ones — and note that the first four are required:

TARGETNAME: This required directive specifies the name of the intended binary. For example, you might give TARGETNAME=MyDriver (build will append .sys to the driver’s name).
TARGETTYPE: This required directive dictates the type of binary to be built. Most common are:
TARGETYPE=DRIVER for a plain-vanilla driver (a .sys file).
TARGETTYPE=EXPORT_DRIVER for a driver that exports entry points as a DLL does. (The resulting binary is a .sys file.) A kernel DLL is such a creature. See Tim Roberts’ article about kernel-mode DLLs for more information about this option.
TARGETTYPE=DRIVER_LIBRARY (or TARGETTYPE=LIBRARY) for a .lib binary that will be link-edited into an executable. This feature is typically used when there are two or more source directories (perhaps because each is owned by a different developer). The sources file in one directory could have TARGETTYPE=DRIVER (for a .sys file); the sources in the second (or third, etc.) directory would give TARGETTYPE=DRIVER_LIBRARY. A complete sources file has an example.
TARGETTYPE=PROGRAM for a user-space program (an .exe file), for example, a program to call the driver.
TARGETTYPE=DYNLINK for a user-space DLL, which an .exe file might use.
In case you’re wondering how names like ntoskrnl.exe and hal.dll are generated for kernel modules, the answer is that build has special-case logic. Don’t try to create kernel modules of your own with an extension of .exe or .dll.

TARGETPATH: This required directive says where to put the binary.
Note: The specific form TARGETPATH=lib$(BUILD_ALT_DIR) is often employed, and the resulting directory path is one beneath the one where the sources file is located; for a WinXP checked-build target, that path would be libchk_wxp_x86/i386. The path name comes from concatenating “lib” with the evaluated environmental variable BUILD_ALT_DIR (see environmental variables) and with “/i386,” which designates the platform type. If you’re using this form to produce a .lib file, you will specify the file’s path in TARGETPATH differently from the way you will specify the path for that same file in building an executable incorporating the .lib file (see TARGETLIBS).

SOURCES: This required directive names the source files. For example,
SOURCES= /
BozoStuff.cpp /
miniport.c /
passthru.c /
passthru.rc /
protocol.c /
UtilRtns.c

Notice the mixture of .c and .cpp files: The compiler’s default action is to apply C or C++ language rules according to file type.

Note: If a directive is to have more than one entry, you may specify all on a single line, or you may put them on several lines, but then you must end each line with a backslash and no following characters, not even blanks. This restriction applies to directives in sources files and in dirs files.

A further note: In SOURCES, names can take the form filename.ext, ./ filename.ext and ../ filename.ext, but not ../../ filename.ext and not a fully qualified name like c:/dir/ filename.ext. That is another quirk of build.

INCLUDES: This specifies the directories for any header files (other than the target OS’s standard directories). For example,
INCLUDES= /
./inc /
C:/JA.pgm/src/C/Drivers/JADriver/inc

Note: Unlike SOURCES, INCLUDES allows forms like ../../Dir and fully qualified directory paths. Do not, however, specify paths with imbedded blanks. Instead, use the 8.3 name (you can discover the 8.3 names of directories and files below a directory by issuing the command dir /x <directory> in a command window).

C_DEFINES: A directive to affect compilation. Its value is passed to the C compiler (and to the MIDL compiler). See compiler parameters for some possibilities.
USER_C_FLAGS: Another directive to affect compilation. Its value is passed to the C/C++ compiler. For example,
USER_C_FLAGS=$(USER_C_FLAGS) /FAsc

would pick up any value defined in the environmental variable USER_C_FLAGS in the command window and would append /FAsc to that value. See compiler parameters for other possibilities.

MSC_OPTIMIZATION: This overrides the default compiler optimization of build. A checked build will have /Od /Oi, to prevent optimization.
MSC_WARNING_LEVEL: This controls the compiler’s warning level. The default is /W3, but it is better practice to set it to /W3 /Wx so that warnings are considered errors.
TARGETLIBS: This names one or more libraries needed by link-edit to resolve references (APIs, sections of code, variables). Many OS libraries like ntoskrnl.lib are automatically searched, but you may need to specify some. If you were building an NDIS intermediate driver, for example, you would specify TARGETLIBS=$(DDK_LIB_PATH) /ndis.lib. And you would of course name any libraries of your own.
Note: TARGETLIBS does not have the quite same special handling that TARGETPATH does. If you created a .lib file with a path of TARGETPATH=lib$(BUILD_ALT_DIR), in the sources file for the .sys file you would designate the .lib file’s path more fully, by lib$(BUILD_ALT_DIR)/*/ (asterisk means implicit platform type).

To make the point above clear, let’s suppose you have this for the .lib file:

TARGETNAME=DriverPart2
TARGETTYPE=DRIVER_LIBRARY # output is a .lib file
TARGETPATH=lib$(BUILD_ALT_DIR) # where to put the .lib file.

For the .sys file, you would have:

TARGETNAME=MyDriver
TARGETTYPE=DRIVER # output is a .sys file
TARGETLIBS=lib$(BUILD_ALT_DIR)/*/DriverPart2 # where to find the .lib file.
TARGETPATH=lib$(BUILD_ALT_DIR) # where to put the .sys file.

LINKER_FLAGS: This is information supplied to the linker as a parameter. If you want the linker to produce a map, for example, specify "-MAP" as the value of this parameter.
Browser information: You specify this if you intend to use the Visual Studio browser to find definitions of APIs, symbols and variables (see browsable defintions); in a large project, especially in one to which you are new, this capability is a very handy one. You need two directives to get browser information: the first to make build create it, and the second to indicate the name of the browser database file.
BROWSER_INFO=1
BROWSERFILE=$(TARGETNAME).bsc -n

You can use conditional logic. For example:
!if !defined(DDK_TARGET_OS) || "$(DDK_TARGET_OS)"=="Win2K"
# The driver is for Win2K. Build with NDIS 4.0
C_DEFINES=$(C_DEFINES) -DNDIS40_MINIPORT=1
C_DEFINES=$(C_DEFINES) -DNDIS40=1
!else
# The driver is for WinXP or Win2003, so build# with NDIS 5.1.
C_DEFINES=$(C_DEFINES) -DNDIS51_MINIPORT=1
C_DEFINES=$(C_DEFINES) -DNDIS51=1
!endif

Putting it all together, you might have this in a complete sources file:

TARGETNAME=MyPassthru
TARGETYPE=DRIVER
TARGETPATH=lib$(BUILD_ALT_DIR)
TARGETLIBS=$(DDK_LIB_PATH)/ndis.lib

SOURCES= /
BozoStuff.cpp /
miniport.c /
passthru.c /
passthru.rc /
protocol.c /
UtilRtns.c

INCLUDES= /
./inc /
C:/JA.pgm/src/C/Drivers/JADriver/inc

USER_C_FLAGS=$(USER_C_FLAGS) /FAsc

!if !defined(DDK_TARGET_OS) || "$(DDK_TARGET_OS)"=="Win2K"
C_DEFINES=$(C_DEFINES) -DNDIS40_MINIPORT=1
C_DEFINES=$(C_DEFINES) -DNDIS40=1
!else
C_DEFINES=$(C_DEFINES) -DNDIS51_MINIPORT=1
C_DEFINES=$(C_DEFINES) -DNDIS51=1
!endif

BROWSER_INFO=1
BROWSERFILE=$(TARGETNAME).bsc -n

You can find a full list of build directives in the DDK documentation under the heading “Build Utility Macros.”

Note: After you make a change in a sources file, it is usually necessary to rebuild completely to pick up the changes.  

發佈了114 篇原創文章 · 獲贊 25 · 訪問量 42萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章