makedepend equivalent for use with nmake?

守給你的承諾、 提交于 2019-11-29 06:20:54

You can use the /showIncludes switch to cl.exe to list the headers #included by your source files. Nested includes are indicated by indentation with spaces. You can also turn on syntax-checking mode with the /Zs switch, to increase speed and avoid creation of .obj files.

If you have Perl and a version of uniq (e.g. from GnuWin32) installed, the following one-liner will dump the list of unique headers used by myfile.cpp:

cl /Zs /showIncludes /EHsc myfile.cpp | perl -ne "print if s/^Note: including file: *//" | sort | uniq

It should not be too difficult to pipe this through another script that creates the relevant nmake rules.

I assume you use NMAKE to build project like me. I'm in need of makedepend-like tool in Windows, too. So, I use MinGW to generate header dependencies. First create Makefile to generate dependencies, which I named it Makedepends, like this:

    OBJS=... list object files in your project...

    all: Makefile.deps

    Makefile.deps: $(OBJS:.obj=.dep)
        cat $+ > $@
        rm -f $+

    %.dep: %.cpp
        g++ -MM -MG -MT$(@:.dep=.obj) -o$@ $<

In your Makefile that will be used by NMAKE, add this line at bottom:

    !INCLUDE Makefile.deps

When you want to create dependencies, run GMAKE like this:

    make -fMakedepends

And then, you can build your project with NMAKE as usual:

    nmake

PS: Sorry for poor language, I'm suck at writing. -_-

.SUFFIXES:
.SUFFIXES: .c

all: x.obj

# Sample batch-mode rule which both compiles and produces .dep files suitable for NMAKE.
# Also works around the fact that CL.EXE spits diagnostics in stdout instead of stderr.
# This is equivalent to -MD -MP -MT$@ -MF$(@R).dep in GNU Make + GCC.
CCOMMAND = $(CC) $(CFLAGS) /c $<
.c.obj::
!IF "$(MAKEFLAGS:S=)" == "$(MAKEFLAGS)"
    @echo " $(CCOMMAND)"
!ENDIF
    @$(COMSPEC) /E:ON /V:ON /C "$(CCOMMAND) /showIncludes & echo Exit: !ERRORLEVEL!" | \
        $(COMSPEC) /E:ON /V:ON /C "for /f "tokens=1,* delims=]" %%A in ('find /v /n ""') do \
        @if %%~xB == .c (set _=%%~nB&rem.>!_!.dep&echo %%B) else for /f "tokens=1,2,3,*" %%C in ("%%B") do \
        @if %%C == Note: ((echo !_!.obj: "%%F"&echo "%%F":) >> !_!.dep) \
        else if %%C == Exit: (exit /b %%D) else echo %%B"

# Include the generated deps.
!IF ![(for %i in (*.dep) do @echo !INCLUDE %i) >Build.tmp]
!       INCLUDE Build.tmp
!       IF ![del Build.tmp]
!       ENDIF
!ENDIF
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!