~ubuntu-branches/ubuntu/wily/opencollada/wily-proposed

« back to all changes in this revision

Viewing changes to scripts/vcproj2cmake.rb

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2015-05-14 17:23:27 UTC
  • Revision ID: package-import@ubuntu.com-20150514172327-f862u8envms01fra
Tags: upstream-0.1.0~20140703.ddf8f47+dfsg1
ImportĀ upstreamĀ versionĀ 0.1.0~20140703.ddf8f47+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/ruby
 
2
 
 
3
# Given a Visual Studio project, create a CMakeLists.txt file to serve
 
4
# as a starting point. Supports simple DLL/Static/Executable projects,
 
5
# but custom build steps and build events are ignored.
 
6
 
 
7
# Author: Jesper Eskilson 
 
8
# Email: jesper [at] eskilson [dot] se
 
9
# Version: 1.0
 
10
 
 
11
require 'fileutils'
 
12
require 'rexml/document'
 
13
include FileUtils::Verbose
 
14
 
 
15
configuration="Debug|Win32"
 
16
 
 
17
# Usage: vcproj2cmake.rb <input.vcproj> [<output CMakeLists.txt>]
 
18
 
 
19
filename = ARGV.shift
 
20
output_file = ARGV.shift or output_file = File.join(File.dirname(filename), "CMakeLists.txt")
 
21
 
 
22
# Change \ to /, and remove leading ./
 
23
def normalize(p)
 
24
  felems = p.gsub("\\", "/").split("/")
 
25
  felems.shift if felems[0] == "."
 
26
  File.join(felems)
 
27
end
 
28
 
 
29
if File.exists?(output_file)
 
30
  mv(output_file, output_file + ".backup")
 
31
end
 
32
 
 
33
File.open(output_file, "w") { |out|
 
34
  File.open(filename) { |io|
 
35
    doc = REXML::Document.new io
 
36
    
 
37
    doc.elements.each("VisualStudioProject") { |project|
 
38
      name = project.attributes["Name"]
 
39
      
 
40
      out.puts "project( #{name} )"
 
41
 
 
42
      out.puts
 
43
      out.puts( "set(SOURCES " )
 
44
      
 
45
      project.elements.each("Files//File") { |file|
 
46
        
 
47
        f = normalize(file.attributes["RelativePath"])
 
48
 
 
49
        # Ignore header files
 
50
        next if f =~ /\.(h|H|lex|y|ico|bmp|txt)$/
 
51
 
 
52
        # Ignore files which have the ExcludedFromBuild attribute set to TRUE
 
53
        excluded_from_build = false
 
54
        file.elements.each("FileConfiguration") { |file_config|
 
55
          if file_config.attributes["ExcludedFromBuild"] == "TRUE"
 
56
            excluded_from_build = true
 
57
          end
 
58
        }
 
59
        
 
60
        # Ignore files with custom build steps
 
61
        included_in_build = true
 
62
        file.elements.each("FileConfiguration/Tool") { |tool|
 
63
          if tool.attributes["Name"] == "VCCustomBuildTool"
 
64
            included_in_build = false
 
65
          end
 
66
        }
 
67
        
 
68
        if not excluded_from_build and included_in_build
 
69
          out.puts "  " + f
 
70
        end
 
71
 
 
72
      }
 
73
 
 
74
      out.puts ")"
 
75
 
 
76
      project.elements.each("Configurations/Configuration[@Name=\"#{configuration}\"]") { |config|
 
77
        config_name = config.attributes["Name"].split("|")[0]
 
78
        
 
79
        # 0 == no MFC
 
80
        # 1 == static MFC
 
81
        # 2 == shared MFC
 
82
        use_of_mfc = config.attributes["UseOfMFC"].to_i
 
83
        if use_of_mfc > 0
 
84
          out.puts
 
85
          out.puts "set(CMAKE_MFC_FLAG #{use_of_mfc})"
 
86
        end
 
87
 
 
88
 
 
89
        config.elements.each('Tool[@Name="VCCLCompilerTool"]') { |compiler|
 
90
 
 
91
          out.puts
 
92
          out.puts "include_directories( \n"
 
93
 
 
94
          include_dirs = compiler.attributes["AdditionalIncludeDirectories"].split(/[,;]/).sort.each { |s|
 
95
            incpath = normalize(s).strip
 
96
            if incpath["Vc7/atlmfc/src/mfc"]
 
97
              out.puts "  ${MFC_INCLUDE}"
 
98
            else
 
99
              out.puts "  " + incpath
 
100
            end
 
101
          }
 
102
          out.puts ")"
 
103
          
 
104
          out.puts
 
105
          out.puts "add_definitions( \n"
 
106
          compiler.attributes["PreprocessorDefinitions"].split(";").sort.each { |s|
 
107
            out.puts "  -D" + s.strip
 
108
          }
 
109
 
 
110
          if use_of_mfc == 2
 
111
            out.puts "  -D_AFXEXT"
 
112
            out.puts "  -D_AFXDLL"
 
113
          end
 
114
          
 
115
          out.puts ")"
 
116
 
 
117
        }
 
118
 
 
119
        config_type = config.attributes["ConfigurationType"].to_i
 
120
        
 
121
        out.puts
 
122
 
 
123
        if config_type == 1    # Executable
 
124
          out.puts "add_executable( #{name} WIN32 ${SOURCES} )"
 
125
        elsif config_type == 2    # DLL
 
126
          out.puts "add_library( #{name} SHARED ${SOURCES} )"
 
127
        elsif config_type == 4    # Static
 
128
          out.puts "add_library( #{name} STATIC ${SOURCES} )"
 
129
        else
 
130
          $stderr.puts "Project type #{config_type} not supported."
 
131
          exit 1
 
132
        end
 
133
 
 
134
        out.puts
 
135
        config.elements.each('Tool[@Name="VCLinkerTool"]') { |linker|
 
136
          deps = linker.attributes["AdditionalDependencies"]
 
137
          if deps and deps.length > 0
 
138
            out.print "target_link_libraries( #{name} "
 
139
 
 
140
            deps.split.each  { |lib|
 
141
              lib = lib.gsub(/\\/, "/")
 
142
              out.print File.basename(lib, ".lib") + " "
 
143
            }
 
144
 
 
145
            out.puts ")"
 
146
          end
 
147
        }
 
148
      }
 
149
    }
 
150
  }
 
151
}
 
152
 
 
153
puts "Wrote #{output_file}"
 
154