gen_msvs_vcxproj.sh 18.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/bin/bash
##
##  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
##
##  Use of this source code is governed by a BSD-style license
##  that can be found in the LICENSE file in the root of the source
##  tree. An additional intellectual property rights grant can be found
##  in the file PATENTS.  All contributing project authors may
##  be found in the AUTHORS file in the root of the source tree.
##


self=$0
self_basename=${self##*/}
self_dirname=$(dirname "$0")
EOL=$'\n'
17 18 19 20 21 22
if [ "$(uname -o 2>/dev/null)" = "Cygwin" ] \
   && cygpath --help >/dev/null 2>&1; then
    FIXPATH='cygpath -m'
else
    FIXPATH='echo'
fi
23 24 25 26 27 28 29 30 31 32 33 34 35 36

show_help() {
    cat <<EOF
Usage: ${self_basename} --name=projname [options] file1 [file2 ...]

This script generates a Visual Studio project file from a list of source
code files.

Options:
    --help                      Print this message
    --exe                       Generate a project for building an Application
    --lib                       Generate a project for creating a static library
    --dll                       Generate a project for creating a dll
    --static-crt                Use the static C runtime (/MT)
37
    --enable-werror             Treat warnings as errors (/WX)
38 39 40 41 42
    --target=isa-os-cc          Target specifier (required)
    --out=filename              Write output to a file [stdout]
    --name=project_name         Name of the project (required)
    --proj-guid=GUID            GUID to use for the project
    --module-def=filename       File containing export definitions (for DLLs)
Yaowu Xu's avatar
Yaowu Xu committed
43
    --ver=version               Version (10,11,12) of visual studio to generate for
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    --src-path-bare=dir         Path to root of source tree
    -Ipath/to/include           Additional include directories
    -DFLAG[=value]              Preprocessor macros to define
    -Lpath/to/lib               Additional library search paths
    -llibname                   Library to link against
EOF
    exit 1
}

die() {
    echo "${self_basename}: $@" >&2
    exit 1
}

die_unknown(){
    echo "Unknown option \"$1\"." >&2
    echo "See ${self_basename} --help for available options." >&2
    exit 1
}

64 65 66 67
fix_path() {
    $FIXPATH "$1"
}

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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
generate_uuid() {
    local hex="0123456789ABCDEF"
    local i
    local uuid=""
    local j
    #93995380-89BD-4b04-88EB-625FBE52EBFB
    for ((i=0; i<32; i++)); do
        (( j = $RANDOM % 16 ))
        uuid="${uuid}${hex:$j:1}"
    done
    echo "${uuid:0:8}-${uuid:8:4}-${uuid:12:4}-${uuid:16:4}-${uuid:20:12}"
}

indent1="    "
indent=""
indent_push() {
    indent="${indent}${indent1}"
}
indent_pop() {
    indent="${indent%${indent1}}"
}

tag_attributes() {
    for opt in "$@"; do
        optval="${opt#*=}"
        [ -n "${optval}" ] ||
            die "Missing attribute value in '$opt' while generating $tag tag"
        echo "${indent}${opt%%=*}=\"${optval}\""
    done
}

open_tag() {
    local tag=$1
    shift
    if [ $# -ne 0 ]; then
        echo "${indent}<${tag}"
        indent_push
        tag_attributes "$@"
        echo "${indent}>"
    else
        echo "${indent}<${tag}>"
        indent_push
    fi
}

close_tag() {
    local tag=$1
    indent_pop
    echo "${indent}</${tag}>"
}

tag() {
    local tag=$1
    shift
    if [ $# -ne 0 ]; then
        echo "${indent}<${tag}"
        indent_push
        tag_attributes "$@"
        indent_pop
        echo "${indent}/>"
    else
        echo "${indent}<${tag}/>"
    fi
}

tag_content() {
    local tag=$1
    local content=$2
    shift
    shift
    if [ $# -ne 0 ]; then
        echo "${indent}<${tag}"
        indent_push
        tag_attributes "$@"
        echo "${indent}>${content}</${tag}>"
        indent_pop
    else
        echo "${indent}<${tag}>${content}</${tag}>"
    fi
}

generate_filter() {
    local name=$1
    local pats=$2
    local file_list_sz
    local i
    local f
    local saveIFS="$IFS"
    local pack
    echo "generating filter '$name' from ${#file_list[@]} files" >&2
    IFS=*

    file_list_sz=${#file_list[@]}
    for i in ${!file_list[@]}; do
        f=${file_list[i]}
        for pat in ${pats//;/$IFS}; do
            if [ "${f##*.}" == "$pat" ]; then
                unset file_list[i]

167
                objf=$(echo ${f%.*}.obj | sed -e 's/^[\./]\+//g' -e 's,[:/],_,g')
168

169
                if ([ "$pat" == "asm" ] || [ "$pat" == "s" ]) && $asm_use_custom_step; then
170 171 172 173
                    # Avoid object file name collisions, i.e. vpx_config.c and
                    # vpx_config.asm produce the same object file without
                    # this additional suffix.
                    objf=${objf%.obj}_asm.obj
174
                    open_tag CustomBuild \
175
                        Include="$f"
176 177 178 179 180 181 182 183 184 185 186
                    for plat in "${platforms[@]}"; do
                        for cfg in Debug Release; do
                            tag_content Message "Assembling %(Filename)%(Extension)" \
                                Condition="'\$(Configuration)|\$(Platform)'=='$cfg|$plat'"
                            tag_content Command "$(eval echo \$asm_${cfg}_cmdline) -o \$(IntDir)$objf" \
                                Condition="'\$(Configuration)|\$(Platform)'=='$cfg|$plat'"
                            tag_content Outputs "\$(IntDir)$objf" \
                                Condition="'\$(Configuration)|\$(Platform)'=='$cfg|$plat'"
                        done
                    done
                    close_tag CustomBuild
187 188
                elif [ "$pat" == "c" ] || \
                     [ "$pat" == "cc" ] || [ "$pat" == "cpp" ]; then
189
                    open_tag ClCompile \
190
                        Include="$f"
191 192
                    # Separate file names with Condition?
                    tag_content ObjectFileName "\$(IntDir)$objf"
193 194 195 196
                    # Check for AVX and turn it on to avoid warnings.
                    if [[ $f =~ avx.?\.c$ ]]; then
                        tag_content AdditionalOptions "/arch:AVX"
                    fi
197 198 199
                    close_tag ClCompile
                elif [ "$pat" == "h" ] ; then
                    tag ClInclude \
200
                        Include="$f"
201 202 203 204 205 206 207 208 209
                elif [ "$pat" == "vcxproj" ] ; then
                    open_tag ProjectReference \
                        Include="$f"
                    depguid=`grep ProjectGuid "$f" | sed 's,.*<.*>\(.*\)</.*>.*,\1,'`
                    tag_content Project "$depguid"
                    tag_content ReferenceOutputAssembly false
                    close_tag ProjectReference
                else
                    tag None \
210
                        Include="$f"
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
                fi

                break
            fi
        done
    done

    IFS="$saveIFS"
}

# Process command line
unset target
for opt in "$@"; do
    optval="${opt#*=}"
    case "$opt" in
        --help|-h) show_help
        ;;
        --target=*) target="${optval}"
        ;;
        --out=*) outfile="$optval"
        ;;
        --name=*) name="${optval}"
        ;;
        --proj-guid=*) guid="${optval}"
        ;;
        --module-def=*) module_def="${optval}"
        ;;
        --exe) proj_kind="exe"
        ;;
        --dll) proj_kind="dll"
        ;;
        --lib) proj_kind="lib"
        ;;
