Repository: conan-io/conan-center-index

Issue:
[package] cppcheck/2.4.1: Running cppcheck missing cfg files
### Package and Environment Details (include every applicable attribute)
  * Package Name/Version: **cppcheck/2.4.1**
  * Operating System+version: **Linux Ubuntu 18.04**
  * Compiler+version: **GCC 7.5**
  * Conan version: **conan 1.37.1**
  * Python version: **Python 3.6.9**


### Conan profile (output of `conan profile show default` or `conan profile show <profile>` if custom profile is in use)
```
Configuration for profile default:

[settings]
os=Linux
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=7
compiler.libcxx=libstdc++11
build_type=Release
[options]
[build_requires]
[env]
```


### Steps to reproduce (Include if Applicable)

* Install cppcheck with a runenv:
```
$ conan install "cppcheck/2.4.1@" -r conan-center  -g virtualrunenv
$ source activate_run.sh
```
* Run cppcheck on directory containing C++ files
```
cppcheck --xml --enable=warning,style,performance --std=c++11 . 2> test.xml
cppcheck: Failed to load library configuration file 'std.cfg'. File not found
```

It seems that cppcheck needs to be build locally because it uses some hardcoded paths, so `build_policy = "always"` should be set.
Also [conanfile.py#L66](https://github.com/conan-io/conan-center-index/blob/master/recipes/cppcheck/all/conanfile.py#L66) deletes the `share` directory that's where the *cfg* files are installed by default and where the hardcoded path points to.
With this line deleted (and build locally) I can run the cppcheck installed via conan properly.

If you want, I can prepare a PR for these 2 changes.



Discussion:
It would be nice if the `cppcheck` binary could be made relocatable.
Perhaps the files can be loaded from a relative location to the binary location?

Anyway, please open a pr + a test in the test package.

Resolving patch:
diff --git a/recipes/cppcheck/all/conanfile.py b/recipes/cppcheck/all/conanfile.py
--- a/recipes/cppcheck/all/conanfile.py
+++ b/recipes/cppcheck/all/conanfile.py
@@ -1,4 +1,5 @@
 from conans import ConanFile, CMake, tools
+import functools
 import os
 
 required_conan_version = ">=1.33.0"
@@ -17,8 +18,6 @@ class CppcheckConan(ConanFile):
     default_options = {"with_z3": True, "have_rules": True}
     exports_sources = ["CMakeLists.txt", "patches/**"]
 
-    _cmake = None
-
     @property
     def _source_subfolder(self):
         return "source_subfolder"
@@ -44,16 +43,15 @@ def source(self):
         tools.get(**self.conan_data["sources"][self.version],
                   destination=self._source_subfolder, strip_root=True)
 
+    @functools.lru_cache(1)
     def _configure_cmake(self):
-        if self._cmake:
-            return self._cmake
-        self._cmake = CMake(self)
-        self._cmake.definitions["USE_Z3"] = self.options.with_z3
-        self._cmake.definitions["HAVE_RULES"] = self.options.have_rules
-        self._cmake.definitions["USE_MATCHCOMPILER"] = "Auto"
-        self._cmake.definitions["ENABLE_OSS_FUZZ"] = False
-        self._cmake.configure(build_folder=self._build_subfolder)
-        return self._cmake
+        cmake = CMake(self)
+        cmake.definitions["USE_Z3"] = self.options.with_z3
+        cmake.definitions["HAVE_RULES"] = self.options.have_rules
+        cmake.definitions["USE_MATCHCOMPILER"] = "Auto"
+        cmake.definitions["ENABLE_OSS_FUZZ"] = False
+        cmake.configure(build_folder=self._build_subfolder)
+        return cmake
 
     def build(self):
         self._patch_sources()


Test patch:
diff --git a/recipes/cppcheck/all/test_package/conanfile.py b/recipes/cppcheck/all/test_package/conanfile.py
--- a/recipes/cppcheck/all/test_package/conanfile.py
+++ b/recipes/cppcheck/all/test_package/conanfile.py
@@ -1,13 +1,18 @@
 from conans import ConanFile, tools
 import sys
+import os
+import shutil
 
 
 class TestPackageConan(ConanFile):
     settings = "os", "arch"
 
     def test(self):
+        shutil.copy(os.path.join(self.source_folder, "file_to_check.cpp"),
+                    os.path.join(self.build_folder, "file_to_check.cpp"))
         if not tools.cross_building(self.settings):
-            self.run("cppcheck --version", run_environment=True)
+            self.run("cppcheck --enable=warning,style,performance --std=c++11 .",
+                    cwd=self.source_folder, run_environment=True)
             # On windows we need to explicitly use python to run the python script
             if self.settings.os == 'Windows':
                 self.run("{} {} -h".format(sys.executable, tools.get_env("CPPCHECK_HTMLREPORT")))
diff --git a/recipes/cppcheck/all/test_package/file_to_check.cpp b/recipes/cppcheck/all/test_package/file_to_check.cpp
new file mode 100644
--- /dev/null
+++ b/recipes/cppcheck/all/test_package/file_to_check.cpp
@@ -0,0 +1,3 @@
+int main() {
+    return 0;
+}