<commit_before>/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.nativebinaries.language.cpp

import org.gradle.nativebinaries.language.cpp.fixtures.AbstractInstalledToolChainIntegrationSpec
import org.gradle.nativebinaries.language.cpp.fixtures.RequiresInstalledToolChain
import org.gradle.nativebinaries.language.cpp.fixtures.ToolChainRequirement
import org.gradle.nativebinaries.language.cpp.fixtures.app.CHelloWorldApp
import org.hamcrest.Matchers

import static org.gradle.nativebinaries.language.cpp.fixtures.ToolChainRequirement.GccCompatible

@RequiresInstalledToolChain(GccCompatible)
class GccToolChainDiscoveryIntegrationTest extends AbstractInstalledToolChainIntegrationSpec {
    def helloWorldApp = new CHelloWorldApp()

    def setup() {
        buildFile << """
            apply plugin: 'c'

            model {
                toolChains {
                    ${toolChain.buildScriptConfig}
                }
            }

            executables {
                main {
                    binaries.all {
                        lib libraries.hello.static
                    }
                }
            }
            libraries {
                hello {}
            }
"""

        helloWorldApp.executable.writeSources(file("src/main"))
        helloWorldApp.library.writeSources(file("src/hello"))
    }

    def "can build when language tools that are not required are not available"() {
        when:
        buildFile << """
            model {
                toolChains {
                    ${toolChain.id} {
                        cppCompiler.executable = 'does-not-exist'
                    }
                }
            }
"""
        succeeds "mainExecutable"

        then:
        executable("build/binaries/mainExecutable/main").exec().out == helloWorldApp.englishOutput
    }

    def "does not break when compiler not available and not building"() {
        when:
        buildFile << """
            model {
                toolChains {
                    ${toolChain.id} {
                        all {
                            executable = 'does-not-exist'
                        }
                    }
                }
            }
"""

        then:
        succeeds "help"
    }

    def "tool chain is not available when no tools are available"() {
        when:
        buildFile << """
            model {
                toolChains {
                    ${toolChain.id} {
                        all {
                            executable = 'does-not-exist'
                        }
                    }
                }
            }
"""
        fails "mainExecutable"

        then:
        failure.assertHasDescription("Execution failed for task ':compileMainExecutableMainC'.")
        failure.assertThatCause(Matchers.startsWith("No tool chain is available to build for platform 'current'"))
        failure.assertThatCause(Matchers.containsString("- ${toolChain.instanceDisplayName}: Could not find ${toolChain.meets(ToolChainRequirement.Gcc) ? 'Cpp' : "C"} compiler 'does-not-exist'"))
    }

    def "fails when required language tool is not available but other language tools are available"() {
        when:
        buildFile << """
            model {
                toolChains {
                    ${toolChain.id} {
                        cCompiler.executable = 'does-not-exist'
                    }
                }
            }<commit_msg>fix typo in integtTest assertion<commit_after>/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.nativebinaries.language.cpp

import org.gradle.nativebinaries.language.cpp.fixtures.AbstractInstalledToolChainIntegrationSpec
import org.gradle.nativebinaries.language.cpp.fixtures.RequiresInstalledToolChain
import org.gradle.nativebinaries.language.cpp.fixtures.ToolChainRequirement
import org.gradle.nativebinaries.language.cpp.fixtures.app.CHelloWorldApp
import org.hamcrest.Matchers

import static org.gradle.nativebinaries.language.cpp.fixtures.ToolChainRequirement.GccCompatible

@RequiresInstalledToolChain(GccCompatible)
class GccToolChainDiscoveryIntegrationTest extends AbstractInstalledToolChainIntegrationSpec {
    def helloWorldApp = new CHelloWorldApp()

    def setup() {
        buildFile << """
            apply plugin: 'c'

            model {
                toolChains {
                    ${toolChain.buildScriptConfig}
                }
            }

            executables {
                main {
                    binaries.all {
                        lib libraries.hello.static
                    }
                }
            }
            libraries {
                hello {}
            }
"""

        helloWorldApp.executable.writeSources(file("src/main"))
        helloWorldApp.library.writeSources(file("src/hello"))
    }

    def "can build when language tools that are not required are not available"() {
        when:
        buildFile << """
            model {
                toolChains {
                    ${toolChain.id} {
                        cppCompiler.executable = 'does-not-exist'
                    }
                }
            }
"""
        succeeds "mainExecutable"

        then:
        executable("build/binaries/mainExecutable/main").exec().out == helloWorldApp.englishOutput
    }

    def "does not break when compiler not available and not building"() {
        when:
        buildFile << """
            model {
                toolChains {
                    ${toolChain.id} {
                        all {
                            executable = 'does-not-exist'
                        }
                    }
                }
            }
"""

        then:
        succeeds "help"
    }

    def "tool chain is not available when no tools are available"() {
        when:
        buildFile << """
            model {
                toolChains {
                    ${toolChain.id} {
                        all {
                            executable = 'does-not-exist'
                        }
                    }
                }
            }
"""
        fails "mainExecutable"

        then:
        failure.assertHasDescription("Execution failed for task ':compileMainExecutableMainC'.")
        failure.assertThatCause(Matchers.startsWith("No tool chain is available to build for platform 'current'"))
        failure.assertThatCause(Matchers.containsString("- ${toolChain.instanceDisplayName}: Could not find ${toolChain.meets(ToolChainRequirement.Gcc) ? 'C++' : 'C'} compiler 'does-not-exist'"))
    }

    def "fails when required language tool is not available but other language tools are available"() {
        when:
        buildFile << """
            model {
                toolChains {
                    ${toolChain.id} {
                        cCompiler.executable = 'does-not-exist'
                    }
                }
            }
{'fraction_non_alphanumeric': 0.08452454940957116, 'fraction_numerical': 0.0019888129272840273, 'mean_word_length': 1.6793206793206794, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1701162', 'n_tokens_mistral': 2046, 'n_tokens_neox': 1934, 'n_words': 492}