244
        --src-path-bare=*) src_path_bare=$(fix_path "$optval")
245 246 247
        ;;
        --static-crt) use_static_runtime=true
        ;;
248 249
        --enable-werror) werror=true
        ;;
250 251 252
        --ver=*)
            vs_ver="$optval"
            case "$optval" in
Yaowu Xu's avatar
Yaowu Xu committed
253
                10|11|12)
254 255 256 257 258 259 260
                ;;
                *) die Unrecognized Visual Studio Version in $opt
                ;;
            esac
        ;;
        -I*)
            opt="${opt%/}"
261 262 263 264
            opt=${opt##-I}
            opt=$(fix_path "$opt")
            incs="${incs}${incs:+;}&quot;${opt}&quot;"
            yasmincs="${yasmincs} -I${opt}"
265 266 267 268 269
        ;;
        -D*) defines="${defines}${defines:+;}${opt##-D}"
        ;;
        -L*) # fudge . to $(OutDir)
            if [ "${opt##-L}" == "." ]; then
270
                libdirs="${libdirs}${libdirs:+;}&quot;\$(OutDir)&quot;"
271 272
            else
                 # Also try directories for this platform/configuration
273 274 275 276 277
                 opt=${opt##-L}
                 opt=$(fix_path "$opt")
                 libdirs="${libdirs}${libdirs:+;}&quot;${opt}&quot;"
                 libdirs="${libdirs}${libdirs:+;}&quot;${opt}/\$(PlatformName)/\$(Configuration)&quot;"
                 libdirs="${libdirs}${libdirs:+;}&quot;${opt}/\$(PlatformName)&quot;"
278 279 280 281 282 283 284
            fi
        ;;
        -l*) libs="${libs}${libs:+ }${opt##-l}.lib"
        ;;
        -*) die_unknown $opt
        ;;
        *)
