Skip to content

Commit

Permalink
Merge pull request #107 from cosmo0920/support-replace-cmake-command-…
Browse files Browse the repository at this point in the history
…via-initialize

cmake: Support cmake command replacement on #initialize
  • Loading branch information
flavorjones authored Aug 31, 2021
2 parents 3d344cd + 8d9401e commit bf1f0c4
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 13 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name: Continuous Integration

concurrency:
group: "${{github.workflow}}-${{github.ref}}"
cancel-in-progress: true
on:
workflow_dispatch:
push:
Expand All @@ -20,6 +22,7 @@ jobs:
env:
MAKEFLAGS: -j2
strategy:
fail-fast: false
matrix:
platform: [ubuntu-latest, windows-latest]
ruby: ["2.3", "2.4", "2.5", "2.6", "2.7", "3.0", "head"]
Expand All @@ -43,6 +46,7 @@ jobs:
env:
MAKEFLAGS: -j2
strategy:
fail-fast: false
matrix:
platform: [ubuntu-latest, windows-latest]
ruby: ["3.0"]
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## mini_portile changelog

### next-minor / unreleased

### Added

The commands used for "make", "compile", and "cmake" are configurable via keyword arguments. [#107] (Thanks, @cosmo0920!)


### 2.6.1 / 2021-05-31

#### Dependencies
Expand Down
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,60 @@ library into a namespaced structure.
`#activate` ensures GCC will find this library and prefer it over a
system-wide installation.

Some keyword arguments can be passed to the constructor to configure the commands used:

#### `gcc_command`

The compiler command that is used is configurable, and in order of preference will use:

- the `CC` environment variable (if present)
- the `gcc_command` value passed in to the constructor
- `RbConfig::CONFIG["CC"]`
- `"gcc"`

You can pass it in like so:

``` ruby
MiniPortile.new("libiconv", "1.13.1", gcc_command: "cc")
```

#### `make_command`

The configuration/make command that is used is configurable, and in order of preference will use:

- the `MAKE` environment variable (if present)
- the `make_command` value passed in to the constructor
- the `make` environment variable (if present)
- `"make"`

You can pass it in like so:

``` ruby
MiniPortile.new("libiconv", "1.13.1", make_command: "nmake")
```


### How to use (for cmake projects)

Same as above, but instead of `MiniPortile.new`, call `MiniPortileCMake.new`.

#### `make_command`

This is configurable as above, except for Windows systems where it's hardcoded to `"nmake"`.

#### `cmake_command`

The cmake command used is configurable, and in order of preference will use:

- the `CMAKE` environment variable (if present)
- the `cmake_command` value passed in to the constructor
- `"cmake"`

You can pass it in like so:

``` ruby
MiniPortileCMake.new("libfoobar", "1.3.5", cmake_command: "cmake3")
```

### Local source directories

Expand Down
23 changes: 12 additions & 11 deletions lib/mini_portile2/mini_portile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def self.mswin?
RbConfig::CONFIG['target_os'] =~ /mswin/
end

def initialize(name, version)
def initialize(name, version, **kwargs)
@name = name
@version = version
@target = 'ports'
Expand All @@ -57,6 +57,9 @@ def initialize(name, version)
@source_directory = nil

@original_host = @host = detect_host

@gcc_command = kwargs[:gcc_command]
@make_command = kwargs[:make_command]
end

def source_directory=(path)
Expand Down Expand Up @@ -222,6 +225,14 @@ def path
File.expand_path(port_path)
end

def gcc_cmd
(ENV["CC"] || @gcc_command || RbConfig::CONFIG["CC"] || "gcc").dup
end

def make_cmd
(ENV["MAKE"] || @make_command || ENV["make"] || "make").dup
end

private

def tmp_path
Expand Down Expand Up @@ -583,14 +594,4 @@ def with_tempfile(filename, full_path)
FileUtils.mkdir_p File.dirname(full_path)
FileUtils.mv temp_file.path, full_path, :force => true
end

def gcc_cmd
cc = ENV["CC"] || RbConfig::CONFIG["CC"] || "gcc"
return cc.dup
end

def make_cmd
m = ENV['MAKE'] || ENV['make'] || 'make'
return m.dup
end
end
11 changes: 10 additions & 1 deletion lib/mini_portile2/mini_portile_cmake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ def configure_prefix
"-DCMAKE_INSTALL_PREFIX=#{File.expand_path(port_path)}"
end

def initialize(name, version, **kwargs)
super(name, version, **kwargs)
@cmake_command = kwargs[:cmake_command]
end

def configure_defaults
if MiniPortile.mswin?
['-G', 'NMake Makefiles']
Expand All @@ -21,7 +26,7 @@ def configure
cache_file = File.join(tmp_path, 'configure.options_cache')
File.open(cache_file, "w") { |f| f.write computed_options.to_s }

execute('configure', %w(cmake) + computed_options + ["."])
execute('configure', [cmake_cmd] + computed_options + ["."])
end

def configured?
Expand All @@ -39,4 +44,8 @@ def make_cmd
return "nmake" if MiniPortile.mswin?
super
end

def cmake_cmd
(ENV["CMAKE"] || @cmake_command || "cmake").dup
end
end
16 changes: 16 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,20 @@ def with_custom_git_dir(dir)
ensure
ENV['GIT_DIR'] = old
end

def with_env(env)
before = ENV.to_h.dup
env.each { |k, v| ENV[k] = v }
yield
ensure
ENV.replace(before)
end

def without_env(*keys, &blk)
before = ENV.to_h.dup
keys.flatten.each { |k| ENV.delete(k) }
yield
ensure
ENV.replace(before)
end
end
30 changes: 30 additions & 0 deletions test/test_cmake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,33 @@ def test_install
assert File.exist?(binary), binary
end
end

class TestCMakeConfig < TestCase
def test_make_command_configuration
MiniPortile.stub(:mswin?, false) do
without_env("MAKE") do
assert_equal("make", MiniPortileCMake.new("test", "1.0.0").make_cmd)
assert_equal("xyzzy", MiniPortileCMake.new("test", "1.0.0", make_command: "xyzzy").make_cmd)
end
with_env("MAKE"=>"asdf") do
assert_equal("asdf", MiniPortileCMake.new("test", "1.0.0").make_cmd)
assert_equal("asdf", MiniPortileCMake.new("test", "1.0.0", make_command: "xyzzy").make_cmd)
end
end

MiniPortile.stub(:mswin?, true) do
assert_equal("nmake", MiniPortileCMake.new("test", "1.0.0").make_cmd)
end
end

def test_cmake_command_configuration
without_env("CMAKE") do
assert_equal("cmake", MiniPortileCMake.new("test", "1.0.0").cmake_cmd)
assert_equal("xyzzy", MiniPortileCMake.new("test", "1.0.0", cmake_command: "xyzzy").cmake_cmd)
end
with_env("CMAKE"=>"asdf") do
assert_equal("asdf", MiniPortileCMake.new("test", "1.0.0").cmake_cmd)
assert_equal("asdf", MiniPortileCMake.new("test", "1.0.0", cmake_command: "xyzzy").cmake_cmd)
end
end
end
26 changes: 26 additions & 0 deletions test/test_cook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,32 @@ def test_install
end
end

class TestCookConfiguration < TestCase
def test_make_command_configuration
without_env("MAKE") do
assert_equal("make", MiniPortile.new("test", "1.0.0").make_cmd)
assert_equal("xyzzy", MiniPortile.new("test", "1.0.0", make_command: "xyzzy").make_cmd)
end
with_env("MAKE"=>"asdf") do
assert_equal("asdf", MiniPortile.new("test", "1.0.0").make_cmd)
assert_equal("asdf", MiniPortile.new("test", "1.0.0", make_command: "xyzzy").make_cmd)
end
end

def test_gcc_command_configuration
without_env("CC") do
expected_compiler = RbConfig::CONFIG["CC"] || "gcc"
assert_equal(expected_compiler, MiniPortile.new("test", "1.0.0").gcc_cmd)
assert_equal("xyzzy", MiniPortile.new("test", "1.0.0", gcc_command: "xyzzy").gcc_cmd)
end
with_env("CC"=>"asdf") do
assert_equal("asdf", MiniPortile.new("test", "1.0.0").gcc_cmd)
assert_equal("asdf", MiniPortile.new("test", "1.0.0", gcc_command: "xyzzy").gcc_cmd)
end
end
end


class TestCookWithBrokenGitDir < TestCase
#
# this is a test for #69
Expand Down

0 comments on commit bf1f0c4

Please sign in to comment.