Home > Software Development > Converting Visual Studio solutions to Makefiles

Converting Visual Studio solutions to Makefiles

A python script to convert Microsoft Visual Studio 2005 solution files (*.sln) and the associated project files (*.vcproj) into a set of Makefile’s.

The class Sln2Make does all the work, parses the sln and vcproj files and generates a main Makefile and one Makefile.ag for every project in the solution. The Makefiles.ag generated will have two targets, ‘all’ and ‘clean’, the script also includes the dependent libraries if they are properly defined in the solution. To use it, just instantiate the class with the following parameters:

Sln2Make(slnpath, exlist, dirrepl, librepl)

where:

  • slnpath is the path to the solution file
  • exlist an optional list of files to exclude, optionally you can pass the content of an alternative make file using this syntax: [[dest, make_all_replacement, make_clean_replacement], …]
  • dirrepl a list of directory replacement rules
  • librepl library name replacement rules (when win library name doesn’t match OS’s library name)

example:

exlist = [('../extsrc/zlib/projects/visualc6/Makefile.ag',
'	$(MAKE) -C $(dir_root)extsrc/zlib -f Makefile libz.a\n',
'	$(MAKE) -C $(dir_root)extsrc/zlib -f Makefile clean\n')]
dirrepl = [['winnt', 'linux']]
librepl = [['zlib', 'z'], ['nspr', 'nspr4 -lplc4 -lplds4']]
#
Sln2Make("../winnt/test.sln", exlist, dirrepl, librepl)