285
            file_list[${#file_list[@]}]="$(fix_path $opt)"
286
            case "$opt" in
287
                 *.asm|*.s) uses_asm=true
288 289 290 291 292 293 294 295 296 297
                 ;;
            esac
        ;;
    esac
done
outfile=${outfile:-/dev/stdout}
guid=${guid:-`generate_uuid`}
asm_use_custom_step=false
uses_asm=${uses_asm:-false}
case "${vs_ver:-11}" in
Yaowu Xu's avatar
Yaowu Xu committed
298
    10|11|12)
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
       asm_use_custom_step=$uses_asm
    ;;
esac

[ -n "$name" ] || die "Project name (--name) must be specified!"
[ -n "$target" ] || die "Target (--target) must be specified!"

if ${use_static_runtime:-false}; then
    release_runtime=MultiThreaded
    debug_runtime=MultiThreadedDebug
    lib_sfx=mt
else
    release_runtime=MultiThreadedDLL
    debug_runtime=MultiThreadedDebugDLL
    lib_sfx=md
fi

# Calculate debug lib names: If a lib ends in ${lib_sfx}.lib, then rename
# it to ${lib_sfx}d.lib. This precludes linking to release libs from a
# debug exe, so this may need to be refactored later.
for lib in ${libs}; do
    if [ "$lib" != "${lib%${lib_sfx}.lib}" ]; then
        lib=${lib%.lib}d.lib
    fi
    debug_libs="${debug_libs}${debug_libs:+ }${lib}"
done
debug_libs=${debug_libs// /;}
libs=${libs// /;}


# List of all platforms supported for this target
case "$target" in
    x86_64*)
        platforms[0]="x64"
        asm_Debug_cmdline="yasm -Xvc -g cv8 -f \$(PlatformName) ${yasmincs} &quot;%(FullPath)&quot;"
        asm_Release_cmdline="yasm -Xvc -f \$(PlatformName) ${yasmincs} &quot;%(FullPath)&quot;"
    ;;
    x86*)
        platforms[0]="Win32"
        asm_Debug_cmdline="yasm -Xvc -g cv8 -f \$(PlatformName) ${yasmincs} &quot;%(FullPath)&quot;"
        asm_Release_cmdline="yasm -Xvc -f \$(PlatformName) ${yasmincs} &quot;%(FullPath)&quot;"
    ;;
341 342 343 344 345 346 347 348 349 350 351
    arm*)
        asm_Debug_cmdline="armasm -nologo &quot;%(FullPath)&quot;"
        asm_Release_cmdline="armasm -nologo &quot;%(FullPath)&quot;"
        if [ "$name" = "obj_int_extract" ]; then
            # We don't want to build this tool for the target architecture,
            # but for an architecture we can run locally during the build.
            platforms[0]="Win32"
        else
            platforms[0]="ARM"
        fi
    ;;
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
    *) die "Unsupported target $target!"
    ;;
