<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest

from conans.client.build.compiler_flags import architecture_flags, libcxx_flags, pic_flags, CompilerFlags, \
    adjust_path, sysroot_flags, format_defines, format_include_paths, format_library_paths, format_libraries, \
    build_type_flags


class CompilerFlagsTest(unittest.TestCase):
    def test_merge(self):
        flags1 = CompilerFlags(cflags=['--foo'], cxxflags=['--bar'])
        flags2 = CompilerFlags(cflags=['--fizz'], ldflags=['--buzz'])
        flags1.append(additional_flags=flags2)
        self.assertEquals(flags1.cflags, ['--foo', '--fizz'])
        self.assertEquals(flags1.cxxflags, ['--bar'])
        self.assertEquals(flags1.ldflags, ['--buzz'])
        self.assertEquals(flags1.defines, [])

    def test_arch_flags(self):
        arch_flags = architecture_flags(arch='x86', compiler='gcc')
        self.assertEquals(arch_flags.cflags, ['-m32'])
        self.assertEquals(arch_flags.cxxflags, ['-m32'])
        self.assertEquals(arch_flags.ldflags, ['-m32'])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = architecture_flags(arch='x86', compiler='clang')
        self.assertEquals(arch_flags.cflags, ['-m32'])
        self.assertEquals(arch_flags.cxxflags, ['-m32'])
        self.assertEquals(arch_flags.ldflags, ['-m32'])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = architecture_flags(arch='sparc', compiler='sun-cc')
        self.assertEquals(arch_flags.cflags, ['-m32'])
        self.assertEquals(arch_flags.cxxflags, ['-m32'])
        self.assertEquals(arch_flags.ldflags, ['-m32'])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = architecture_flags(arch='x86_64', compiler='gcc')
        self.assertEquals(arch_flags.cflags, ['-m64'])
        self.assertEquals(arch_flags.cxxflags, ['-m64'])
        self.assertEquals(arch_flags.ldflags, ['-m64'])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = architecture_flags(arch='x86_64', compiler='clang')
        self.assertEquals(arch_flags.cflags, ['-m64'])
        self.assertEquals(arch_flags.cxxflags, ['-m64'])
        self.assertEquals(arch_flags.ldflags, ['-m64'])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = architecture_flags(arch='sparcv9', compiler='sun-cc')
        self.assertEquals(arch_flags.cflags, ['-m64'])
        self.assertEquals(arch_flags.cxxflags, ['-m64'])
        self.assertEquals(arch_flags.ldflags, ['-m64'])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = architecture_flags(arch='armv7', compiler='gcc')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, [])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = architecture_flags(arch='x86', compiler='Visual Studio')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, [])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

    def test_libcxx_flags(self):
        arch_flags = libcxx_flags(compiler='gcc', libcxx='libstdc++')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, [])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, ['_GLIBCXX_USE_CXX11_ABI=0'])

        arch_flags = libcxx_flags(compiler='gcc', libcxx='libstdc++11')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, [])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, ['_GLIBCXX_USE_CXX11_ABI=1'])

        arch_flags = libcxx_flags(compiler='clang', libcxx='libc++')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, ['-stdlib=libc++'])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = libcxx_flags(compiler='clang', libcxx='libstdc++')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, ['-stdlib=libstdc++'])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, ['_GLIBCXX_USE_CXX11_ABI=0'])

        arch_flags = libcxx_flags(compiler='clang', libcxx='libstdc++11')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, ['-stdlib=libstdc++'])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, ['_GLIBCXX_USE_CXX11_ABI=1'])

        arch_flags = libcxx_flags(compiler='apple-clang', libcxx='libc++')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, ['-stdlib=libc++'])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = libcxx_flags(compiler='Visual Studio')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, [])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = libcxx_flags(compiler='sun-cc', libcxx='libCstd')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, ['-library=Cstd'])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = libcxx_flags(compiler='sun-cc', libcxx='libstdcxx')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, ['-library=stdcxx4'])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = libcxx_flags(compiler='sun-cc', libcxx='libstlport')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, ['-library=stlport4'])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = libcxx_flags(compiler='sun-cc', libcxx='libstdc++')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, ['-library=stdcpp'])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

    def test_pic_flags(self):
        flags = pic_flags()
        self.assertEquals(flags.cflags, ['-fPIC'])
        self.assertEquals(flags.cxxflags, ['-fPIC'])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, [])

        flags = pic_flags(compiler='gcc')
        self.assertEquals(flags.cflags, ['-fPIC'])
        self.assertEquals(flags.cxxflags, ['-fPIC'])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, [])

        flags = pic_flags(compiler='Visual Studio')
        self.assertEquals(flags.cflags, [])
        self.assertEquals(flags.cxxflags, [])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, [])

    def test_build_type_flags(self):
        flags = build_type_flags(compiler='Visual Studio', build_type='Debug')
        self.assertEquals(flags.cflags, ['/Zi'])
        self.assertEquals(flags.cxxflags, ['/Zi'])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, [])

        flags = build_type_flags(compiler='Visual Studio', build_type='Release')
        self.assertEquals(flags.cflags, [])
        self.assertEquals(flags.cxxflags, [])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, [])

        flags = build_type_flags(compiler='gcc', build_type='Debug')
        self.assertEquals(flags.cflags, ['-g'])
        self.assertEquals(flags.cxxflags, ['-g'])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, [])

        flags = build_type_flags(compiler='gcc', build_type='Release')
        self.assertEquals(flags.cflags, ['-s'])
        self.assertEquals(flags.cxxflags, ['-s'])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, ['NDEBUG'])

        flags = build_type_flags(compiler='clang', build_type='Debug')
        self.assertEquals(flags.cflags, ['-g'])
        self.assertEquals(flags.cxxflags, ['-g'])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, [])

        flags = build_type_flags(compiler='clang', build_type='Release')
        self.assertEquals(flags.cflags, [])
        self.assertEquals(flags.cxxflags, [])
        self.assertEquals(flags.cxxflags, [])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, [])

    def test_adjust_path(self):
        self.assertEquals('home/www', adjust_path('home\\www'))
        self.assertEquals('home/www', adjust_path('home\\www', compiler='gcc'))
        self.assertEquals('home\\www', adjust_path('home/www', compiler='Visual Studio'))
        self.assertEquals('"home/www root"', adjust_path('home\\www root'))
        self.assertEquals('"home/www root"', adjust_path('home\\www root', compiler='gcc'))
        self.assertEquals('"home\\www root"', adjust_path('home/www root', compiler='Visual Studio'))
        self.assertEquals('home/www', adjust_path('home\\www', compiler='Visual Studio', win_bash=True))
        self.assertEquals('home/www', adjust_path('home/www', compiler='Visual Studio', win_bash=True))
        self.assertEquals('"home/www root"', adjust_path('home\\www root', compiler='Visual Studio', win_bash=True))
        self.assertEquals('"home/www root"', adjust_path('home/www root', compiler='Visual Studio', win_bash=True))

    def test_sysroot_flag(self):
        sysroot = sysroot_flags(sysroot=None)
        self.assertEquals(sysroot.cflags, [])
        self.assertEquals(sysroot.cxxflags, [])
        self.assertEquals(sysroot.ldflags, [])
        self.assertEquals(sysroot.defines, [])
        sysroot = sysroot_flags(sysroot='sys/root', compiler='Visual Studio')
        self.assertEquals(sysroot.cflags, [])
        self.assertEquals(sysroot.cxxflags, [])
        self.assertEquals(sysroot.ldflags, [])
        self.assertEquals(sysroot.defines, [])
        sysroot = sysroot_flags(sysroot='sys/root')
        self.assertEquals(sysroot.cflags, ['--sysroot=sys/root'])
        self.assertEquals(sysroot.cxxflags, ['--sysroot=sys/root'])
        self.assertEquals(sysroot.ldflags, ['--sysroot=sys/root'])
        self.assertEquals(sysroot.defines, [])<commit_msg>- make test for Visual Studio paths Windows-only (uses tools.unix_path)<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest
import platform
from nose.plugins.attrib import attr

from conans.client.build.compiler_flags import architecture_flags, libcxx_flags, pic_flags, CompilerFlags, \
    adjust_path, sysroot_flags, format_defines, format_include_paths, format_library_paths, format_libraries, \
    build_type_flags


class CompilerFlagsTest(unittest.TestCase):
    def test_merge(self):
        flags1 = CompilerFlags(cflags=['--foo'], cxxflags=['--bar'])
        flags2 = CompilerFlags(cflags=['--fizz'], ldflags=['--buzz'])
        flags1.append(additional_flags=flags2)
        self.assertEquals(flags1.cflags, ['--foo', '--fizz'])
        self.assertEquals(flags1.cxxflags, ['--bar'])
        self.assertEquals(flags1.ldflags, ['--buzz'])
        self.assertEquals(flags1.defines, [])

    def test_arch_flags(self):
        arch_flags = architecture_flags(arch='x86', compiler='gcc')
        self.assertEquals(arch_flags.cflags, ['-m32'])
        self.assertEquals(arch_flags.cxxflags, ['-m32'])
        self.assertEquals(arch_flags.ldflags, ['-m32'])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = architecture_flags(arch='x86', compiler='clang')
        self.assertEquals(arch_flags.cflags, ['-m32'])
        self.assertEquals(arch_flags.cxxflags, ['-m32'])
        self.assertEquals(arch_flags.ldflags, ['-m32'])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = architecture_flags(arch='sparc', compiler='sun-cc')
        self.assertEquals(arch_flags.cflags, ['-m32'])
        self.assertEquals(arch_flags.cxxflags, ['-m32'])
        self.assertEquals(arch_flags.ldflags, ['-m32'])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = architecture_flags(arch='x86_64', compiler='gcc')
        self.assertEquals(arch_flags.cflags, ['-m64'])
        self.assertEquals(arch_flags.cxxflags, ['-m64'])
        self.assertEquals(arch_flags.ldflags, ['-m64'])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = architecture_flags(arch='x86_64', compiler='clang')
        self.assertEquals(arch_flags.cflags, ['-m64'])
        self.assertEquals(arch_flags.cxxflags, ['-m64'])
        self.assertEquals(arch_flags.ldflags, ['-m64'])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = architecture_flags(arch='sparcv9', compiler='sun-cc')
        self.assertEquals(arch_flags.cflags, ['-m64'])
        self.assertEquals(arch_flags.cxxflags, ['-m64'])
        self.assertEquals(arch_flags.ldflags, ['-m64'])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = architecture_flags(arch='armv7', compiler='gcc')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, [])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = architecture_flags(arch='x86', compiler='Visual Studio')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, [])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

    def test_libcxx_flags(self):
        arch_flags = libcxx_flags(compiler='gcc', libcxx='libstdc++')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, [])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, ['_GLIBCXX_USE_CXX11_ABI=0'])

        arch_flags = libcxx_flags(compiler='gcc', libcxx='libstdc++11')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, [])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, ['_GLIBCXX_USE_CXX11_ABI=1'])

        arch_flags = libcxx_flags(compiler='clang', libcxx='libc++')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, ['-stdlib=libc++'])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = libcxx_flags(compiler='clang', libcxx='libstdc++')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, ['-stdlib=libstdc++'])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, ['_GLIBCXX_USE_CXX11_ABI=0'])

        arch_flags = libcxx_flags(compiler='clang', libcxx='libstdc++11')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, ['-stdlib=libstdc++'])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, ['_GLIBCXX_USE_CXX11_ABI=1'])

        arch_flags = libcxx_flags(compiler='apple-clang', libcxx='libc++')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, ['-stdlib=libc++'])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = libcxx_flags(compiler='Visual Studio')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, [])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = libcxx_flags(compiler='sun-cc', libcxx='libCstd')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, ['-library=Cstd'])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = libcxx_flags(compiler='sun-cc', libcxx='libstdcxx')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, ['-library=stdcxx4'])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = libcxx_flags(compiler='sun-cc', libcxx='libstlport')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, ['-library=stlport4'])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

        arch_flags = libcxx_flags(compiler='sun-cc', libcxx='libstdc++')
        self.assertEquals(arch_flags.cflags, [])
        self.assertEquals(arch_flags.cxxflags, ['-library=stdcpp'])
        self.assertEquals(arch_flags.ldflags, [])
        self.assertEquals(arch_flags.defines, [])

    def test_pic_flags(self):
        flags = pic_flags()
        self.assertEquals(flags.cflags, ['-fPIC'])
        self.assertEquals(flags.cxxflags, ['-fPIC'])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, [])

        flags = pic_flags(compiler='gcc')
        self.assertEquals(flags.cflags, ['-fPIC'])
        self.assertEquals(flags.cxxflags, ['-fPIC'])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, [])

        flags = pic_flags(compiler='Visual Studio')
        self.assertEquals(flags.cflags, [])
        self.assertEquals(flags.cxxflags, [])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, [])

    def test_build_type_flags(self):
        flags = build_type_flags(compiler='Visual Studio', build_type='Debug')
        self.assertEquals(flags.cflags, ['/Zi'])
        self.assertEquals(flags.cxxflags, ['/Zi'])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, [])

        flags = build_type_flags(compiler='Visual Studio', build_type='Release')
        self.assertEquals(flags.cflags, [])
        self.assertEquals(flags.cxxflags, [])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, [])

        flags = build_type_flags(compiler='gcc', build_type='Debug')
        self.assertEquals(flags.cflags, ['-g'])
        self.assertEquals(flags.cxxflags, ['-g'])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, [])

        flags = build_type_flags(compiler='gcc', build_type='Release')
        self.assertEquals(flags.cflags, ['-s'])
        self.assertEquals(flags.cxxflags, ['-s'])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, ['NDEBUG'])

        flags = build_type_flags(compiler='clang', build_type='Debug')
        self.assertEquals(flags.cflags, ['-g'])
        self.assertEquals(flags.cxxflags, ['-g'])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, [])

        flags = build_type_flags(compiler='clang', build_type='Release')
        self.assertEquals(flags.cflags, [])
        self.assertEquals(flags.cxxflags, [])
        self.assertEquals(flags.cxxflags, [])
        self.assertEquals(flags.ldflags, [])
        self.assertEquals(flags.defines, [])

    def test_adjust_path(self):
        self.assertEquals('home/www', adjust_path('home\\www'))
        self.assertEquals('home/www', adjust_path('home\\www', compiler='gcc'))

        self.assertEquals('"home/www root"', adjust_path('home\\www root'))
        self.assertEquals('"home/www root"', adjust_path('home\\www root', compiler='gcc'))

    @attr('visual_studio')
    def test_adjust_path_visual_studio(self):
        # NOTE : test cannot be run on *nix systems, as adjust_path uses tools.unix_path which is Windows-only
        if platform.system() != "Windows":
            return
        self.assertEquals('home\\www', adjust_path('home/www', compiler='Visual Studio'))
        self.assertEquals('"home\\www root"', adjust_path('home/www root', compiler='Visual Studio'))
        self.assertEquals('home/www', adjust_path('home\\www', compiler='Visual Studio', win_bash=True))
        self.assertEquals('home/www', adjust_path('home/www', compiler='Visual Studio', win_bash=True))
        self.assertEquals('"home/www root"', adjust_path('home\\www root', compiler='Visual Studio', win_bash=True))
        self.assertEquals('"home/www root"', adjust_path('home/www root', compiler='Visual Studio', win_bash=True))

    def test_sysroot_flag(self):
        sysroot = sysroot_flags(sysroot=None)
        self.assertEquals(sysroot.cflags, [])
        self.assertEquals(sysroot.cxxflags, [])
        self.assertEquals(sysroot.ldflags, [])
        self.assertEquals(sysroot.defines, [])
        sysroot = sysroot_flags(sysroot='sys/root', compiler='Visual Studio')
        self.assertEquals(sysroot.cflags, [])
        self.assertEquals(sysroot.cxxflags, [])
        self.assertEquals(sysroot.ldflags, [])
        self.assertEquals(sysroot.defines, [])
        sysroot = sysroot_flags(sysroot='sys/root')
        self.assertEquals(sysroot.cflags, ['--sysroot=sys/root'])
        self.assertEquals(sysroot.cxxflags, ['--sysroot=sys/root'])
        self.assertEquals(sysroot.ldflags, ['--sysroot=sys/root'])
        self.assertEquals(sysroot.defines, [])
{'fraction_non_alphanumeric': 0.15554936638351205, 'fraction_numerical': 0.007334168871559207, 'mean_word_length': 3.5634399491633126, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 40, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7550833', 'n_tokens_mistral': 6861, 'n_tokens_neox': 6625, 'n_words': 764}