To correct the path case, I’ve used a script published by Moshe Zadka here.
Download sln2make here. Use this code at your own risk, it is released under BSD license.

  1. July 9th, 2008 at 21:28 | #1

    Guy, I would going to present you a BIG bottle or beer, but seems that I so stuped, and it’s not working well for me. :(

    Finaly, I undestand how to run it properly, but seems my MAKE not like generated Makefile.
    Here is a generated Makefile:
    ===================
    # sln2make autogenerated Makefile.ag
    dir_root=../
    dir_tmp=tmp
    dir_obj=$(dir_tmp)/obj
    dir_lib=lib

    all:
    $(MAKE) -C consprog -f Makefile.ag

    clean:
    $(MAKE) -C consprog -f Makefile.ag clean
    rm $(dir_lib)/Release/* bin/Release/*
    ===================

    and here is a error mesage from make:
    ===================
    $ make
    Makefile:8: *** missing separator. Stop.

    ===================

    Can you say me, where I’m wrong, and how I can fully enjoy this coolest script for the moment?

  2. July 9th, 2008 at 21:32 | #2

    OOps, here a solution:
    > The problem is that that white space at the start of line 8
    > *must* be a TAB character, not ordinary spaces.
    >

    THANKS!

  3. kperlotto
    February 27th, 2009 at 11:36 | #3

    Dear fvicente,
    Thanks for the tool, I am in great need of it but I have no python skills, and cannot resolve this error. Can you help? THANKS !

    python sln2make.py
    Traceback (most recent call last):
    File “sln2make.py”, line 449, in ?
    Sln2Make(“ug104.sln”, exlist, dirrepl, librepl)
    File “sln2make.py”, line 153, in __init__
    vcprojContent = self._getFileContent(casedir.correct(proj.path))
    File “h:\myapps\ug104\pathcorrect.py”, line 14, in correct
    ret = self._correct(file)
    File “h:\myapps\ug104\pathcorrect.py”, line 22, in _correct
    dir = self.correct(dir)
    File “h:\myapps\ug104\pathcorrect.py”, line 14, in correct
    ret = self._correct(file)
    File “h:\myapps\ug104\pathcorrect.py”, line 22, in _correct
    dir = self.correct(dir)
    File “h:\myapps\ug104\pathcorrect.py”, line 14, in correct
    ret = self._correct(file)
    File “h:\myapps\ug104\pathcorrect.py”, line 22, in _correct
    dir = self.correct(dir)
    File “h:\myapps\ug104\pathcorrect.py”, line 14, in correct
    ret = self._correct(file)
    File “h:\myapps\ug104\pathcorrect.py”, line 22, in _correct
    dir = self.correct(dir)
    File “h:\myapps\ug104\pathcorrect.py”, line 14, in correct
    ret = self._correct(file)
    File “h:\myapps\ug104\pathcorrect.py”, line 28, in _correct
    file = files[map(self.hash, files).index(self.hash(file))]
    ValueError: list.index(x): x not in list

  4. fvicente
    March 3rd, 2009 at 23:17 | #4

    Hi kperlotto,
    this seems to happen when the PathCorrent can not find the file, so make sure that ”ug104.sln” exist in the same directory of sln2make remember that you can use a relative path e.g. “..\\xxx\\ug104.sln”. Best regards.

  5. Craig A
    May 21st, 2009 at 16:20 | #5

    I have the same problem as
    kperlotto on February 27th, 2009 at 11:36 am

    No matter what I do, it fails in the same fashion

  6. Craig A
    May 21st, 2009 at 16:20 | #6

    C:\cwtrunk\tools\Sln2Make>c:\Python25\python sln2make.py ..\..\cwtrunk\sw\TCU-Co
    mm\COM-provider\TcuCommProvider.sln
    Traceback (most recent call last):
    File “sln2make.py”, line 450, in
    Sln2Make(sys.argv[1], exlist, dirrepl, librepl)
    File “sln2make.py”, line 72, in __init__
    file = open(casedir.correct(slnFile),”r”)
    File “c:\cwtrunk\tools\Sln2Make\pathcorrect.py”, line 14, in correct
    ret = self._correct(file)
    File “c:\cwtrunk\tools\Sln2Make\pathcorrect.py”, line 22, in _correct
    dir = self.correct(dir)
    File “c:\cwtrunk\tools\Sln2Make\pathcorrect.py”, line 14, in correct
    ret = self._correct(file)
    File “c:\cwtrunk\tools\Sln2Make\pathcorrect.py”, line 22, in _correct
    dir = self.correct(dir)
    File “c:\cwtrunk\tools\Sln2Make\pathcorrect.py”, line 14, in correct
    ret = self._correct(file)
    File “c:\cwtrunk\tools\Sln2Make\pathcorrect.py”, line 22, in _correct
    dir = self.correct(dir)
    File “c:\cwtrunk\tools\Sln2Make\pathcorrect.py”, line 14, in correct
    ret = self._correct(file)
    File “c:\cwtrunk\tools\Sln2Make\pathcorrect.py”, line 22, in _correct
    dir = self.correct(dir)
    File “c:\cwtrunk\tools\Sln2Make\pathcorrect.py”, line 14, in correct
    ret = self._correct(file)
    File “c:\cwtrunk\tools\Sln2Make\pathcorrect.py”, line 28, in _correct
    file = files[map(self.hash, files).index(self.hash(file))]
    ValueError: list.index(x): x not in list

    C:\cwtrunk\tools\Sln2Make>

  7. fvicente
    May 23rd, 2009 at 01:04 | #7

    Hi Craig,
    Here is the problem: I didn’t make the script to use as a command line tool… It may sound silly but the reason is that I’m using it in another script in my project.
    So instead of passing the path to your .sln as an argument, please open the sln2make.py and modify the last line of the script with the corresponding path.

    Fernando.

  8. Adam
    January 20th, 2010 at 18:58 | #8

    How do I run the script after putting in the path on the last line?

  9. fvicente
    February 5th, 2010 at 20:42 | #9

    Hi Adam,
    I presume you’re trying to run the script in a Windows environment, so after you modified the last line of the just enter into a command line box and type “python.exe sln2make.py” (without quoting). Of course you must have python interpreter installed.

  1. No trackbacks yet.