esac

generate_vcxproj() {
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    open_tag Project \
        DefaultTargets="Build" \
        ToolsVersion="4.0" \
        xmlns="http://schemas.microsoft.com/developer/msbuild/2003" \

    open_tag ItemGroup \
        Label="ProjectConfigurations"
    for plat in "${platforms[@]}"; do
        for config in Debug Release; do
            open_tag ProjectConfiguration \
                Include="$config|$plat"
            tag_content Configuration $config
            tag_content Platform $plat
            close_tag ProjectConfiguration
        done
    done
    close_tag ItemGroup

    open_tag PropertyGroup \
        Label="Globals"
        tag_content ProjectGuid "{${guid}}"
        tag_content RootNamespace ${name}
        tag_content Keyword ManagedCProj
    close_tag PropertyGroup

    tag Import \
        Project="\$(VCTargetsPath)\\Microsoft.Cpp.Default.props"

    for plat in "${platforms[@]}"; do
        for config in Release Debug; do
            open_tag PropertyGroup \
                Condition="'\$(Configuration)|\$(Platform)'=='$config|$plat'" \
                Label="Configuration"
            if [ "$proj_kind" = "exe" ]; then
                tag_content ConfigurationType Application
            elif [ "$proj_kind" = "dll" ]; then
                tag_content ConfigurationType DynamicLibrary
            else
                tag_content ConfigurationType StaticLibrary
            fi
398
            if [ "$vs_ver" = "11" ]; then
399 400 401 402 403 404 405 406 407 408 409 410
                if [ "$plat" = "ARM" ]; then
                    # Setting the wp80 toolchain automatically sets the
                    # WINAPI_FAMILY define, which is required for building
                    # code for arm with the windows headers. Alternatively,
                    # one could add AppContainerApplication=true in the Globals
                    # section and add PrecompiledHeader=NotUsing and
                    # CompileAsWinRT=false in ClCompile and SubSystem=Console
                    # in Link.
                    tag_content PlatformToolset v110_wp80
                else
                    tag_content PlatformToolset v110
                fi
411
            fi
Yaowu Xu's avatar
Yaowu Xu committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425
            if [ "$vs_ver" = "12" ]; then
                if [ "$plat" = "ARM" ]; then
                    # Setting the wp80 toolchain automatically sets the
                    # WINAPI_FAMILY define, which is required for building
                    # code for arm with the windows headers. Alternatively,
                    # one could add AppContainerApplication=true in the Globals
                    # section and add PrecompiledHeader=NotUsing and
                    # CompileAsWinRT=false in ClCompile and SubSystem=Console
                    # in Link.
                    tag_content PlatformToolset v120_wp80
                else
                    tag_content PlatformToolset v120
                fi
            fi
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
            tag_content CharacterSet Unicode
            if [ "$config" = "Release" ]; then
                tag_content WholeProgramOptimization true
            fi
            close_tag PropertyGroup
        done
    done

    tag Import \
        Project="\$(VCTargetsPath)\\Microsoft.Cpp.props"

    open_tag ImportGroup \
        Label="PropertySheets"
        tag Import \
            Project="\$(UserRootDir)\\Microsoft.Cpp.\$(Platform).user.props" \
            Condition="exists('\$(UserRootDir)\\Microsoft.Cpp.\$(Platform).user.props')" \
            Label="LocalAppDataPlatform"
    close_tag ImportGroup

    tag PropertyGroup \
        Label="UserMacros"

    for plat in "${platforms[@]}"; do
        plat_no_ws=`echo $plat | sed 's/[^A-Za-z0-9_]/_/g'`
        for config in Debug Release; do
            open_tag PropertyGroup \
                Condition="'\$(Configuration)|\$(Platform)'=='$config|$plat'"
            tag_content OutDir "\$(SolutionDir)$plat_no_ws\\\$(Configuration)\\"
            tag_content IntDir "$plat_no_ws\\\$(Configuration)\\${name}\\"
455 456 457 458 459 460 461 462
            if [ "$proj_kind" == "lib" ]; then
              if [ "$config" == "Debug" ]; then
                config_suffix=d
              else
                config_suffix=""
              fi
              tag_content TargetName "${name}${lib_sfx}${config_suffix}"
            fi
463 464 465 466 467 468 469 470
            close_tag PropertyGroup
        done
    done

    for plat in "${platforms[@]}"; do
        for config in Debug Release; do
            open_tag ItemDefinitionGroup \
                Condition="'\$(Configuration)|\$(Platform)'=='$config|$plat'"
471 472 473 474 475
            if [ "$name" == "vpx" ]; then
                hostplat=$plat
                if [ "$hostplat" == "ARM" ]; then
                    hostplat=Win32
                fi
476
                open_tag PreBuildEvent
477
                tag_content Command "call obj_int_extract.bat $src_path_bare $hostplat\\\$(Configuration)"
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
                close_tag PreBuildEvent
            fi
            open_tag ClCompile
            if [ "$config" = "Debug" ]; then
                opt=Disabled
                runtime=$debug_runtime
                curlibs=$debug_libs
                case "$name" in
                obj_int_extract)
                    debug=DEBUG
                    ;;
                *)
                    debug=_DEBUG
                    ;;
                esac
            else
                opt=MaxSpeed
                runtime=$release_runtime
                curlibs=$libs
                tag_content FavorSizeOrSpeed Speed
                debug=NDEBUG
            fi
            case "$name" in
            obj_int_extract)
                extradefines=";_CONSOLE"
                ;;
            *)
                extradefines=";$defines"
                ;;
            esac
            tag_content Optimization $opt
            tag_content AdditionalIncludeDirectories "$incs;%(AdditionalIncludeDirectories)"
            tag_content PreprocessorDefinitions "WIN32;$debug;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE$extradefines;%(PreprocessorDefinitions)"
            tag_content RuntimeLibrary $runtime
            tag_content WarningLevel Level3
