• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2##
3##  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4##
5##  Use of this source code is governed by a BSD-style license
6##  that can be found in the LICENSE file in the root of the source
7##  tree. An additional intellectual property rights grant can be found
8##  in the file PATENTS.  All contributing project authors may
9##  be found in the AUTHORS file in the root of the source tree.
10##
11
12
13self=$0
14self_basename=${self##*/}
15self_dirname=$(dirname "$0")
16EOL=$'\n'
17
18show_help() {
19    cat <<EOF
20Usage: ${self_basename} --name=projname [options] file1 [file2 ...]
21
22This script generates a Visual Studio project file from a list of source
23code files.
24
25Options:
26    --help                      Print this message
27    --exe                       Generate a project for building an Application
28    --lib                       Generate a project for creating a static library
29    --static-crt                Use the static C runtime (/MT)
30    --target=isa-os-cc          Target specifier (required)
31    --out=filename              Write output to a file [stdout]
32    --name=project_name         Name of the project (required)
33    --proj-guid=GUID            GUID to use for the project
34    --module-def=filename       File containing export definitions (for DLLs)
35    --ver=version               Version (7,8,9) of visual studio to generate for
36    --src-path-bare=dir         Path to root of source tree
37    -Ipath/to/include           Additional include directories
38    -DFLAG[=value]              Preprocessor macros to define
39    -Lpath/to/lib               Additional library search paths
40    -llibname                   Library to link against
41EOF
42    exit 1
43}
44
45die() {
46    echo "${self_basename}: $@" >&2
47    exit 1
48}
49
50die_unknown(){
51    echo "Unknown option \"$1\"." >&2
52    echo "See ${self_basename} --help for available options." >&2
53    exit 1
54}
55
56generate_uuid() {
57    local hex="0123456789ABCDEF"
58    local i
59    local uuid=""
60    local j
61    #93995380-89BD-4b04-88EB-625FBE52EBFB
62    for ((i=0; i<32; i++)); do
63        (( j = $RANDOM % 16 ))
64        uuid="${uuid}${hex:$j:1}"
65    done
66    echo "${uuid:0:8}-${uuid:8:4}-${uuid:12:4}-${uuid:16:4}-${uuid:20:12}"
67}
68
69indent1="    "
70indent=""
71indent_push() {
72    indent="${indent}${indent1}"
73}
74indent_pop() {
75    indent="${indent%${indent1}}"
76}
77
78tag_attributes() {
79    for opt in "$@"; do
80        optval="${opt#*=}"
81        [ -n "${optval}" ] ||
82            die "Missing attribute value in '$opt' while generating $tag tag"
83        echo "${indent}${opt%%=*}=\"${optval}\""
84    done
85}
86
87open_tag() {
88    local tag=$1
89    shift
90    if [ $# -ne 0 ]; then
91        echo "${indent}<${tag}"
92        indent_push
93        tag_attributes "$@"
94        echo "${indent}>"
95    else
96        echo "${indent}<${tag}>"
97        indent_push
98    fi
99}
100
101close_tag() {
102    local tag=$1
103    indent_pop
104    echo "${indent}</${tag}>"
105}
106
107tag() {
108    local tag=$1
109    shift
110    if [ $# -ne 0 ]; then
111        echo "${indent}<${tag}"
112        indent_push
113        tag_attributes "$@"
114        indent_pop
115        echo "${indent}/>"
116    else
117        echo "${indent}<${tag}/>"
118    fi
119}
120
121generate_filter() {
122    local var=$1
123    local name=$2
124    local pats=$3
125    local file_list_sz
126    local i
127    local f
128    local saveIFS="$IFS"
129    local pack
130    echo "generating filter '$name' from ${#file_list[@]} files" >&2
131    IFS=*
132
133    open_tag Filter \
134        Name=$name \
135        Filter=$pats \
136        UniqueIdentifier=`generate_uuid` \
137
138    file_list_sz=${#file_list[@]}
139    for i in ${!file_list[@]}; do
140        f=${file_list[i]}
141        for pat in ${pats//;/$IFS}; do
142            if [ "${f##*.}" == "$pat" ]; then
143                unset file_list[i]
144
145                open_tag File RelativePath="./$f"
146                if [ "$pat" == "asm" ] && $asm_use_custom_step; then
147                    for plat in "${platforms[@]}"; do
148                        for cfg in Debug Release; do
149                            open_tag FileConfiguration \
150                                Name="${cfg}|${plat}" \
151
152                            tag Tool \
153                                Name="VCCustomBuildTool" \
154                                Description="Assembling \$(InputFileName)" \
155                                CommandLine="$(eval echo \$asm_${cfg}_cmdline)" \
156                                Outputs="\$(InputName).obj" \
157
158                            close_tag FileConfiguration
159                        done
160                    done
161                fi
162
163                close_tag File
164
165                break
166            fi
167        done
168    done
169
170    close_tag Filter
171    IFS="$saveIFS"
172}
173
174# Process command line
175unset target
176for opt in "$@"; do
177    optval="${opt#*=}"
178    case "$opt" in
179        --help|-h) show_help
180        ;;
181        --target=*) target="${optval}"
182        ;;
183        --out=*) outfile="$optval"
184        ;;
185        --name=*) name="${optval}"
186        ;;
187        --proj-guid=*) guid="${optval}"
188        ;;
189        --module-def=*) link_opts="${link_opts} ModuleDefinitionFile=${optval}"
190        ;;
191        --exe) proj_kind="exe"
192        ;;
193        --lib) proj_kind="lib"
194        ;;
195        --src-path-bare=*) src_path_bare="$optval"
196        ;;
197        --static-crt) use_static_runtime=true
198        ;;
199        --ver=*)
200            vs_ver="$optval"
201            case "$optval" in
202                [789])
203                ;;
204                *) die Unrecognized Visual Studio Version in $opt
205                ;;
206            esac
207        ;;
208        -I*)
209            opt="${opt%/}"
210            incs="${incs}${incs:+;}&quot;${opt##-I}&quot;"
211            yasmincs="${yasmincs} ${opt}"
212        ;;
213        -D*) defines="${defines}${defines:+;}${opt##-D}"
214        ;;
215        -L*) # fudge . to $(OutDir)
216            if [ "${opt##-L}" == "." ]; then
217                libdirs="${libdirs}${libdirs:+;}&quot;\$(OutDir)&quot;"
218            else
219                 # Also try directories for this platform/configuration
220                 libdirs="${libdirs}${libdirs:+;}&quot;${opt##-L}&quot;"
221                 libdirs="${libdirs}${libdirs:+;}&quot;${opt##-L}/\$(PlatformName)/\$(ConfigurationName)&quot;"
222                 libdirs="${libdirs}${libdirs:+;}&quot;${opt##-L}/\$(PlatformName)&quot;"
223            fi
224        ;;
225        -l*) libs="${libs}${libs:+ }${opt##-l}.lib"
226        ;;
227        -*) die_unknown $opt
228        ;;
229        *)
230            file_list[${#file_list[@]}]="$opt"
231            case "$opt" in
232                 *.asm) uses_asm=true
233                 ;;
234            esac
235        ;;
236    esac
237done
238outfile=${outfile:-/dev/stdout}
239guid=${guid:-`generate_uuid`}
240asm_use_custom_step=false
241uses_asm=${uses_asm:-false}
242case "${vs_ver:-8}" in
243    7) vs_ver_id="7.10"
244       asm_use_custom_step=$uses_asm
245    ;;
246    8) vs_ver_id="8.00"
247    ;;
248    9) vs_ver_id="9.00"
249    ;;
250esac
251
252[ -n "$name" ] || die "Project name (--name) must be specified!"
253[ -n "$target" ] || die "Target (--target) must be specified!"
254
255if ${use_static_runtime:-false}; then
256    release_runtime=0
257    debug_runtime=1
258    lib_sfx=mt
259else
260    release_runtime=2
261    debug_runtime=3
262    lib_sfx=md
263fi
264
265# Calculate debug lib names: If a lib ends in ${lib_sfx}.lib, then rename
266# it to ${lib_sfx}d.lib. This precludes linking to release libs from a
267# debug exe, so this may need to be refactored later.
268for lib in ${libs}; do
269    if [ "$lib" != "${lib%${lib_sfx}.lib}" ]; then
270        lib=${lib%.lib}d.lib
271    fi
272    debug_libs="${debug_libs}${debug_libs:+ }${lib}"
273done
274
275
276# List Keyword for this target
277case "$target" in
278    x86*) keyword="ManagedCProj"
279    ;;
280    *) die "Unsupported target $target!"
281esac
282
283# List of all platforms supported for this target
284case "$target" in
285    x86_64*)
286        platforms[0]="x64"
287    ;;
288    x86*)
289        platforms[0]="Win32"
290        # these are only used by vs7
291        asm_Debug_cmdline="yasm -Xvc -g cv8 -f \$(PlatformName) ${yasmincs} &quot;\$(InputPath)&quot;"
292        asm_Release_cmdline="yasm -Xvc -f \$(PlatformName) ${yasmincs} &quot;\$(InputPath)&quot;"
293    ;;
294    *) die "Unsupported target $target!"
295    ;;
296esac
297
298generate_vcproj() {
299    case "$proj_kind" in
300        exe) vs_ConfigurationType=1
301        ;;
302        *)   vs_ConfigurationType=4
303        ;;
304    esac
305
306    echo "<?xml version=\"1.0\" encoding=\"Windows-1252\"?>"
307    open_tag VisualStudioProject \
308        ProjectType="Visual C++" \
309        Version="${vs_ver_id}" \
310        Name="${name}" \
311        ProjectGUID="{${guid}}" \
312        RootNamespace="${name}" \
313        Keyword="${keyword}" \
314
315    open_tag Platforms
316    for plat in "${platforms[@]}"; do
317        tag Platform Name="$plat"
318    done
319    close_tag Platforms
320
321    open_tag ToolFiles
322    case "$target" in
323        x86*) $uses_asm && tag ToolFile RelativePath="$self_dirname/../x86-msvs/yasm.rules"
324        ;;
325    esac
326    close_tag ToolFiles
327
328    open_tag Configurations
329    for plat in "${platforms[@]}"; do
330        plat_no_ws=`echo $plat | sed 's/[^A-Za-z0-9_]/_/g'`
331        open_tag Configuration \
332            Name="Debug|$plat" \
333            OutputDirectory="\$(SolutionDir)$plat_no_ws/\$(ConfigurationName)" \
334            IntermediateDirectory="$plat_no_ws/\$(ConfigurationName)/${name}" \
335            ConfigurationType="$vs_ConfigurationType" \
336            CharacterSet="1" \
337
338        case "$target" in
339            x86*)
340                case "$name" in
341                    obj_int_extract)
342                        tag Tool \
343                            Name="VCCLCompilerTool" \
344                            Optimization="0" \
345                            AdditionalIncludeDirectories="$incs" \
346                            PreprocessorDefinitions="WIN32;DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE" \
347                            RuntimeLibrary="$debug_runtime" \
348                            WarningLevel="3" \
349                            Detect64BitPortabilityProblems="true" \
350                            DebugInformationFormat="1" \
351                    ;;
352                    vpx)
353                        tag Tool \
354                            Name="VCPreBuildEventTool" \
355                            CommandLine="call obj_int_extract.bat $src_path_bare" \
356
357                        tag Tool \
358                            Name="VCCLCompilerTool" \
359                            Optimization="0" \
360                            AdditionalIncludeDirectories="$incs" \
361                            PreprocessorDefinitions="WIN32;_DEBUG;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE;$defines" \
362                            RuntimeLibrary="$debug_runtime" \
363                            UsePrecompiledHeader="0" \
364                            WarningLevel="3" \
365                            DebugInformationFormat="1" \
366                            Detect64BitPortabilityProblems="true" \
367
368                        $uses_asm && tag Tool Name="YASM"  IncludePaths="$incs" Debug="1"
369                    ;;
370                    *)
371                        tag Tool \
372                            Name="VCCLCompilerTool" \
373                            Optimization="0" \
374                            AdditionalIncludeDirectories="$incs" \
375                            PreprocessorDefinitions="WIN32;_DEBUG;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE;$defines" \
376                            RuntimeLibrary="$debug_runtime" \
377                            UsePrecompiledHeader="0" \
378                            WarningLevel="3" \
379                            DebugInformationFormat="1" \
380                            Detect64BitPortabilityProblems="true" \
381
382                        $uses_asm && tag Tool Name="YASM"  IncludePaths="$incs" Debug="1"
383                    ;;
384                esac
385            ;;
386        esac
387
388        case "$proj_kind" in
389            exe)
390                case "$target" in
391                    x86*)
392                        case "$name" in
393                            obj_int_extract)
394                                tag Tool \
395                                    Name="VCLinkerTool" \
396                                    OutputFile="${name}.exe" \
397                                    GenerateDebugInformation="true" \
398                            ;;
399                            *)
400                                tag Tool \
401                                    Name="VCLinkerTool" \
402                                    AdditionalDependencies="$debug_libs \$(NoInherit)" \
403                                    AdditionalLibraryDirectories="$libdirs" \
404                                    GenerateDebugInformation="true" \
405                                    ProgramDatabaseFile="\$(OutDir)/${name}.pdb" \
406                            ;;
407                        esac
408                    ;;
409                 esac
410            ;;
411            lib)
412                case "$target" in
413                    x86*)
414                        tag Tool \
415                            Name="VCLibrarianTool" \
416                            OutputFile="\$(OutDir)/${name}${lib_sfx}d.lib" \
417
418                    ;;
419                esac
420            ;;
421            dll)
422                tag Tool \
423                    Name="VCLinkerTool" \
424                    AdditionalDependencies="\$(NoInherit)" \
425                    LinkIncremental="2" \
426                    GenerateDebugInformation="true" \
427                    AssemblyDebug="1" \
428                    TargetMachine="1" \
429                    $link_opts \
430
431            ;;
432        esac
433
434        close_tag Configuration
435
436        open_tag Configuration \
437            Name="Release|$plat" \
438            OutputDirectory="\$(SolutionDir)$plat_no_ws/\$(ConfigurationName)" \
439            IntermediateDirectory="$plat_no_ws/\$(ConfigurationName)/${name}" \
440            ConfigurationType="$vs_ConfigurationType" \
441            CharacterSet="1" \
442            WholeProgramOptimization="0" \
443
444        case "$target" in
445            x86*)
446                case "$name" in
447                    obj_int_extract)
448                        tag Tool \
449                            Name="VCCLCompilerTool" \
450                            AdditionalIncludeDirectories="$incs" \
451                            PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE" \
452                            RuntimeLibrary="$release_runtime" \
453                            UsePrecompiledHeader="0" \
454                            WarningLevel="3" \
455                            Detect64BitPortabilityProblems="true" \
456                            DebugInformationFormat="0" \
457                    ;;
458                    vpx)
459                        tag Tool \
460                            Name="VCPreBuildEventTool" \
461                            CommandLine="call obj_int_extract.bat $src_path_bare" \
462
463                        tag Tool \
464                            Name="VCCLCompilerTool" \
465                            AdditionalIncludeDirectories="$incs" \
466                            PreprocessorDefinitions="WIN32;NDEBUG;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE;$defines" \
467                            RuntimeLibrary="$release_runtime" \
468                            UsePrecompiledHeader="0" \
469                            WarningLevel="3" \
470                            DebugInformationFormat="0" \
471                            Detect64BitPortabilityProblems="true" \
472
473                        $uses_asm && tag Tool Name="YASM"  IncludePaths="$incs"
474                    ;;
475                    *)
476                        tag Tool \
477                            Name="VCCLCompilerTool" \
478                            AdditionalIncludeDirectories="$incs" \
479                            PreprocessorDefinitions="WIN32;NDEBUG;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE;$defines" \
480                            RuntimeLibrary="$release_runtime" \
481                            UsePrecompiledHeader="0" \
482                            WarningLevel="3" \
483                            DebugInformationFormat="0" \
484                            Detect64BitPortabilityProblems="true" \
485
486                        $uses_asm && tag Tool Name="YASM"  IncludePaths="$incs"
487                    ;;
488                esac
489            ;;
490        esac
491
492        case "$proj_kind" in
493            exe)
494                case "$target" in
495                    x86*)
496                        case "$name" in
497                            obj_int_extract)
498                                tag Tool \
499                                    Name="VCLinkerTool" \
500                                    OutputFile="${name}.exe" \
501                                    GenerateDebugInformation="true" \
502                            ;;
503                            *)
504                                tag Tool \
505                                    Name="VCLinkerTool" \
506                                    AdditionalDependencies="$libs \$(NoInherit)" \
507                                    AdditionalLibraryDirectories="$libdirs" \
508
509                            ;;
510                        esac
511                    ;;
512                 esac
513            ;;
514            lib)
515                case "$target" in
516                    x86*)
517                        tag Tool \
518                            Name="VCLibrarianTool" \
519                            OutputFile="\$(OutDir)/${name}${lib_sfx}.lib" \
520
521                    ;;
522                esac
523            ;;
524            dll) # note differences to debug version: LinkIncremental, AssemblyDebug
525                tag Tool \
526                    Name="VCLinkerTool" \
527                    AdditionalDependencies="\$(NoInherit)" \
528                    LinkIncremental="1" \
529                    GenerateDebugInformation="true" \
530                    TargetMachine="1" \
531                    $link_opts \
532
533            ;;
534        esac
535
536        close_tag Configuration
537    done
538    close_tag Configurations
539
540    open_tag Files
541    generate_filter srcs   "Source Files"   "c;def;odl;idl;hpj;bat;asm;asmx"
542    generate_filter hdrs   "Header Files"   "h;hm;inl;inc;xsd"
543    generate_filter resrcs "Resource Files" "rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
544    generate_filter resrcs "Build Files"    "mk"
545    close_tag Files
546
547    tag       Globals
548    close_tag VisualStudioProject
549
550    # This must be done from within the {} subshell
551    echo "Ignored files list (${#file_list[@]} items) is:" >&2
552    for f in "${file_list[@]}"; do
553        echo "    $f" >&2
554    done
555}
556
557generate_vcproj |
558    sed  -e '/"/s;\([^ "]\)/;\1\\;g' > ${outfile}
559
560exit
561<!--
562TODO: Add any files not captured by filters.
563                <File
564                        RelativePath=".\ReadMe.txt"
565                        >
566                </File>
567-->
568