forked from nberserk/common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcproj2cmake.ps1
142 lines (121 loc) · 4.04 KB
/
vcproj2cmake.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# history
# 2010.12.1 remove CMakeList_t.txt by here string
#
# Warning: supports VS2005 only. not tested any other version
#
# usage example
# powershell .\vcproj2cmake.ps1 c:\xxx.vcproj -conf "Debug"
param(
[string] $file=$(throw 'vcproj file is required'),
[string] $conf='SHP Debug|Win32'
)
$Template = @'
# -*- cmake -*-
# written by Darren Ha([email protected])
# replace <target> with binary name. e.g. mbase or FBase
SET (this_target <target>)
#PROJECT(${this_target})
## section: include directory
INCLUDE_DIRECTORIES(
<include>
)
## section: source files
# Add your source files here (one file per line), please SORT in alphabetical order for future maintenance
SET (${this_target}_SOURCE_FILES
<src>
)
## section: header files
# Add your header files here(one file per line), please SORT in alphabetical order for future maintenance!
SET(${this_target}_HEADER_FILES
<header>
)
SET_SOURCE_FILES_PROPERTIES(${this_target}_HEADER_FILES
PROPERTIES HEADER_FILE_ONLY TRUE)
LIST(APPEND ${this_target}_SOURCE_FILES ${${this_target}_HEADER_FILES})
## section: add definitions
# add prefix -D. example> -DSHP
# - DO NOT add the following definitions(already defined in ${OSP_DEFINITIONS}:
# -DSHP, -DWIN32, -D_WINDOWS, -D_DEBUG, -D_USRDLL, -D_CRT_SECURE_NO_DEPRECATE
ADD_DEFINITIONS(
<def>
)
## section: add target
ADD_LIBRARY (${this_target} SHARED ${${this_target}_SOURCE_FILES} )
## section: add dependency
# dependency determines overall build order.
ADD_DEPENDENCIES(${this_target} <lib>)
## section: set link libraries
TARGET_LINK_LIBRARIES( ${this_target}
<lib>)
'@
function ConvToCMake($vcprojFile, $prjConf){
$files = @()
[xml]$proj = gc $vcprojFile
$proj.selectNodes("/VisualStudioProject/Files//File") |
% {
$_.RelativePath
if ($_.FileConfiguration -and ($_.FileConfiguration.GetType().name -eq 'Object[]')){
#$_.RelativePath
$exclude = $false
foreach ($f in $_.FileConfiguration){
#$f
if ($f.Name -eq $prjConf -and $f.ExcludedFromBuild -eq 'true'){
$exclude = $true
Write-Host skipping $_.RelativePath
break
}
}
if (!$exclude){
#Write-Host adding $_.RelativePath
$file = $_.RelativePath -replace '\\','/'; $files+=$file
}
#$_.SelectSingleNode("FileConfiguration[@Name=""$slnConf""][@excludedFromBuild=yes]")
#$_.FileConfiguration
#$destNode
#Read-Host
}elseif ($_.FileConfiguration ){
if ($_.FileConfiguration.Name -eq $prjConf -and $_.FileConfiguration.ExcludedFromBuild -eq 'true'){
Write-Host skipping $_.RelativePath
}else{
$file = $_.RelativePath -replace '\\','/'; $files+=$file
}
}else{
#Write-Host adding $_.RelativePath
$file = $_.RelativePath -replace '\\','/'; $files+=$file
}
}
$src=@()
$header=@()
foreach ($f in $files)
{
if ($f -match '\.h'){
$header += $f
}else{
$src += $f
}
}
$header = $header | sort
$src = $src | sort
# header & src files
$text = $Template -replace '<src>',[string]::Join("`n`t", $src)
$text = $text -replace '<header>',[string]::Join("`n`t", $header)
$text = $text -replace '<target>',$proj.VisualStudioProject.name
# link lib
$target = $proj.SelectSingleNode("/VisualStudioProject/Configurations/Configuration[@Name=""$prjConf""]/Tool[10]")
$raw = $target.AdditionalDependencies
$raw = $raw -replace '\.lib',''
$text = $text -replace '<lib>',$raw
# definition
$target = $proj.SelectSingleNode("/VisualStudioProject/Configurations/Configuration[@Name=""$prjConf""]/Tool[6]")
$target.PreprocessorDefinitions
$text = $text -replace '<def>',$target.PreprocessorDefinitions
#include
$raw = $target.AdditionalIncludeDirectories
$raw = $raw -replace '\\','/'
$raw = $raw -replace ';',"`n`t"
$text = $text -replace '<include>',$raw
#save
$path = Split-Path $vcprojFile
$text | Out-File (Join-Path $path CMakeLists.txt) -Encoding ASCII
}
ConvToCMake $file "$conf|Win32"