513 514 515
            if ${werror:-false}; then
                tag_content TreatWarningAsError true
            fi
516 517 518 519
            close_tag ClCompile
            case "$proj_kind" in
            exe)
                open_tag Link
520
                if [ "$name" != "obj_int_extract" ]; then
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
                    tag_content AdditionalDependencies "$curlibs"
                    tag_content AdditionalLibraryDirectories "$libdirs;%(AdditionalLibraryDirectories)"
                fi
                tag_content GenerateDebugInformation true
                close_tag Link
                ;;
            dll)
                open_tag Link
                tag_content GenerateDebugInformation true
                tag_content ModuleDefinitionFile $module_def
                close_tag Link
                ;;
            lib)
                ;;
            esac
            close_tag ItemDefinitionGroup
        done

    done

    open_tag ItemGroup
542
    generate_filter "Source Files"   "c;cc;cpp;def;odl;idl;hpj;bat;asm;asmx;s"
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
    close_tag ItemGroup
    open_tag ItemGroup
    generate_filter "Header Files"   "h;hm;inl;inc;xsd"
    close_tag ItemGroup
    open_tag ItemGroup
    generate_filter "Build Files"    "mk"
    close_tag ItemGroup
    open_tag ItemGroup
    generate_filter "References"     "vcxproj"
    close_tag ItemGroup

    tag Import \
        Project="\$(VCTargetsPath)\\Microsoft.Cpp.targets"

    open_tag ImportGroup \
        Label="ExtensionTargets"
    close_tag ImportGroup

    close_tag Project

    # This must be done from within the {} subshell
    echo "Ignored files list (${#file_list[@]} items) is:" >&2
    for f in "${file_list[@]}"; do
        echo "    $f" >&2
    done
}

# This regexp doesn't catch most of the strings in the vcxproj format,
# since they're like <tag>path</tag> instead of <tag attr="path" />
# as previously. It still seems to work ok despite this.
generate_vcxproj |
    sed  -e '/"/s;\([^ "]\)/;\1\\;g' |
    sed  -e '/xmlns/s;\\;/;g' > ${outfile}

exit